Exemple #1
0
def _garbage_collect_legacy_manifest(legacy_manifest_id, context):
    assert legacy_manifest_id is not None

    # Add the labels to be GCed.
    query = TagManifestLabel.select().where(
        TagManifestLabel.annotated == legacy_manifest_id)
    for manifest_label in query:
        context.add_label_id(manifest_label.label_id)

    # Delete the tag manifest.
    with db_transaction():
        try:
            tag_manifest = TagManifest.select().where(
                TagManifest.id == legacy_manifest_id).get()
        except TagManifest.DoesNotExist:
            return False

        assert tag_manifest.id == legacy_manifest_id
        assert tag_manifest.tag.repository_id == context.repository.id

        # Delete any label mapping rows.
        (TagManifestLabelMap.delete().where(
            TagManifestLabelMap.tag_manifest == legacy_manifest_id).execute())

        # Delete the label rows.
        TagManifestLabel.delete().where(
            TagManifestLabel.annotated == legacy_manifest_id).execute()

        # Delete the mapping row if it exists.
        try:
            tmt = (TagManifestToManifest.select().where(
                TagManifestToManifest.tag_manifest == tag_manifest).get())
            context.add_manifest_id(tmt.manifest_id)
            tmt_deleted = tmt.delete_instance()
            if tmt_deleted:
                gc_table_rows_deleted.labels(
                    table="TagManifestToManifest").inc()
        except TagManifestToManifest.DoesNotExist:
            pass

        # Delete the tag manifest.
        tag_manifest_deleted = tag_manifest.delete_instance()
        if tag_manifest_deleted:
            gc_table_rows_deleted.labels(table="TagManifest").inc()
    return True
Exemple #2
0
def _get_dangling_manifest_count():
    manifest_ids = set([current.id for current in Manifest.select()])
    referenced_by_tag_manifest = set(
        [tmt.manifest_id for tmt in TagManifestToManifest.select()])
    return len(manifest_ids - referenced_by_tag_manifest)
Exemple #3
0
def create_manifest_label(manifest_id,
                          key,
                          value,
                          source_type_name,
                          media_type_name=None,
                          adjust_old_model=True):
    """ Creates a new manifest label on a specific tag manifest. """
    if not key:
        raise InvalidLabelKeyException()

    # Note that we don't prevent invalid label names coming from the manifest to be stored, as Docker
    # does not currently prevent them from being put into said manifests.
    if not validate_label_key(key) and source_type_name != "manifest":
        raise InvalidLabelKeyException("Key `%s` is invalid" % key)

    # Find the matching media type. If none specified, we infer.
    if media_type_name is None:
        media_type_name = "text/plain"
        if is_json(value):
            media_type_name = "application/json"

    try:
        media_type_id = Label.media_type.get_id(media_type_name)
    except MediaType.DoesNotExist:
        raise InvalidMediaTypeException()

    source_type_id = Label.source_type.get_id(source_type_name)

    # Ensure the manifest exists.
    try:
        manifest = (Manifest.select(Manifest,
                                    Repository).join(Repository).where(
                                        Manifest.id == manifest_id).get())
    except Manifest.DoesNotExist:
        return None

    repository = manifest.repository

    # TODO: Remove this code once the TagManifest table is gone.
    tag_manifest = None
    if adjust_old_model:
        try:
            mapping_row = (TagManifestToManifest.select(
                TagManifestToManifest, TagManifest).join(TagManifest).where(
                    TagManifestToManifest.manifest == manifest).get())
            tag_manifest = mapping_row.tag_manifest
        except TagManifestToManifest.DoesNotExist:
            tag_manifest = None

    with db_transaction():
        label = Label.create(key=key,
                             value=value,
                             source_type=source_type_id,
                             media_type=media_type_id)
        manifest_label = ManifestLabel.create(manifest=manifest_id,
                                              label=label,
                                              repository=repository)

        # If there exists a mapping to a TagManifest, add the old-style label.
        # TODO: Remove this code once the TagManifest table is gone.
        if tag_manifest:
            tag_manifest_label = TagManifestLabel.create(
                annotated=tag_manifest, label=label, repository=repository)
            TagManifestLabelMap.create(
                manifest_label=manifest_label,
                tag_manifest_label=tag_manifest_label,
                label=label,
                manifest=manifest,
                tag_manifest=tag_manifest,
            )

    return label