def SetUp(self): self._repository = docker_name.Repository('gcr.io/my-project/my-image') self.messages = apis.GetMessagesModule('containeranalysis', 'v1alpha1') self.client = apitools_mock.Client( client_class=apis.GetClientClass('containeranalysis', 'v1alpha1')) self.client.Mock() self.addCleanup(self.client.Unmock)
def ValidateRepositoryPath(repository_path): """Validates the repository path. Args: repository_path: str, The repository path supplied by a user. Returns: The parsed docker_name.Repository object. Raises: InvalidImageNameError: If the image name is invalid. docker.UnsupportedRegistryError: If the path is valid, but belongs to a registry we don't support. """ if IsFullySpecified(repository_path): raise InvalidImageNameError( 'Image names must not be fully-qualified. Remove the tag or digest ' 'and try again.') if repository_path.endswith('/'): raise InvalidImageNameError('Image name cannot end with \'/\'. ' 'Remove the trailing \'/\' and try again.') try: if repository_path in constants.MIRROR_REGISTRIES: repository = docker_name.Registry(repository_path) else: repository = docker_name.Repository(repository_path) if repository.registry not in constants.ALL_SUPPORTED_REGISTRIES: raise docker.UnsupportedRegistryError(repository_path) return repository except docker_name.BadNameException as e: # Reraise with the proper base class so the message gets shown. raise InvalidImageNameError(six.text_type(e))
def _get_digests(self, repo): name = docker_name.Repository(repo) creds = docker_creds.DefaultKeychain.Resolve(name) transport = transport_pool.Http(httplib2.Http) with docker_image.FromRegistry(name, creds, transport) as img: digests = [d[len('sha256:'):] for d in img.manifests()] return digests raise AssertionError('Unable to get digests from {0}'.format(repo))
def SetUp(self): self._repository = docker_name.Repository('gcr.io/my-project/my-image') self.messages = apis.GetMessagesModule('containeranalysis', 'v1alpha1') self.client = apitools_mock.Client( client_class=apis.GetClientClass('containeranalysis', 'v1alpha1')) self.client.Mock() self.addCleanup(self.client.Unmock) self._original_maximum_resource_url_chunk_size = ( containeranalysis_util._MAXIMUM_RESOURCE_URL_CHUNK_SIZE)
def main(): args = parser.parse_args() creds = docker_creds.Anonymous() transport = httplib2.Http() repo = docker_name.Repository(args.repository) latest = docker_name.Tag(str(repo) + ":latest") with docker_image.FromRegistry(latest, creds, transport) as img: latest_digest = img.digest() debug = docker_name.Tag(str(repo) + ":debug") with docker_image.FromRegistry(debug, creds, transport) as img: if img.exists(): debug_digest = img.digest() else: debug_digest = latest_digest with open(args.output, 'w') as f: f.write("""\ # Copyright 2017 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. \"\"\" Generated file with dependencies for language rule.\"\"\" # !!!! THIS IS A GENERATED FILE TO NOT EDIT IT BY HAND !!!! # # To regenerate this file, run ./update_deps.sh from the root of the # git repository. DIGESTS = {{ # "{debug_tag}" circa {date} "debug": "{debug}", # "{latest_tag}" circa {date} "latest": "{latest}", }} """.format(debug_tag=debug, debug=debug_digest, latest_tag=latest, latest=latest_digest, date=time.strftime("%Y-%m-%d %H:%M %z")))
def get_digest_from_prefix(self, repo, prefix): name = docker_name.Repository(repo) creds = docker_creds.DefaultKeychain.Resolve(name) transport = transport_pool.Http(httplib2.Http) with docker_image.FromRegistry(name, creds, transport) as img: digests = [d[len('sha256:'):] for d in img.manifests()] matches = [d for d in digests if d.startswith(prefix)] if len(matches) == 1: return matches[0] if len(matches) == 0: raise AssertionError( '{0} is not a valid prefix'.format(prefix)) raise AssertionError( '{0} is not a unique digest prefix'.format(prefix))
def SetUp(self): self._repository = docker_name.Repository('gcr.io/my-project/my-image') self.fetch_occurrence_mock = self.StartPatch( 'googlecloudsdk.api_lib.container.images.util.FetchOccurrences')