summaryrefslogtreecommitdiffstats
path: root/cli/objects/images.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/objects/images.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/objects/images.py')
-rw-r--r--cli/objects/images.py82
1 files changed, 47 insertions, 35 deletions
diff --git a/cli/objects/images.py b/cli/objects/images.py
index e6e61d0..10f6fd5 100644
--- a/cli/objects/images.py
+++ b/cli/objects/images.py
@@ -21,10 +21,10 @@ def get_repository(repository_id, rest):
def get_repositories(rest):
- r = rest.get('/repositories')
- if not r or r.status_code != requests.codes.ok:
+ res = rest.get('/repositories')
+ if not res:
return None
- repositories = r.json()['repositories']
+ repositories = res.json()['repositories']
return repositories
@@ -32,8 +32,11 @@ class OgImage():
@staticmethod
def list_images(rest):
- r = rest.get('/images')
- print_json(r.text)
+ res = rest.get('/images')
+ if not res:
+ return 1
+ print_json(res.text)
+ return 0
@staticmethod
def restore_image(rest, args):
@@ -78,8 +81,8 @@ class OgImage():
help='Specific client IP')
parsed_args = parser.parse_args(args)
- r = rest.get('/scopes')
- scopes = r.json()
+ res = rest.get('/scopes')
+ scopes = res.json()
ips = set()
for center in parsed_args.center_id:
@@ -93,34 +96,37 @@ class OgImage():
if not ips:
print('Missing --client-ip, or --room-id/--center-id. No clients provided.')
- return
+ return 1
- r = rest.get('/images')
- images = r.json()
+ 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
+ return 1
else:
found_image = found_image[0]
selected_repo_id = 0
for ip in parsed_args.client_ip:
- r = rest.get('/client/info', payload={'client': ip})
- repo_id = r.json()['repo_id']
+ 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
+ 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)}
- r = rest.post('/image/restore', payload=payload)
+ res = rest.post('/image/restore', payload=payload)
+ if not res:
+ return 1
+ return 0
@staticmethod
def create_image(rest, args):
@@ -149,19 +155,19 @@ class OgImage():
help='Specific client IP')
parsed_args = parser.parse_args(args)
- r = rest.get('/client/info', payload={'client': parsed_args.client_ip})
- center_id = r.json()['center']
- repo_id = r.json()['repo_id']
+ res = rest.get('/client/info', payload={'client': parsed_args.client_ip})
+ center_id = res.json()['center']
+ repo_id = res.json()['repo_id']
- r = rest.get('/client/setup',
+ res = rest.get('/client/setup',
payload={'client': parsed_args.client_ip})
- if r.status_code == 200:
+ if res.status_code == 200:
part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and
x['partition'] == int(parsed_args.part),
- r.json()['partitions']))
+ res.json()['partitions']))
if not part_info:
print('Partition not found.')
- return
+ return 1
fs_code = list(part_info)[0]['code']
image_name = remove_accents(parsed_args.name)
@@ -171,7 +177,10 @@ class OgImage():
if parsed_args.desc:
payload['description'] = parsed_args.desc
- rest.post('/image/create', payload=payload)
+ res = rest.post('/image/create', payload=payload)
+ if not res:
+ return 1
+ return 0
@staticmethod
def update_image(rest, args):
@@ -202,32 +211,32 @@ class OgImage():
help='Specific client IP')
parsed_args = parser.parse_args(args)
- r = rest.get('/client/info', payload={'client': parsed_args.client_ip})
- center_id = r.json()['center']
- repo_id = int(r.json()['repo_id'])
+ res = rest.get('/client/info', payload={'client': parsed_args.client_ip})
+ center_id = res.json()['center']
+ repo_id = int(res.json()['repo_id'])
- r = rest.get('/client/setup',
+ res = rest.get('/client/setup',
payload={'client': parsed_args.client_ip})
- if r.status_code == 200:
+ if res.status_code == 200:
part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and
x['partition'] == int(parsed_args.part),
- r.json()['partitions']))
+ res.json()['partitions']))
if not part_info:
print('Partition not found.')
- return
+ return 1
fs_code = list(part_info)[0]['code']
- r = rest.get('/images')
+ res = rest.get('/images')
image_name = None
- if r.status_code == 200:
- for image in r.json()['images']:
+ 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
+ return 1
else:
print(f'Updating {image_name} image')
@@ -241,4 +250,7 @@ class OgImage():
'code': str(fs_code),
'name': image_name}
- rest.post('/image/update', payload=payload)
+ res = rest.post('/image/update', payload=payload)
+ if not res:
+ return 1
+ return 0