-
Notifications
You must be signed in to change notification settings - Fork 336
/
single.go
304 lines (268 loc) · 9.33 KB
/
single.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package devcontainer
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/loft-sh/devpod/pkg/devcontainer/config"
"github.com/loft-sh/devpod/pkg/devcontainer/metadata"
"github.com/loft-sh/devpod/pkg/driver"
provider2 "github.com/loft-sh/devpod/pkg/provider"
"github.com/pkg/errors"
)
var dockerlessImage = "ghcr.io/loft-sh/dockerless:0.1.4"
func (r *runner) runSingleContainer(ctx context.Context, parsedConfig *config.SubstitutedConfig, substitutionContext *config.SubstitutionContext, options UpOptions) (*config.Result, error) {
containerDetails, err := r.Driver.FindDevContainer(ctx, r.ID)
if err != nil {
return nil, fmt.Errorf("find dev container: %w", err)
}
// does the container already exist?
var (
mergedConfig *config.MergedDevContainerConfig
)
// if options.Recreate is true, and workspace is a running container, we should not rebuild
if options.Recreate && parsedConfig.Config.ContainerID != "" {
return nil, fmt.Errorf("cannot recreate container not created by DevPod")
} else if !options.Recreate && containerDetails != nil {
// start container if not running
if strings.ToLower(containerDetails.State.Status) != "running" {
err = r.Driver.StartDevContainer(ctx, r.ID)
if err != nil {
return nil, err
}
}
// if we are working with a non-managed container, and it has set workingDir, set it as the workspaceFolder
if parsedConfig.Config.ContainerID != "" && containerDetails.Config.WorkingDir != "" {
substitutionContext.ContainerWorkspaceFolder = containerDetails.Config.WorkingDir
}
imageMetadataConfig, err := metadata.GetImageMetadataFromContainer(containerDetails, substitutionContext, r.Log)
if err != nil {
return nil, err
}
mergedConfig, err = config.MergeConfiguration(parsedConfig.Config, imageMetadataConfig.Config)
if err != nil {
return nil, errors.Wrap(err, "merge config")
}
// If driver can reprovision, rerun the devcontainer and let the driver handle follow-up steps
if d, ok := r.Driver.(driver.ReprovisioningDriver); ok && d.CanReprovision() {
err = r.Driver.RunDevContainer(ctx, r.ID, nil)
if err != nil {
return nil, errors.Wrap(err, "start dev container")
}
// get from build info
containerDetails, err = r.Driver.FindDevContainer(ctx, r.ID)
if err != nil {
return nil, fmt.Errorf("find dev container: %w", err)
}
}
} else {
// we need to build the container
buildInfo, err := r.build(ctx, parsedConfig, substitutionContext, provider2.BuildOptions{
CLIOptions: provider2.CLIOptions{
PrebuildRepositories: options.PrebuildRepositories,
ForceDockerless: options.ForceDockerless,
},
NoBuild: options.NoBuild,
})
if err != nil {
return nil, errors.Wrap(err, "build image")
}
// delete container on recreation
if options.Recreate {
err := r.Delete(ctx)
if err != nil {
return nil, errors.Wrap(err, "delete devcontainer")
}
}
// merge configuration
mergedConfig, err = config.MergeConfiguration(parsedConfig.Config, buildInfo.ImageMetadata.Config)
if err != nil {
return nil, errors.Wrap(err, "merge config")
}
// run dev container
err = r.runContainer(ctx, parsedConfig, substitutionContext, mergedConfig, buildInfo)
if err != nil {
return nil, errors.Wrap(err, "start dev container")
}
// TODO: wait here a bit for correct startup?
// get from build info
containerDetails, err = r.Driver.FindDevContainer(ctx, r.ID)
if err != nil {
return nil, fmt.Errorf("find dev container: %w", err)
}
}
// setup container
return r.setupContainer(ctx, parsedConfig.Raw, containerDetails, mergedConfig, substitutionContext)
}
func (r *runner) runContainer(
ctx context.Context,
parsedConfig *config.SubstitutedConfig,
substitutionContext *config.SubstitutionContext,
mergedConfig *config.MergedDevContainerConfig,
buildInfo *config.BuildInfo,
) error {
var err error
// build run options for dockerless mode
var runOptions *driver.RunOptions
if buildInfo.Dockerless != nil {
runOptions, err = r.getDockerlessRunOptions(mergedConfig, substitutionContext, buildInfo)
if err != nil {
return fmt.Errorf("build dockerless run options: %w", err)
}
} else {
// build run options
runOptions, err = r.getRunOptions(mergedConfig, substitutionContext, buildInfo)
if err != nil {
return fmt.Errorf("build run options: %w", err)
}
}
// add environment variables that signals that we are in a remote container
// (vscode compatibility) and specifically that we are using devpod.
if runOptions.Env == nil {
runOptions.Env = make(map[string]string)
}
runOptions.Env["DEVPOD"] = "true"
runOptions.Env["REMOTE_CONTAINERS"] = "true"
// check if docker
dockerDriver, ok := r.Driver.(driver.DockerDriver)
if ok {
return dockerDriver.RunDockerDevContainer(
ctx,
r.ID,
runOptions,
parsedConfig.Config,
mergedConfig.Init,
r.WorkspaceConfig.Workspace.IDE.Name,
r.WorkspaceConfig.Workspace.IDE.Options,
)
}
// build run options for regular driver
return r.Driver.RunDevContainer(ctx, r.ID, runOptions)
}
func (r *runner) getDockerlessRunOptions(
mergedConfig *config.MergedDevContainerConfig,
substitutionContext *config.SubstitutionContext,
buildInfo *config.BuildInfo,
) (*driver.RunOptions, error) {
// parse workspace mount
workspaceMountParsed := config.ParseMount(substitutionContext.WorkspaceMount)
// add metadata as label here
marshalled, err := json.Marshal(buildInfo.ImageMetadata.Raw)
if err != nil {
return nil, errors.Wrap(err, "marshal config")
}
env := map[string]string{
"DOCKERLESS": "true",
"DOCKERLESS_CONTEXT": buildInfo.Dockerless.Context,
"DOCKERLESS_DOCKERFILE": buildInfo.Dockerless.Dockerfile,
}
for k, v := range mergedConfig.ContainerEnv {
env[k] = v
}
if buildInfo.Dockerless.Target != "" {
env["DOCKERLESS_TARGET"] = buildInfo.Dockerless.Target
}
if len(buildInfo.Dockerless.BuildArgs) > 0 {
out, err := json.Marshal(config.ObjectToList(buildInfo.Dockerless.BuildArgs))
if err != nil {
return nil, fmt.Errorf("marshal build args: %w", err)
}
env["DOCKERLESS_BUILD_ARGS"] = string(out)
}
image := dockerlessImage
if r.WorkspaceConfig != nil && r.WorkspaceConfig.Agent.Dockerless.Image != "" {
image = r.WorkspaceConfig.Agent.Dockerless.Image
}
// we need to add an extra mount here, because otherwise the build config might get lost
mounts := mergedConfig.Mounts
mounts = append(mounts, &config.Mount{
Type: "volume",
Source: "dockerless-" + r.ID,
Target: "/workspaces/.dockerless",
})
uid := ""
if r.WorkspaceConfig != nil && r.WorkspaceConfig.Workspace != nil {
uid = r.WorkspaceConfig.Workspace.UID
}
// build run options
return &driver.RunOptions{
UID: uid,
Image: image,
User: "root",
Entrypoint: "/.dockerless/dockerless",
Cmd: []string{
"start",
"--wait",
"--entrypoint", "/.dockerless/bin/sh",
"--cmd", "-c",
"--cmd", GetStartScript(mergedConfig),
"--user", buildInfo.Dockerless.User,
},
Env: env,
CapAdd: mergedConfig.CapAdd,
Labels: []string{
metadata.ImageMetadataLabel + "=" + string(marshalled),
config.UserLabel + "=" + buildInfo.Dockerless.User,
},
Privileged: mergedConfig.Privileged,
WorkspaceMount: &workspaceMountParsed,
Mounts: mounts,
}, nil
}
func (r *runner) getRunOptions(
mergedConfig *config.MergedDevContainerConfig,
substitutionContext *config.SubstitutionContext,
buildInfo *config.BuildInfo,
) (*driver.RunOptions, error) {
// parse workspace mount
workspaceMountParsed := config.ParseMount(substitutionContext.WorkspaceMount)
// add metadata as label here
marshalled, err := json.Marshal(buildInfo.ImageMetadata.Raw)
if err != nil {
return nil, errors.Wrap(err, "marshal config")
}
// build labels & entrypoint
entrypoint, cmd := GetContainerEntrypointAndArgs(mergedConfig, buildInfo.ImageDetails)
labels := []string{
metadata.ImageMetadataLabel + "=" + string(marshalled),
config.UserLabel + "=" + buildInfo.ImageDetails.Config.User,
}
user := buildInfo.ImageDetails.Config.User
if mergedConfig.ContainerUser != "" {
user = mergedConfig.ContainerUser
}
uid := ""
if r.WorkspaceConfig != nil && r.WorkspaceConfig.Workspace != nil {
uid = r.WorkspaceConfig.Workspace.UID
}
return &driver.RunOptions{
UID: uid,
Image: buildInfo.ImageName,
User: user,
Entrypoint: entrypoint,
Cmd: cmd,
Env: mergedConfig.ContainerEnv,
CapAdd: mergedConfig.CapAdd,
Labels: labels,
Privileged: mergedConfig.Privileged,
WorkspaceMount: &workspaceMountParsed,
SecurityOpt: mergedConfig.SecurityOpt,
Mounts: mergedConfig.Mounts,
}, nil
}
func GetStartScript(mergedConfig *config.MergedDevContainerConfig) string {
customEntrypoints := mergedConfig.Entrypoints
return `echo Container started
trap "exit 0" 15
` + strings.Join(customEntrypoints, "\n") + `
exec "$@"
while sleep 1 & wait $!; do :; done`
}
func GetContainerEntrypointAndArgs(mergedConfig *config.MergedDevContainerConfig, imageDetails *config.ImageDetails) (string, []string) {
cmd := []string{"-c", GetStartScript(mergedConfig), "-"} // `wait $!` allows for the `trap` to run (synchronous `sleep` would not).
if imageDetails != nil && mergedConfig.OverrideCommand != nil && !*mergedConfig.OverrideCommand {
cmd = append(cmd, imageDetails.Config.Entrypoint...)
cmd = append(cmd, imageDetails.Config.Cmd...)
}
return "/bin/sh", cmd
}