summaryrefslogtreecommitdiffstats
path: root/cli/cli.py
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-11-21 16:46:18 +0100
committerOpenGnSys Support Team <soporte-og@soleta.eu>2024-11-21 17:05:24 +0100
commit10a3897f92ee058155987cea23eec28b28377d4b (patch)
tree9bbc4f0bb696adbb4c97b5c524b674eff8d1a853 /cli/cli.py
parentea8210a805e860154e8b0b9446bdd30043b893e6 (diff)
cli: ensure the program returns 0 on success and 1 on error
propagate a returncode in each operation and make it the returncode of the program. Prevent sys.exit calls in post(), get() and delete() request handlers to enable cleanup code and error handling. Keep a basic error log inside the request functions if the connection can't be established or if the response contains an error code.
Diffstat (limited to 'cli/cli.py')
-rw-r--r--cli/cli.py160
1 files changed, 91 insertions, 69 deletions
diff --git a/cli/cli.py b/cli/cli.py
index ed65a4f..89c3b73 100644
--- a/cli/cli.py
+++ b/cli/cli.py
@@ -32,43 +32,48 @@ class OgREST():
def get(self, path, payload=None):
try:
- r = requests.get(f'{self.URL}{path}',
+ res = requests.get(f'{self.URL}{path}',
headers=self.HEADERS,
json=payload)
- if r.status_code != 200:
- sys.exit(f"Unsuccessful request to ogServer: "
- f"Response with {r.status_code} HTTP status code")
- except IOError as e:
- sys.exit(f"Cannot connect to ogServer: {e}")
- return r
+
+ if res.status_code != 200:
+ print(f"Error: Request to ogServer failed with status code {res.status_code}")
+ 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:
- r = requests.post(f'{self.URL}{path}',
+ res = requests.post(f'{self.URL}{path}',
headers=self.HEADERS,
json=payload)
- if r.text:
- print(r.text)
- if r.status_code not in {200, 202}:
- sys.exit(f"Unsuccessful request to ogServer: "
- f"Response with {r.status_code} HTTP status code")
- except IOError as e:
- sys.exit(f"Cannot connect to ogServer: {e}")
- return r
+ if res.status_code not in {200, 202}:
+ print(f"Error: Request to ogServer failed with status code {res.status_code}")
+ 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:
- r = requests.delete(f'{self.URL}{path}',
+ res = requests.delete(f'{self.URL}{path}',
headers=self.HEADERS,
json=payload)
- print(r.text)
- if r.status_code != 200:
- sys.exit(f"Unsuccessful request to ogServer: "
- f"Response with {r.status_code} HTTP status code")
+ if res.status_code != 200:
+ print(f"Error: Request to ogServer failed with status code {res.status_code}")
+ return None
except IOError as e:
- sys.exit(f"Cannot connect to ogServer: {e}")
- return r
+ print(f"Error: Cannot connect to ogServer: {e}")
+ return None
+ return res
class OgCLI():
@@ -84,29 +89,31 @@ class OgCLI():
if not args:
print('Missing list subcommand', file=sys.stderr)
parser.print_help(file=sys.stderr)
- sys.exit(1)
+ return 1
parsed_args = parser.parse_args([args[0]])
+ ret = 0
if parsed_args.item == 'clients':
- OgClient.list_clients(self.rest)
+ ret = OgClient.list_clients(self.rest)
elif parsed_args.item == 'client':
- OgClient.get_client_properties(self.rest, args[1:])
+ ret = OgClient.get_client_properties(self.rest, args[1:])
elif parsed_args.item == 'hardware':
- OgClient.list_client_hardware(self.rest, args[1:])
+ ret = OgClient.list_client_hardware(self.rest, args[1:])
elif parsed_args.item == 'modes':
- OgModes.list_available_modes(self.rest)
+ ret = OgModes.list_available_modes(self.rest)
elif parsed_args.item == 'scopes':
- OgScope.list_scopes(self.rest, args[1:])
+ ret = OgScope.list_scopes(self.rest, args[1:])
elif parsed_args.item == 'images':
- OgImage.list_images(self.rest)
+ ret = OgImage.list_images(self.rest)
elif parsed_args.item == 'disks':
- OgDisk.list_disks(self.rest, args[1:])
+ ret = OgDisk.list_disks(self.rest, args[1:])
elif parsed_args.item == 'repos':
- OgRepo.list_repos(self.rest)
+ ret = OgRepo.list_repos(self.rest)
elif parsed_args.item == 'servers':
- OgServer.list_servers(self.rest)
+ ret = OgServer.list_servers(self.rest)
elif parsed_args.item == 'live':
- OgLive.list_live(self.rest)
+ ret = OgLive.list_live(self.rest)
+ return ret
def set(self, args):
choices = ['modes', 'mode', 'repo']
@@ -116,13 +123,15 @@ class OgCLI():
if not args:
print('Missing set subcommand', file=sys.stderr)
parser.print_help(file=sys.stderr)
- sys.exit(1)
+ return 1
parsed_args = parser.parse_args([args[0]])
+ ret = 0
if parsed_args.item in ['modes', 'mode']:
- OgModes.set_modes(self.rest, args[1:])
+ ret = OgModes.set_modes(self.rest, args[1:])
elif parsed_args.item == 'repo':
- OgRepo.set_repo(self.rest, args[1:])
+ ret = OgRepo.set_repo(self.rest, args[1:])
+ return ret
def request(self, args):
choices = ['reboot', 'refresh', 'poweroff', 'wol', 'session']
@@ -132,19 +141,21 @@ class OgCLI():
if not args:
print('Missing request subcommand', file=sys.stderr)
parser.print_help(file=sys.stderr)
- sys.exit(1)
+ return 1
parsed_args = parser.parse_args([args[0]])
+ ret = 0
if parsed_args.request_obj == 'wol':
- OgWol.request_wol(self.rest, args[1:])
+ ret = OgWol.request_wol(self.rest, args[1:])
elif parsed_args.request_obj == 'poweroff':
- OgPoweroff.request_poweroff(self.rest, args[1:])
+ ret = OgPoweroff.request_poweroff(self.rest, args[1:])
elif parsed_args.request_obj == 'refresh':
- OgClient.request_refresh(self.rest, args[1:])
+ ret = OgClient.request_refresh(self.rest, args[1:])
elif parsed_args.request_obj == 'reboot':
- OgReboot.request_reboot(self.rest, args[1:])
+ ret = OgReboot.request_reboot(self.rest, args[1:])
elif parsed_args.request_obj == 'session':
- OgSession.request_session(self.rest, args[1:])
+ ret = OgSession.request_session(self.rest, args[1:])
+ return ret
def restore(self, args):
choices = ['image']
@@ -154,11 +165,13 @@ class OgCLI():
if not args:
print('Missing restore subcommand', file=sys.stderr)
parser.print_help(file=sys.stderr)
- sys.exit(1)
+ return 1
parsed_args = parser.parse_args([args[0]])
+ ret = 0
if parsed_args.restore_obj == 'image':
- OgImage.restore_image(self.rest, args[1:])
+ ret = OgImage.restore_image(self.rest, args[1:])
+ return ret
def create(self, args):
choices = ['image']
@@ -168,11 +181,13 @@ class OgCLI():
if not args:
print('Missing create subcommand', file=sys.stderr)
parser.print_help(file=sys.stderr)
- sys.exit(1)
+ return 1
parsed_args = parser.parse_args([args[0]])
+ ret = 0
if parsed_args.create_obj == 'image':
- OgImage.create_image(self.rest, args[1:])
+ ret = OgImage.create_image(self.rest, args[1:])
+ return ret
def setup(self, args):
choices = ['disk']
@@ -182,11 +197,13 @@ class OgCLI():
if not args:
print('Missing setup subcommand', file=sys.stderr)
parser.print_help(file=sys.stderr)
- sys.exit(1)
+ return 1
parsed_args = parser.parse_args([args[0]])
+ ret = 0
if parsed_args.setup_obj == 'disk':
- OgDisk.setup_disk(self.rest, args[1:])
+ ret = OgDisk.setup_disk(self.rest, args[1:])
+ return ret
def update(self, args):
choices = ['image', 'repo', 'center', 'room', 'folder']
@@ -196,19 +213,21 @@ class OgCLI():
if not args:
print('Missing update subcommand', file=sys.stderr)
parser.print_help(file=sys.stderr)
- sys.exit(1)
+ return 1
parsed_args = parser.parse_args([args[0]])
+ ret = 0
if parsed_args.update_obj == 'image':
- OgImage.update_image(self.rest, args[1:])
+ ret = OgImage.update_image(self.rest, args[1:])
elif parsed_args.update_obj == 'center':
- OgCenter.update_center(self.rest, args[1:])
+ ret = OgCenter.update_center(self.rest, args[1:])
elif parsed_args.update_obj == 'room':
- OgRoom.update_room(self.rest, args[1:])
+ ret = OgRoom.update_room(self.rest, args[1:])
elif parsed_args.update_obj == 'folder':
- OgFolder.update_folder(self.rest, args[1:])
+ ret = OgFolder.update_folder(self.rest, args[1:])
elif parsed_args.update_obj == 'repo':
- OgRepo.update_repo(self.rest, args[1:])
+ ret = OgRepo.update_repo(self.rest, args[1:])
+ return ret
def delete(self, args):
choices = ['server', 'repo', 'center', 'room', 'client', 'folder']
@@ -218,21 +237,22 @@ class OgCLI():
if not args:
print('Missing delete subcommand', file=sys.stderr)
parser.print_help(file=sys.stderr)
- sys.exit(1)
+ return 1
parsed_args = parser.parse_args([args[0]])
if parsed_args.delete_obj == 'server':
- OgServer.delete_server(self.rest, args[1:])
+ ret = OgServer.delete_server(self.rest, args[1:])
elif parsed_args.delete_obj == 'repo':
- OgRepo.delete_repo(self.rest, args[1:])
+ ret = OgRepo.delete_repo(self.rest, args[1:])
elif parsed_args.delete_obj == 'center':
- OgCenter.delete_center(self.rest, args[1:])
+ ret = OgCenter.delete_center(self.rest, args[1:])
elif parsed_args.delete_obj == 'room':
- OgRoom.delete_room(self.rest, args[1:])
+ ret = OgRoom.delete_room(self.rest, args[1:])
elif parsed_args.delete_obj == 'client':
- OgClient.delete_client(self.rest, args[1:])
+ ret = OgClient.delete_client(self.rest, args[1:])
elif parsed_args.delete_obj == 'folder':
- OgFolder.delete_folder(self.rest, args[1:])
+ ret = OgFolder.delete_folder(self.rest, args[1:])
+ return ret
def add(self, args):
choices = ['server', 'repo', 'center', 'room', 'client', 'folder']
@@ -242,18 +262,20 @@ class OgCLI():
if not args:
print('Missing add subcommand', file=sys.stderr)
parser.print_help(file=sys.stderr)
- sys.exit(1)
+ return 1
parsed_args = parser.parse_args([args[0]])
+ ret = 0
if parsed_args.add_obj == 'server':
- OgServer.add_server(self.rest, args[1:])
+ ret = OgServer.add_server(self.rest, args[1:])
elif parsed_args.add_obj == 'repo':
- OgRepo.add_repo(self.rest, args[1:])
+ ret = OgRepo.add_repo(self.rest, args[1:])
elif parsed_args.add_obj == 'center':
- OgCenter.add_center(self.rest, args[1:])
+ ret = OgCenter.add_center(self.rest, args[1:])
elif parsed_args.add_obj == 'room':
- OgRoom.add_room(self.rest, args[1:])
+ ret = OgRoom.add_room(self.rest, args[1:])
elif parsed_args.add_obj == 'client':
- OgClient.add_client(self.rest, args[1:])
+ ret = OgClient.add_client(self.rest, args[1:])
elif parsed_args.add_obj == 'folder':
- OgFolder.add_folder(self.rest, args[1:])
+ ret = OgFolder.add_folder(self.rest, args[1:])
+ return ret