diff options
author | Jose M. Guisado <jguisado@soleta.eu> | 2021-03-30 13:36:42 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2021-03-30 14:01:39 +0200 |
commit | 6fd3cb7a251c3c0e6b98947f420c3d5f3146e582 (patch) | |
tree | 56f9054878557b55ac18eb4767fce3d3675a44b9 /cli/objects/images.py | |
parent | 5abe86222892be4c21fe0d041d2e05d07ddcd7bb (diff) |
Add 'restore image' command
Adds cli capability to send image restore commands to the ogServer.
It has the following options:
--id
To specify image id
--disk
Target disk
--part
Target partition
--type
To specify type of restore to send. UNICAST-DIRECT or
UNICAST-CACHE are supported as of this patch.
--repo
Optional argument to specify image repo to fetch the image. This
applies for UNICAST-DIRECT cases. (not restoring from a cache
partition). If not specified it will default to rest.URL ip
(ie. ogServer ip)
--client-ip, --room-id and --center-id
Target client args
Diffstat (limited to 'cli/objects/images.py')
-rw-r--r-- | cli/objects/images.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/cli/objects/images.py b/cli/objects/images.py index 94c6386..723a7b0 100644 --- a/cli/objects/images.py +++ b/cli/objects/images.py @@ -6,9 +6,92 @@ # Free Software Foundation, version 3. # +from urllib.parse import urlparse +from cli.utils import * + +import argparse + class OgImage(): @staticmethod def list_images(rest): r = rest.get('/images') print(r.text) + + @staticmethod + def restore_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='?', + type=int, + required=True, + help='Image id to be restored') + parser.add_argument('--type', + nargs='?', + required=True, + choices=['unicast', 'unicast-direct'], + help='Image id to be restored') + parser.add_argument('--repo', + nargs='?', + default=urlparse(rest.URL).netloc, + help='Images repository ip') + group = parser.add_argument_group('clients', 'Client selection args') + group.add_argument('--center-id', + type=int, + action='append', + default=[], + required=False, + help='Clients from given center id') + group.add_argument('--room-id', + type=int, + action='append', + default=[], + required=False, + help='Clients from given room id') + group.add_argument('--client-ip', + action='append', + default=[], + required=False, + help='Specific client IP') + parsed_args = parser.parse_args(args) + + r = rest.get('/scopes') + scopes = r.json() + ips = set() + + for center in parsed_args.center_id: + center_scope = scope_lookup(center, 'center', scopes) + ips.update(ips_in_scope(center_scope)) + for room in parsed_args.room_id: + room_scope = scope_lookup(room, 'room', scopes) + ips.update(ips_in_scope(room_scope)) + for l in parsed_args.client_ip: + ips.add(l) + + if not ips: + print('No clients specified.') + return + + r = rest.get('/images') + images = r.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 + else: + found_image = found_image[0] + + 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': parsed_args.repo, + 'type': parsed_args.type.upper(), 'clients': list(ips)} + r = rest.post('/image/restore', payload=payload) |