# 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 cli.objects.client import OgClient from cli.objects.scopes import OgScope from cli.objects.modes import OgModes from cli.objects.wol import OgWol from cli.objects.images import OgImage from cli.objects.disks import OgDisk from cli.objects.poweroff import OgPoweroff from cli.objects.reboot import OgReboot from cli.objects.repo import OgRepo from cli.objects.server import OgServer from cli.objects.live import OgLive from cli.objects.center import OgCenter from cli.objects.room import OgRoom from cli.objects.folder import OgFolder from cli.objects.session import OgSession from cli.config import cfg import argparse import requests import sys def _log_http_status_code(res): if res.status_code == 400: print('Error 400: invalid payload') elif res.status_code == 404: print('Error 404: object not found') elif res.status_code == 405: print('Error 405: method not allowed') elif res.status_code == 409: print('Error 409: object already exists') elif res.status_code == 423: print('Error 423: object in use') elif res.status_code == 501: print('Error 501: cannot connect to database') elif res.status_code == 507: print('Error 500: disk full') else: print(f'Received status code {res.status_code}') class OgREST(): def __init__(self, ip, port, api_token): self.URL = f'http://{ip}:{port}' self.HEADERS = {'Authorization': api_token} def get(self, path, payload=None): try: res = requests.get(f'{self.URL}{path}', headers=self.HEADERS, json=payload) if res.status_code != 200: _log_http_status_code(res) 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: res = requests.post(f'{self.URL}{path}', headers=self.HEADERS, json=payload) if res.status_code not in {200, 202}: _log_http_status_code(res) 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: res = requests.delete(f'{self.URL}{path}', headers=self.HEADERS, json=payload) if res.status_code != 200: _log_http_status_code(res) return None except IOError as e: print(f"Error: Cannot connect to ogServer: {e}") return None return res class OgCLI(): def __init__(self): self.rest = OgREST(cfg['ip'], cfg['port'], cfg['api_token']) def list(self, args): choices = ['clients', 'scopes', 'modes', 'hardware', 'client', 'images', 'disks', 'servers', 'repos', 'live', 'lives'] parser = argparse.ArgumentParser(prog='ogcli list') parser.add_argument('item', choices=choices) if not args: print('Missing list subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) return 1 parsed_args = parser.parse_args([args[0]]) ret = 0 if parsed_args.item == 'clients': ret = OgClient.list_clients(self.rest) elif parsed_args.item == 'client': ret = OgClient.get_client_properties(self.rest, args[1:]) elif parsed_args.item == 'hardware': ret = OgClient.list_client_hardware(self.rest, args[1:]) elif parsed_args.item == 'modes': ret = OgModes.list_available_modes(self.rest) elif parsed_args.item == 'scopes': ret = OgScope.list_scopes(self.rest, args[1:]) elif parsed_args.item == 'images': ret = OgImage.list_images(self.rest) elif parsed_args.item == 'disks': ret = OgDisk.list_disks(self.rest, args[1:]) elif parsed_args.item == 'repos': ret = OgRepo.list_repos(self.rest) elif parsed_args.item == 'servers': ret = OgServer.list_servers(self.rest) elif parsed_args.item in ['live', 'lives']: ret = OgLive.list_live(self.rest, args[1:]) return ret def set(self, args): choices = ['modes', 'mode', 'repo', 'live', 'lives'] parser = argparse.ArgumentParser(prog='ogcli set') parser.add_argument('item', choices=choices) if not args: print('Missing set subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) return 1 parsed_args = parser.parse_args([args[0]]) ret = 0 if parsed_args.item in ['modes', 'mode']: ret = OgModes.set_modes(self.rest, args[1:]) elif parsed_args.item == 'repo': ret = OgRepo.set_repo(self.rest, args[1:]) elif parsed_args.item in ['live', 'lives']: ret = OgLive.set_live(self.rest, args[1:]) return ret def request(self, args): choices = ['reboot', 'refresh', 'poweroff', 'wol', 'session'] parser = argparse.ArgumentParser(prog='ogcli request') parser.add_argument('request_obj', choices=choices) if not args: print('Missing request subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) return 1 parsed_args = parser.parse_args([args[0]]) ret = 0 if parsed_args.request_obj == 'wol': ret = OgWol.request_wol(self.rest, args[1:]) elif parsed_args.request_obj == 'poweroff': ret = OgPoweroff.request_poweroff(self.rest, args[1:]) elif parsed_args.request_obj == 'refresh': ret = OgClient.request_refresh(self.rest, args[1:]) elif parsed_args.request_obj == 'reboot': ret = OgReboot.request_reboot(self.rest, args[1:]) elif parsed_args.request_obj == 'session': ret = OgSession.request_session(self.rest, args[1:]) return ret def restore(self, args): choices = ['image'] parser = argparse.ArgumentParser(prog='ogcli restore') parser.add_argument('restore_obj', choices=choices) if not args: print('Missing restore subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) return 1 parsed_args = parser.parse_args([args[0]]) ret = 0 if parsed_args.restore_obj == 'image': ret = OgImage.restore_image(self.rest, args[1:]) return ret def create(self, args): choices = ['image'] parser = argparse.ArgumentParser(prog='ogcli create') parser.add_argument('create_obj', choices=choices) if not args: print('Missing create subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) return 1 parsed_args = parser.parse_args([args[0]]) ret = 0 if parsed_args.create_obj == 'image': ret = OgImage.create_image(self.rest, args[1:]) return ret def setup(self, args): choices = ['disk'] parser = argparse.ArgumentParser(prog='ogcli setup') parser.add_argument('setup_obj', choices=choices) if not args: print('Missing setup subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) return 1 parsed_args = parser.parse_args([args[0]]) ret = 0 if parsed_args.setup_obj == 'disk': ret = OgDisk.setup_disk(self.rest, args[1:]) return ret def update(self, args): choices = ['image', 'repo', 'center', 'room', 'folder'] parser = argparse.ArgumentParser(prog='ogcli update') parser.add_argument('update_obj', choices=choices) if not args: print('Missing update subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) return 1 parsed_args = parser.parse_args([args[0]]) ret = 0 if parsed_args.update_obj == 'image': ret = OgImage.update_image(self.rest, args[1:]) elif parsed_args.update_obj == 'center': ret = OgCenter.update_center(self.rest, args[1:]) elif parsed_args.update_obj == 'room': ret = OgRoom.update_room(self.rest, args[1:]) elif parsed_args.update_obj == 'folder': ret = OgFolder.update_folder(self.rest, args[1:]) elif parsed_args.update_obj == 'repo': ret = OgRepo.update_repo(self.rest, args[1:]) return ret def delete(self, args): choices = ['server', 'repo', 'center', 'room', 'client', 'folder', 'live', 'lives'] parser = argparse.ArgumentParser(prog='ogcli delete') parser.add_argument('delete_obj', choices=choices) if not args: print('Missing delete subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) return 1 parsed_args = parser.parse_args([args[0]]) if parsed_args.delete_obj == 'server': ret = OgServer.delete_server(self.rest, args[1:]) elif parsed_args.delete_obj == 'repo': ret = OgRepo.delete_repo(self.rest, args[1:]) elif parsed_args.delete_obj == 'center': ret = OgCenter.delete_center(self.rest, args[1:]) elif parsed_args.delete_obj == 'room': ret = OgRoom.delete_room(self.rest, args[1:]) elif parsed_args.delete_obj == 'client': ret = OgClient.delete_client(self.rest, args[1:]) elif parsed_args.delete_obj == 'folder': ret = OgFolder.delete_folder(self.rest, args[1:]) elif parsed_args.delete_obj in ['live', 'lives']: ret = OgLive.delete_live(self.rest, args[1:]) return ret def add(self, args): choices = ['server', 'repo', 'center', 'room', 'client', 'folder'] parser = argparse.ArgumentParser(prog='ogcli add') parser.add_argument('add_obj', choices=choices) if not args: print('Missing add subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) return 1 parsed_args = parser.parse_args([args[0]]) ret = 0 if parsed_args.add_obj == 'server': ret = OgServer.add_server(self.rest, args[1:]) elif parsed_args.add_obj == 'repo': ret = OgRepo.add_repo(self.rest, args[1:]) elif parsed_args.add_obj == 'center': ret = OgCenter.add_center(self.rest, args[1:]) elif parsed_args.add_obj == 'room': ret = OgRoom.add_room(self.rest, args[1:]) elif parsed_args.add_obj == 'client': ret = OgClient.add_client(self.rest, args[1:]) elif parsed_args.add_obj == 'folder': ret = OgFolder.add_folder(self.rest, args[1:]) return ret def install(self, args): choices = ['live', 'lives'] parser = argparse.ArgumentParser(prog='ogcli install') parser.add_argument('install_obj', choices=choices) if not args: print('Missing install subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) return 1 parsed_args = parser.parse_args([args[0]]) ret = 0 if parsed_args.install_obj in ['live', 'lives']: ret = OgLive.install_live(self.rest, args[1:]) return ret