Skip to content

Commit

Permalink
fix exec
Browse files Browse the repository at this point in the history
  • Loading branch information
tedim52 committed Mar 4, 2024
1 parent a30d67e commit 21e4ead
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 49 deletions.
36 changes: 26 additions & 10 deletions core/server/api_container/server/startosis_engine/plan.yml
@@ -1,17 +1,33 @@
packageId: github.com/kurtosis-tech/plan-yaml-prac
services:
- uuid: "2"
name: database
- uuid: "1"
name: db
image:
name: postgres:latest
envVars:
- key: VAR_1
value: '{{ kurtosis.1.output }}'
- key: VAR_2
value: '{{ kurtosis.1.code }}'
- key: POSTGRES_PASSWORD
value: tedi
- key: POSTGRES_DB
value: tedi
- key: POSTGRES_USER
value: tedi
- uuid: "3"
name: db2
image:
name: postgres:latest
envVars:
- key: POSTGRES_DB
value: '{{ kurtosis.2.code }}'
- key: POSTGRES_USER
value: '{{ kurtosis.2.output }}'
- key: POSTGRES_PASSWORD
value: tedi
tasks:
- uuid: "1"
taskType: sh
- uuid: "2"
taskType: exec
command:
- echo some stuff
image: badouralix/curl-jq
- echo
- Hello, world
serviceName: db
acceptableCodes:
- 0
Expand Up @@ -408,7 +408,7 @@ func (pyg *PlanYamlGeneratorImpl) updatePlanYamlFromRunSh(runShInstruction *inst
returnValue := runShInstruction.GetReturnedValue()
runShStruct, ok := returnValue.(*starlarkstruct.Struct)
if !ok {
return stacktrace.NewError("Cast to service didn't work")
return stacktrace.NewError("Casting run sh return value to struct didn't work")
}
starlarkCodeVal, err := runShStruct.Attr("code")
if err != nil {
Expand Down Expand Up @@ -559,7 +559,7 @@ func (pyg *PlanYamlGeneratorImpl) updatePlanYamlFromRunPython(runPythonInstructi
returnValue := runPythonInstruction.GetReturnedValue()
runPythonStruct, ok := returnValue.(*starlarkstruct.Struct)
if !ok {
return stacktrace.NewError("Cast to service didn't work")
return stacktrace.NewError("Casting run python return value to a struct didn't work")
}
starlarkCodeVal, err := runPythonStruct.Attr("code")
if err != nil {
Expand Down Expand Up @@ -765,23 +765,29 @@ func (pyg *PlanYamlGeneratorImpl) updatePlanYamlFromExec(execInstruction *instru

// store future references
returnValue := execInstruction.GetReturnedValue()
execStruct, ok := returnValue.(*starlarkstruct.Struct)
execDict, ok := returnValue.(*starlark.Dict)
if !ok {
return stacktrace.NewError("Cast to service didn't work")
return stacktrace.NewError("Casting exec return value to a struct didn't work")
}
starlarkCodeVal, err := execStruct.Attr("code")
starlarkCodeVal, found, err := execDict.Get(starlark.String("code"))
if err != nil {
return err
}
if !found {
return stacktrace.NewError("No code value found on exec dict")
}
starlarkCodeFutureRefStr, interpErr := kurtosis_types.SafeCastToString(starlarkCodeVal, "exec code")
if interpErr != nil {
return interpErr
}
pyg.futureReferenceIndex[starlarkCodeFutureRefStr] = fmt.Sprintf("{{ kurtosis.%v.code }}", taskUuid)
starlarkOutputVal, err := execStruct.Attr("output")
starlarkOutputVal, found, err := execDict.Get(starlark.String("output"))
if err != nil {
return err
}
if !found {
return stacktrace.NewError("No code value found on exec dict")
}
starlarkOutputFutureRefStr, interpErr := kurtosis_types.SafeCastToString(starlarkOutputVal, "exec output")
if interpErr != nil {
return interpErr
Expand Down
Expand Up @@ -111,30 +111,8 @@ func (suite *PlanYamlGeneratorTestSuite) TearDownTest() {
func (suite *PlanYamlGeneratorTestSuite) TestCurrentlyBeingWorkedOn() {
dockerfileModulePath := "github.com/kurtosis-tech/plan-yaml-prac/server/Dockerfile"
serverModulePath := "github.com/kurtosis-tech/plan-yaml-prac/server"
dockerfileContents := `# Use an existing docker image as a base
FROM alpine:latest
dockerfileContents := ``

# Run commands to install necessary dependencies
RUN apk add --update nodejs npm
# Set the working directory inside the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install app dependencies
RUN npm install
# Expose a port the app runs on
EXPOSE 3000
# Define environment variable
ENV NODE_ENV=production
# Command to run the application
CMD ["node", "app.js"]
`
require.Nil(suite.T(), suite.packageContentProvider.AddFileContent(dockerfileModulePath, dockerfileContents))
require.Nil(suite.T(), suite.packageContentProvider.AddFileContent(serverModulePath, ""))

Expand All @@ -143,17 +121,34 @@ CMD ["node", "app.js"]
relativePathToMainFile := "main.star"

serializedScript := `def run(plan, args):
result = plan.run_sh(
run="echo some stuff",
plan.add_service(
name="db",
config=ServiceConfig(
image="postgres:latest",
env_vars={
"POSTGRES_DB": "tedi",
"POSTGRES_USER": "tedi",
"POSTGRES_PASSWORD": "tedi",
}
)
)
result = plan.exec(
service_name="db",
recipe=ExecRecipe(command=["echo", "Hello, world"]),
acceptable_codes=[0],
)
plan.print(result)
plan.add_service(
name="db2",
config=ServiceConfig(
image="postgres:latest",
env_vars={
"POSTGRES_DB": result["code"],
"POSTGRES_USER": result["output"],
"POSTGRES_PASSWORD": "tedi",
}
)
)
database = plan.add_service(name="database", config=ServiceConfig(
image="postgres:latest",
env_vars={
"VAR_1": result.output,
"VAR_2": result.code
}
))
`
serializedJsonParams := "{}"
_, instructionsPlan, interpretationError := suite.interpreter.Interpret(context.Background(), packageId, mainFunctionName, noPackageReplaceOptions, relativePathToMainFile, serializedScript, serializedJsonParams, defaultNonBlockingMode, emptyEnclaveComponents, emptyInstructionsPlanMask)
Expand Down

0 comments on commit 21e4ead

Please sign in to comment.