Skip to content

Commit

Permalink
refactor some shit
Browse files Browse the repository at this point in the history
  • Loading branch information
sreya committed Jul 19, 2024
1 parent e7f70bf commit bde2f13
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cli/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.Docker
bootDir := filepath.Join(imgMeta.HomeDir, ".coder")

blog.Infof("Creating %q directory to host Coder assets...", bootDir)
_, _, err = dockerutil.ExecContainer(ctx, client, dockerutil.ExecConfig{
_, err = dockerutil.ExecContainer(ctx, client, dockerutil.ExecConfig{
ContainerID: containerID,
User: imgMeta.UID,
Cmd: "mkdir",
Expand Down
2 changes: 1 addition & 1 deletion dockerutil/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func BootstrapContainer(ctx context.Context, client DockerClient, conf Bootstrap
var err error
for r, n := retry.New(time.Second, time.Second*2), 0; r.Wait(ctx) && n < 10; n++ {
var out io.Reader
out, _, err = ExecContainer(ctx, client, ExecConfig{
out, err = ExecContainer(ctx, client, ExecConfig{
ContainerID: conf.ContainerID,
User: conf.User,
Cmd: "/bin/sh",
Expand Down
20 changes: 10 additions & 10 deletions dockerutil/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ExecConfig struct {

// ExecContainer runs a command in a container. It returns the output and any error.
// If an error occurs during the execution of the command, the output is appended to the error.
func ExecContainer(ctx context.Context, client DockerClient, config ExecConfig) (io.Reader, int, error) {
func ExecContainer(ctx context.Context, client DockerClient, config ExecConfig) (io.Reader, error) {
exec, err := client.ContainerExecCreate(ctx, config.ContainerID, dockertypes.ExecConfig{
Detach: config.Detach,
Cmd: append([]string{config.Cmd}, config.Args...),
Expand All @@ -37,23 +37,23 @@ func ExecContainer(ctx context.Context, client DockerClient, config ExecConfig)
Env: config.Env,
})
if err != nil {
return nil, 0, xerrors.Errorf("exec create: %w", err)
return nil, xerrors.Errorf("exec create: %w", err)
}

resp, err := client.ContainerExecAttach(ctx, exec.ID, dockertypes.ExecStartCheck{})
if err != nil {
return nil, 0, xerrors.Errorf("attach to exec: %w", err)
return nil, xerrors.Errorf("attach to exec: %w", err)
}
defer resp.Close()

if config.Stdin != nil {
_, err = io.Copy(resp.Conn, config.Stdin)
if err != nil {
return nil, 0, xerrors.Errorf("copy stdin: %w", err)
return nil, xerrors.Errorf("copy stdin: %w", err)
}
err = resp.CloseWrite()
if err != nil {
return nil, 0, xerrors.Errorf("close write: %w", err)
return nil, xerrors.Errorf("close write: %w", err)
}
}

Expand All @@ -75,24 +75,24 @@ func ExecContainer(ctx context.Context, client DockerClient, config ExecConfig)

_, err = io.Copy(wr, resp.Reader)
if err != nil {
return nil, 0, xerrors.Errorf("copy cmd output: %w", err)
return nil, xerrors.Errorf("copy cmd output: %w", err)
}
resp.Close()

inspect, err := client.ContainerExecInspect(ctx, exec.ID)
if err != nil {
return nil, 0, xerrors.Errorf("exec inspect: %w", err)
return nil, xerrors.Errorf("exec inspect: %w", err)
}

if inspect.Running {
return nil, 0, xerrors.Errorf("unexpectedly still running")
return nil, xerrors.Errorf("unexpectedly still running")
}

if inspect.ExitCode > 0 {
return nil, 0, xerrors.Errorf("%s: exit code %d", buf.Bytes(), inspect.ExitCode)
return nil, xerrors.Errorf("%s: exit code %d", buf.Bytes(), inspect.ExitCode)
}

return &buf, inspect.Pid, nil
return &buf, nil
}

func GetExecPID(ctx context.Context, client DockerClient, execID string) (int, error) {
Expand Down
4 changes: 2 additions & 2 deletions dockerutil/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ func GetImageMetadata(ctx context.Context, client DockerClient, image, username
return ImageMetadata{}, xerrors.Errorf("CVMs do not support NFS volumes")
}

_, _, err = ExecContainer(ctx, client, ExecConfig{
_, err = ExecContainer(ctx, client, ExecConfig{
ContainerID: inspect.ID,
Cmd: "stat",
Args: []string{"/sbin/init"},
})
initExists := err == nil

out, _, err := ExecContainer(ctx, client, ExecConfig{
out, err := ExecContainer(ctx, client, ExecConfig{
ContainerID: inspect.ID,
Cmd: "getent",
Args: []string{"passwd", username},
Expand Down
2 changes: 1 addition & 1 deletion integration/integrationtest/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
HelloWorldImage = "gcr.io/coder-dev-1/sreya/hello-world"
// UbuntuImage is just vanilla ubuntu (80MB) but the user is set to a non-root
// user .
UbuntuImage = "gcr.io/coder-dev-1/sreya/ubuntu-coder:jon"
UbuntuImage = "gcr.io/coder-dev-1/sreya/ubuntu-coder"
)

// TODO use df to determine if an environment is running in a docker container or not.
Expand Down

0 comments on commit bde2f13

Please sign in to comment.