Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit fa6c4b3

Browse files
committed
rename on_open to on_start
- Rename the on_open label to on_start - Use docker cli directly to run on_start label rather than docker API - Drop (*runner).runInContainer(...) in favour of internal/dockutil - Drop expandDir(...) in favour of an existing function
1 parent d41cd2b commit fa6c4b3

File tree

4 files changed

+29
-63
lines changed

4 files changed

+29
-63
lines changed

internal/dockutil/exec.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ func DetachedExec(cntName, cmd string, args ...string) *exec.Cmd {
2525
return exec.Command("docker", args...)
2626
}
2727

28+
func DetachedExecDir(cntName, dir, cmd string, args ...string) *exec.Cmd {
29+
args = append([]string{"exec", "-dw", dir, cntName, cmd}, args...)
30+
return exec.Command("docker", args...)
31+
}
32+
2833
func ExecEnv(cntName string, envs []string, cmd string, args ...string) *exec.Cmd {
2934
args = append([]string{"exec", "-e", strings.Join(envs, ","), "-i", cntName, cmd}, args...)
3035
return exec.Command("docker", args...)

runner.go

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"golang.org/x/xerrors"
2222

2323
"go.coder.com/flog"
24+
"go.coder.com/sail/internal/dockutil"
2425
)
2526

2627
// containerLogPath is the location of the code-server log.
@@ -32,17 +33,6 @@ const containerLogPath = "/tmp/code-server.log"
3233
// For example, when setting environment variables for the container.
3334
const containerHome = "/home/user"
3435

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-
4636
// Docker labels for sail state.
4737
const (
4838
sailLabel = "com.coder.sail"
@@ -57,7 +47,7 @@ const (
5747

5848
// Docker labels for user configuration.
5949
const (
60-
onOpenLabel = "on_open"
50+
onStartLabel = "on_start"
6151
projectRootLabel = "project_root"
6252
)
6353

@@ -85,7 +75,7 @@ type runner struct {
8575
// the container's root process.
8676
// We want code-server to be the root process as it gives us the nice guarantee that
8777
// 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
8979
// command inside of the project directory.
9080
func (r *runner) runContainer(image string) error {
9181
cli := dockerClient()
@@ -150,9 +140,9 @@ func (r *runner) runContainer(image string) error {
150140
return xerrors.Errorf("failed to start container: %w", err)
151141
}
152142

153-
err = r.runOnOpen(ctx, image)
143+
err = r.runOnStart(image)
154144
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)
156146
}
157147

158148
return nil
@@ -515,61 +505,32 @@ func runnerFromContainer(name string) (*runner, error) {
515505
}, nil
516506
}
517507

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 {
520510
cli := dockerClient()
521511
defer cli.Close()
522512

523-
// get project directory.
513+
// Get project directory.
524514
projectDir, err := r.projectDir(image)
525515
if err != nil {
526516
return err
527517
}
518+
projectDir = resolvePath(containerHome, projectDir)
528519

529-
// get on_open label from image
520+
// Get on_start label from image.
530521
img, _, err := cli.ImageInspectWithRaw(context.Background(), image)
531522
if err != nil {
532523
return xerrors.Errorf("failed to inspect image: %w", err)
533524
}
534-
onOpenCmd, ok := img.Config.Labels[onOpenLabel]
525+
onStartCmd, ok := img.Config.Labels[onStartLabel]
535526
if !ok {
536-
// no on_open label, so we quit early.
527+
// No on_start label, so we quit early.
537528
return nil
538529
}
539530

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()
573534
}
574535

575536
func (r *runner) forkProxy() error {

site/content/docs/concepts/labels.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,26 @@ LABEL project_root "~/go/src/"
2424

2525
Will bind mount the host directory `$project_root/<org>/<repo>` to `~/go/src/<repo>` in the container.
2626

27-
### Run on Open Labels
27+
### On Start Labels
2828

2929
You can run a command in your sail container after it starts by specifying
30-
the `on_open` label. If you'd like to run multiple commands on launch, we
31-
recommend using a `.sh` file as your `on_open` label, as you cannot provide
32-
multiple `on_open` statements.
30+
the `on_start` label. If you'd like to run multiple commands on launch, we
31+
recommend using a `.sh` file as your `on_start` label, as you cannot
32+
provide multiple `on_start` labels in your image.
3333

34-
The `on_open` label is run detached inside of `/bin/sh` as soon as the
34+
The `on_start` label is run detached inside of `/bin/bash` as soon as the
3535
container is started, with the work directory set to your `project_root`
3636
(see the section above).
3737

3838
For example:
3939
```Dockerfile
40-
LABEL on_open "npm install"
40+
LABEL on_start "npm install"
4141
```
4242
```Dockerfile
43-
LABEL on_open "go get"
43+
LABEL on_start "go get"
4444
```
4545
```Dockerfile
46-
LABEL on_open "./.sail/on_open.sh"
46+
LABEL on_start "./.sail/on_start.sh"
4747
```
4848

4949
Make sure any scripts you make are executable, otherwise sail will fail to

versionmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
var version string
1111

12-
type versioncmd struct {}
12+
type versioncmd struct{}
1313

1414
func (v *versioncmd) Spec() cli.CommandSpec {
1515
return cli.CommandSpec{

0 commit comments

Comments
 (0)