mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
feat(pre-deploy): correct progress events for pre-deploy hooks, make other container events more aligned with plan
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user