diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/cli.py | 6 | ||||
-rw-r--r-- | cli/config.py | 31 | ||||
-rw-r--r-- | cli/objects/live.py | 22 | ||||
-rw-r--r-- | cli/objects/scopes.py | 6 |
4 files changed, 47 insertions, 18 deletions
@@ -20,7 +20,7 @@ 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 config import cfg +from cli.config import cfg import argparse import requests import sys @@ -82,7 +82,7 @@ class OgCLI(): self.rest = OgREST(cfg['ip'], cfg['port'], cfg['api_token']) def list(self, args): - choices = ['clients', 'scope', 'modes', 'hardware', + choices = ['clients', 'scopes', 'modes', 'hardware', 'client', 'images', 'disks', 'servers', 'repos', 'live'] parser = argparse.ArgumentParser(prog='ogcli list') parser.add_argument('item', choices=choices) @@ -102,7 +102,7 @@ class OgCLI(): 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 == 'scope': + elif parsed_args.item == 'scopes': ret = OgScope.list_scopes(self.rest, args[1:]) elif parsed_args.item == 'images': ret = OgImage.list_images(self.rest) diff --git a/cli/config.py b/cli/config.py new file mode 100644 index 0000000..5a79698 --- /dev/null +++ b/cli/config.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2020-2024 Soleta Networks <opengnsys@soleta.eu> +# +# 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. + +import json + +OG_CLI_CFG_PATH = "/opt/opengnsys/etc/ogcli.json" + +def load_config(): + data = None + try: + with open(OG_CLI_CFG_PATH, 'r') as json_file: + data = json.load(json_file) + except json.JSONDecodeError: + sys.exit(f'ERROR: Failed parse malformed JSON file {OG_CLI_CFG_PATH}') + except Exception as e: + sys.exit(f'ERROR: cannot open {OG_CLI_CFG_PATH}: {e}') + + required_cfg_params = {'api_token', 'ip', 'port'} + difference_cfg_params = required_cfg_params - data.keys() + if len(difference_cfg_params) > 0: + sys.exit(f'Missing {difference_cfg_params} key in ' + f'json config file') + return data + +cfg = load_config() diff --git a/cli/objects/live.py b/cli/objects/live.py index e7403b5..ca8824a 100644 --- a/cli/objects/live.py +++ b/cli/objects/live.py @@ -97,7 +97,6 @@ class OgLive(): print(f'Request failed for {file_url}: {e}') return 1 - err = 0 if response.status_code == 200: try: with open(local_path, 'wb') as f: @@ -106,17 +105,9 @@ class OgLive(): f.write(chunk) except OSError as e: print(f'File system error occurred: {e}') - err = 1 + return 1 else: print(f'ERROR: Failed to download {live_file}. Status code: {response.status_code}') - err = 1 - - if err: - try: - if os.path.exists(local_path): - os.remove(local_path) - except OSError as e: - print(f'ERROR: Failed to delete file {live_file}: {e}') return 1 return 0 @@ -137,6 +128,7 @@ class OgLive(): download_err = OgLive._download_from_server('ogrelive.json', local_extension=OgLive.tmp_extension) if download_err: + OgLive._delete_tmp_live_files('') return 1 remote_json = os.path.join(local_live_dir, 'ogrelive.json') @@ -211,6 +203,7 @@ class OgLive(): live_file + '.full.sum'), local_extension=OgLive.tmp_extension) if download_err: + OgLive._delete_tmp_live_files(live_name) return download_err file_path = os.path.join(local_dir, live_file) @@ -231,6 +224,7 @@ class OgLive(): live_file), local_extension=OgLive.tmp_extension) if download_err: + OgLive._delete_tmp_live_files(live_name) return download_err if not OgLive._is_same_checksum(file_path_tmp, checksum_path_tmp): @@ -239,6 +233,12 @@ class OgLive(): return 1 print(f'Checksum is OK for {live_file}') + + for file_name in os.listdir(local_dir): + if not file_name.endswith(OgLive.tmp_extension): + continue + file_path_tmp = os.path.join(local_dir, file_name) + file_path = file_path_tmp[:-len(OgLive.tmp_extension)] try: shutil.move(file_path_tmp, file_path) except OSError as e: @@ -246,8 +246,6 @@ class OgLive(): OgLive._delete_tmp_live_files(live_name) return 1 - OgLive._delete_tmp_live_files(live_name) - payload = {'name': live_name} res = rest.post('/oglive/add', payload=payload) diff --git a/cli/objects/scopes.py b/cli/objects/scopes.py index 5779f78..88f5521 100644 --- a/cli/objects/scopes.py +++ b/cli/objects/scopes.py @@ -41,7 +41,7 @@ class OgScope(): @staticmethod def list_scopes(rest, args): - parser = argparse.ArgumentParser(prog='ogcli list scope') + parser = argparse.ArgumentParser(prog='ogcli list scopes') group = parser.add_mutually_exclusive_group(required=False) group.add_argument('--client-ip', action='append', @@ -58,7 +58,7 @@ class OgScope(): ips.add(ip) res = rest.get('/scopes') - json_data = json.loads(r.text) + json_data = json.loads(res.text) if parsed_args.name: path = _get_client_path(json_data, None, parsed_args.name) @@ -72,6 +72,6 @@ class OgScope(): for i, item in enumerate(path): print(' ' * i + item) else: - print_json(r.text) + print_json(res.text) return 0 |