From 3d0aff3d1d47a053a678ecbec69d585cf1bbb275 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Fri, 8 Nov 2024 10:06:00 +0100 Subject: cli: ensure the program returns 0 on success and 1 on error propagate a returncode in each operation and make it the returncode of the program. Prevent sys.exit calls in post(), get() and delete() request handlers to enable cleanup code and error handling. Keep a basic error log inside the request functions if the connection can't be established or if the response contains an error code. --- cli/cli.py | 160 +++++++++++++++++++++++++++--------------------- cli/objects/center.py | 15 ++++- cli/objects/client.py | 42 +++++++++---- cli/objects/disks.py | 32 ++++++---- cli/objects/folder.py | 15 ++++- cli/objects/images.py | 82 ++++++++++++++----------- cli/objects/live.py | 7 ++- cli/objects/modes.py | 18 ++++-- cli/objects/poweroff.py | 11 ++-- cli/objects/reboot.py | 11 ++-- cli/objects/repo.py | 63 ++++++++++++------- cli/objects/room.py | 19 ++++-- cli/objects/scopes.py | 5 +- cli/objects/server.py | 19 ++++-- cli/objects/session.py | 11 ++-- cli/objects/wol.py | 11 ++-- ogcli | 3 +- 17 files changed, 330 insertions(+), 194 deletions(-) diff --git a/cli/cli.py b/cli/cli.py index c3accdd..2a47ece 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -32,43 +32,48 @@ class OgREST(): def get(self, path, payload=None): try: - r = requests.get(f'{self.URL}{path}', + res = requests.get(f'{self.URL}{path}', headers=self.HEADERS, json=payload) - if r.status_code != 200: - sys.exit(f"Unsuccessful request to ogServer: " - f"Response with {r.status_code} HTTP status code") - except IOError as e: - sys.exit(f"Cannot connect to ogServer: {e}") - return r + + if res.status_code != 200: + print(f"Error: Request to ogServer failed with status code {res.status_code}") + return None + + except requests.exceptions.RequestException as e: + print(f"Error: Cannot connect to ogServer: {e}") + return None + + return res def post(self, path, payload): try: - r = requests.post(f'{self.URL}{path}', + res = requests.post(f'{self.URL}{path}', headers=self.HEADERS, json=payload) - if r.text: - print(r.text) - if r.status_code not in {200, 202}: - sys.exit(f"Unsuccessful request to ogServer: " - f"Response with {r.status_code} HTTP status code") - except IOError as e: - sys.exit(f"Cannot connect to ogServer: {e}") - return r + if res.status_code not in {200, 202}: + print(f"Error: Request to ogServer failed with status code {res.status_code}") + return None + + except requests.exceptions.RequestException as e: + print(f"Error: Cannot connect to ogServer: {e}") + return None + + return res def delete(self, path, payload): try: - r = requests.delete(f'{self.URL}{path}', + res = requests.delete(f'{self.URL}{path}', headers=self.HEADERS, json=payload) - print(r.text) - if r.status_code != 200: - sys.exit(f"Unsuccessful request to ogServer: " - f"Response with {r.status_code} HTTP status code") + if res.status_code != 200: + print(f"Error: Request to ogServer failed with status code {res.status_code}") + return None except IOError as e: - sys.exit(f"Cannot connect to ogServer: {e}") - return r + print(f"Error: Cannot connect to ogServer: {e}") + return None + return res class OgCLI(): @@ -84,29 +89,31 @@ class OgCLI(): if not args: print('Missing list subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 parsed_args = parser.parse_args([args[0]]) + ret = 0 if parsed_args.item == 'clients': - OgClient.list_clients(self.rest) + ret = OgClient.list_clients(self.rest) elif parsed_args.item == 'client': - OgClient.get_client_properties(self.rest, args[1:]) + ret = OgClient.get_client_properties(self.rest, args[1:]) elif parsed_args.item == 'hardware': - OgClient.list_client_hardware(self.rest, args[1:]) + ret = OgClient.list_client_hardware(self.rest, args[1:]) elif parsed_args.item == 'modes': - OgModes.list_available_modes(self.rest) + ret = OgModes.list_available_modes(self.rest) elif parsed_args.item == 'scope': - OgScope.list_scopes(self.rest, args[1:]) + ret = OgScope.list_scopes(self.rest, args[1:]) elif parsed_args.item == 'images': - OgImage.list_images(self.rest) + ret = OgImage.list_images(self.rest) elif parsed_args.item == 'disks': - OgDisk.list_disks(self.rest, args[1:]) + ret = OgDisk.list_disks(self.rest, args[1:]) elif parsed_args.item == 'repos': - OgRepo.list_repos(self.rest) + ret = OgRepo.list_repos(self.rest) elif parsed_args.item == 'servers': - OgServer.list_servers(self.rest) + ret = OgServer.list_servers(self.rest) elif parsed_args.item == 'live': - OgLive.list_live(self.rest) + ret = OgLive.list_live(self.rest) + return ret def set(self, args): choices = ['modes', 'mode', 'repo'] @@ -116,13 +123,15 @@ class OgCLI(): if not args: print('Missing set subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 parsed_args = parser.parse_args([args[0]]) + ret = 0 if parsed_args.item in ['modes', 'mode']: - OgModes.set_modes(self.rest, args[1:]) + ret = OgModes.set_modes(self.rest, args[1:]) elif parsed_args.item == 'repo': - OgRepo.set_repo(self.rest, args[1:]) + ret = OgRepo.set_repo(self.rest, args[1:]) + return ret def request(self, args): choices = ['reboot', 'refresh', 'poweroff', 'wol', 'session'] @@ -132,19 +141,21 @@ class OgCLI(): if not args: print('Missing request subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 parsed_args = parser.parse_args([args[0]]) + ret = 0 if parsed_args.request_obj == 'wol': - OgWol.request_wol(self.rest, args[1:]) + ret = OgWol.request_wol(self.rest, args[1:]) elif parsed_args.request_obj == 'poweroff': - OgPoweroff.request_poweroff(self.rest, args[1:]) + ret = OgPoweroff.request_poweroff(self.rest, args[1:]) elif parsed_args.request_obj == 'refresh': - OgClient.request_refresh(self.rest, args[1:]) + ret = OgClient.request_refresh(self.rest, args[1:]) elif parsed_args.request_obj == 'reboot': - OgReboot.request_reboot(self.rest, args[1:]) + ret = OgReboot.request_reboot(self.rest, args[1:]) elif parsed_args.request_obj == 'session': - OgSession.request_session(self.rest, args[1:]) + ret = OgSession.request_session(self.rest, args[1:]) + return ret def restore(self, args): choices = ['image'] @@ -154,11 +165,13 @@ class OgCLI(): if not args: print('Missing restore subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 parsed_args = parser.parse_args([args[0]]) + ret = 0 if parsed_args.restore_obj == 'image': - OgImage.restore_image(self.rest, args[1:]) + ret = OgImage.restore_image(self.rest, args[1:]) + return ret def create(self, args): choices = ['image'] @@ -168,11 +181,13 @@ class OgCLI(): if not args: print('Missing create subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 parsed_args = parser.parse_args([args[0]]) + ret = 0 if parsed_args.create_obj == 'image': - OgImage.create_image(self.rest, args[1:]) + ret = OgImage.create_image(self.rest, args[1:]) + return ret def setup(self, args): choices = ['disk'] @@ -182,11 +197,13 @@ class OgCLI(): if not args: print('Missing setup subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 parsed_args = parser.parse_args([args[0]]) + ret = 0 if parsed_args.setup_obj == 'disk': - OgDisk.setup_disk(self.rest, args[1:]) + ret = OgDisk.setup_disk(self.rest, args[1:]) + return ret def update(self, args): choices = ['image', 'repo', 'center', 'room', 'folder'] @@ -196,19 +213,21 @@ class OgCLI(): if not args: print('Missing update subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 parsed_args = parser.parse_args([args[0]]) + ret = 0 if parsed_args.update_obj == 'image': - OgImage.update_image(self.rest, args[1:]) + ret = OgImage.update_image(self.rest, args[1:]) elif parsed_args.update_obj == 'center': - OgCenter.update_center(self.rest, args[1:]) + ret = OgCenter.update_center(self.rest, args[1:]) elif parsed_args.update_obj == 'room': - OgRoom.update_room(self.rest, args[1:]) + ret = OgRoom.update_room(self.rest, args[1:]) elif parsed_args.update_obj == 'folder': - OgFolder.update_folder(self.rest, args[1:]) + ret = OgFolder.update_folder(self.rest, args[1:]) elif parsed_args.update_obj == 'repo': - OgRepo.update_repo(self.rest, args[1:]) + ret = OgRepo.update_repo(self.rest, args[1:]) + return ret def delete(self, args): choices = ['server', 'repo', 'center', 'room', 'client', 'folder'] @@ -218,21 +237,22 @@ class OgCLI(): if not args: print('Missing delete subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 parsed_args = parser.parse_args([args[0]]) if parsed_args.delete_obj == 'server': - OgServer.delete_server(self.rest, args[1:]) + ret = OgServer.delete_server(self.rest, args[1:]) elif parsed_args.delete_obj == 'repo': - OgRepo.delete_repo(self.rest, args[1:]) + ret = OgRepo.delete_repo(self.rest, args[1:]) elif parsed_args.delete_obj == 'center': - OgCenter.delete_center(self.rest, args[1:]) + ret = OgCenter.delete_center(self.rest, args[1:]) elif parsed_args.delete_obj == 'room': - OgRoom.delete_room(self.rest, args[1:]) + ret = OgRoom.delete_room(self.rest, args[1:]) elif parsed_args.delete_obj == 'client': - OgClient.delete_client(self.rest, args[1:]) + ret = OgClient.delete_client(self.rest, args[1:]) elif parsed_args.delete_obj == 'folder': - OgFolder.delete_folder(self.rest, args[1:]) + ret = OgFolder.delete_folder(self.rest, args[1:]) + return ret def add(self, args): choices = ['server', 'repo', 'center', 'room', 'client', 'folder'] @@ -242,18 +262,20 @@ class OgCLI(): if not args: print('Missing add subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 parsed_args = parser.parse_args([args[0]]) + ret = 0 if parsed_args.add_obj == 'server': - OgServer.add_server(self.rest, args[1:]) + ret = OgServer.add_server(self.rest, args[1:]) elif parsed_args.add_obj == 'repo': - OgRepo.add_repo(self.rest, args[1:]) + ret = OgRepo.add_repo(self.rest, args[1:]) elif parsed_args.add_obj == 'center': - OgCenter.add_center(self.rest, args[1:]) + ret = OgCenter.add_center(self.rest, args[1:]) elif parsed_args.add_obj == 'room': - OgRoom.add_room(self.rest, args[1:]) + ret = OgRoom.add_room(self.rest, args[1:]) elif parsed_args.add_obj == 'client': - OgClient.add_client(self.rest, args[1:]) + ret = OgClient.add_client(self.rest, args[1:]) elif parsed_args.add_obj == 'folder': - OgFolder.add_folder(self.rest, args[1:]) + ret = OgFolder.add_folder(self.rest, args[1:]) + return ret diff --git a/cli/objects/center.py b/cli/objects/center.py index 80e5e3e..9948254 100644 --- a/cli/objects/center.py +++ b/cli/objects/center.py @@ -23,7 +23,10 @@ class OgCenter(): payload = {'name': parsed_args.name} if parsed_args.desc: payload['comment'] = parsed_args.desc - rest.post('/center/add', payload=payload) + res = rest.post('/center/add', payload=payload) + if not res: + return 1 + return 0 @staticmethod def update_center(rest, args): @@ -48,7 +51,10 @@ class OgCenter(): } if parsed_args.comment: payload['comment'] = parsed_args.comment - rest.post('/center/update', payload=payload) + res = rest.post('/center/update', payload=payload) + if not res: + return 1 + return 0 @staticmethod def delete_center(rest, args): @@ -60,4 +66,7 @@ class OgCenter(): help='center id in database') parsed_args = parser.parse_args(args) payload = {'id': parsed_args.id} - rest.post('/center/delete', payload=payload) + res = rest.post('/center/delete', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/client.py b/cli/objects/client.py index cf5e0b9..a2f8356 100644 --- a/cli/objects/client.py +++ b/cli/objects/client.py @@ -14,8 +14,11 @@ class OgClient(): @staticmethod def list_clients(rest): - r = rest.get('/clients') - print_json(r.text) + res = rest.get('/clients') + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def list_client_hardware(rest, args): @@ -28,8 +31,11 @@ class OgClient(): parsed_args = parser.parse_args(args) payload = {'client': parsed_args.client_ip} - r = rest.get('/hardware', payload=payload) - print_json(r.text) + res = rest.get('/hardware', payload=payload) + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def get_client_properties(rest, args): @@ -41,8 +47,11 @@ class OgClient(): parsed_args = parser.parse_args(args) payload = {'client': parsed_args.client_ip} - r = rest.get('/client/info', payload=payload) - print_json(r.text) + res = rest.get('/client/info', payload=payload) + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def request_refresh(rest, args): @@ -55,7 +64,10 @@ class OgClient(): parsed_args = parser.parse_args(args) payload = {'clients': parsed_args.client_ip} - rest.post('/refresh', payload=payload) + res = rest.post('/refresh', payload=payload) + if not res: + return 1 + return 0 @staticmethod def add_client(rest, args): @@ -82,8 +94,8 @@ class OgClient(): nargs='?', required=True, help='specify the ip address of the client') - r = rest.get('/mode') - boot_choices = r.json()['modes'] + res = rest.get('/mode') + boot_choices = res.json()['modes'] parser.add_argument('--boot-mode', nargs='?', required=True, @@ -100,7 +112,7 @@ class OgClient(): err = True if err: parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 payload = { 'name': parsed_args.hostname, 'netmask': '0', @@ -113,7 +125,10 @@ class OgClient(): 'netdriver': 'generic', 'livedir': 'ogLive' } - rest.post('/client/add', payload=payload) + res = rest.post('/client/add', payload=payload) + if not res: + return 1 + return 0 @staticmethod def delete_client(rest, args): @@ -127,4 +142,7 @@ class OgClient(): payload = { 'clients': parsed_args.ip, } - rest.post('/client/delete', payload=payload) + res = rest.post('/client/delete', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/disks.py b/cli/objects/disks.py index e00b760..81b404e 100644 --- a/cli/objects/disks.py +++ b/cli/objects/disks.py @@ -23,8 +23,11 @@ class OgDisk(): parsed_args = parser.parse_args(args) payload = {'client': [parsed_args.client_ip]} - r = rest.get('/client/setup', payload=payload) - print_json(r.text) + res = rest.get('/client/setup', payload=payload) + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def setup_disk(rest, args): @@ -79,8 +82,8 @@ class OgDisk(): help='Specific client IP') parsed_args = parser.parse_args(args) - r = rest.get('/scopes') - scopes = r.json() + res = rest.get('/scopes') + scopes = res.json() ips = set() for center in parsed_args.center_id: @@ -93,11 +96,11 @@ class OgDisk(): ips.add(l) if not ips: print("Missing --client-ip, or --room-id/--center-id. No clients provided.") - return None + return 1 if not parsed_args.num.isdigit(): print(f'Invalid disk number: must be an integer value') - return + return 1 payload = {'clients': parsed_args.client_ip, 'type': disk_type_map[parsed_args.type], 'disk': str(parsed_args.num), 'cache': '0', 'cache_size': '0', 'partition_setup': []} @@ -109,31 +112,31 @@ class OgDisk(): if len(p) != 4: print(f'Invalid partition: requires "num,part_scheme,fs,size", "{",".join(p)}" provided') - return + return 1 part_num, code, fs, size = p[0], p[1].upper(), p[2].upper(), p[3] if not part_num.isdigit(): print(f'Invalid partition: the first parameter must be a number, "{part_num}" provided') - return + return 1 if part_num in defined_part_indices: print(f'Invalid partition: partition number "{part_num}" has multiple definitions') - return + return 1 defined_part_indices.add(part_num) if code not in part_types: print(f'Invalid partition {i}: specified partition type {code} is not supported. The supported formats are {part_types}') - return + return 1 if fs not in fs_types: print(f'Invalid partition {i}: specified filesystem {fs} is not supported. The supported formats are {fs_types}') - return + return 1 try: size = parse_size(size) except ValueError as error: print(f'Invalid partition {i}: {str(error)}') - return + return 1 for j in range(i, int(part_num)): part = {'partition': str(j), 'code': 'EMPTY', @@ -158,4 +161,7 @@ class OgDisk(): 'format': '0'} payload['partition_setup'].append(part) - rest.post('/setup', payload=payload) + res = rest.post('/setup', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/folder.py b/cli/objects/folder.py index c4da900..2519446 100644 --- a/cli/objects/folder.py +++ b/cli/objects/folder.py @@ -39,7 +39,10 @@ class OgFolder(): payload['room'] = parsed_args.room_id if parsed_args.center_id: payload['center'] = parsed_args.center_id - rest.post('/folder/add', payload=payload) + res = restest.post('/folder/add', payload=payload) + if not res: + return 1 + return 0 @staticmethod def update_folder(rest, args): @@ -58,7 +61,10 @@ class OgFolder(): 'id': parsed_args.id, 'name': parsed_args.name, } - rest.post('/folder/update', payload=payload) + res = rest.post('/folder/update', payload=payload) + if not res: + return 1 + return 0 @staticmethod def delete_folder(rest, args): @@ -70,4 +76,7 @@ class OgFolder(): help='folder id in database') parsed_args = parser.parse_args(args) payload = {'id': parsed_args.id} - rest.post('/folder/delete', payload=payload) + res = rest.post('/folder/delete', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/images.py b/cli/objects/images.py index e6e61d0..10f6fd5 100644 --- a/cli/objects/images.py +++ b/cli/objects/images.py @@ -21,10 +21,10 @@ def get_repository(repository_id, rest): def get_repositories(rest): - r = rest.get('/repositories') - if not r or r.status_code != requests.codes.ok: + res = rest.get('/repositories') + if not res: return None - repositories = r.json()['repositories'] + repositories = res.json()['repositories'] return repositories @@ -32,8 +32,11 @@ class OgImage(): @staticmethod def list_images(rest): - r = rest.get('/images') - print_json(r.text) + res = rest.get('/images') + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def restore_image(rest, args): @@ -78,8 +81,8 @@ class OgImage(): help='Specific client IP') parsed_args = parser.parse_args(args) - r = rest.get('/scopes') - scopes = r.json() + res = rest.get('/scopes') + scopes = res.json() ips = set() for center in parsed_args.center_id: @@ -93,34 +96,37 @@ class OgImage(): if not ips: print('Missing --client-ip, or --room-id/--center-id. No clients provided.') - return + return 1 - r = rest.get('/images') - images = r.json() + res = rest.get('/images') + images = res.json() found_image = [img for img in images['images'] if img['id'] == parsed_args.id] if not found_image: print(f'Image with id {parsed_args.id} not found.') - return + return 1 else: found_image = found_image[0] selected_repo_id = 0 for ip in parsed_args.client_ip: - r = rest.get('/client/info', payload={'client': ip}) - repo_id = r.json()['repo_id'] + res = rest.get('/client/info', payload={'client': ip}) + repo_id = res.json()['repo_id'] if selected_repo_id == 0: selected_repo_id = repo_id elif selected_repo_id != repo_id: print(f'cannot restore clients assigned to different repositories') - return + return 1 payload = {'disk': parsed_args.disk, 'partition': parsed_args.part, 'id': str(parsed_args.id), 'name': found_image['name'], 'profile': str(found_image['software_id']), 'repository_id': repo_id, 'type': parsed_args.type.upper(), 'clients': list(ips)} - r = rest.post('/image/restore', payload=payload) + res = rest.post('/image/restore', payload=payload) + if not res: + return 1 + return 0 @staticmethod def create_image(rest, args): @@ -149,19 +155,19 @@ class OgImage(): help='Specific client IP') parsed_args = parser.parse_args(args) - r = rest.get('/client/info', payload={'client': parsed_args.client_ip}) - center_id = r.json()['center'] - repo_id = r.json()['repo_id'] + res = rest.get('/client/info', payload={'client': parsed_args.client_ip}) + center_id = res.json()['center'] + repo_id = res.json()['repo_id'] - r = rest.get('/client/setup', + res = rest.get('/client/setup', payload={'client': parsed_args.client_ip}) - if r.status_code == 200: + if res.status_code == 200: part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and x['partition'] == int(parsed_args.part), - r.json()['partitions'])) + res.json()['partitions'])) if not part_info: print('Partition not found.') - return + return 1 fs_code = list(part_info)[0]['code'] image_name = remove_accents(parsed_args.name) @@ -171,7 +177,10 @@ class OgImage(): if parsed_args.desc: payload['description'] = parsed_args.desc - rest.post('/image/create', payload=payload) + res = rest.post('/image/create', payload=payload) + if not res: + return 1 + return 0 @staticmethod def update_image(rest, args): @@ -202,32 +211,32 @@ class OgImage(): help='Specific client IP') parsed_args = parser.parse_args(args) - r = rest.get('/client/info', payload={'client': parsed_args.client_ip}) - center_id = r.json()['center'] - repo_id = int(r.json()['repo_id']) + res = rest.get('/client/info', payload={'client': parsed_args.client_ip}) + center_id = res.json()['center'] + repo_id = int(res.json()['repo_id']) - r = rest.get('/client/setup', + res = rest.get('/client/setup', payload={'client': parsed_args.client_ip}) - if r.status_code == 200: + if res.status_code == 200: part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and x['partition'] == int(parsed_args.part), - r.json()['partitions'])) + res.json()['partitions'])) if not part_info: print('Partition not found.') - return + return 1 fs_code = list(part_info)[0]['code'] - r = rest.get('/images') + res = rest.get('/images') image_name = None - if r.status_code == 200: - for image in r.json()['images']: + if res.status_code == 200: + for image in res.json()['images']: if image['id'] == int(parsed_args.id): image_name = image['name'] break if not image_name: print('Image not found') - return + return 1 else: print(f'Updating {image_name} image') @@ -241,4 +250,7 @@ class OgImage(): 'code': str(fs_code), 'name': image_name} - rest.post('/image/update', payload=payload) + res = rest.post('/image/update', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/live.py b/cli/objects/live.py index c90a4c2..7a057bf 100644 --- a/cli/objects/live.py +++ b/cli/objects/live.py @@ -13,5 +13,8 @@ class OgLive(): @staticmethod def list_live(rest): - r = rest.get('/oglive/list') - print_json(r.text) + res = rest.get('/oglive/list') + if not res: + return 1 + print_json(res.text) + return 0 diff --git a/cli/objects/modes.py b/cli/objects/modes.py index 7cf78e2..2374777 100644 --- a/cli/objects/modes.py +++ b/cli/objects/modes.py @@ -14,8 +14,11 @@ class OgModes(): @staticmethod def list_available_modes(rest): - r = rest.get('/mode') - print_json(r.text) + res = rest.get('/mode') + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def set_modes(rest, args): @@ -44,8 +47,8 @@ class OgModes(): help='Boot mode to be set') parsed_args = parser.parse_args(args) - r = rest.get('/scopes') - scopes = r.json() + res = rest.get('/scopes') + scopes = res.json() ips = set() for center in parsed_args.center_id: @@ -59,7 +62,10 @@ class OgModes(): if not ips: print("Missing --client-ip, or --room-id/--center-id. No clients provided.") - return None + return 1 payload = {'clients': list(ips), 'mode': parsed_args.mode[0]} - r = rest.post('/mode', payload=payload) + res = rest.post('/mode', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/poweroff.py b/cli/objects/poweroff.py index af309bf..ab14b0d 100644 --- a/cli/objects/poweroff.py +++ b/cli/objects/poweroff.py @@ -36,8 +36,8 @@ class OgPoweroff(): help='Specific client IP') parsed_args = parser.parse_args(args) - r = rest.get('/scopes') - scopes = r.json() + res = rest.get('/scopes') + scopes = res.json() ips = set() for center in parsed_args.center_id: @@ -51,7 +51,10 @@ class OgPoweroff(): if not ips: print("Missing --client-ip, or --room-id/--center-id. No clients provided.") - return None + return 1 payload = {'clients': list(ips)} - r = rest.post('/poweroff', payload=payload) + res = rest.post('/poweroff', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/reboot.py b/cli/objects/reboot.py index ca2ee9f..4f7f755 100644 --- a/cli/objects/reboot.py +++ b/cli/objects/reboot.py @@ -36,8 +36,8 @@ class OgReboot(): help='Specific client IP') parsed_args = parser.parse_args(args) - r = rest.get('/scopes') - scopes = r.json() + res = rest.get('/scopes') + scopes = res.json() ips = set() for center in parsed_args.center_id: @@ -51,7 +51,10 @@ class OgReboot(): if not ips: print("Missing --client-ip, or --room-id/--center-id. No clients provided.") - return None + return 1 payload = {'clients': list(ips)} - r = rest.post('/reboot', payload=payload) + res = rest.post('/reboot', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/repo.py b/cli/objects/repo.py index 7085633..d172a0d 100644 --- a/cli/objects/repo.py +++ b/cli/objects/repo.py @@ -13,8 +13,11 @@ class OgRepo(): @staticmethod def list_repos(rest): - r = rest.get('/repositories') - print_json(r.text) + res = rest.get('/repositories') + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def _add_repo(rest, parsed_args): @@ -22,37 +25,43 @@ class OgRepo(): 'addr': parsed_args.ip, 'name': parsed_args.name, } - rest.post('/repository/add', payload=payload) + res = rest.post('/repository/add', payload=payload) + if not res: + return 1 + return 0 @staticmethod def _add_repo_ip(rest, parsed_args): for ip in parsed_args.ip: if not check_address(ip): print(f'Invalid IP address: {ip}') - return + return 1 - r = rest.get('/repositories') + res = rest.get('/repositories') target_repo = None - for repo in r.json()['repositories']: + for repo in res.json()['repositories']: if repo['id'] == parsed_args.id: target_repo = repo break if not target_repo: print('Invalid repository id specified') - return + return 1 for ip in parsed_args.ip: if ip in target_repo['addr']: print(f'The repository already contains the address {ip}') - return + return 1 payload = { 'id': parsed_args.id, 'addr': target_repo['addr'] + parsed_args.ip, 'name': target_repo['name'], } - rest.post('/repository/update', payload=payload) + res = rest.post('/repository/update', payload=payload) + if not res: + return 1 + return 0 @staticmethod def add_repo(rest, args): @@ -101,30 +110,33 @@ class OgRepo(): 'name': parsed_args.name, } - rest.post('/repository/update', payload=payload) + res = rest.post('/repository/update', payload=payload) + if not res: + return 1 + return 0 @staticmethod def _delete_repo_ip(rest, parsed_args): for ip in parsed_args.ip: if not check_address(ip): print(f'Invalid IP address: {ip}') - return + return 1 - r = rest.get('/repositories') + res = rest.get('/repositories') target_repo = None - for repo in r.json()['repositories']: + for repo in res.json()['repositories']: if repo['id'] == parsed_args.id: target_repo = repo break if not target_repo: print('Invalid repository id specified') - return + return 1 for ip in parsed_args.ip: if ip not in target_repo['addr']: print(f'Invalid address {ip}: The repository has the following IPs: {target_repo["addr"]}') - return + return 1 target_repo['addr'].remove(ip) @@ -133,12 +145,18 @@ class OgRepo(): 'addr': target_repo['addr'], 'name': target_repo['name'], } - rest.post('/repository/update', payload=payload) + res = rest.post('/repository/update', payload=payload) + if not res: + return 1 + return 0 @staticmethod def _delete_repo(rest, parsed_args): payload = {'id': parsed_args.id} - rest.post('/repository/delete', payload=payload) + res = rest.post('/repository/delete', payload=payload) + if not res: + return 1 + return 0 @staticmethod def delete_repo(rest, args): @@ -184,8 +202,8 @@ class OgRepo(): help='Any valid client ip address') parsed_args = parser.parse_args(args) - r = rest.get('/scopes') - scopes = r.json() + res = rest.get('/scopes') + scopes = res.json() ips = set() for room in parsed_args.room_id: @@ -195,7 +213,10 @@ class OgRepo(): ips.add(l) if not ips: print('No valid clients specified.') - return + return 1 payload = {'id': parsed_args.id, 'clients': list(ips)} - rest.post('/client/repo', payload=payload) + res = rest.post('/client/repo', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/room.py b/cli/objects/room.py index f22c843..da9a872 100644 --- a/cli/objects/room.py +++ b/cli/objects/room.py @@ -65,7 +65,7 @@ class OgRoom(): err = True if err: parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 payload = { 'name': parsed_args.name, @@ -82,7 +82,10 @@ class OgRoom(): payload['folder_id'] = parsed_args.folder if parsed_args.desc: payload['location'] = parsed_args.desc - rest.post('/room/add', payload=payload) + res = rest.post('/room/add', payload=payload) + if not res: + return 1 + return 0 @staticmethod def update_room(rest, args): @@ -115,7 +118,7 @@ class OgRoom(): err = True if err: parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 payload = { 'id': parsed_args.id, @@ -124,7 +127,10 @@ class OgRoom(): 'gateway': parsed_args.gateway, } - rest.post('/room/update', payload=payload) + res = rest.post('/room/update', payload=payload) + if not res: + return 1 + return 0 @staticmethod def delete_room(rest, args): @@ -136,5 +142,8 @@ class OgRoom(): help='room id in database') parsed_args = parser.parse_args(args) payload = {'id': parsed_args.id} - rest.post('/room/delete', payload=payload) + res = rest.post('/room/delete', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/scopes.py b/cli/objects/scopes.py index 548b93c..5779f78 100644 --- a/cli/objects/scopes.py +++ b/cli/objects/scopes.py @@ -57,7 +57,7 @@ class OgScope(): for ip in parsed_args.client_ip: ips.add(ip) - r = rest.get('/scopes') + res = rest.get('/scopes') json_data = json.loads(r.text) if parsed_args.name: @@ -71,8 +71,7 @@ class OgScope(): path = _get_client_path(json_data, client_ip, None) for i, item in enumerate(path): print(' ' * i + item) - return None else: print_json(r.text) - return None + return 0 diff --git a/cli/objects/server.py b/cli/objects/server.py index 468ff00..26b2c50 100644 --- a/cli/objects/server.py +++ b/cli/objects/server.py @@ -13,8 +13,11 @@ class OgServer(): @staticmethod def list_servers(rest): - r = rest.get('/server') - print_json(r.text) + res = rest.get('/server') + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def add_server(rest, args): @@ -27,10 +30,13 @@ class OgServer(): if not check_address(parsed_args.ip): print(f'Invalid IP address: {parsed_args.ip}') - return + return 1 payload = {'address': parsed_args.ip} - rest.post('/server', payload=payload) + res = rest.post('/server', payload=payload) + if not res: + return 1 + return 0 @staticmethod def delete_server(rest, args): @@ -42,4 +48,7 @@ class OgServer(): help='server id in the database') parsed_args = parser.parse_args(args) payload = {'id': parsed_args.id} - rest.delete('/server', payload=payload) + res = rest.delete('/server', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/session.py b/cli/objects/session.py index 0168cb3..4fde4d0 100644 --- a/cli/objects/session.py +++ b/cli/objects/session.py @@ -44,8 +44,8 @@ class OgSession(): help='Specific client IP') parsed_args = parser.parse_args(args) - r = rest.get('/scopes') - scopes = r.json() + res = rest.get('/scopes') + scopes = res.json() ips = set() for center in parsed_args.center_id: @@ -59,11 +59,14 @@ class OgSession(): if not ips: print("Missing --client-ip, or --room-id/--center-id. No clients provided.") - return None + return 1 payload = { 'clients': list(ips), 'disk': parsed_args.disk, 'partition': parsed_args.part, } - r = rest.post('/session', payload=payload) + res = rest.post('/session', payload=payload) + if not res: + return 1 + return 0 diff --git a/cli/objects/wol.py b/cli/objects/wol.py index 9eba702..7284895 100644 --- a/cli/objects/wol.py +++ b/cli/objects/wol.py @@ -53,8 +53,8 @@ class OgWol(): help='Specific client IP') parsed_args = parser.parse_args(args) - r = rest.get('/scopes') - scopes = r.json() + res = rest.get('/scopes') + scopes = res.json() ips = set() for center in parsed_args.center_id: @@ -68,7 +68,10 @@ class OgWol(): if not ips: print("Missing --client-ip, or --room-id/--center-id. No clients provided.") - return None + return 1 payload = {'type': parsed_args.type, 'clients': list(ips)} - r = rest.post('/wol', payload=payload) + res = rest.post('/wol', payload=payload) + if not res: + return 1 + return 0 diff --git a/ogcli b/ogcli index b2cbe38..f4b55d5 100755 --- a/ogcli +++ b/ogcli @@ -59,7 +59,8 @@ class CLI(): sys.exit(1) # Call the command with the same name. - getattr(self.ogcli, args.command)(sys.argv[2:]) + res = getattr(self.ogcli, args.command)(sys.argv[2:]) + sys.exit(res) if __name__ == "__main__": -- cgit v1.2.3-18-g5258