Example #1
0
def _backfill_labels(tag_manifest, manifest, repository):
    tmls = list(TagManifestLabel.select().where(
        TagManifestLabel.annotated == tag_manifest))
    if not tmls:
        return

    for tag_manifest_label in tmls:
        label = tag_manifest_label.label
        try:
            TagManifestLabelMap.get(tag_manifest_label=tag_manifest_label)
            continue
        except TagManifestLabelMap.DoesNotExist:
            pass

        try:
            manifest_label = ManifestLabel.create(manifest=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_label.annotated)
        except IntegrityError:
            continue
Example #2
0
def create_manifest_label(tag_manifest,
                          key,
                          value,
                          source_type_name,
                          media_type_name=None):
    """
    Creates a new manifest label on a specific tag manifest.
    """
    if not key:
        raise InvalidLabelKeyException("Missing key on label")

    # 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(
            "Label key `%s` is invalid or reserved" % 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"

    media_type_id = _get_media_type_id(media_type_name)
    if media_type_id is None:
        raise InvalidMediaTypeException()

    source_type_id = _get_label_source_type_id(source_type_name)

    with db_transaction():
        label = Label.create(key=key,
                             value=value,
                             source_type=source_type_id,
                             media_type=media_type_id)
        tag_manifest_label = TagManifestLabel.create(
            annotated=tag_manifest,
            label=label,
            repository=tag_manifest.tag.repository)
        try:
            mapping_row = TagManifestToManifest.get(tag_manifest=tag_manifest)
            if mapping_row.manifest:
                manifest_label = ManifestLabel.create(
                    manifest=mapping_row.manifest,
                    label=label,
                    repository=tag_manifest.tag.repository,
                )
                TagManifestLabelMap.create(
                    manifest_label=manifest_label,
                    tag_manifest_label=tag_manifest_label,
                    label=label,
                    manifest=mapping_row.manifest,
                    tag_manifest=tag_manifest,
                )
        except TagManifestToManifest.DoesNotExist:
            pass

    return label
Example #3
0
def backfill_label(tag_manifest_label):
    logger.info("Backfilling label %s", tag_manifest_label.id)

    # Ensure that a mapping row doesn't already exist. If it does, we've been preempted.
    if lookup_map_row(tag_manifest_label):
        return False

    # Ensure the tag manifest has been backfilled into the manifest table.
    try:
        tmt = TagManifestToManifest.get(
            tag_manifest=tag_manifest_label.annotated)
    except TagManifestToManifest.DoesNotExist:
        # We'll come back to this later.
        logger.debug(
            "Tag Manifest %s for label %s has not yet been backfilled",
            tag_manifest_label.annotated.id,
            tag_manifest_label.id,
        )
        return True

    repository = tag_manifest_label.repository

    # Create the new mapping entry and label.
    with db_transaction():
        if lookup_map_row(tag_manifest_label):
            return False

        label = tag_manifest_label.label
        if tmt.manifest:
            try:
                manifest_label = ManifestLabel.create(manifest=tmt.manifest,
                                                      label=label,
                                                      repository=repository)
                TagManifestLabelMap.create(
                    manifest_label=manifest_label,
                    tag_manifest_label=tag_manifest_label,
                    label=label,
                    manifest=tmt.manifest,
                    tag_manifest=tag_manifest_label.annotated,
                )
            except IntegrityError:
                return False

    logger.info("Backfilled label %s", tag_manifest_label.id)
    return True
Example #4
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