Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make run_python not create additional files artifact #1851

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tasks
import (
"context"
"fmt"
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/shared_utils"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/exec_result"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service_directory"
Expand All @@ -23,9 +22,6 @@ import (
"github.com/kurtosis-tech/stacktrace"
"github.com/xtgo/uuid"
"go.starlark.net/starlark"
"io"
"os"
"path"
"strings"
)

Expand All @@ -41,17 +37,7 @@ const (

pipInstallCmd = "pip install"

pythonScriptFileName = "main.py"
pythonWorkspace = "/tmp/python"

defaultTmpDir = ""
pythonScriptReadPermission = 0644
enforceMaxSizeLimit = true
temporaryPythonDirectoryPrefix = "run-python-*"

successfulPipRunExitCode = 0

scriptArtifactFormat = "%v-python-script"
)

func NewRunPythonService(serviceNetwork service_network.ServiceNetwork, runtimeValueStore *runtime_value_store.RuntimeValueStore) *kurtosis_plan_instruction.KurtosisPlanInstruction {
Expand Down Expand Up @@ -160,17 +146,6 @@ func (builtin *RunPythonCapabilities) Interpret(_ string, arguments *builtin_arg
}
builtin.run = pythonScript.GoString()

compressedScript, compressedScriptMd5, scriptCompressionInterpretationErr := getCompressedPythonScriptForUpload(builtin.run)
if err != nil {
return nil, scriptCompressionInterpretationErr
}
defer compressedScript.Close()
uniqueFilesArtifactName := fmt.Sprintf(scriptArtifactFormat, builtin.name)
_, err = builtin.serviceNetwork.UploadFilesArtifact(compressedScript, compressedScriptMd5, uniqueFilesArtifactName)
if err != nil {
return nil, startosis_errors.WrapWithInterpretationError(err, "An error occurred while storing the python script to disk")
}

if arguments.IsSet(PythonArgumentsArgName) {
argsValue, err := builtin_argument.ExtractArgumentValue[*starlark.List](arguments, PythonArgumentsArgName)
if err != nil {
Expand Down Expand Up @@ -217,20 +192,11 @@ func (builtin *RunPythonCapabilities) Interpret(_ string, arguments *builtin_arg
if interpretationErr != nil {
return nil, interpretationErr
}
filesArtifactMountDirPaths[pythonWorkspace] = uniqueFilesArtifactName
filesArtifactExpansion, interpretationErr = service_config.ConvertFilesArtifactsMounts(filesArtifactMountDirPaths, builtin.serviceNetwork)
if interpretationErr != nil {
return nil, interpretationErr
}
}
} else {
filesArtifactMountDirPaths := map[string]string{}
filesArtifactMountDirPaths[pythonWorkspace] = uniqueFilesArtifactName
var interpretationErr *startosis_errors.InterpretationError
filesArtifactExpansion, interpretationErr = service_config.ConvertFilesArtifactsMounts(filesArtifactMountDirPaths, builtin.serviceNetwork)
if interpretationErr != nil {
return nil, interpretationErr
}
}

// build a service config from image and files artifacts expansion.
Expand Down Expand Up @@ -375,26 +341,8 @@ func getPythonCommandToRun(builtin *RunPythonCapabilities) (string, error) {
}
argumentsAsString := strings.Join(maybePythonArgumentsWithRuntimeValueReplaced, spaceDelimiter)

pythonScriptAbsolutePath := path.Join(pythonWorkspace, pythonScriptFileName)
if len(argumentsAsString) > 0 {
return fmt.Sprintf("python %s %s", pythonScriptAbsolutePath, argumentsAsString), nil
}
return fmt.Sprintf("python %s", pythonScriptAbsolutePath), nil
}

func getCompressedPythonScriptForUpload(pythonScript string) (io.ReadCloser, []byte, *startosis_errors.InterpretationError) {
temporaryPythonScriptDir, err := os.MkdirTemp(defaultTmpDir, temporaryPythonDirectoryPrefix)
defer os.Remove(temporaryPythonScriptDir)
if err != nil {
return nil, nil, startosis_errors.NewInterpretationError("an error occurred while creating a temporary folder to write the python script too")
}
pythonScriptFilePath := path.Join(temporaryPythonScriptDir, pythonScriptFileName)
if err = os.WriteFile(pythonScriptFilePath, []byte(pythonScript), pythonScriptReadPermission); err != nil {
return nil, nil, startosis_errors.NewInterpretationError("an error occurred while writing python script to disk")
}
compressed, _, contentMd5, err := shared_utils.CompressPath(pythonScriptFilePath, enforceMaxSizeLimit)
if err != nil {
return nil, nil, startosis_errors.NewInterpretationError("an error occurred while compressing the python script")
return fmt.Sprintf("python -c '%s' %s", builtin.run, argumentsAsString), nil
}
return compressed, contentMd5, nil
return fmt.Sprintf("python -c '%s'", builtin.run), nil
}