diff --git a/setup.cfg b/setup.cfg index ec864f3..ac885ed 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = sloopengine-cli -version = 1.1.5 +version = 2.1.1 description = SloopEngine CLI for managing resources and agent. long_description = file: README.md long_description_content_type = text/markdown diff --git a/source/sloopengine/__init__.py b/source/sloopengine/__init__.py index b9f9733..c25bb69 100644 --- a/source/sloopengine/__init__.py +++ b/source/sloopengine/__init__.py @@ -9,7 +9,7 @@ # Global variables. -version = '1.1.5' +version = '2.1.1' base_dir = '/opt/sloopengine' data_dir = '%s/data' %(base_dir) log_dir = '%s/log' %(base_dir) @@ -21,9 +21,8 @@ # Import custom modules. from sloopengine.includes.common import url_validate -from sloopengine.includes.common import api_key_id_validate -from sloopengine.includes.common import api_key_token_validate -from sloopengine.resource.identity import identity +from sloopengine.includes.common import token_validate +from sloopengine.resource.credential import credential # CLI controller. @@ -55,7 +54,7 @@ def __init__(self): Commands: init Initialize CLI. configure Configure CLI. - identity Manage Identities. + credential Manage Credentials. agent Manage Agent. version Print CLI version. '''), @@ -73,15 +72,15 @@ def __init__(self): 'command', nargs='?', default='version', - choices=['init','configure','identity','agent','version'] + choices=['init','configure','credential','agent','version'] ) args = parser.parse_args(sys.argv[1:2]) if args.command=='init': self.initialize() elif args.command=='configure': self.configure() - elif args.command=='identity': - self.identity() + elif args.command=='credential': + self.credential() elif args.command=='agent': self.agent() elif args.command=='version': @@ -143,21 +142,15 @@ def configure(self): }, 'credential':{ 'user':{ - 'api_key':{ - 'id':None, - 'token':None - } + 'token':None } } } params['main']['account']['url'] = input('Enter Account url: ') if url_validate(params['main']['account']['url']) is not True: sys.exit(1) - params['credential']['user']['api_key']['id'] = input('Enter User API key identifier: ') - if api_key_id_validate(params['credential']['user']['api_key']['id']) is not True: - sys.exit(1) - params['credential']['user']['api_key']['token'] = input('Enter User API key token: ') - if api_key_token_validate(params['credential']['user']['api_key']['token']) is not True: + params['credential']['user']['token'] = input('Enter User token: ') + if token_validate(params['credential']['user']['token']) is not True: sys.exit(1) main_conf_file = open(self.main_conf_path,'wt') main_conf_file.write(json.dumps( @@ -180,20 +173,20 @@ def configure(self): sys.exit(0) # Manage Identities using CLI. - def identity(self): + def credential(self): try: assert os.path.exists(self.base_dir) is True,'CLI not initialized.' assert os.path.exists(self.conf_dir) is True,'CLI not initialized.' assert os.path.isfile(self.main_conf_path) is True,'CLI not initialized.' assert os.path.isfile(self.credential_conf_path) is True,'CLI not initialized.' parser = argparse.ArgumentParser( - prog='sloopengine identity', + prog='sloopengine credential', description=dedent(''' Manage Identities using SloopEngine CLI. Commands: - sync Sync Identity. - delete Delete Identity from the machine. + sync Sync Credential. + delete Delete Credential from the machine. '''), formatter_class=argparse.RawDescriptionHelpFormatter, usage='%(prog)s []', @@ -205,27 +198,27 @@ def identity(self): ''') ) parser.add_argument('command',choices=['sync','delete']) - parser.add_argument('--stack-id',type=int,metavar='',help='Stack identifier.') - parser.add_argument('--id',type=int,metavar='',help='Identity identifier.') - parser.add_argument('--name',metavar='',help='Identity name.') + parser.add_argument('--workspace-id',type=int,metavar='',help='Workspace identifier.') + parser.add_argument('--id',type=int,metavar='',help='Credential identifier.') + parser.add_argument('--name',metavar='',help='Credential name.') args = parser.parse_args(sys.argv[2:]) if args.command=='sync': - if args.stack_id is None or args.id is None: + if args.workspace_id is None or args.id is None: cprint('Invalid args.','red') sys.exit(1) params = { - 'stack':{ - 'id':args.stack_id + 'workspace':{ + 'id':args.workspace_id }, 'id':args.id } - identity().sync(params) + credential().sync(params) elif args.command=='delete': if args.name is None: cprint('Invalid args.','red') sys.exit(1) - identity().delete(args.name) + credential().delete(args.name) else: cprint('Invalid command.','red') sys.exit(1) diff --git a/source/sloopengine/includes/common.py b/source/sloopengine/includes/common.py index c077189..4050c3c 100644 --- a/source/sloopengine/includes/common.py +++ b/source/sloopengine/includes/common.py @@ -34,15 +34,8 @@ def url_validate(url): else: cprint('Cannot accept empty url.','red') -# Validate API key identifier. -def api_key_id_validate(id): - if id: - return True - else: - cprint('Cannot accept empty identifier.','red') - -# Validate API key token. -def api_key_token_validate(token): +# Validate token. +def token_validate(token): if token: return True else: diff --git a/source/sloopengine/resource/identity.py b/source/sloopengine/resource/credential.py similarity index 75% rename from source/sloopengine/resource/identity.py rename to source/sloopengine/resource/credential.py index 8cab8a3..103c7c5 100644 --- a/source/sloopengine/resource/identity.py +++ b/source/sloopengine/resource/credential.py @@ -11,8 +11,8 @@ from sloopengine.config import credential_conf -# Identity controller. -class identity(object): +# Credential controller. +class credential(object): # Initializer. def __init__(self): @@ -26,7 +26,7 @@ def __init__(self): } } - # Create Identity. + # Create Credential. def create(self,name): try: home_dir = '/home/%s' %(name) @@ -35,12 +35,12 @@ def create(self,name): raise call(['chmod','750',home_dir]) except Exception as error: - cprint('Error creating Identity.','red') + cprint('Error creating Credential.','red') sys.exit(1) else: - cprint('Identity created.','green') + cprint('Credential created.','green') - # Configure Identity. + # Configure Credential. def configure(self,name): try: home_dir = '/home/%s' %(name) @@ -70,12 +70,12 @@ def ssh_client(home_dir): ssh_client(home_dir) except Exception as error: - cprint('Error configuring Identity.','red') + cprint('Error configuring Credential.','red') sys.exit(1) else: - cprint('Identity configured.','green') + cprint('Credential configured.','green') - # Update Identity. + # Update Credential. def update(self,name,key): try: home_dir = '/home/%s' %(name) @@ -95,19 +95,19 @@ def ssh_client(home_dir): ssh_client(home_dir) except Exception as error: - cprint('Error updating Identity.','red') + cprint('Error updating Credential.','red') sys.exit(1) else: - cprint('Identity updated.','green') + cprint('Credential updated.','green') - # Get Identity. + # Get Credential. def get(self,data): try: - self.stack = data['stack'] + self.workspace = data['workspace'] self.id = data['id'] url = ''.join( - '%s/API/Stack/%s/Identity/%s' - %(self.account['url'],self.stack['id'],self.id) + '%s/API/Workspace/%s/Credential/%s' + %(self.account['url'],self.workspace['id'],self.id) ) headers = { 'Authorization':''.join( @@ -118,26 +118,26 @@ def get(self,data): request = requests.get(url,headers=headers) response = request.json() if request.status_code==200 and response['status']=='success': - identity_data = response['result']['identity'] + credential_data = response['result']['credential'] else: return except Exception as error: raise else: - return identity_data + return credential_data - # Sync Identity. + # Sync Credential. def sync(self,data): try: - identity_data = self.get(data) - assert identity_data is not None,'Identity does not exist.' - name = identity_data['name'] + credential_data = self.get(data) + assert credential_data is not None,'Credential does not exist.' + name = credential_data['name'] key = { - 'private':identity_data['private_key'], - 'public':identity_data['public_key'] + 'private':credential_data['private_key'], + 'public':credential_data['public_key'] } - if self.exists(name) is True: - cprint('Identity already exists.','yellow') + if self.exist(name) is True: + cprint('Credential exist.','yellow') self.update(name,key) sys.exit(0) self.create(name) @@ -147,31 +147,31 @@ def sync(self,data): cprint(error.args[0],'red') sys.exit(1) except Exception as error: - cprint('Error syncing Identity.','red') + cprint('Error syncing Credential.','red') sys.exit(1) else: - cprint('Identity synced.','green') + cprint('Credential synced.','green') sys.exit(0) - # Delete Identity. + # Delete Credential. def delete(self,name): try: - if self.exists(name) is True: + if self.exist(name) is True: delete_user = call(['userdel','-r',name],stdout=DEVNULL,stderr=STDOUT) if delete_user!=0: raise else: - cprint('Identity does not exist.','red') + cprint('Credential does not exist.','red') sys.exit(1) except Exception as error: - cprint('Error deleting Identity.','red') + cprint('Error deleting Credential.','red') sys.exit(1) else: - cprint('Identity deleted.','green') + cprint('Credential deleted.','green') sys.exit(0) - # Check Identity exists. - def exists(self,name): + # Check Credential exist. + def exist(self,name): id = call(['id',name],stdout=DEVNULL,stderr=STDOUT) if id==0: return True