@@ -21,6 +21,7 @@ import (
21
21
"golang.org/x/xerrors"
22
22
23
23
"go.coder.com/flog"
24
+ "go.coder.com/sail/internal/dockutil"
24
25
)
25
26
26
27
// containerLogPath is the location of the code-server log.
@@ -32,17 +33,6 @@ const containerLogPath = "/tmp/code-server.log"
32
33
// For example, when setting environment variables for the container.
33
34
const containerHome = "/home/user"
34
35
35
- // expandDir expands ~ to be the containerHome variable.
36
- func expandDir (path string ) string {
37
- path = filepath .Clean (path )
38
- if path == "~" {
39
- path = containerHome
40
- } else if strings .HasPrefix (path , "~/" ) {
41
- path = filepath .Join (containerHome , path [2 :])
42
- }
43
- return filepath .Clean (path )
44
- }
45
-
46
36
// Docker labels for sail state.
47
37
const (
48
38
sailLabel = "com.coder.sail"
@@ -57,7 +47,7 @@ const (
57
47
58
48
// Docker labels for user configuration.
59
49
const (
60
- onOpenLabel = "on_open "
50
+ onStartLabel = "on_start "
61
51
projectRootLabel = "project_root"
62
52
)
63
53
@@ -85,7 +75,7 @@ type runner struct {
85
75
// the container's root process.
86
76
// We want code-server to be the root process as it gives us the nice guarantee that
87
77
// the container is only online when code-server is working.
88
- // Additionally, runContainer also runs the image's on_open label as a sh
78
+ // Additionally, runContainer also runs the image's `on_start` label as a bash
89
79
// command inside of the project directory.
90
80
func (r * runner ) runContainer (image string ) error {
91
81
cli := dockerClient ()
@@ -150,9 +140,9 @@ func (r *runner) runContainer(image string) error {
150
140
return xerrors .Errorf ("failed to start container: %w" , err )
151
141
}
152
142
153
- err = r .runOnOpen ( ctx , image )
143
+ err = r .runOnStart ( image )
154
144
if err != nil {
155
- return xerrors .Errorf ("failed to run on_open label in container: %w" , err )
145
+ return xerrors .Errorf ("failed to run on_start label in container: %w" , err )
156
146
}
157
147
158
148
return nil
@@ -515,61 +505,32 @@ func runnerFromContainer(name string) (*runner, error) {
515
505
}, nil
516
506
}
517
507
518
- // runOnOpen runs the image's `on_open ` label in the container in the project directory.
519
- func (r * runner ) runOnOpen ( ctx context. Context , image string ) error {
508
+ // runOnStart runs the image's `on_start ` label in the container in the project directory.
509
+ func (r * runner ) runOnStart ( image string ) error {
520
510
cli := dockerClient ()
521
511
defer cli .Close ()
522
512
523
- // get project directory.
513
+ // Get project directory.
524
514
projectDir , err := r .projectDir (image )
525
515
if err != nil {
526
516
return err
527
517
}
518
+ projectDir = resolvePath (containerHome , projectDir )
528
519
529
- // get on_open label from image
520
+ // Get on_start label from image.
530
521
img , _ , err := cli .ImageInspectWithRaw (context .Background (), image )
531
522
if err != nil {
532
523
return xerrors .Errorf ("failed to inspect image: %w" , err )
533
524
}
534
- onOpenCmd , ok := img .Config .Labels [onOpenLabel ]
525
+ onStartCmd , ok := img .Config .Labels [onStartLabel ]
535
526
if ! ok {
536
- // no on_open label, so we quit early.
527
+ // No on_start label, so we quit early.
537
528
return nil
538
529
}
539
530
540
- cmd := []string {onOpenCmd }
541
- return r .runInContainer (ctx , expandDir (projectDir ), cmd , true )
542
- }
543
-
544
- // runInContainer runs a command in the container (optionally using /bin/sh -c) using exec (detached).
545
- func (r * runner ) runInContainer (ctx context.Context , workDir string , cmd []string , useSh bool ) error {
546
- cli := dockerClient ()
547
- defer cli .Close ()
548
-
549
- if useSh {
550
- cmd = append ([]string {"/bin/sh" , "-c" }, cmd ... )
551
- }
552
-
553
- execID , err := cli .ContainerExecCreate (ctx , r .cntName , types.ExecConfig {
554
- Cmd : cmd ,
555
- Detach : true ,
556
- WorkingDir : workDir ,
557
-
558
- // the following options don't attach it, but makes the script think it's running in a terminal.
559
- Tty : true ,
560
- AttachStdin : true ,
561
- AttachStderr : true ,
562
- AttachStdout : true ,
563
- })
564
- if err != nil {
565
- return xerrors .Errorf ("failed to create exec configuration: %v" , err )
566
- }
567
-
568
- err = cli .ContainerExecStart (ctx , execID .ID , types.ExecStartCheck {Detach : true })
569
- if err != nil {
570
- return xerrors .Errorf ("failed to start exec process: %v" , err )
571
- }
572
- return nil
531
+ // Execute the command detached in the container.
532
+ cmd := dockutil .DetachedExecDir (r .cntName , projectDir , "/bin/bash" , "-c" , onStartCmd )
533
+ return cmd .Run ()
573
534
}
574
535
575
536
func (r * runner ) forkProxy () error {
0 commit comments