Exemple #1
0
    def batch_create_manifest_labels(self, manifest):
        """ Returns a context manager for batch creation of labels on a manifest.

        Can raise InvalidLabelKeyException or InvalidMediaTypeException depending
        on the validation errors.
    """
        try:
            tag_manifest = database.TagManifest.get(id=manifest._db_id)
        except database.TagManifest.DoesNotExist:
            yield None
            return

        labels_to_add = []

        def add_label(key, value, source_type_name, media_type_name=None):
            labels_to_add.append(
                dict(key=key,
                     value=value,
                     source_type_name=source_type_name,
                     media_type_name=media_type_name))

        yield add_label

        # TODO: make this truly batch once we've fully transitioned to V2_2 and no longer need
        # the mapping tables.
        for label in labels_to_add:
            with db_transaction():
                # Create the label itself.
                model.label.create_manifest_label(tag_manifest, **label)

                # Apply any changes to the manifest that the label prescribes.
                apply_label_to_manifest(label, manifest, self)
Exemple #2
0
    def create_manifest_label(self,
                              manifest,
                              key,
                              value,
                              source_type_name,
                              media_type_name=None):
        """ Creates a label on the manifest with the given key and value. """
        try:
            tag_manifest = database.TagManifest.get(id=manifest._db_id)
        except database.TagManifest.DoesNotExist:
            return None

        label_data = dict(key=key,
                          value=value,
                          source_type_name=source_type_name,
                          media_type_name=media_type_name)

        with db_transaction():
            # Create the label itself.
            label = model.label.create_manifest_label(tag_manifest, key, value,
                                                      source_type_name,
                                                      media_type_name)

            # Apply any changes to the manifest that the label prescribes.
            apply_label_to_manifest(label_data, manifest, self)

        return Label.for_label(label)
Exemple #3
0
    def create_manifest_label(self,
                              manifest,
                              key,
                              value,
                              source_type_name,
                              media_type_name=None):
        """
        Creates a label on the manifest with the given key and value.
        """
        label_data = dict(key=key,
                          value=value,
                          source_type_name=source_type_name,
                          media_type_name=media_type_name)

        # Create the label itself.
        label = oci.label.create_manifest_label(
            manifest._db_id,
            key,
            value,
            source_type_name,
            media_type_name,
        )
        if label is None:
            return None

        # Apply any changes to the manifest that the label prescribes.
        apply_label_to_manifest(label_data, manifest, self)

        return Label.for_label(label)
Exemple #4
0
    def create_manifest_and_retarget_tag(self,
                                         repository_ref,
                                         manifest_interface_instance,
                                         tag_name,
                                         storage,
                                         raise_on_error=False):
        """
        Creates a manifest in a repository, adding all of the necessary data in the model.

        The `manifest_interface_instance` parameter must be an instance of the manifest
        interface as returned by the image/docker package.

        Note that all blobs referenced by the manifest must exist under the repository or this
        method will fail and return None.

        Returns a reference to the (created manifest, tag) or (None, None) on error, unless
        raise_on_error is set to True, in which case a CreateManifestException may also be
        raised.
        """
        with db_disallow_replica_use():
            # Get or create the manifest itself.
            created_manifest = oci.manifest.get_or_create_manifest(
                repository_ref._db_id,
                manifest_interface_instance,
                storage,
                for_tagging=True,
                raise_on_error=raise_on_error,
            )
            if created_manifest is None:
                return (None, None)

            # Re-target the tag to it.
            tag = oci.tag.retarget_tag(
                tag_name,
                created_manifest.manifest,
                raise_on_error=raise_on_error,
            )
            if tag is None:
                return (None, None)

            wrapped_manifest = Manifest.for_manifest(
                created_manifest.manifest, self._legacy_image_id_handler)

            # Apply any labels that should modify the created tag.
            if created_manifest.labels_to_apply:
                for key, value in created_manifest.labels_to_apply.items():
                    apply_label_to_manifest(dict(key=key, value=value),
                                            wrapped_manifest, self)

                # Reload the tag in case any updates were applied.
                tag = database.Tag.get(id=tag.id)

            return (
                wrapped_manifest,
                Tag.for_tag(tag,
                            self._legacy_image_id_handler,
                            manifest_row=created_manifest.manifest),
            )