Skip to content

Commit

Permalink
Naming changes done to suit revamped SloopEngine App services.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnSundarraj committed Nov 5, 2022
1 parent 31bf47d commit 0a4a4a1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 73 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down
51 changes: 22 additions & 29 deletions source/sloopengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
'''),
Expand All @@ -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':
Expand Down Expand Up @@ -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(
Expand All @@ -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 [<args>]',
Expand All @@ -205,27 +198,27 @@ def identity(self):
''')
)
parser.add_argument('command',choices=['sync','delete'])
parser.add_argument('--stack-id',type=int,metavar='<integer>',help='Stack identifier.')
parser.add_argument('--id',type=int,metavar='<integer>',help='Identity identifier.')
parser.add_argument('--name',metavar='<string>',help='Identity name.')
parser.add_argument('--workspace-id',type=int,metavar='<integer>',help='Workspace identifier.')
parser.add_argument('--id',type=int,metavar='<integer>',help='Credential identifier.')
parser.add_argument('--name',metavar='<string>',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)
Expand Down
11 changes: 2 additions & 9 deletions source/sloopengine/includes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from sloopengine.config import credential_conf


# Identity controller.
class identity(object):
# Credential controller.
class credential(object):

# Initializer.
def __init__(self):
Expand All @@ -26,7 +26,7 @@ def __init__(self):
}
}

# Create Identity.
# Create Credential.
def create(self,name):
try:
home_dir = '/home/%s' %(name)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 0a4a4a1

Please sign in to comment.