Skip to content

Commit

Permalink
fix: make run_python not create additional files artifact (#1851)
Browse files Browse the repository at this point in the history
## Description:
we'd create a files artifact which would

1. make interpret depend on service network - an anti pattern
2. bloat enclave inspect

This fixes both; tested against etheruem-package
  • Loading branch information
h4ck3rk3y authored Nov 27, 2023
1 parent 179c541 commit 82d0058
Showing 1 changed file with 2 additions and 54 deletions.
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 @@ -378,26 +344,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
}

0 comments on commit 82d0058

Please sign in to comment.