From a2c2976035a1fb961991d0a6f588d9bdedb2d0af Mon Sep 17 00:00:00 2001 From: Adam Olech Date: Wed, 8 Feb 2023 14:10:47 +0100 Subject: [PATCH] Add support for selecting the container architecture --- dockersave/__init__.py | 13 +++++++++---- dockersave/cli.py | 5 ++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/dockersave/__init__.py b/dockersave/__init__.py index 8970c68..1acf148 100644 --- a/dockersave/__init__.py +++ b/dockersave/__init__.py @@ -55,7 +55,7 @@ def get_token(image, user=None, password=None, auth_endpoint="https://auth.docke return r.json()['token'] -def get_manifest(image, tag, token, registry_url): +def get_manifest(image, tag, token, registry_url, arch="amd64"): request_url = "{}/v2/{}/manifests/{}".format(registry_url, image, tag) supported_fat_manifests = [ @@ -79,7 +79,12 @@ def get_manifest(image, tag, token, registry_url): ) if r.headers["content-type"] in supported_fat_manifests: - return get_manifest(image, r.json()['manifests'][0]["digest"], token, registry_url) + return get_manifest( + image, + next(x['digest'] for x in r.json()['manifests'] if x['platform']['architecture'] == arch), + token, + registry_url + ) r.raise_for_status() return r @@ -155,7 +160,7 @@ def lexer_wrapper(string, user=None, password=None, secure=True): return img class Image: - def __init__(self, image, tag, user=None, password=None, other_tags=False, registry_url="https://registry-1.docker.io"): + def __init__(self, image, tag, user=None, password=None, other_tags=False, registry_url="https://registry-1.docker.io", arch="amd64"): self.image = image self.tag = tag self.registry_url = registry_url @@ -181,7 +186,7 @@ def __init__(self, image, tag, user=None, password=None, other_tags=False, regis else: self.taglist = None - self.manifest_response = get_manifest(image, tag, self.token, self.registry_url) + self.manifest_response = get_manifest(image, tag, self.token, self.registry_url, arch) self.manifest = self.manifest_response.json() diff --git a/dockersave/cli.py b/dockersave/cli.py index 5b08d56..4ab8a2a 100644 --- a/dockersave/cli.py +++ b/dockersave/cli.py @@ -32,7 +32,9 @@ def download(args): user=args.user, password=args.password, other_tags=args.tags, - registry_url=l['registry_url']) + registry_url=l['registry_url'], + arch=args.arch + ) if args.tags: from pprint import PrettyPrinter as pp @@ -72,6 +74,7 @@ def get_parser(): parser.add_argument('--metadata', action='store_true') parser.add_argument('--sha', action='store_true') parser.add_argument('--tags', action='store_true') + parser.add_argument('--arch', type=str, default="amd64") parser.add_argument('--version', action='version', version=__version__) parser.set_defaults(func=download)