feat(pre-deploy): correct progress events for pre-deploy hooks, make other container events more aligned with plan

This commit is contained in:
Pasha Sviderski
2026-04-08 19:16:42 +10:00
parent 0e820f9c52
commit 0da9df17a4
8 changed files with 110 additions and 27 deletions
+2 -2
View File
@@ -20,10 +20,10 @@ type Client interface {
type ContainerClient interface {
CreateContainer(
ctx context.Context, serviceID string, spec ServiceSpec, machineID string,
) (container.CreateResponse, error)
) (CreateContainerResponse, error)
CreatePreDeployHookContainer(
ctx context.Context, serviceID string, spec ServiceSpec, machineID string,
) (container.CreateResponse, error)
) (CreateContainerResponse, error)
ExecContainer(ctx context.Context, serviceNameOrID, containerNameOrID string, config ExecOptions) (int, error)
InspectContainer(ctx context.Context, serviceNameOrID, containerNameOrID string) (MachineServiceContainer, error)
StartContainer(ctx context.Context, serviceNameOrID, containerNameOrID string) error
+7
View File
@@ -164,6 +164,13 @@ func (c *Container) UnmarshalJSON(data []byte) error {
return nil
}
// CreateContainerResponse wraps a container creation response with the container name assigned during creation.
type CreateContainerResponse struct {
container.CreateResponse
// Name is the container name assigned during creation.
Name string
}
type ServiceContainer struct {
Container
ServiceSpec ServiceSpec
+16 -13
View File
@@ -2,13 +2,12 @@ package client
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"encoding/json"
"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/pkg/jsonmessage"
@@ -28,7 +27,7 @@ import (
// CreateContainer creates a new container for the given service on the specified machine.
func (cli *Client) CreateContainer(
ctx context.Context, serviceID string, spec api.ServiceSpec, machineID string,
) (container.CreateResponse, error) {
) (api.CreateContainerResponse, error) {
return cli.createServiceContainerWithPull(ctx, serviceID, spec, machineID, pb.CreateServiceContainerRequest_SERVICE)
}
@@ -36,7 +35,7 @@ func (cli *Client) CreateContainer(
// on the specified machine.
func (cli *Client) CreatePreDeployHookContainer(
ctx context.Context, serviceID string, spec api.ServiceSpec, machineID string,
) (container.CreateResponse, error) {
) (api.CreateContainerResponse, error) {
return cli.createServiceContainerWithPull(
ctx, serviceID, spec, machineID, pb.CreateServiceContainerRequest_PRE_DEPLOY)
}
@@ -49,8 +48,8 @@ func (cli *Client) createServiceContainerWithPull(
spec api.ServiceSpec,
machineID string,
containerType pb.CreateServiceContainerRequest_ContainerType,
) (container.CreateResponse, error) {
var resp container.CreateResponse
) (api.CreateContainerResponse, error) {
var resp api.CreateContainerResponse
spec = spec.SetDefaults()
if err := spec.Validate(); err != nil {
@@ -69,16 +68,16 @@ func (cli *Client) createServiceContainerWithPull(
}
containerName := fmt.Sprintf("%s-%s", spec.Name, suffix)
eventID := fmt.Sprintf("Container %s on %s", containerName, machine.Machine.Name)
if containerType == pb.CreateServiceContainerRequest_PRE_DEPLOY {
containerName = fmt.Sprintf("%s-%s-%s", spec.Name, api.LabelHookPreDeploy, suffix)
eventID = cliprogress.PreDeployHookEventID(spec.Name, machine.Machine.Name)
}
resp.Name = containerName
// Proxy Docker gRPC requests to the selected machine.
ctx = proxyToMachine(ctx, machine.Machine)
pw := progress.ContextWriter(ctx)
eventID := cliprogress.NewContainerEventID(ctx, containerName, machine.Machine.Name)
pw.Event(progress.CreatingEvent(eventID))
if spec.Container.PullPolicy == api.PullPolicyAlways {
@@ -122,7 +121,7 @@ func (cli *Client) createServiceContainerWithPull(
}
}
if err = json.Unmarshal(grpcResp.Response, &resp); err != nil {
if err = json.Unmarshal(grpcResp.Response, &resp.CreateResponse); err != nil {
return resp, fmt.Errorf("unmarshal gRPC response: %w", err)
}
pw.Event(progress.CreatedEvent(eventID))
@@ -293,7 +292,7 @@ func (cli *Client) StartContainer(ctx context.Context, serviceNameOrID, containe
ctx = proxyToMachine(ctx, machine.Machine)
pw := progress.ContextWriter(ctx)
eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Name, machine.Machine.Name)
eventID := cliprogress.ContainerEventID(ctx, ctr.Container.ServiceSpec.Name, ctr.Container.ID, machine.Machine.Name)
pw.Event(progress.StartingEvent(eventID))
if err = cli.Docker.StartContainer(ctx, ctr.Container.ID, container.StartOptions{}); err != nil {
@@ -305,6 +304,8 @@ func (cli *Client) StartContainer(ctx context.Context, serviceNameOrID, containe
}
// StopContainer stops the specified container within the service.
//
//nolint:dupl // Structurally similar to RemoveContainer but performs a different operation.
func (cli *Client) StopContainer(
ctx context.Context, serviceNameOrID, containerNameOrID string, opts container.StopOptions,
) error {
@@ -320,7 +321,7 @@ func (cli *Client) StopContainer(
ctx = proxyToMachine(ctx, machine.Machine)
pw := progress.ContextWriter(ctx)
eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Name, machine.Machine.Name)
eventID := cliprogress.ContainerEventID(ctx, ctr.Container.ServiceSpec.Name, ctr.Container.ID, machine.Machine.Name)
pw.Event(progress.StoppingEvent(eventID))
if err = cli.Docker.StopContainer(ctx, ctr.Container.ID, opts); err != nil {
@@ -332,6 +333,8 @@ func (cli *Client) StopContainer(
}
// RemoveContainer removes the specified container within the service.
//
//nolint:dupl // Structurally similar to StopContainer but performs a different operation.
func (cli *Client) RemoveContainer(
ctx context.Context, serviceNameOrID, containerNameOrID string, opts container.RemoveOptions,
) error {
@@ -347,7 +350,7 @@ func (cli *Client) RemoveContainer(
ctx = proxyToMachine(ctx, machine.Machine)
pw := progress.ContextWriter(ctx)
eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Name, machine.Machine.Name)
eventID := cliprogress.ContainerEventID(ctx, ctr.Container.ServiceSpec.Name, ctr.Container.ID, machine.Machine.Name)
pw.Event(progress.RemovingEvent(eventID))
if err = cli.Docker.RemoveServiceContainer(ctx, ctr.Container.ID, opts); err != nil {
@@ -427,7 +430,7 @@ func (cli *Client) WaitContainerHealthy(
}
pw := progress.ContextWriter(ctx)
eventID := fmt.Sprintf("Container %s on %s", mc.Container.Name, machine.Machine.Name)
eventID := cliprogress.ContainerEventID(ctx, mc.Container.ServiceSpec.Name, mc.Container.ID, machine.Machine.Name)
var monitor time.Duration
if opts.MonitorPeriod == nil {
+9 -2
View File
@@ -8,6 +8,7 @@ import (
"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/pkg/stringid"
cliprogress "github.com/psviderski/uncloud/internal/cli/progress"
"github.com/psviderski/uncloud/internal/cli/tui"
"github.com/psviderski/uncloud/pkg/api"
)
@@ -28,6 +29,10 @@ func (o *RunContainerOperation) Execute(ctx context.Context, cli Client) error {
if err != nil {
return fmt.Errorf("create container: %w", err)
}
// Override event ID so StartContainer and WaitContainerHealthy update the same progress line as creation.
// TODO: This is a hack to work around the limitations of the compose progress library.
// We likely need to fork or create our own to decouple event IDs from presentation layer.
ctx = cliprogress.WithEventID(ctx, cliprogress.NewContainerEventID(ctx, resp.Name, o.MachineName))
if err = cli.StartContainer(ctx, o.ServiceID, resp.ID); err != nil {
return fmt.Errorf("start container: %w", err)
}
@@ -171,13 +176,15 @@ func (o *ReplaceContainerOperation) Execute(ctx context.Context, cli Client) err
if err != nil {
return fmt.Errorf("create new container: %w", err)
}
if err = cli.StartContainer(ctx, o.ServiceID, resp.ID); err != nil {
// Override event ID so StartContainer and WaitContainerHealthy update the same progress line as creation.
newCtx := cliprogress.WithEventID(ctx, cliprogress.NewContainerEventID(ctx, resp.Name, o.MachineName))
if err = cli.StartContainer(newCtx, o.ServiceID, resp.ID); err != nil {
return fmt.Errorf("start new container: %w", err)
}
if !o.SkipHealthMonitor {
opts := api.WaitContainerHealthyOptions{MonitorPeriod: o.Spec.UpdateConfig.MonitorPeriod}
if err = cli.WaitContainerHealthy(ctx, o.ServiceID, resp.ID, opts); err != nil {
if err = cli.WaitContainerHealthy(newCtx, o.ServiceID, resp.ID, opts); err != nil {
// New container failed to become healthy. Stop it and roll back to the previous container.
// Don't remove the new stopped container to allow users to inspect logs and state.
// TODO: collect logs from the new container and include in the error message to speed up debugging.
+25 -6
View File
@@ -6,6 +6,8 @@ import (
"strings"
"time"
"charm.land/lipgloss/v2"
"github.com/charmbracelet/x/ansi"
"github.com/containerd/errdefs"
"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/docker/api/types/container"
@@ -65,13 +67,18 @@ type RunPreDeployOperation struct {
func (o *RunPreDeployOperation) Execute(ctx context.Context, cli Client) error {
// Remove old pre-deploy containers.
for _, id := range o.OldContainerIDs {
_ = cli.StopContainer(ctx, o.ServiceID, id, container.StopOptions{})
err := cli.RemoveContainer(ctx, o.ServiceID, id, container.RemoveOptions{RemoveVolumes: true})
oldCtx := cliprogress.WithEventID(ctx, cliprogress.OldPreDeployHookEventID(o.Spec.Name, id, o.MachineName))
_ = cli.StopContainer(oldCtx, o.ServiceID, id, container.StopOptions{})
err := cli.RemoveContainer(oldCtx, o.ServiceID, id, container.RemoveOptions{RemoveVolumes: true})
if err != nil && !errdefs.IsNotFound(err) {
return fmt.Errorf("remove old pre-deploy hook container '%s': %w", id, err)
}
}
// Set the event ID override so all downstream client methods (create, start, stop) use the same
// pre-deploy hook progress event instead of the generic container one.
ctx = cliprogress.WithEventID(ctx, cliprogress.PreDeployHookEventID(o.Spec.Name, o.MachineName))
resp, err := cli.CreatePreDeployHookContainer(ctx, o.ServiceID, o.Spec, o.MachineID)
if err != nil {
return fmt.Errorf("create pre-deploy hook container: %w", err)
@@ -107,7 +114,6 @@ func (o *RunPreDeployOperation) waitForExit(
select {
case <-timeoutCtx.Done():
// Stop the container on timeout or context cancellation.
pw.Event(progress.StoppingEvent(eventID))
_ = cli.StopContainer(ctx, o.ServiceID, containerID, container.StopOptions{})
ctrID := containerID
@@ -159,12 +165,25 @@ func (o *RunPreDeployOperation) waitForExit(
func (o *RunPreDeployOperation) Format() string {
cmd := strings.Join(o.Spec.PreDeploy.Command, " ")
return tui.BoldGreen.Render("▶") + " " +
prefix := tui.BoldGreen.Render("▶") + " " +
tui.Faint.Render("run pre-deploy hook") + " " +
// TODO: truncate a long cmd to fit the width of the terminal.
o.Spec.Name + " (" + cmd + ") " +
o.Spec.Name + " ["
suffix := "] " +
tui.Faint.Render("on") + " " +
o.MachineName
// Truncate the command to fit within the terminal width.
termWidth := tui.TerminalWidth()
if termWidth > 0 {
buffer := 10
maxCmdWidth := termWidth - lipgloss.Width(prefix) - lipgloss.Width(suffix) - buffer
if maxCmdWidth <= 0 {
maxCmdWidth = 10
}
cmd = ansi.Truncate(cmd, maxCmdWidth, "…")
}
return prefix + cmd + suffix
}
func (o *RunPreDeployOperation) String() string {