-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathrun.go
More file actions
276 lines (229 loc) · 7.59 KB
/
run.go
File metadata and controls
276 lines (229 loc) · 7.59 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package devcontainer
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/loft-sh/devpod/pkg/devcontainer/config"
"github.com/loft-sh/devpod/pkg/driver"
"github.com/loft-sh/devpod/pkg/driver/drivercreate"
"github.com/loft-sh/devpod/pkg/encoding"
"github.com/loft-sh/devpod/pkg/language"
provider2 "github.com/loft-sh/devpod/pkg/provider"
"github.com/loft-sh/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Runner interface {
Up(ctx context.Context, options UpOptions, timeout time.Duration) (*config.Result, error)
Build(ctx context.Context, options provider2.BuildOptions) (string, error)
Find(ctx context.Context) (*config.ContainerDetails, error)
Command(
ctx context.Context,
user string,
command string,
stdin io.Reader,
stdout io.Writer,
stderr io.Writer,
) error
Stop(ctx context.Context) error
Delete(ctx context.Context) error
Logs(ctx context.Context, writer io.Writer) error
}
func NewRunner(
agentPath, agentDownloadURL string,
workspaceConfig *provider2.AgentWorkspaceInfo,
log log.Logger,
) (Runner, error) {
driver, err := drivercreate.NewDriver(workspaceConfig, log)
if err != nil {
return nil, err
}
// we use the workspace uid as id to avoid conflicts between container names
return &runner{
Driver: driver,
AgentPath: agentPath,
AgentDownloadURL: agentDownloadURL,
LocalWorkspaceFolder: workspaceConfig.ContentFolder,
ID: GetRunnerIDFromWorkspace(workspaceConfig.Workspace),
WorkspaceConfig: workspaceConfig,
Log: log,
}, nil
}
type runner struct {
Driver driver.Driver
WorkspaceConfig *provider2.AgentWorkspaceInfo
AgentPath string
AgentDownloadURL string
LocalWorkspaceFolder string
ID string
Log log.Logger
}
type UpOptions struct {
provider2.CLIOptions
NoBuild bool
ForceBuild bool
RegistryCache string
}
func (r *runner) Up(ctx context.Context, options UpOptions, timeout time.Duration) (*config.Result, error) {
r.Log.Debugf("Up devcontainer for workspace '%s' with timeout %s", r.WorkspaceConfig.Workspace.ID, timeout)
// probe local user environment for localEnv substitution
probedEnv, err := config.ProbeUserEnvWithUserSwitch(ctx, string(config.DefaultUserEnvProbe), "", false, r.Log)
if err != nil {
r.Log.Warnf("failed to probe local user environment, localEnv variables may not work: %v", err)
probedEnv = map[string]string{}
}
substitutedConfig, substitutionContext, err := r.getSubstitutedConfig(options.CLIOptions, probedEnv)
if err != nil {
return nil, err
}
defer cleanupBuildInformation(substitutedConfig.Config)
// do not run initialize command in platform mode
if !options.CLIOptions.Platform.Enabled {
if err := runInitializeCommand(r.LocalWorkspaceFolder, substitutedConfig.Config, options.InitEnv, r.Log); err != nil {
return nil, err
}
} else if len(substitutedConfig.Config.InitializeCommand) > 0 {
r.Log.Info("Skipping initializeCommand on platform")
}
switch {
case isDockerFileConfig(substitutedConfig.Config),
substitutedConfig.Config.Image != "",
substitutedConfig.Config.ContainerID != "":
return r.runSingleContainer(
ctx,
substitutedConfig,
substitutionContext,
options,
timeout,
)
case isDockerComposeConfig(substitutedConfig.Config):
return r.runDockerCompose(ctx, substitutedConfig, substitutionContext, options, timeout)
default:
return r.runDefaultContainer(ctx, options, substitutedConfig, substitutionContext, timeout)
}
}
func (r *runner) runDefaultContainer(ctx context.Context, options UpOptions, substitutedConfig *config.SubstitutedConfig, substitutionContext *config.SubstitutionContext, timeout time.Duration) (*config.Result, error) {
if options.FallbackImage != "" {
r.Log.Warn("dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties, using fallback image " + options.FallbackImage)
substitutedConfig.Config.ImageContainer = config.ImageContainer{
Image: options.FallbackImage,
}
} else {
r.Log.Warn("dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties, defaulting to auto-detection")
lang, err := language.DetectLanguage(r.LocalWorkspaceFolder)
if err != nil {
return nil, fmt.Errorf("could not detect project language and dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties")
}
if language.MapConfig[lang] == nil {
return nil, fmt.Errorf("could not detect project language and dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties")
}
substitutedConfig.Config.ImageContainer = language.MapConfig[lang].ImageContainer
}
return r.runSingleContainer(ctx, substitutedConfig, substitutionContext, options, timeout)
}
func (r *runner) Command(
ctx context.Context,
user string,
command string,
stdin io.Reader,
stdout io.Writer,
stderr io.Writer,
) error {
return r.Driver.CommandDevContainer(ctx, r.ID, user, command, stdin, stdout, stderr)
}
func (r *runner) Find(ctx context.Context) (*config.ContainerDetails, error) {
containerDetails, err := r.Driver.FindDevContainer(ctx, r.ID)
if err != nil {
return nil, errors.Wrap(err, "find dev container")
}
return containerDetails, nil
}
func (r *runner) Logs(ctx context.Context, writer io.Writer) error {
return r.Driver.GetDevContainerLogs(ctx, r.ID, writer, writer)
}
func isDockerFileConfig(config *config.DevContainerConfig) bool {
return config.GetDockerfile() != ""
}
func runInitializeCommand(
workspaceFolder string,
config *config.DevContainerConfig,
extraEnvVars []string,
log log.Logger,
) error {
if len(config.InitializeCommand) == 0 {
return nil
}
shellArgs := []string{"sh", "-c"}
// According to the devcontainer spec, `initializeCommand` needs to be run on the host.
// On Windows we can't assume everyone has `sh` added to their PATH so we need to use Windows default shell (usually cmd.exe)
if runtime.GOOS == "windows" {
comSpec := os.Getenv("COMSPEC")
if comSpec != "" {
shellArgs = []string{comSpec, "/c"}
} else {
shellArgs = []string{"cmd.exe", "/c"}
}
}
for _, cmd := range config.InitializeCommand {
// should run in shell?
var args []string
if len(cmd) == 1 {
args = []string{shellArgs[0], shellArgs[1], cmd[0]}
} else {
args = cmd
}
// run the command
log.Infof("Running initializeCommand from devcontainer.json: '%s'", strings.Join(args, " "))
writer := log.Writer(logrus.InfoLevel, false)
errwriter := log.Writer(logrus.ErrorLevel, false)
defer writer.Close()
defer errwriter.Close()
cmd := exec.Command(args[0], args[1:]...)
env := cmd.Environ()
env = append(env, extraEnvVars...)
cmd.Stdout = writer
cmd.Stderr = errwriter
cmd.Dir = workspaceFolder
cmd.Env = env
err := cmd.Run()
if err != nil {
return err
}
}
return nil
}
func getWorkspace(
workspaceFolder, workspaceID string,
conf *config.DevContainerConfig,
) (string, string) {
if conf.WorkspaceMount != "" {
mount := config.ParseMount(conf.WorkspaceMount)
return conf.WorkspaceMount, mount.Target
}
containerMountFolder := conf.WorkspaceFolder
if containerMountFolder == "" {
containerMountFolder = "/workspaces/" + workspaceID
}
consistency := ""
if runtime.GOOS != "linux" {
consistency = ",consistency='consistent'"
}
return fmt.Sprintf(
"type=bind,source=%s,target=%s%s",
workspaceFolder,
containerMountFolder,
consistency,
), containerMountFolder
}
func GetRunnerIDFromWorkspace(workspace *provider2.Workspace) string {
ID := workspace.UID
if encoding.IsLegacyUID(workspace.UID) {
ID = workspace.ID
}
return ID
}