-
Notifications
You must be signed in to change notification settings - Fork 336
/
setup.go
123 lines (109 loc) · 3.19 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package devcontainer
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"runtime"
"github.com/loft-sh/devpod/pkg/agent"
"github.com/loft-sh/devpod/pkg/agent/tunnelserver"
"github.com/loft-sh/devpod/pkg/compress"
"github.com/loft-sh/devpod/pkg/devcontainer/config"
"github.com/loft-sh/devpod/pkg/driver"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func (r *Runner) setupContainer(
ctx context.Context,
containerDetails *config.ContainerDetails,
mergedConfig *config.MergedDevContainerConfig,
) (*config.Result, error) {
// inject agent
err := agent.InjectAgent(ctx, func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
return r.Driver.CommandDevContainer(ctx, r.ID, "root", command, stdin, stdout, stderr)
}, false, agent.ContainerDevPodHelperLocation, agent.DefaultAgentDownloadURL(), false, r.Log)
if err != nil {
return nil, errors.Wrap(err, "inject agent")
}
r.Log.Debugf("Injected into container")
defer r.Log.Debugf("Done setting up container")
// compress info
result := &config.Result{
MergedConfig: mergedConfig,
SubstitutionContext: r.SubstitutionContext,
ContainerDetails: containerDetails,
}
marshalled, err := json.Marshal(result)
if err != nil {
return nil, err
}
compressed, err := compress.Compress(string(marshalled))
if err != nil {
return nil, err
}
// compress workspace info
workspaceConfigRaw, err := json.Marshal(r.WorkspaceConfig)
if err != nil {
return nil, err
}
workspaceConfigCompressed, err := compress.Compress(string(workspaceConfigRaw))
if err != nil {
return nil, err
}
// check if docker driver
_, isDockerDriver := r.Driver.(driver.DockerDriver)
// setup container
r.Log.Infof("Setup container...")
command := fmt.Sprintf("'%s' agent container setup --setup-info '%s' --workspace-info '%s'", agent.ContainerDevPodHelperLocation, compressed, workspaceConfigCompressed)
if runtime.GOOS == "linux" || !isDockerDriver {
command += " --chown-workspace"
}
if !isDockerDriver {
command += " --stream-mounts"
}
if r.Log.GetLevel() == logrus.DebugLevel {
command += " --debug"
}
// create pipes
stdoutReader, stdoutWriter, err := os.Pipe()
if err != nil {
return nil, err
}
stdinReader, stdinWriter, err := os.Pipe()
if err != nil {
return nil, err
}
defer stdoutWriter.Close()
defer stdinWriter.Close()
// start machine on stdio
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
errChan := make(chan error, 1)
go func() {
defer r.Log.Debugf("Done executing up command")
defer cancel()
writer := r.Log.Writer(logrus.InfoLevel, false)
defer writer.Close()
r.Log.Debugf("Run command in container: %s", command)
err = r.Driver.CommandDevContainer(cancelCtx, r.ID, "root", command, stdinReader, stdoutWriter, writer)
if err != nil {
errChan <- fmt.Errorf("executing container command: %w", err)
} else {
errChan <- nil
}
}()
// start server
result, err = tunnelserver.RunSetupServer(
cancelCtx,
stdoutReader,
stdinWriter,
r.WorkspaceConfig.Agent.InjectDockerCredentials != "false",
config.GetMounts(result),
r.Log,
)
if err != nil {
return nil, errors.Wrap(err, "run tunnel machine")
}
return result, <-errChan
}