Skip to content

Commit

Permalink
fix: inherit host env vars when creating devcontainer (#863)
Browse files Browse the repository at this point in the history
Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
Tpuljak authored Jul 30, 2024
1 parent ce500c6 commit 0374265
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions pkg/docker/create_devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ func (d *DockerClient) createProjectFromDevcontainer(opts *CreateProjectOptions,
}
}

devcontainerCmd := []string{
hostEnvVars, err := d.getHostEnvVars(opts.SshClient)
if err != nil {
return "", err
}

devcontainerCmd := append(hostEnvVars, []string{
"devcontainer",
"up",
"--workspace-folder=" + paths.ProjectTarget,
Expand All @@ -202,7 +207,7 @@ func (d *DockerClient) createProjectFromDevcontainer(opts *CreateProjectOptions,
"--id-label=daytona.workspace.id=" + opts.Project.WorkspaceId,
"--id-label=daytona.project.name=" + opts.Project.Name,
"--skip-non-blocking-commands",
}
}...)

if prebuild {
devcontainerCmd = append(devcontainerCmd, "--prebuild")
Expand Down Expand Up @@ -299,27 +304,12 @@ func (d *DockerClient) ensureDockerSockForward(logWriter io.Writer) (string, err
func (d *DockerClient) readDevcontainerConfig(opts *CreateProjectOptions, paths DevcontainerPaths, socketForwardId string) (string, *devcontainer.Root, error) {
opts.LogWriter.Write([]byte("Reading devcontainer configuration...\n"))

env := os.Environ()
if opts.SshClient != nil {
var err error
env, err = opts.SshClient.GetEnv(nil)
if err != nil {
return "", nil, err
}
}

env = slices.DeleteFunc(env, func(el string) bool {
return strings.Contains(el, ";") || strings.Contains(el, "PATH")
})

sanitizedEnv := []string{}
for _, el := range env {
parts := strings.Split(el, "=")
santizedEl := fmt.Sprintf(`%s="%s"`, parts[0], parts[1])
sanitizedEnv = append(sanitizedEnv, santizedEl+";")
envVars, err := d.getHostEnvVars(opts.SshClient)
if err != nil {
return "", nil, err
}

devcontainerCmd := append(sanitizedEnv, []string{
devcontainerCmd := append(envVars, []string{
"devcontainer",
"read-configuration",
"--workspace-folder=" + paths.ProjectTarget,
Expand All @@ -329,7 +319,7 @@ func (d *DockerClient) readDevcontainerConfig(opts *CreateProjectOptions, paths

cmd := strings.Join(devcontainerCmd, " ")

output, err := d.execInContainer(cmd, opts, paths, paths.ProjectTarget, socketForwardId, false, nil)
output, err := d.execInContainer(cmd, opts, paths, paths.ProjectTarget, socketForwardId, true, nil)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -534,6 +524,30 @@ func (d *DockerClient) getDevcontainerPaths(opts *CreateProjectOptions) Devconta
}
}

func (d *DockerClient) getHostEnvVars(sshClient *ssh.Client) ([]string, error) {
env := os.Environ()
if sshClient != nil {
var err error
env, err = sshClient.GetEnv(nil)
if err != nil {
return nil, err
}
}

env = slices.DeleteFunc(env, func(el string) bool {
return strings.Contains(el, ";") || strings.Contains(el, "PATH")
})

sanitizedEnv := []string{}
for _, el := range env {
parts := strings.Split(el, "=")
santizedEl := fmt.Sprintf(`%s="%s"`, parts[0], parts[1])
sanitizedEnv = append(sanitizedEnv, santizedEl)
}

return sanitizedEnv, nil
}

func execDevcontainerCommand(command []string, logWriter io.Writer, sshClient *ssh.Client) error {
if sshClient != nil {
if command[0] == "sh" {
Expand Down

0 comments on commit 0374265

Please sign in to comment.