# Copyright (C) 2020-2024 Soleta Networks # # This program is free software: you can redistribute it and/or modify it under # the terms of the GNU Affero General Public License as published by the # Free Software Foundation; either version 3 of the License, or # (at your option) any later version. from urllib.parse import urlparse from cli.utils import * import argparse import requests def get_repository(repository_id, rest): repositories = get_repositories(rest) if not repositories: return None [repository] = [repository for repository in repositories if repository['id'] == repository_id] return repository def get_repositories(rest): res = rest.get('/repositories') if not res: return None repositories = res.json()['repositories'] return repositories class OgImage(): @staticmethod def list_images(rest): res = rest.get('/images') if not res: return 1 print_json(res.text) return 0 @staticmethod def restore_image(rest, args): parser = argparse.ArgumentParser(prog='ogcli restore image') 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=False, choices=['unicast', 'unicast-direct', 'tiptorrent'], default='tiptorrent', help='Transfer method. (Default: tiptorrent)') 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) res = rest.get('/scopes') scopes = res.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('Missing --client-ip, or --room-id/--center-id. No clients provided.') return 1 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 1 else: found_image = found_image[0] selected_repo_id = 0 for ip in parsed_args.client_ip: 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 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)} res = rest.post('/image/restore', payload=payload) if not res: return 1 return 0 @staticmethod def create_image(rest, args): parser = argparse.ArgumentParser(prog='ogcli create image') parser.add_argument('--disk', nargs='?', required=True, help='Disk') parser.add_argument('--part', nargs='?', required=True, help='Partition') parser.add_argument('--name', nargs='?', required=True, help='Image name') parser.add_argument('--desc', nargs='?', required=True, help='Image description') 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) res = rest.get('/client/info', payload={'client': parsed_args.client_ip}) center_id = res.json()['center'] repo_id = res.json()['repo_id'] res = rest.get('/client/setup', payload={'client': parsed_args.client_ip}) if res.status_code == 200: part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and x['partition'] == int(parsed_args.part), res.json()['partitions'])) if not part_info: print('Partition not found.') return 1 fs_code = list(part_info)[0]['code'] image_name = remove_accents(parsed_args.name) payload = {'clients': parsed_args.client_ip, 'disk': parsed_args.disk, 'center_id': center_id, 'partition': parsed_args.part, 'code': str(fs_code), 'name': image_name, 'id': '0', 'repository_id': repo_id} if parsed_args.desc: payload['description'] = parsed_args.desc res = rest.post('/image/create', payload=payload) if not res: return 1 return 0 @staticmethod def update_image(rest, args): parser = argparse.ArgumentParser(prog='ogcli update image') parser.add_argument('--disk', nargs='?', required=True, help='Disk') parser.add_argument('--part', nargs='?', required=True, help='Partition') parser.add_argument('--id', type=int, nargs='?', required=True, help='Image id') parser.add_argument('--backup', action='store_true', required=False, default=False, help='Create a backup before updating the image (No backup is created by default)') 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) res = rest.get('/client/info', payload={'client': parsed_args.client_ip}) center_id = res.json()['center'] repo_id = int(res.json()['repo_id']) res = rest.get('/client/setup', payload={'client': parsed_args.client_ip}) if res.status_code == 200: part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and x['partition'] == int(parsed_args.part), res.json()['partitions'])) if not part_info: print('Partition not found.') return 1 fs_code = list(part_info)[0]['code'] res = rest.get('/images') image_name = None 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 1 else: print(f'Updating {image_name} image') payload = {'repository_id': repo_id, 'clients': parsed_args.client_ip, 'partition': parsed_args.part, 'backup': parsed_args.backup, 'disk': parsed_args.disk, 'center_id': center_id, 'id': str(parsed_args.id), 'code': str(fs_code), 'name': image_name} res = rest.post('/image/update', payload=payload) if not res: return 1 return 0