From a9ce379da8d92e50e982554e51f48dd8deafa247 Mon Sep 17 00:00:00 2001 From: git-hyagi <45576767+git-hyagi@users.noreply.github.com> Date: Wed, 24 Apr 2024 13:30:27 -0300 Subject: [PATCH] draft/wip --- .../commands/container-handle-image-data.py | 22 ++++++++++++++++++- ...ta.py => 0039_artifactless_config_blob.py} | 2 +- pulp_container/app/models.py | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) rename pulp_container/app/migrations/{0039_blob_data.py => 0039_artifactless_config_blob.py} (87%) diff --git a/pulp_container/app/management/commands/container-handle-image-data.py b/pulp_container/app/management/commands/container-handle-image-data.py index ed10e7547..fc998075a 100644 --- a/pulp_container/app/management/commands/container-handle-image-data.py +++ b/pulp_container/app/management/commands/container-handle-image-data.py @@ -6,10 +6,11 @@ from django.core.exceptions import ObjectDoesNotExist from django.core.management import BaseCommand +from django.core.files.storage import default_storage as storage from pulp_container.app.models import Manifest -from pulp_container.constants import MEDIA_TYPE +from pulp_container.constants import ARTIFACTLESS_BLOB_SIZE, MEDIA_TYPE class Command(BaseCommand): @@ -41,11 +42,30 @@ def handle(self, *args, **options): media_type__in=[MEDIA_TYPE.MANIFEST_LIST, MEDIA_TYPE.INDEX_OCI], annotations={} ) manifests_updated_count += self.update_manifests(manifest_lists) + manifests_updated_count += self.update_config_blobs() self.stdout.write( self.style.SUCCESS("Successfully updated %d manifests." % manifests_updated_count) ) + # TODO: check if it worth finding a better way to do it because here we are iterating over + # all of the available manifests again (we are also iterating over a list of manifests in + # the update_manifests method) + def update_config_blobs(self): + manifests_updated_count = 0 + manifests = Manifest.objects.all() + for manifest in manifests.iterator(): + artifact = manifest.config_blob._artifacts.get().file + blob_size = artifact.size + if blob_size < ARTIFACTLESS_BLOB_SIZE: + with storage.open(artifact.name) as artifact_file: + raw_data = artifact_file.read() + blob = manifest.config_blob + blob.data = raw_data + blob.save() + manifests_updated_count += 1 + return manifests_updated_count + def update_manifests(self, manifests_qs): manifests_updated_count = 0 manifests_to_update = [] diff --git a/pulp_container/app/migrations/0039_blob_data.py b/pulp_container/app/migrations/0039_artifactless_config_blob.py similarity index 87% rename from pulp_container/app/migrations/0039_blob_data.py rename to pulp_container/app/migrations/0039_artifactless_config_blob.py index e4a28d97b..26770849d 100644 --- a/pulp_container/app/migrations/0039_blob_data.py +++ b/pulp_container/app/migrations/0039_artifactless_config_blob.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.10 on 2024-04-16 12:41 +# Generated by Django 4.2.10 on 2024-04-24 15:52 from django.db import migrations, models diff --git a/pulp_container/app/models.py b/pulp_container/app/models.py index 46b8baa01..b938335c4 100644 --- a/pulp_container/app/models.py +++ b/pulp_container/app/models.py @@ -145,7 +145,7 @@ def init_labels(self): config = json.loads(self.config_blob.config_blob.instance.data)["config"] if "Labels" in config.keys(): self.labels = config["Labels"] or {} - except KeyError: + except json.decoder.JSONDecodeError: # if not, we'll retrieve the config from file config_artifact = self.config_blob._artifacts.get()