diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/cli.py | 9 | ||||
-rw-r--r-- | cli/objects/images.py | 74 |
2 files changed, 80 insertions, 3 deletions
@@ -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) |