def list_all_active_repository_tags(self, repository_ref, include_legacy_images=False): """ Returns a list of all the active tags in the repository. Note that this is a *HEAVY* operation on repositories with a lot of tags, and should only be used for testing or where other more specific operations are not possible. """ tags = list(oci.tag.list_alive_tags(repository_ref._db_id)) legacy_images_map = {} if include_legacy_images: legacy_images_map = oci.tag.get_legacy_images_for_tags(tags) return [ Tag.for_tag(tag, legacy_image=LegacyImage.for_image( legacy_images_map.get(tag.id))) for tag in tags ]
def get_legacy_images(self, repository_ref): """ Returns an iterator of all the LegacyImage's defined in the matching repository. """ repo = model.repository.lookup_repository(repository_ref._db_id) if repo is None: return None all_images = model.image.get_repository_images_without_placements(repo) all_images_map = {image.id: image for image in all_images} all_tags = model.tag.list_repository_tags(repo.namespace_user.username, repo.name) tags_by_image_id = defaultdict(list) for tag in all_tags: tags_by_image_id[tag.image_id].append(tag) return [ LegacyImage.for_image(image, images_map=all_images_map, tags_map=tags_by_image_id) for image in all_images ]
def create_manifest_with_temp_tag( self, repository_ref, manifest_interface_instance, expiration_sec, storage ): """ Creates a manifest under the repository and sets a temporary tag to point to it. Returns the manifest object created or None on error. """ # Get or create the manifest itself. get_or_create_manifest will take care of the # temporary tag work. created_manifest = oci.manifest.get_or_create_manifest( repository_ref._db_id, manifest_interface_instance, storage, temp_tag_expiration_sec=expiration_sec, ) if created_manifest is None: return None legacy_image = oci.shared.get_legacy_image_for_manifest(created_manifest.manifest) li = LegacyImage.for_image(legacy_image) return Manifest.for_manifest(created_manifest.manifest, li)
def get_repo_tag(self, repository_ref, tag_name, include_legacy_image=False): """ Returns the latest, *active* tag found in the repository, with the matching name or None if none. """ assert isinstance(tag_name, basestring) tag = model.tag.get_active_tag_for_repo(repository_ref._db_id, tag_name) if tag is None: return None legacy_image = LegacyImage.for_image( tag.image) if include_legacy_image else None tag_manifest = model.tag.get_tag_manifest(tag) manifest_digest = tag_manifest.digest if tag_manifest else None return Tag.for_repository_tag(tag, legacy_image=legacy_image, manifest_digest=manifest_digest)
def list_all_active_repository_tags(self, repository_ref, include_legacy_images=False): """ Returns a list of all the active tags in the repository. Note that this is a *HEAVY* operation on repositories with a lot of tags, and should only be used for testing or where other more specific operations are not possible. """ if not include_legacy_images: tags = model.tag.list_active_repo_tags(repository_ref._db_id, include_images=False) return [Tag.for_repository_tag(tag) for tag in tags] tags = model.tag.list_active_repo_tags(repository_ref._db_id) return [ Tag.for_repository_tag( tag, legacy_image=LegacyImage.for_image(tag.image), manifest_digest=(tag.tagmanifest.digest if hasattr( tag, 'tagmanifest') else None)) for tag in tags ]
def get_repo_tag(self, repository_ref, tag_name, include_legacy_image=False): """ Returns the latest, *active* tag found in the repository, with the matching name or None if none. """ assert isinstance(tag_name, basestring) tag = oci.tag.get_tag(repository_ref._db_id, tag_name) if tag is None: return None legacy_image = None if include_legacy_image: legacy_images = oci.tag.get_legacy_images_for_tags([tag]) legacy_image = legacy_images.get(tag.id) return Tag.for_tag(tag, legacy_image=LegacyImage.for_image(legacy_image))
def list_repository_tag_history(self, repository_ref, page=1, size=100, specific_tag_name=None, active_tags_only=False, since_time_ms=None): """ Returns the history of all tags in the repository (unless filtered). This includes tags that have been made in-active due to newer versions of those tags coming into service. """ tags, has_more = oci.tag.list_repository_tag_history( repository_ref._db_id, page, size, specific_tag_name, active_tags_only, since_time_ms) # TODO: do we need legacy images here? legacy_images_map = oci.tag.get_legacy_images_for_tags(tags) return [ Tag.for_tag(tag, LegacyImage.for_image(legacy_images_map.get(tag.id))) for tag in tags ], has_more
def get_legacy_image(self, repository_ref, docker_image_id, include_parents=False, include_blob=False): """ Returns the matching LegacyImages under the matching repository, if any. If none, returns None. """ repo = model.repository.lookup_repository(repository_ref._db_id) if repo is None: return None image = model.image.get_image(repository_ref._db_id, docker_image_id) if image is None: return None parent_images_map = None if include_parents: parent_images = model.image.get_parent_images( repo.namespace_user.username, repo.name, image) parent_images_map = {image.id: image for image in parent_images} blob = None if include_blob: placements = list( model.storage.get_storage_locations(image.storage.uuid)) blob = Blob.for_image_storage( image.storage, storage_path=model.storage.get_layer_path(image.storage), placements=placements, ) return LegacyImage.for_image(image, images_map=parent_images_map, blob=blob)
def retarget_tag( self, repository_ref, tag_name, manifest_or_legacy_image, storage, legacy_manifest_key, is_reversion=False, ): """ Creates, updates or moves a tag to a new entry in history, pointing to the manifest or legacy image specified. If is_reversion is set to True, this operation is considered a reversion over a previous tag move operation. Returns the updated Tag or None on error. """ assert legacy_manifest_key is not None manifest_id = manifest_or_legacy_image._db_id if isinstance(manifest_or_legacy_image, LegacyImage): # If a legacy image was required, build a new manifest for it and move the tag to that. try: image_row = database.Image.get(id=manifest_or_legacy_image._db_id) except database.Image.DoesNotExist: return None manifest_instance = self._build_manifest_for_legacy_image(tag_name, image_row) if manifest_instance is None: return None created = oci.manifest.get_or_create_manifest( repository_ref._db_id, manifest_instance, storage ) if created is None: return None manifest_id = created.manifest.id else: # If the manifest is a schema 1 manifest and its tag name does not match that # specified, then we need to create a new manifest, but with that tag name. if manifest_or_legacy_image.media_type in DOCKER_SCHEMA1_CONTENT_TYPES: try: parsed = manifest_or_legacy_image.get_parsed_manifest() except ManifestException: logger.exception( "Could not parse manifest `%s` in retarget_tag", manifest_or_legacy_image._db_id, ) return None if parsed.tag != tag_name: logger.debug( "Rewriting manifest `%s` for tag named `%s`", manifest_or_legacy_image._db_id, tag_name, ) repository_id = repository_ref._db_id updated = parsed.with_tag_name(tag_name, legacy_manifest_key) assert updated.is_signed created = oci.manifest.get_or_create_manifest(repository_id, updated, storage) if created is None: return None manifest_id = created.manifest.id tag = oci.tag.retarget_tag(tag_name, manifest_id, is_reversion=is_reversion) legacy_image = LegacyImage.for_image(oci.shared.get_legacy_image_for_manifest(manifest_id)) return Tag.for_tag(tag, legacy_image)