Skip to content

Commit

Permalink
fix: replace env var with runtime values in run_sh (#2254)
Browse files Browse the repository at this point in the history
## Description:
Env vars weren't being replaced with runtime value in `run_sh` eg. 
```
def run(plan, args):
    database = plan.add_service(name="database", config=ServiceConfig(image="postgres:latest"))

    plan.run_sh(
        run="echo $VAR_1",
        env_vars={
            "VAR_1": database.ip_address
        }
    )
 ```
 used to output
 ```
 Command returned with exit code '0' and the following output:
--------------------
{{kurtosis:b49259098950411ba1e409e8a749c562:ip_address.runtime_value}}
--------------------
```
but now we get:
```
 Command returned with exit code '0' and the following output:
--------------------
172.16.24.5
--------------------
```

## Is this change user facing?
YES
  • Loading branch information
tedim52 committed Mar 6, 2024
1 parent 10b7900 commit 4107443
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
Expand Up @@ -188,9 +188,6 @@ func replaceMagicStrings(
if serviceConfig.GetEnvVars() != nil {
envVars = make(map[string]string, len(serviceConfig.GetEnvVars()))
for envVarName, envVarValue := range serviceConfig.GetEnvVars() {
if err != nil {
return "", nil, stacktrace.Propagate(err, "Error occurred while replacing IP address in env vars for '%v'", envVarValue)
}
envVarValueWithRuntimeValueReplaced, err := magic_string_helper.ReplaceRuntimeValueInString(envVarValue, runtimeValueStore)
if err != nil {
return "", nil, stacktrace.Propagate(err, "Error occurred while replacing runtime value in command args for '%s': '%s'", envVarName, envVarValue)
Expand Down
Expand Up @@ -219,7 +219,13 @@ func (builtin *RunShCapabilities) Validate(_ *builtin_argument.ArgumentValuesSet
// TODO Create an mechanism for other services to retrieve files from the task container
// Make task as its own entity instead of currently shown under services
func (builtin *RunShCapabilities) Execute(ctx context.Context, _ *builtin_argument.ArgumentValuesSet) (string, error) {
_, err := builtin.serviceNetwork.AddService(ctx, service.ServiceName(builtin.name), builtin.serviceConfig)
// swap env vars with their runtime value
serviceConfigWithReplacedEnvVars, err := repacaeMagicStringsInEnvVars(builtin.runtimeValueStore, builtin.serviceConfig)
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred replacing magic strings in env vars.")
}

_, err = builtin.serviceNetwork.AddService(ctx, service.ServiceName(builtin.name), serviceConfigWithReplacedEnvVars)
if err != nil {
return "", stacktrace.Propagate(err, "error occurred while creating a run_sh task with image: %v", builtin.serviceConfig.GetContainerImageName())
}
Expand Down Expand Up @@ -295,3 +301,47 @@ func getCommandToRun(builtin *RunShCapabilities) (string, error) {

return maybeSubCommandWithRuntimeValues, nil
}

func repacaeMagicStringsInEnvVars(runtimeValueStore *runtime_value_store.RuntimeValueStore, serviceConfig *service.ServiceConfig) (
*service.ServiceConfig,
error) {
var envVars map[string]string
if serviceConfig.GetEnvVars() != nil {
envVars = make(map[string]string, len(serviceConfig.GetEnvVars()))
for envVarName, envVarValue := range serviceConfig.GetEnvVars() {
envVarValueWithRuntimeValueReplaced, err := magic_string_helper.ReplaceRuntimeValueInString(envVarValue, runtimeValueStore)
if err != nil {
return nil, stacktrace.Propagate(err, "Error occurred while replacing runtime value in command args for '%s': '%s'", envVarName, envVarValue)
}
envVars[envVarName] = envVarValueWithRuntimeValueReplaced
}
}

renderedServiceConfig, err := service.CreateServiceConfig(
serviceConfig.GetContainerImageName(),
serviceConfig.GetImageBuildSpec(),
serviceConfig.GetImageRegistrySpec(),
serviceConfig.GetNixBuildSpec(),
serviceConfig.GetPrivatePorts(),
serviceConfig.GetPublicPorts(),
serviceConfig.GetEntrypointArgs(),
serviceConfig.GetCmdArgs(),
envVars,
serviceConfig.GetFilesArtifactsExpansion(),
serviceConfig.GetPersistentDirectories(),
serviceConfig.GetCPUAllocationMillicpus(),
serviceConfig.GetMemoryAllocationMegabytes(),
serviceConfig.GetPrivateIPAddrPlaceholder(),
serviceConfig.GetMinCPUAllocationMillicpus(),
serviceConfig.GetMinMemoryAllocationMegabytes(),
serviceConfig.GetLabels(),
serviceConfig.GetUser(),
serviceConfig.GetTolerations(),
serviceConfig.GetNodeSelectors(),
)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred creating a service config with env var magric strings replaced.")
}

return renderedServiceConfig, nil
}

0 comments on commit 4107443

Please sign in to comment.