From 6f9da3fdbb2905d79151e2e4b6a00b0d35a64d18 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Wed, 5 Jun 2024 15:19:42 +0200 Subject: objects: fix restore image default repo value Obtain the image's repository from the API REST. Don't send 127.0.0.1 as image server when --repo is not defined. --- cli/objects/images.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/cli/objects/images.py b/cli/objects/images.py index 67bbf45..21f727c 100644 --- a/cli/objects/images.py +++ b/cli/objects/images.py @@ -9,6 +9,23 @@ 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): + r = rest.get('/repositories') + if not r or r.status_code != requests.codes.ok: + return None + repositories = r.json()['repositories'] + return repositories class OgImage(): @@ -43,7 +60,7 @@ class OgImage(): help='Transfer method. (Default: tiptorrent)') parser.add_argument('--repo', nargs='?', - default=urlparse(rest.URL).netloc.split(':')[0], + required=False, help='Repository IP address') group = parser.add_argument_group('clients', 'Client selection args') group.add_argument('--center-id', @@ -92,6 +109,13 @@ class OgImage(): else: found_image = found_image[0] + if not parsed_args.repo: + repository = get_repository(found_image['repo_id'], rest) + if not repository: + print(f'Could not find repository for image {found_image["name"]}') + return + parsed_args.repo = repository['ip'] + payload = {'disk': parsed_args.disk, 'partition': parsed_args.part, 'id': str(parsed_args.id), 'name': found_image['name'], 'profile': str(found_image['software_id']), -- cgit v1.2.3-18-g5258