From 8a5edd11bdb235692b7a1bd58ef8bb82d0f68551 Mon Sep 17 00:00:00 2001 From: git-hyagi <45576767+git-hyagi@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:28:48 -0300 Subject: [PATCH] draft --- pulp_container/app/serializers.py | 31 ++++++++++++++++++++++++++--- pulp_container/app/tasks/builder.py | 4 ++-- pulp_container/app/viewsets.py | 2 +- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/pulp_container/app/serializers.py b/pulp_container/app/serializers.py index 6a767d857..b84a620c6 100644 --- a/pulp_container/app/serializers.py +++ b/pulp_container/app/serializers.py @@ -798,15 +798,28 @@ def validate(self, data): """Validates that all the fields make sense.""" data = super().validate(data) - if bool(data.get("containerfile", None)) == bool(data.get("containerfile_name", None)): + #if bool(data.get("containerfile", None)) == bool(data.get("containerfile_name", None)): + if data.get("containerfile", None) and data.get("containerfile_name", None): raise serializers.ValidationError( - _("Exactly one of 'containerfile' or 'containerfile_name' must be specified.") + _("Only one of 'containerfile' or 'containerfile_name' must be specified.") ) if "containerfile_name" in data and "build_context" not in data: raise serializers.ValidationError( _("A 'build_context' must be specified when 'containerfile_name' is present.") ) + + # with none of build_context and containerfile_name and containerfile + # there is not enough information to build + if not ( + data.get("containerfile", None) and + data.get("containerfile_name", None) and + data.get("build_context",None) + ): + raise serializers.ValidationError( + _("""At least one of 'build_context' or 'containerfile' or 'containerfile_name' + must be provided""") + ) # TODO: this can be removed after https://github.com/pulp/pulpcore/issues/5786 if data.get("build_context", None): @@ -831,7 +844,7 @@ def deferred_files_validation(self, data): ) ) - # check if the containerfile_name exists in the build_context (File Repository) + # check if the provided containerfile_name exists in the build_context (File Repository) if ( data.get("containerfile_name", None) and not FileContent.objects.filter( @@ -846,6 +859,18 @@ def deferred_files_validation(self, data): + '" in the build_context provided' ) ) + + # check if a Containerfile exists in the build_context when + # no containerfile_name is provided + if not data.get("containerfile_name",None) and not FileContent.objects.filter( + repositories__in=[build_context.repository.pk], + relative_path="Containerfile", + ).exists(): + raise serializers.ValidationError( + _( + 'Could not find the Containerfile in the build_context provided' + ) + ) data["build_context_pk"] = build_context.repository.pk diff --git a/pulp_container/app/tasks/builder.py b/pulp_container/app/tasks/builder.py index 0739242aa..79e9c88b0 100644 --- a/pulp_container/app/tasks/builder.py +++ b/pulp_container/app/tasks/builder.py @@ -125,8 +125,8 @@ def build_image( image and tag. """ - if not containerfile_tempfile_pk and not containerfile_name: - raise RuntimeError("Neither a name nor temporary file for the Containerfile was specified.") + #if not containerfile_tempfile_pk and not containerfile_name: + # raise RuntimeError("Neither a name nor temporary file for the Containerfile was specified.") if containerfile_tempfile_pk: containerfile_artifact = PulpTemporaryFile.objects.get(pk=containerfile_tempfile_pk) diff --git a/pulp_container/app/viewsets.py b/pulp_container/app/viewsets.py index 67db707cb..0a3ebe7af 100644 --- a/pulp_container/app/viewsets.py +++ b/pulp_container/app/viewsets.py @@ -945,7 +945,7 @@ def build_image(self, request, pk): temp_file.save() containerfile_tempfile_pk = str(temp_file.pk) - containerfile_name = serialized_data.get("containerfile_name", None) + containerfile_name = serialized_data.get("containerfile_name", "Containerfile") tag = serialized_data["tag"] result = dispatch(