From 5fc9c7d9ae3e48362df880874d50a4e0e468d4e8 Mon Sep 17 00:00:00 2001 From: git-hyagi <45576767+git-hyagi@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:15:55 -0300 Subject: [PATCH] draft --- pulp_container/app/serializers.py | 41 ++++++++++++++++++----------- pulp_container/app/tasks/builder.py | 3 --- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/pulp_container/app/serializers.py b/pulp_container/app/serializers.py index b84a620c6..19d7caf56 100644 --- a/pulp_container/app/serializers.py +++ b/pulp_container/app/serializers.py @@ -798,7 +798,9 @@ 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)): + # only one of containerfile or containerfile_name should be provided, none of them is ok + # in case we have a build_context because Pulp will try to find a file called Containerfile + # in the build_context provided if data.get("containerfile", None) and data.get("containerfile_name", None): raise serializers.ValidationError( _("Only one of 'containerfile' or 'containerfile_name' must be specified.") @@ -808,17 +810,19 @@ def validate(self, 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 + + # with none of build_context nor containerfile_name nor 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) + data.get("containerfile", None) + or data.get("containerfile_name", None) + or data.get("build_context", None) ): raise serializers.ValidationError( - _("""At least one of 'build_context' or 'containerfile' or 'containerfile_name' - must be provided""") + _( + "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 @@ -834,6 +838,12 @@ def deferred_files_validation(self, data): """ if build_context := data.get("build_context", None): + data["build_context_pk"] = build_context.repository.pk + + # if a containerfile was passed with the build_context we can skip the following checks + if data.get("containerfile", None): + return data + # check if the on_demand_artifacts exist for on_demand_artifact in build_context.on_demand_artifacts.iterator(): if not on_demand_artifact.content_artifact.artifact: @@ -859,21 +869,20 @@ 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( + if ( + not data.get("containerfile_name", None) + and not FileContent.objects.filter( repositories__in=[build_context.repository.pk], relative_path="Containerfile", - ).exists(): + ).exists() + ): raise serializers.ValidationError( - _( - 'Could not find the Containerfile in the build_context provided' - ) + _("Could not find the Containerfile in the build_context provided") ) - data["build_context_pk"] = build_context.repository.pk - return data class Meta: diff --git a/pulp_container/app/tasks/builder.py b/pulp_container/app/tasks/builder.py index 79e9c88b0..1d79526de 100644 --- a/pulp_container/app/tasks/builder.py +++ b/pulp_container/app/tasks/builder.py @@ -125,9 +125,6 @@ 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 containerfile_tempfile_pk: containerfile_artifact = PulpTemporaryFile.objects.get(pk=containerfile_tempfile_pk)