This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
runner.go
578 lines (482 loc) · 15.5 KB
/
runner.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package main
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/go-connections/nat"
"golang.org/x/xerrors"
"go.coder.com/flog"
"go.coder.com/sail/internal/dockutil"
)
// containerLogPath is the location of the code-server log.
const containerLogPath = "/tmp/code-server.log"
// containerHome is the location of the user's home directory
// inside of the container. This is only used in places where
// docker won't expand the `~` path or the `$HOME` variable.
// For example, when setting environment variables for the container.
const containerHome = "/home/user"
// Docker labels for sail state.
const (
sailLabel = "com.coder.sail"
baseImageLabel = sailLabel + ".base_image"
hatLabel = sailLabel + ".hat"
projectLocalDirLabel = sailLabel + ".project_local_dir"
projectDirLabel = sailLabel + ".project_dir"
projectNameLabel = sailLabel + ".project_name"
proxyURLLabel = sailLabel + ".proxy_url"
)
// Docker labels for user configuration.
const (
onStartLabel = "on_start"
projectRootLabel = "project_root"
)
// runner holds all the information needed to assemble a new sail container.
// The runner stores itself as state on the container.
// It enables quick iteration on a container with small modifications to it's config.
// All mounts should be configured from the image.
type runner struct {
cntName string
projectName string
hostname string
port string
projectLocalDir string
testCmd string
proxyURL string
}
// runContainer creates and runs a new container.
// It handles installing code-server, and uses code-server as
// the container's root process.
// We want code-server to be the root process as it gives us the nice guarantee that
// the container is only online when code-server is working.
// Additionally, runContainer also runs the image's `on_start` label as a bash
// command inside of the project directory.
func (r *runner) runContainer(image string) error {
cli := dockerClient()
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
projectDir, err := r.projectDir(image)
if err != nil {
return err
}
var envs []string
envs = r.environment(envs)
containerConfig := &container.Config{
Hostname: r.hostname,
Env: envs,
Cmd: strslice.StrSlice{
"bash", "-c", r.constructCommand(projectDir),
},
Image: image,
Labels: map[string]string{
sailLabel: "",
projectDirLabel: projectDir,
projectLocalDirLabel: r.projectLocalDir,
projectNameLabel: r.projectName,
proxyURLLabel: r.proxyURL,
},
// The user inside has uid 1000. This works even on macOS where the default user has uid 501.
// See https://stackoverflow.com/questions/43097341/docker-on-macosx-does-not-translate-file-ownership-correctly-in-volumes
// The docker image runs it as uid 1000 so we don't need to set anything.
User: "",
}
err = r.addImageDefinedLabels(image, containerConfig.Labels)
if err != nil {
return xerrors.Errorf("failed to add image defined labels: %w", err)
}
var mounts []mount.Mount
mounts = r.addHatMount(mounts, containerConfig.Labels)
mounts, err = r.mounts(mounts, image)
if err != nil {
return xerrors.Errorf("failed to assemble mounts: %w", err)
}
hostConfig, err := r.hostConfig(containerConfig, mounts)
if err != nil {
return err
}
_, err = cli.ContainerCreate(ctx, containerConfig, hostConfig, nil, r.cntName)
if err != nil {
return xerrors.Errorf("failed to create container: %w", err)
}
err = cli.ContainerStart(ctx, r.cntName, types.ContainerStartOptions{})
if err != nil {
return xerrors.Errorf("failed to start container: %w", err)
}
err = r.runOnStart(image)
if err != nil {
return xerrors.Errorf("failed to run on_start label in container: %w", err)
}
return nil
}
// constructCommand constructs the code-server command that will be used
// as the Sail container's init process.
func (r *runner) constructCommand(projectDir string) string {
containerAddr := "localhost"
containerPort := r.port
if runtime.GOOS == "darwin" {
// See justification in `runner.hostConfig`.
containerPort = "8443"
containerAddr = "0.0.0.0"
}
// We want the code-server logs to be available inside the container for easy
// access during development, but also going to stdout so `docker logs` can be used
// to debug a failed code-server startup.
//
// We start code-server such that extensions installed through the UI are placed in the host's extension dir.
cmd := fmt.Sprintf(`set -euxo pipefail || exit 1
cd %v
# This is necessary in case the .vscode directory wasn't created inside the container, as mounting to the host
# extension dir will create it as root.
sudo chown user:user ~/.vscode
/usr/bin/code-server --host %v --port %v --user-data-dir ~/.config/Code --extensions-dir %v --extra-extensions-dir ~/.vscode/extensions --auth=none \
--allow-http 2>&1 | tee %v`,
projectDir, containerAddr, containerPort, hostExtensionsDir, containerLogPath)
if r.testCmd != "" {
cmd = r.testCmd + "\n exit 1"
}
return cmd
}
// hostConfig constructs the container.HostConfig required for starting the sail container.
func (r *runner) hostConfig(containerConfig *container.Config, mounts []mount.Mount) (*container.HostConfig, error) {
hostConfig := &container.HostConfig{
Mounts: mounts,
NetworkMode: "host",
Privileged: true,
ExtraHosts: []string{
r.hostname + ":127.0.0.1",
},
}
// macOS does not support host networking.
// See https://github.com/docker/for-mac/issues/2716
if runtime.GOOS == "darwin" {
portSpec := fmt.Sprintf("127.0.0.1:%v:%v/tcp", r.port, "8443")
hostConfig.NetworkMode = ""
exposed, bindings, err := nat.ParsePortSpecs([]string{portSpec})
if err != nil {
return nil, xerrors.Errorf("failed to parse port spec: %w", err)
}
containerConfig.ExposedPorts = exposed
hostConfig.PortBindings = bindings
}
return hostConfig, nil
}
// environment sets any environment variables that may need to be set inside
// the container.
func (r *runner) environment(envs []string) []string {
sshAuthSock, exists := os.LookupEnv("SSH_AUTH_SOCK")
if exists {
s := fmt.Sprintf("SSH_AUTH_SOCK=%s", sshAuthSock)
envs = append(envs, s)
}
if runtime.GOOS == "linux" {
// When on linux and the display variable exists we forward it so
// that GUI applications can run.
if os.Getenv("DISPLAY") != "" {
envs = append(envs, "DISPLAY="+os.Getenv("DISPLAY"))
}
if os.Getenv("XAUTHORITY") != "" {
envs = append(envs, "XAUTHORITY="+filepath.Join(containerHome, ".Xauthority"))
}
}
return envs
}
// addHatMount mounts the hat into the user's container if they've specified one.
func (r *runner) addHatMount(mounts []mount.Mount, labels map[string]string) []mount.Mount {
hatPath, ok := labels[hatLabel]
if !ok {
return mounts
}
return append(mounts, mount.Mount{
Type: "bind",
Source: hatPath,
Target: "~/.hat",
})
}
const hostExtensionsDir = "~/.vscode/host-extensions"
func (r *runner) mounts(mounts []mount.Mount, image string) ([]mount.Mount, error) {
// Mount in VS Code configs.
mounts = append(mounts, mount.Mount{
Type: "bind",
Source: vscodeConfigDir(),
Target: "~/.config/Code",
})
mounts = append(mounts, mount.Mount{
Type: "bind",
Source: vscodeExtensionsDir(),
Target: hostExtensionsDir,
})
mounts = mountGUI(mounts)
// 'SSH_AUTH_SOCK' is provided by a running ssh-agent. Passing in the
// socket to the container allows for using the user's existing setup for
// ssh authentication instead of having to create a new keys or explicity
// pass them in.
if sshAuthSock, exists := os.LookupEnv("SSH_AUTH_SOCK"); exists {
mounts = append(mounts, mount.Mount{
Type: "bind",
Source: sshAuthSock,
Target: sshAuthSock,
})
}
localGlobalStorageDir := filepath.Join(metaRoot(), r.cntName, "globalStorage")
err := os.MkdirAll(localGlobalStorageDir, 0750)
if err != nil {
return nil, err
}
// globalStorage holds the UI state, and other code-server specific
// state.
mounts = append(mounts, mount.Mount{
Type: "bind",
Source: localGlobalStorageDir,
Target: "~/.local/share/code-server/globalStorage/",
})
projectDir, err := r.projectDir(image)
if err != nil {
return nil, err
}
mounts = append(mounts, mount.Mount{
Type: "bind",
Source: r.projectLocalDir,
Target: projectDir,
})
// Mount in code-server
codeServerBinPath, err := loadCodeServer(context.Background())
if err != nil {
return nil, xerrors.Errorf("failed to load code-server: %w", err)
}
mounts = append(mounts, mount.Mount{
Type: mount.TypeBind,
Source: codeServerBinPath,
Target: "/usr/bin/code-server",
})
// We take the mounts from the final image so that it includes the hat and the baseImage.
mounts, err = r.imageDefinedMounts(image, mounts)
if err != nil {
return nil, err
}
r.resolveMounts(mounts)
err = r.ensureMountSources(mounts)
if err != nil {
return nil, err
}
return mounts, nil
}
// mountGUI mounts in any x11 sockets so that they can be used
// inside the container.
func mountGUI(mounts []mount.Mount) []mount.Mount {
if runtime.GOOS == "linux" {
// Only mount in the x11 socket if the DISPLAY env exists.
if os.Getenv("DISPLAY") == "" {
return mounts
}
const xsock = "/tmp/.X11-unix"
mounts = append(mounts, mount.Mount{
Type: "bind",
Source: xsock,
Target: xsock,
})
// We also mount the xauthority file in so any xsessions can store
// session cookies.
if os.Getenv("XAUTHORITY") != "" {
mounts = append(mounts, mount.Mount{
Type: "bind",
Source: os.Getenv("XAUTHORITY"),
Target: "~/.Xauthority",
})
}
}
return mounts
}
// ensureMountSources ensures that the mount's source exists. If the source
// doesn't exist, it will be created as a directory on the host.
func (r *runner) ensureMountSources(mounts []mount.Mount) error {
for _, mount := range mounts {
_, err := os.Stat(mount.Source)
if err == nil {
continue
}
if !os.IsNotExist(err) {
return xerrors.Errorf("failed to stat mount source %v: %w", mount.Source, err)
}
err = os.MkdirAll(mount.Source, 0755)
if err != nil {
return xerrors.Errorf("failed to create mount source %v: %w", mount.Source, err)
}
}
return nil
}
// imageDefinedMounts adds a list of shares to the shares map from the image.
func (r *runner) imageDefinedMounts(image string, mounts []mount.Mount) ([]mount.Mount, error) {
cli := dockerClient()
defer cli.Close()
ins, _, err := cli.ImageInspectWithRaw(context.Background(), image)
if err != nil {
return nil, xerrors.Errorf("failed to inspect %v: %w", image, err)
}
for k, v := range ins.ContainerConfig.Labels {
const prefix = "share."
if !strings.HasPrefix(k, prefix) {
continue
}
tokens := strings.Split(v, ":")
if len(tokens) != 2 {
return nil, xerrors.Errorf("invalid share %q", v)
}
mounts = append(mounts, mount.Mount{
Type: mount.TypeBind,
Source: tokens[0],
Target: tokens[1],
})
}
return mounts, nil
}
// addImageDefinedLabels adds any sail labels that were defined on the image onto the container.
func (r *runner) addImageDefinedLabels(image string, labels map[string]string) error {
cli := dockerClient()
defer cli.Close()
ins, _, err := cli.ImageInspectWithRaw(context.Background(), image)
if err != nil {
return xerrors.Errorf("failed to inspect %v: %w", image, err)
}
for k, v := range ins.ContainerConfig.Labels {
if !strings.HasPrefix(k, sailLabel) {
continue
}
labels[k] = v
}
return nil
}
func (r *runner) stripDuplicateMounts(mounts []mount.Mount) []mount.Mount {
rmounts := make([]mount.Mount, 0, len(mounts))
dests := make(map[string]struct{})
for _, mnt := range mounts {
if _, ok := dests[mnt.Target]; ok {
continue
}
dests[mnt.Target] = struct{}{}
rmounts = append(rmounts, mnt)
}
return rmounts
}
func panicf(fmtStr string, args ...interface{}) {
panic(fmt.Sprintf(fmtStr, args...))
}
// resolveMounts replaces ~ with appropriate home paths with
// each mount.
func (r *runner) resolveMounts(mounts []mount.Mount) {
hostHomeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
for i := range mounts {
mounts[i].Source, err = filepath.Abs(resolvePath(hostHomeDir, mounts[i].Source))
if err != nil {
panicf("failed to resolve %v: %v", mounts[i].Source, err)
}
mounts[i].Target = resolvePath(guestHomeDir, mounts[i].Target)
}
}
func (r *runner) projectDir(image string) (string, error) {
cli := dockerClient()
defer cli.Close()
img, _, err := cli.ImageInspectWithRaw(context.Background(), image)
if err != nil {
return "", xerrors.Errorf("failed to inspect image: %w", err)
}
proot, ok := img.Config.Labels[projectRootLabel]
if ok {
return filepath.Join(proot, r.projectName), nil
}
return filepath.Join(guestHomeDir, r.projectName), nil
}
// runnerFromContainer gets a runner from container named
// name.
func runnerFromContainer(name string) (*runner, error) {
cli := dockerClient()
defer cli.Close()
cnt, err := cli.ContainerInspect(context.Background(), name)
if err != nil {
return nil, xerrors.Errorf("failed to inspect %v: %w", name, err)
}
port, err := codeServerPort(name)
if err != nil {
return nil, xerrors.Errorf("failed to find code server port: %w", err)
}
return &runner{
cntName: name,
hostname: cnt.Config.Hostname,
port: port,
projectLocalDir: cnt.Config.Labels[projectLocalDirLabel],
projectName: cnt.Config.Labels[projectNameLabel],
proxyURL: cnt.Config.Labels[proxyURLLabel],
}, nil
}
// runOnStart runs the image's `on_start` label in the container in the project directory.
func (r *runner) runOnStart(image string) error {
cli := dockerClient()
defer cli.Close()
// Get project directory.
projectDir, err := r.projectDir(image)
if err != nil {
return err
}
projectDir = resolvePath(containerHome, projectDir)
// Get on_start label from image.
img, _, err := cli.ImageInspectWithRaw(context.Background(), image)
if err != nil {
return xerrors.Errorf("failed to inspect image: %w", err)
}
onStartCmd, ok := img.Config.Labels[onStartLabel]
if !ok {
// No on_start label, so we quit early.
return nil
}
// Execute the command detached in the container.
cmd := dockutil.DetachedExecDir(r.cntName, projectDir, "/bin/bash", "-c", onStartCmd)
return cmd.Run()
}
func (r *runner) forkProxy() error {
var err error
r.proxyURL, err = forkProxy(r.cntName)
return err
}
func forkProxy(cntName string) (proxyURL string, _ error) {
sailProxy := exec.Command(os.Args[0], "proxy", cntName)
stdout, err := sailProxy.StdoutPipe()
if err != nil {
return "", xerrors.Errorf("failed to create stdout pipe: %v", err)
}
defer stdout.Close()
f, err := ioutil.TempFile("", "sailproxy_"+cntName)
if err != nil {
return "", xerrors.Errorf("failed to open /tmp: %w", err)
}
defer f.Close()
flog.Info("writing sail proxy logs to %v", f.Name())
sailProxy.Stderr = f
sailProxy.SysProcAttr = &syscall.SysProcAttr{
// See https://grokbase.com/t/gg/golang-nuts/147jmc4h0k/go-nuts-starting-detached-child-process#201407185ia7a7ldk3veno3linjktq4dve
Setpgid: true,
}
err = sailProxy.Start()
_, err = fmt.Fscan(stdout, &proxyURL)
if err != nil {
return "", xerrors.Errorf("proxy failed to output URL: %v", err)
}
_, err = url.Parse(proxyURL)
if err != nil {
return "", xerrors.Errorf("failed to parse proxy url from proxy: %v", err)
}
return proxyURL, nil
}