From dff298d421afcbe2bdd2246be961d5e963b7fdd0 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Tue, 18 Oct 2022 17:07:04 +0200 Subject: create image: split into create and update Image creation example: ogcli create image --disk --part 1 --name 18oct2 --desc 18oct2 --repo-id 1 --client-ip 192.168.56.11 Image update example: ogcli update image --disk 1 --part 1 --id 20 --client-ip 192.168.56.11 --- cli/cli.py | 9 +++++++ cli/objects/images.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/cli/cli.py b/cli/cli.py index 7d3142d..6837852 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -125,3 +125,12 @@ class OgCLI(): if parsed_args.setup_obj == 'disk': OgDisk.setup_disk(self.rest, args[1:]) + + def update(self, args): + choices = ['image'] + parser = argparse.ArgumentParser(prog='ogcli update') + parser.add_argument('update_obj', choices=choices) + parsed_args = parser.parse_args([args[0]]) + + if parsed_args.update_obj == 'image': + OgImage.update_image(self.rest, args[1:]) diff --git a/cli/objects/images.py b/cli/objects/images.py index 2f9f879..3bed476 100644 --- a/cli/objects/images.py +++ b/cli/objects/images.py @@ -116,12 +116,13 @@ class OgImage(): help='Image name') parser.add_argument('--desc', nargs='?', - required=False, - help='Image description (for new images)') + required=True, + help='Image description') parser.add_argument('--repo-id', nargs='?', default=1, - help='Images repository id') + type=int, + help='Images repository id (Default: 1)') group = parser.add_argument_group('clients', 'Client selection args') group.add_argument('--client-ip', action='append', @@ -152,3 +153,70 @@ class OgImage(): payload['repository_id'] = parsed_args.repo_id rest.post('/image/create', payload=payload) + + @staticmethod + def update_image(rest, args): + parser = argparse.ArgumentParser() + parser.add_argument('--disk', + nargs='?', + required=True, + help='Disk') + parser.add_argument('--part', + nargs='?', + required=True, + help='Partition') + parser.add_argument('--id', + nargs='?', + required=True, + help='Image id') + parser.add_argument('--repo-id', + nargs='?', + default=1, + type=int, + help='Images repository id (Default: 1)') + group = parser.add_argument_group('clients', 'Client selection args') + group.add_argument('--client-ip', + action='append', + default=[], + required=True, + 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'] + + r = rest.get('/client/setup', + payload={'client': parsed_args.client_ip}) + if r.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'])) + if not part_info: + print('Partition not found.') + return + fs_code = list(part_info)[0]['code'] + + r = rest.get('/images') + image_name = None + if r.status_code == 200: + for image in r.json()['images']: + if image['id'] == int(parsed_args.id): + image_name = image['name'] + break + + if not image_name: + print('Image not found') + return + else: + print(f'Updating {image_name} image') + + payload = {'clients': parsed_args.client_ip, + 'disk': parsed_args.disk, + 'center_id': center_id, + 'partition': parsed_args.part, + 'code': str(fs_code), + 'id': parsed_args.id, + 'name': image_name, + 'repository_id': parsed_args.repo_id} + + rest.post('/image/create', payload=payload) -- cgit v1.2.3-18-g5258