fix: address review findings for self-update (#15)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -98,10 +99,6 @@ func NewComposeExecutor(cli ComposeDocker, resolve *Resolver, selfID string, run
|
||||
// When the service is the agent's own container, the recreate runs in a
|
||||
// helper container instead (see selfUpdate), because it stops this process.
|
||||
func (e *ComposeExecutor) Execute(ctx context.Context, job Job, out io.Writer) (string, error) {
|
||||
// What the job has printed so far goes into the helper's labels.
|
||||
soFar := &tailBuffer{max: selfUpdateOutputMax}
|
||||
out = io.MultiWriter(out, tailWriter{soFar})
|
||||
|
||||
project, service := job.ComposeProject, job.ComposeService
|
||||
cts, err := e.cli.ContainerList(ctx, container.ListOptions{
|
||||
All: true,
|
||||
@@ -116,14 +113,23 @@ func (e *ComposeExecutor) Execute(ctx context.Context, job Job, out io.Writer) (
|
||||
if len(cts) == 0 {
|
||||
return "", fmt.Errorf("no container for compose service %s/%s on this machine", project, service)
|
||||
}
|
||||
workingDir := cts[0].Labels[labelWorkingDir]
|
||||
configFiles := cts[0].Labels[labelConfigFiles]
|
||||
envFileLabel := cts[0].Labels[labelEnvFile]
|
||||
|
||||
self, err := inspectSelf(ctx, e.cli, e.selfID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
isSelf := self.ContainerJSONBase != nil && self.ID != "" &&
|
||||
slices.ContainsFunc(cts, func(ct container.Summary) bool { return ct.ID == self.ID })
|
||||
// What an update of the agent's own service has printed so far goes into
|
||||
// the helper's labels.
|
||||
var soFar *tailBuffer
|
||||
if isSelf {
|
||||
soFar = &tailBuffer{max: selfUpdateOutputMax}
|
||||
out = io.MultiWriter(out, tailWriter{soFar})
|
||||
}
|
||||
workingDir := cts[0].Labels[labelWorkingDir]
|
||||
configFiles := cts[0].Labels[labelConfigFiles]
|
||||
envFileLabel := cts[0].Labels[labelEnvFile]
|
||||
|
||||
roots := rootsOf(self)
|
||||
files, dir, err := composeFiles(roots, configFiles)
|
||||
if err != nil {
|
||||
@@ -170,22 +176,29 @@ func (e *ComposeExecutor) Execute(ctx context.Context, job Job, out io.Writer) (
|
||||
}
|
||||
|
||||
base := composeBase(project, workingDir, files, envs)
|
||||
isSelf := false
|
||||
if self.ContainerJSONBase != nil && self.ID != "" {
|
||||
for _, ct := range cts {
|
||||
isSelf = isSelf || ct.ID == self.ID
|
||||
}
|
||||
}
|
||||
for _, step := range [][]string{{"pull", service}, {"up", "-d", service}} {
|
||||
args := append(append([]string(nil), base...), step...)
|
||||
if isSelf && step[0] == "up" {
|
||||
return e.selfUpdate(ctx, job, self, workingDir, args, pinned, out, soFar)
|
||||
}
|
||||
run := func(args []string) error {
|
||||
fmt.Fprintf(out, "$ docker %s\n", strings.Join(args, " "))
|
||||
tail := &tailBuffer{max: errorOutputTail}
|
||||
if err := e.run(ctx, workingDir, io.MultiWriter(out, tailWriter{tail}), args...); err != nil {
|
||||
return "", fmt.Errorf("docker compose %s: %w (output: %s)", step[0], err, strings.TrimSpace(tail.String()))
|
||||
return composeError(args[len(base)], err, tail)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := run(append(append([]string(nil), base...), "pull", service)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
up := append(append([]string(nil), base...), "up", "-d", service)
|
||||
if isSelf {
|
||||
return e.selfUpdate(ctx, job, self, workingDir, up, pinned, out, soFar)
|
||||
}
|
||||
if err := run(up); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return pinned, nil
|
||||
}
|
||||
|
||||
// composeError describes a failed `docker compose <step>` call with the tail
|
||||
// of its output.
|
||||
func composeError(step string, err error, tail *tailBuffer) error {
|
||||
return fmt.Errorf("docker compose %s: %w (output: %s)", step, err, strings.TrimSpace(tail.String()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user