Skip to content

Commit 014849a

Browse files
committed
Expand env variables to job mount path
When installing tools into container it is sometimes desirable to have those tools on the path for the current process. Since the Windows doesn't support variable expansion on the PATH variable and the job mount path as something that isn't know at runtime we need to expand it for the passer. This could go away if the mount path is no longer used in the future. Signed-off-by: James Sturtevant <[email protected]>
1 parent 71baff4 commit 014849a

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

internal/jobcontainers/jobcontainer.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ func splitArgs(cmdLine string) []string {
3939
return r.FindAllString(cmdLine, -1)
4040
}
4141

42-
// Convert environment map to a slice of environment variables in the form [Key1=val1, key2=val2]
43-
func envMapToSlice(m map[string]string) []string {
44-
var s []string
45-
for k, v := range m {
46-
s = append(s, k+"="+v)
47-
}
48-
return s
49-
}
50-
5142
const (
5243
jobContainerNameFmt = "JobContainer_%s"
5344
// Environment variable set in every process in the job detailing where the containers volume
@@ -232,7 +223,15 @@ func (c *JobContainer) CreateProcess(ctx context.Context, config interface{}) (_
232223
if err != nil {
233224
return nil, errors.Wrap(err, "failed to get default environment block")
234225
}
235-
env = append(env, envMapToSlice(conf.Environment)...)
226+
227+
// Convert environment map to a slice of environment variables in the form [Key1=val1, key2=val2]
228+
var envs []string
229+
for k, v := range conf.Environment {
230+
expanded, _ := c.replaceWithMountPoint(v)
231+
envs = append(envs, k+"="+expanded)
232+
}
233+
env = append(env, envs...)
234+
236235
env = append(env, sandboxMountPointEnvVar+"="+c.sandboxMount)
237236

238237
// exec.Cmd internally does its own path resolution and as part of this checks some well known file extensions on the file given (e.g. if
@@ -603,7 +602,7 @@ func systemProcessInformation() ([]*winapi.SYSTEM_PROCESS_INFORMATION, error) {
603602
return procInfos, nil
604603
}
605604

606-
// Takes a string and replaces any occurences of CONTAINER_SANDBOX_MOUNT_POINT with where the containers volume is mounted, as well as returning
605+
// Takes a string and replaces any occurrences of CONTAINER_SANDBOX_MOUNT_POINT with where the containers' volume is mounted, as well as returning
607606
// if the string actually contained the environment variable.
608607
func (c *JobContainer) replaceWithMountPoint(str string) (string, bool) {
609608
newStr := strings.ReplaceAll(str, "%"+sandboxMountPointEnvVar+"%", c.sandboxMount[:len(c.sandboxMount)-1])

0 commit comments

Comments
 (0)