Skip to content

Commit 2eaf138

Browse files
authored
chore: add CODER_DEBUG to enable troubleshooting bootstrap script (#33)
1 parent 591328a commit 2eaf138

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

cli/docker.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ var (
9797
EnvMemory = "CODER_MEMORY"
9898
EnvAddGPU = "CODER_ADD_GPU"
9999
EnvUsrLibDir = "CODER_USR_LIB_DIR"
100+
EnvDebug = "CODER_DEBUG"
100101
)
101102

102103
var envboxPrivateMounts = map[string]struct{}{
@@ -126,14 +127,17 @@ type flags struct {
126127
addTUN bool
127128
addFUSE bool
128129
addGPU bool
129-
noStartupLogs bool
130130
dockerdBridgeCIDR string
131131
boostrapScript string
132-
ethlink string
133132
containerMounts string
134133
hostUsrLibDir string
135134
cpus int
136135
memory int
136+
137+
// Test flags.
138+
noStartupLogs bool
139+
debug bool
140+
ethlink string
137141
}
138142

139143
func dockerCmd() *cobra.Command {
@@ -333,6 +337,7 @@ func dockerCmd() *cobra.Command {
333337

334338
// Test flags.
335339
cliflag.BoolVarP(cmd.Flags(), &flags.noStartupLogs, "no-startup-log", "", "", false, "Do not log startup logs. Useful for testing.")
340+
cliflag.BoolVarP(cmd.Flags(), &flags.debug, "debug", "", EnvDebug, false, "Log additional output.")
336341
cliflag.StringVarP(cmd.Flags(), &flags.ethlink, "ethlink", "", "", defaultNetLink, "The ethernet link to query for the MTU that is passed to docerd. Used for tests.")
337342

338343
return cmd
@@ -644,6 +649,15 @@ func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.Docker
644649

645650
blog.Info("Envbox startup complete!")
646651

652+
// The bootstrap script doesn't return since it execs the agent
653+
// meaning that it can get pretty noisy if we were to log by default.
654+
// In order to allow users to discern issues getting the bootstrap script
655+
// to complete successfully we pipe the output to stdout if
656+
// CODER_DEBUG=true.
657+
debugWriter := io.Discard
658+
if flags.debug {
659+
debugWriter = os.Stdout
660+
}
647661
// Bootstrap the container if a script has been provided.
648662
blog.Infof("Bootstrapping workspace...")
649663
err = dockerutil.BootstrapContainer(ctx, client, dockerutil.BootstrapConfig{
@@ -654,7 +668,8 @@ func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.Docker
654668
// to /tmp/coder.XXXX. This causes a race to happen where we finish
655669
// downloading the binary but before we can execute systemd remounts
656670
// /tmp.
657-
Env: []string{fmt.Sprintf("BINARY_DIR=%s", bootDir)},
671+
Env: []string{fmt.Sprintf("BINARY_DIR=%s", bootDir)},
672+
StdOutErr: debugWriter,
658673
})
659674
if err != nil {
660675
return xerrors.Errorf("boostrap container: %w", err)

dockerutil/container.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dockerutil
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"path/filepath"
78
"strconv"
89
"strings"
@@ -100,6 +101,7 @@ type BootstrapConfig struct {
100101
Script string
101102
Env []string
102103
Detach bool
104+
StdOutErr io.Writer
103105
}
104106

105107
// BoostrapContainer runs a script inside the container as the provided user.
@@ -119,6 +121,7 @@ func BootstrapContainer(ctx context.Context, client DockerClient, conf Bootstrap
119121
Args: []string{"-s"},
120122
Stdin: strings.NewReader(conf.Script),
121123
Env: conf.Env,
124+
StdOutErr: conf.StdOutErr,
122125
})
123126
if err != nil {
124127
err = xerrors.Errorf("boostrap container (%s): %w", out, err)

0 commit comments

Comments
 (0)