Skip to content

Commit

Permalink
feat: integrate nature theme named to render_template and store_service
Browse files Browse the repository at this point in the history
  • Loading branch information
Peeeekay committed Mar 2, 2023
1 parent 4b19e26 commit 2fe08fb
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 25 deletions.
29 changes: 21 additions & 8 deletions cli/cli/commands/files/rendertemplate/rendertemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,10 @@ const (
kurtosisBackendCtxKey = "kurtosis-backend"
engineClientCtxKey = "engine-client"

starlarkTemplate = `
CURRENT_TIME_STR = str(time.now().unix)
ARTIFACT_NAME = "cli-rendered-artifact-" + CURRENT_TIME_STR
starlarkTemplateWithArtifactName = `
def run(plan, args):
name = ARTIFACT_NAME
if args.name != "":
name = args.name
plan.render_templates(
name = name,
name = args.name,
config = {
args.file_name: struct(
template = args.template,
Expand All @@ -52,6 +47,19 @@ def run(plan, args):
}
)
`

starlarkTemplateWithoutArtifactName = `
def run(plan, args):
plan.render_templates(
config = {
args.file_name: struct(
template = args.template,
data = args.template_data,
),
}
)
`

doNotDryRun = false
noParallelism = 1
)
Expand Down Expand Up @@ -209,11 +217,16 @@ func validateDestRelFilePathArg(ctx context.Context, flags *flags.ParsedFlags, a
}

func renderTemplateStarlarkCommand(ctx context.Context, enclaveCtx *enclaves.EnclaveContext, destRelFilepath string, templateFileContents string, templateData interface{}, artifactName string) (string, error) {
template := starlarkTemplateWithArtifactName
if artifactName == defaultName {
template = starlarkTemplateWithoutArtifactName
}

templateDataBytes, err := json.Marshal(templateData)
if err != nil {
return "", stacktrace.Propagate(err, "An error has occurred when parsing input params to render template Starlark command")
}
runResult, err := enclaveCtx.RunStarlarkScriptBlocking(ctx, starlarkTemplate, fmt.Sprintf(`{"file_name": "%s", "template": "%s", "template_data": %s, "name": "%s"}`, destRelFilepath, templateFileContents, string(templateDataBytes), artifactName), doNotDryRun, noParallelism)
runResult, err := enclaveCtx.RunStarlarkScriptBlocking(ctx, template, fmt.Sprintf(`{"file_name": "%s", "template": "%s", "template_data": %s, "name": "%s"}`, destRelFilepath, templateFileContents, string(templateDataBytes), artifactName), doNotDryRun, noParallelism)
if runResult.ExecutionError != nil {
return "", stacktrace.NewError("An error occurred during Starlark script execution for rendering template: %s", runResult.ExecutionError.GetErrorMessage())
}
Expand Down
26 changes: 17 additions & 9 deletions cli/cli/commands/files/storeservice/storeservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@ const (
kurtosisBackendCtxKey = "kurtosis-backend"
engineClientCtxKey = "engine-client"

starlarkTemplate = `
CURRENT_TIME_STR = str(time.now().unix)
ARTIFACT_NAME = "cli-stored-artifact-" + CURRENT_TIME_STR
starlarkTemplateWithArtifactName = `
def run(plan, args):
name = ARTIFACT_NAME
if args.name != "":
name = args.name
plan.store_service_files(
name = name,
src = args.src,
name = args.name,
service_name = args.service_name,
)
`

starlarkTemplateWithoutArtifactName = `
def run(plan, args):
plan.store_service_files(
src = args.src,
service_name = args.service_name,
src = args.src
)
`
noParallelism = 1
Expand Down Expand Up @@ -146,7 +149,12 @@ func run(
}

func storeServiceFileStarlarkCommand(ctx context.Context, enclaveCtx *enclaves.EnclaveContext, serviceName services.ServiceName, filePath string, enclaveIdentifier string, artifactName string) (*enclaves.StarlarkRunResult, error) {
runResult, err := enclaveCtx.RunStarlarkScriptBlocking(ctx, starlarkTemplate, fmt.Sprintf(`{"service_name": "%s", "src": "%s", "name": "%s"}`, serviceName, filePath, artifactName), false, noParallelism)
template := starlarkTemplateWithArtifactName
if artifactName == defaultName {
template = starlarkTemplateWithoutArtifactName
}

runResult, err := enclaveCtx.RunStarlarkScriptBlocking(ctx, template, fmt.Sprintf(`{"service_name": "%s", "src": "%s", "name": "%s"}`, serviceName, filePath, artifactName), false, noParallelism)
if err != nil {
return nil, stacktrace.Propagate(
err,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ func (builtin *StoreServiceFilesCapabilities) Execute(ctx context.Context, _ *bu
if err != nil {
return "", stacktrace.Propagate(err, "Failed to copy file '%v' from service '%v", builtin.src, builtin.serviceName)
}
instructionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", builtin.artifactName, artifactUuid)
instructionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", builtin.artifactName, artifactUuid)
return instructionResult, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ func (builtin *UploadFilesCapabilities) Execute(_ context.Context, _ *builtin_ar
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred while uploading the compressed contents\n'%v'", compressedData)
}
instructionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", builtin.artifactName, filesArtifactUuid)
instructionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", builtin.artifactName, filesArtifactUuid)
return instructionResult, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ func (t *storeServiceFilesTestCase) GetStarlarkCode() string {
func (t *storeServiceFilesTestCase) Assert(interpretationResult starlark.Value, executionResult *string) {
require.Equal(t, starlark.String(TestArtifactName), interpretationResult)

expectedExecutionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", TestArtifactName, TestArtifactUuid)
expectedExecutionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", TestArtifactName, TestArtifactUuid)
require.Equal(t, expectedExecutionResult, *executionResult)
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ func (t *storeServiceFilesWithoutNameTestCase) GetStarlarkCode() string {
func (t *storeServiceFilesWithoutNameTestCase) Assert(interpretationResult starlark.Value, executionResult *string) {
require.Equal(t, starlark.String(mockedFileArtifactName), interpretationResult)

expectedExecutionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", mockedFileArtifactName, TestArtifactUuid)
expectedExecutionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", mockedFileArtifactName, TestArtifactUuid)
require.Equal(t, expectedExecutionResult, *executionResult)
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ func (t uploadFilesTestCase) GetStarlarkCode() string {
func (t *uploadFilesTestCase) Assert(interpretationResult starlark.Value, executionResult *string) {
require.Equal(t, starlark.String(TestArtifactName), interpretationResult)

expectedExecutionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", TestArtifactName, TestArtifactUuid)
expectedExecutionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", TestArtifactName, TestArtifactUuid)
require.Equal(t, expectedExecutionResult, *executionResult)
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ func (t uploadFilesWithoutNameTestCase) GetStarlarkCode() string {
func (t *uploadFilesWithoutNameTestCase) Assert(interpretationResult starlark.Value, executionResult *string) {
require.Equal(t, starlark.String(mockedFileArtifactName), interpretationResult)

expectedExecutionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", mockedFileArtifactName, TestArtifactUuid)
expectedExecutionResult := fmt.Sprintf("Files with artifact name '%s' uploaded with artifact UUID '%s'", mockedFileArtifactName, TestArtifactUuid)
require.Equal(t, expectedExecutionResult, *executionResult)
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Adding service example-datastore-server-1.
Service 'example-datastore-server-1' added with service UUID '[a-z-0-9]+'
Service example-datastore-server-1 deployed successfully.
Command returned with exit code '0' with no output
Files with artifact name 'stored-file' uploaded with artifact UUID '[a-f0-9]{32}'
Files with artifact name 'stored-file' uploaded with artifact UUID '[a-f0-9]{32}'
Stored file at stored-file
Templates artifact name 'rendered-file' rendered with artifact UUID '[a-f0-9]{32}'
Rendered file to rendered-file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestStartosis(t *testing.T) {
require.Nil(t, runResult.ExecutionError, "Unexpected execution error")

expectedScriptOutput := `Adding service example-datastore-server-1.
Files with artifact name 'test-artifact' uploaded with artifact UUID '[a-f0-9]{32}'
Files with artifact name 'test-artifact' uploaded with artifact UUID '[a-f0-9]{32}'
Uploaded test-artifact
Service 'example-datastore-server-1' added with service UUID '[a-z-0-9]+'
`
Expand Down

0 comments on commit 2fe08fb

Please sign in to comment.