This repository was archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 272
Draft: PoC multi-arch support #626
Draft
nightfurys
wants to merge
4
commits into
anchore:master
Choose a base branch
from
nightfurys:multi-arch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| import tempfile | ||
|
|
||
| import anchore_engine.configuration.localconfig | ||
| from anchore_engine.utils import run_command, run_command_list, manifest_to_digest, AnchoreException | ||
| from anchore_engine.utils import run_command, run_command_list, manifest_to_digest, AnchoreException, ManifestDigestArch, ImageInfo | ||
| from anchore_engine.subsys import logger | ||
| from anchore_engine.common.errors import AnchoreError | ||
|
|
||
|
|
@@ -331,6 +331,135 @@ def get_image_manifest_skopeo(url, registry, repo, intag=None, indigest=None, to | |
|
|
||
| return manifest, digest, topdigest, topmanifest | ||
|
|
||
|
|
||
| def get_digest_arch(pullstring, user=None, pw=None, verify=True): | ||
| ret = None | ||
| try: | ||
| proc_env = os.environ.copy() | ||
| if user and pw: | ||
| proc_env['SKOPUSER'] = user | ||
| proc_env['SKOPPASS'] = pw | ||
| credstr = '--creds \"${SKOPUSER}\":\"${SKOPPASS}\"' | ||
| else: | ||
| credstr = "" | ||
|
|
||
| if verify: | ||
| tlsverifystr = "--tls-verify=true" | ||
| else: | ||
| tlsverifystr = "--tls-verify=false" | ||
|
|
||
| localconfig = anchore_engine.configuration.localconfig.get_config() | ||
| global_timeout = localconfig.get('skopeo_global_timeout', 0) | ||
| try: | ||
| global_timeout = int(global_timeout) | ||
| if global_timeout < 0: | ||
| global_timeout = 0 | ||
| except: | ||
| global_timeout = 0 | ||
|
|
||
| if global_timeout: | ||
| global_timeout_str = "--command-timeout {}s".format(global_timeout) | ||
| else: | ||
| global_timeout_str = "" | ||
|
|
||
| os_override_strs = ["", "--override-os windows"] | ||
| try: | ||
| success = False | ||
| for os_override_str in os_override_strs: | ||
| cmd = ["/bin/sh", "-c", | ||
| "skopeo {} {} inspect {} {} docker://{}".format(global_timeout_str, os_override_str, | ||
| tlsverifystr, credstr, pullstring)] | ||
| cmdstr = ' '.join(cmd) | ||
| try: | ||
| rc, sout, serr = run_command_list(cmd, env=proc_env) | ||
| if rc != 0: | ||
| skopeo_error = SkopeoError(cmd=cmd, rc=rc, out=sout, err=serr) | ||
| if skopeo_error.error_code != AnchoreError.OSARCH_MISMATCH.name: | ||
| raise SkopeoError(cmd=cmd, rc=rc, out=sout, err=serr) | ||
| else: | ||
| logger.debug( | ||
| "command succeeded: cmd=" + str(cmdstr) + " stdout=" + str(sout).strip() + " stderr=" + str( | ||
| serr).strip()) | ||
| success = True | ||
| except Exception as err: | ||
| logger.error("command failed with exception - " + str(err)) | ||
| raise err | ||
|
|
||
| if success: | ||
| sout = str(sout, 'utf-8') if sout else None | ||
| ret = sout | ||
| break | ||
|
|
||
| if not success: | ||
| logger.error("could not retrieve manifest") | ||
| raise Exception("could not retrieve manifest") | ||
|
|
||
| except Exception as err: | ||
| raise err | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't have any effect and it is equal to not having it at all |
||
| except Exception as err: | ||
| raise err | ||
|
|
||
| return ret | ||
|
|
||
| def _get_image_manifest_digest(registry, repo, tag=None, digest=None, user=None, pw=None, verify=True): | ||
| if digest: | ||
| pull_string = registry + "/" + repo + "@" + digest | ||
| elif tag: | ||
| pull_string = registry + "/" + repo + ":" + tag | ||
| else: | ||
| raise Exception("invalid input - must supply either an intag or indigest") | ||
|
|
||
| try: | ||
| raw_manifest = get_image_manifest_skopeo_raw(pull_string, user=user, pw=pw, verify=verify) | ||
| s_manifest = json.loads(raw_manifest) | ||
| s_digest = manifest_to_digest(raw_manifest) | ||
| return s_manifest, s_digest | ||
| except Exception as err: | ||
| logger.warn("CMD failed - exception: " + str(err)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this doesn't add anything that the Exception wouldn't already show. Does this imply there is something beyond |
||
| raise err | ||
|
|
||
|
|
||
| def get_image_manifest_v2(url, registry, repo, intag=None, indigest=None, user=None, pw=None, verify=True, default=None): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default should be set to False as it is a bool. |
||
| try: | ||
| s_manifest, s_digest = _get_image_manifest_digest(registry, repo, intag, indigest, user, pw, verify) | ||
| result = ImageInfo(parent=ManifestDigestArch(s_manifest, s_digest, None), children=[]) | ||
|
|
||
| if s_manifest.get('schemaVersion') == 2 and s_manifest.get('mediaType') == 'application/vnd.docker.distribution.manifest.list.v2+json': | ||
| for entry in s_manifest.get('manifests'): | ||
| child_platform = entry.get('platform') | ||
| child_digest = entry.get('digest') | ||
| if child_digest and child_platform and child_platform.get('os') in ['linux', 'windows']: | ||
| child_arch = child_platform.get('architecture') | ||
| child_variant = child_platform.get('variant') | ||
| arch_var = '{}_{}'.format(child_arch, child_variant) if child_variant else child_arch | ||
| if not default: | ||
| s_manifest, s_digest = _get_image_manifest_digest(registry, repo, None, child_digest, user, pw, verify) | ||
| result.children.append(ManifestDigestArch(s_manifest, s_digest, arch_var)) | ||
| elif child_arch in default: | ||
| s_manifest, s_digest = _get_image_manifest_digest(registry, repo, None, child_digest, user, pw, verify) | ||
| result.children.append(ManifestDigestArch(s_manifest, s_digest, arch_var)) | ||
| break | ||
| # else: | ||
| # continue | ||
| else: | ||
| try: | ||
| pull_string = registry + "/" + repo + "@" + s_digest | ||
| raw_inspect_out = get_digest_arch(pull_string, user=user, pw=pw, verify=verify) | ||
| inspect_out = json.loads(raw_inspect_out) | ||
| result.parent.arch = inspect_out.get('Architecture') | ||
| except Exception as err: | ||
| logger.warn("CMD failed - exception: " + str(err)) | ||
| raise err | ||
| except Exception as err: | ||
| logger.exception("Failed to fetch skopeo image manifest", err) | ||
| raise err | ||
|
|
||
| if not result: | ||
| raise SkopeoError(msg="No digest/manifest from skopeo") | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| class SkopeoError(AnchoreException): | ||
|
|
||
| def __init__(self, cmd=None, rc=None, err=None, out=None, msg='Error encountered in skopeo operation'): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that this is used through the entire module which means it should be broken out into a separate function like
This would reduce the amount of code and give us reusable functionality for future expansion.