|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +// Runner executes external commands. It is injectable so compose command |
| 14 | +// construction is unit-testable without a real `docker` binary. |
| 15 | +type Runner interface { |
| 16 | + // Run streams stdout/stderr to the user (image-pull/up progress). |
| 17 | + Run(ctx context.Context, env []string, dir, name string, args ...string) error |
| 18 | + // Output captures stdout (for `compose ps --format json` and friends). |
| 19 | + Output(ctx context.Context, env []string, dir, name string, args ...string) ([]byte, error) |
| 20 | +} |
| 21 | + |
| 22 | +// ExecRunner runs commands via os/exec, inheriting the process env plus extra |
| 23 | +// env. Secrets are passed through env here and never written to a file (§7.5). |
| 24 | +// A failure is wrapped with the command + exit code + captured stderr so the |
| 25 | +// error is self-debuggable (ARCHITECTURE §7.6). |
| 26 | +type ExecRunner struct{} |
| 27 | + |
| 28 | +func (ExecRunner) Run(ctx context.Context, env []string, dir, name string, args ...string) error { |
| 29 | + cmd := exec.CommandContext(ctx, name, args...) |
| 30 | + cmd.Dir = dir |
| 31 | + cmd.Env = append(os.Environ(), env...) |
| 32 | + cmd.Stdout = os.Stdout |
| 33 | + var stderr bytes.Buffer |
| 34 | + cmd.Stderr = io.MultiWriter(os.Stderr, &stderr) |
| 35 | + if err := cmd.Run(); err != nil { |
| 36 | + return &CmdError{Cmd: name + " " + strings.Join(args, " "), Err: err, Stderr: strings.TrimSpace(stderr.String())} |
| 37 | + } |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +func (ExecRunner) Output(ctx context.Context, env []string, dir, name string, args ...string) ([]byte, error) { |
| 42 | + cmd := exec.CommandContext(ctx, name, args...) |
| 43 | + cmd.Dir = dir |
| 44 | + cmd.Env = append(os.Environ(), env...) |
| 45 | + var stdout, stderr bytes.Buffer |
| 46 | + cmd.Stdout = &stdout |
| 47 | + cmd.Stderr = &stderr |
| 48 | + if err := cmd.Run(); err != nil { |
| 49 | + return nil, &CmdError{Cmd: name + " " + strings.Join(args, " "), Err: err, Stderr: strings.TrimSpace(stderr.String())} |
| 50 | + } |
| 51 | + return stdout.Bytes(), nil |
| 52 | +} |
| 53 | + |
| 54 | +// CmdError carries the failed command, its error, and captured stderr. |
| 55 | +type CmdError struct { |
| 56 | + Cmd string |
| 57 | + Stderr string |
| 58 | + Err error |
| 59 | +} |
| 60 | + |
| 61 | +func (e *CmdError) Error() string { |
| 62 | + if e.Stderr != "" { |
| 63 | + return fmt.Sprintf("`%s` failed: %v\n%s", e.Cmd, e.Err, e.Stderr) |
| 64 | + } |
| 65 | + return fmt.Sprintf("`%s` failed: %v", e.Cmd, e.Err) |
| 66 | +} |
| 67 | + |
| 68 | +func (e *CmdError) Unwrap() error { return e.Err } |
| 69 | + |
| 70 | +// Compose drives the `docker compose` CLI for a single stack with an explicit |
| 71 | +// project name and compose file (DECISIONS D5). Lifecycle verbs run here; |
| 72 | +// container enumeration stays on the read-only SDK Client. |
| 73 | +type Compose struct { |
| 74 | + Project string // -p <project> |
| 75 | + File string // -f <compose file> |
| 76 | + Dir string // working dir (build contexts resolve relative to it) |
| 77 | + Env []string // extra env (resolved secrets), appended to os.Environ |
| 78 | + Runner Runner |
| 79 | +} |
| 80 | + |
| 81 | +// NewCompose builds a Compose driver using the real exec runner. |
| 82 | +func NewCompose(project, file, dir string) *Compose { |
| 83 | + return &Compose{Project: project, File: file, Dir: dir, Runner: ExecRunner{}} |
| 84 | +} |
| 85 | + |
| 86 | +func (c *Compose) base() []string { |
| 87 | + return []string{"compose", "-p", c.Project, "-f", c.File} |
| 88 | +} |
| 89 | + |
| 90 | +// Up brings the stack (or the named subset of services) up detached. With no |
| 91 | +// services it brings the whole stack up. It does NOT pass --remove-orphans: the |
| 92 | +// shared stack is brought up service-by-service and stateful services must never |
| 93 | +// be disturbed implicitly (spec 03). |
| 94 | +func (c *Compose) Up(ctx context.Context, services ...string) error { |
| 95 | + args := append(c.base(), "up", "-d") |
| 96 | + args = append(args, services...) |
| 97 | + return c.Runner.Run(ctx, c.Env, c.Dir, "docker", args...) |
| 98 | +} |
| 99 | + |
| 100 | +// Down stops and removes the stack's containers (and its default network). |
| 101 | +// Named external networks are NOT removed by compose — devstack owns those. |
| 102 | +// volumes=true also removes named volumes (destructive — caller confirms). |
| 103 | +func (c *Compose) Down(ctx context.Context, volumes bool) error { |
| 104 | + args := append(c.base(), "down") |
| 105 | + if volumes { |
| 106 | + args = append(args, "--volumes") |
| 107 | + } |
| 108 | + return c.Runner.Run(ctx, c.Env, c.Dir, "docker", args...) |
| 109 | +} |
| 110 | + |
| 111 | +// Stop pauses the stack (or named services) without removing containers — used |
| 112 | +// by `shared gc`/autostop where the data volumes must survive. |
| 113 | +func (c *Compose) Stop(ctx context.Context, services ...string) error { |
| 114 | + args := append(c.base(), "stop") |
| 115 | + args = append(args, services...) |
| 116 | + return c.Runner.Run(ctx, c.Env, c.Dir, "docker", args...) |
| 117 | +} |
| 118 | + |
| 119 | +// Build rebuilds the named services (with --no-cache for the selective-rebuild |
| 120 | +// contexts the generate ledger flagged). With no services, builds all. |
| 121 | +func (c *Compose) Build(ctx context.Context, noCache bool, services ...string) error { |
| 122 | + args := append(c.base(), "build") |
| 123 | + if noCache { |
| 124 | + args = append(args, "--no-cache") |
| 125 | + } |
| 126 | + args = append(args, services...) |
| 127 | + return c.Runner.Run(ctx, c.Env, c.Dir, "docker", args...) |
| 128 | +} |
0 commit comments