mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
feat(deploy): print last logs from new container when fails to become healthy
This commit is contained in:
+41
-16
@@ -9,6 +9,7 @@ import (
|
|||||||
"charm.land/lipgloss/v2"
|
"charm.land/lipgloss/v2"
|
||||||
composecli "github.com/compose-spec/compose-go/v2/cli"
|
composecli "github.com/compose-spec/compose-go/v2/cli"
|
||||||
"github.com/docker/compose/v2/pkg/progress"
|
"github.com/docker/compose/v2/pkg/progress"
|
||||||
|
"github.com/docker/docker/pkg/stringid"
|
||||||
"github.com/psviderski/uncloud/internal/cli"
|
"github.com/psviderski/uncloud/internal/cli"
|
||||||
"github.com/psviderski/uncloud/internal/cli/logs"
|
"github.com/psviderski/uncloud/internal/cli/logs"
|
||||||
"github.com/psviderski/uncloud/internal/cli/tui"
|
"github.com/psviderski/uncloud/internal/cli/tui"
|
||||||
@@ -20,10 +21,6 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// failedContainerLogsTail is the number of recent log lines to print from a failed container to give the user immediate
|
|
||||||
// context without requiring a follow-up 'uc logs' invocation.
|
|
||||||
const failedContainerLogsTail = 10
|
|
||||||
|
|
||||||
type deployOptions struct {
|
type deployOptions struct {
|
||||||
cli.BuildServicesOptions
|
cli.BuildServicesOptions
|
||||||
|
|
||||||
@@ -238,34 +235,46 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
|
|||||||
}, uncli.ProgressOut(), title)
|
}, uncli.ProgressOut(), title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
||||||
|
tail := failedContainerLogsTail()
|
||||||
if hookErr, ok := errors.AsType[*operation.PreDeployHookError](err); ok {
|
if hookErr, ok := errors.AsType[*operation.PreDeployHookError](err); ok {
|
||||||
printPreDeployHookLogs(ctx, clusterClient, hookErr)
|
printFailedContainerLogs(ctx, clusterClient,
|
||||||
|
hookErr.ServiceName, hookErr.ContainerID, hookErr.MachineName, tail,
|
||||||
|
fmt.Sprintf("Last %d log lines from failed pre-deploy hook:", tail))
|
||||||
|
fmt.Println()
|
||||||
|
} else if startErr, ok := errors.AsType[*operation.ContainerHealthError](err); ok {
|
||||||
|
printFailedContainerLogs(ctx, clusterClient,
|
||||||
|
startErr.ServiceName, startErr.ContainerID, startErr.MachineName, tail,
|
||||||
|
fmt.Sprintf("Last %d log lines from failed container:", tail))
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// printPreDeployHookLogs fetches the last log lines from the failed pre-deploy hook container and prints them using
|
// printFailedContainerLogs fetches the last tail log lines from a container that failed during deployment and prints
|
||||||
// the standard log formatter.
|
// them using the standard log formatter under the provided header.
|
||||||
func printPreDeployHookLogs(ctx context.Context, cli *client.Client, hookErr *operation.PreDeployHookError) {
|
func printFailedContainerLogs(
|
||||||
_, ch, err := cli.ServiceLogs(ctx, hookErr.ServiceName, api.ServiceLogsOptions{
|
ctx context.Context, cli *client.Client, serviceName, containerID, machineName string, tail int, header string,
|
||||||
Containers: []string{hookErr.ContainerID},
|
) {
|
||||||
Machines: []string{hookErr.MachineName},
|
_, ch, err := cli.ServiceLogs(ctx, serviceName, api.ServiceLogsOptions{
|
||||||
Tail: failedContainerLogsTail,
|
Containers: []string{containerID},
|
||||||
|
Machines: []string{machineName},
|
||||||
|
Tail: tail,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Failed to fetch pre-deploy hook logs: %v\n", err)
|
shortCtrID := stringid.TruncateID(containerID)
|
||||||
fmt.Fprintf(os.Stderr, "You can try manually with: uc logs %s\n", hookErr.ServiceName)
|
fmt.Fprintf(os.Stderr, "Failed to fetch container logs '%s/%s': %v\n", serviceName, shortCtrID, err)
|
||||||
|
fmt.Fprintf(os.Stderr, "You can try manually with: uc logs %s/%s\n", serviceName, shortCtrID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
header := fmt.Sprintf("Last %d log lines from failed pre-deploy hook:", failedContainerLogsTail)
|
|
||||||
fmt.Println(tui.BoldRed.Render(header))
|
fmt.Println(tui.BoldRed.Render(header))
|
||||||
|
|
||||||
logsEmpty := true
|
logsEmpty := true
|
||||||
formatter := logs.NewFormatter([]string{hookErr.MachineName}, []string{hookErr.ServiceName}, false)
|
formatter := logs.NewFormatter([]string{machineName}, []string{serviceName}, false)
|
||||||
for entry := range ch {
|
for entry := range ch {
|
||||||
logsEmpty = false
|
logsEmpty = false
|
||||||
formatter.PrintEntry(entry)
|
formatter.PrintEntry(entry)
|
||||||
@@ -275,3 +284,19 @@ func printPreDeployHookLogs(ctx context.Context, cli *client.Client, hookErr *op
|
|||||||
fmt.Println("<no logs available>")
|
fmt.Println("<no logs available>")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// defaultFailedContainerLogsTail is the default number of recent log lines to print from a failed container to give
|
||||||
|
// the user immediate context without requiring a follow-up 'uc logs' invocation.
|
||||||
|
// Overridable via UNCLOUD_FAILED_CONTAINER_LOGS_TAIL.
|
||||||
|
const defaultFailedContainerLogsTail = 10
|
||||||
|
|
||||||
|
// failedContainerLogsTail returns the number of log lines to fetch from a failed container, honouring the
|
||||||
|
// UNCLOUD_FAILED_CONTAINER_LOGS_TAIL environment variable override when set and valid.
|
||||||
|
func failedContainerLogsTail() int {
|
||||||
|
if v := os.Getenv("UNCLOUD_FAILED_CONTAINER_LOGS_TAIL"); v != "" {
|
||||||
|
if tail, err := logs.Tail(v); err == nil && (tail == -1 || tail > 0) {
|
||||||
|
return tail
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultFailedContainerLogsTail
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,6 +13,18 @@ import (
|
|||||||
"github.com/psviderski/uncloud/pkg/api"
|
"github.com/psviderski/uncloud/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ContainerHealthError indicates that a service container failed to become healthy during deployment.
|
||||||
|
type ContainerHealthError struct {
|
||||||
|
error
|
||||||
|
ServiceName string
|
||||||
|
ContainerID string
|
||||||
|
MachineName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ContainerHealthError) Unwrap() error {
|
||||||
|
return e.error
|
||||||
|
}
|
||||||
|
|
||||||
// RunContainerOperation creates and starts a new container on a specific machine.
|
// RunContainerOperation creates and starts a new container on a specific machine.
|
||||||
type RunContainerOperation struct {
|
type RunContainerOperation struct {
|
||||||
ServiceID string
|
ServiceID string
|
||||||
@@ -43,8 +55,13 @@ func (o *RunContainerOperation) Execute(ctx context.Context, cli Client) error {
|
|||||||
|
|
||||||
opts := api.WaitContainerHealthyOptions{MonitorPeriod: o.Spec.UpdateConfig.MonitorPeriod}
|
opts := api.WaitContainerHealthyOptions{MonitorPeriod: o.Spec.UpdateConfig.MonitorPeriod}
|
||||||
if err = cli.WaitContainerHealthy(ctx, o.ServiceID, resp.ID, opts); err != nil {
|
if err = cli.WaitContainerHealthy(ctx, o.ServiceID, resp.ID, opts); err != nil {
|
||||||
return fmt.Errorf("container '%s/%s' failed to become healthy: %w",
|
return &ContainerHealthError{
|
||||||
o.Spec.Name, stringid.TruncateID(resp.ID), err)
|
error: fmt.Errorf("container '%s/%s' failed to become healthy: %w",
|
||||||
|
o.Spec.Name, stringid.TruncateID(resp.ID), err),
|
||||||
|
ServiceName: o.Spec.Name,
|
||||||
|
ContainerID: resp.ID,
|
||||||
|
MachineName: o.MachineName,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -187,7 +204,6 @@ func (o *ReplaceContainerOperation) Execute(ctx context.Context, cli Client) err
|
|||||||
if err = cli.WaitContainerHealthy(newCtx, 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.
|
// 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.
|
// 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.
|
|
||||||
|
|
||||||
// Use context without progress to not overwrite the container Unhealthy status with Stopped.
|
// Use context without progress to not overwrite the container Unhealthy status with Stopped.
|
||||||
ctxWithoutProgress := progress.WithContextWriter(ctx, nil)
|
ctxWithoutProgress := progress.WithContextWriter(ctx, nil)
|
||||||
@@ -196,21 +212,28 @@ func (o *ReplaceContainerOperation) Execute(ctx context.Context, cli Client) err
|
|||||||
newCtr := fmt.Sprintf("%s/%s", o.Spec.Name, stringid.TruncateID(resp.ID))
|
newCtr := fmt.Sprintf("%s/%s", o.Spec.Name, stringid.TruncateID(resp.ID))
|
||||||
healthErr := fmt.Errorf(
|
healthErr := fmt.Errorf(
|
||||||
"new container '%s' failed to become healthy: %w. "+
|
"new container '%s' failed to become healthy: %w. "+
|
||||||
"It's stopped and available for inspection. Fetch logs with 'uc logs %s'",
|
"It's stopped and available for inspection. View logs with 'uc logs %s'",
|
||||||
newCtr, err, o.Spec.Name,
|
newCtr, err, newCtr,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
finalErr := healthErr
|
||||||
if stopFirst && wasRunning {
|
if stopFirst && wasRunning {
|
||||||
// Restart the old container only if it was running before we stopped it.
|
// Restart the old container only if it was running before we stopped it.
|
||||||
oldCtr := fmt.Sprintf("%s/%s", o.OldContainer.ServiceSpec.Name, o.OldContainer.ShortID())
|
oldCtr := fmt.Sprintf("%s/%s", o.OldContainer.ServiceSpec.Name, o.OldContainer.ShortID())
|
||||||
if rollbackErr := cli.StartContainer(ctx, o.ServiceID, o.OldContainer.ID); rollbackErr != nil {
|
if rollbackErr := cli.StartContainer(ctx, o.ServiceID, o.OldContainer.ID); rollbackErr != nil {
|
||||||
return fmt.Errorf("%w. Rolled back to old container '%s' but failed to restart it: %w",
|
finalErr = fmt.Errorf("%w. Rolled back to old container '%s' but failed to restart it: %w",
|
||||||
healthErr, oldCtr, rollbackErr)
|
healthErr, oldCtr, rollbackErr)
|
||||||
|
} else {
|
||||||
|
finalErr = fmt.Errorf("%w. Rolled back to old container '%s'", healthErr, oldCtr)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("%w. Rolled back to old container '%s'", healthErr, oldCtr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return healthErr
|
return &ContainerHealthError{
|
||||||
|
error: finalErr,
|
||||||
|
ServiceName: o.Spec.Name,
|
||||||
|
ContainerID: resp.ID,
|
||||||
|
MachineName: o.MachineName,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,10 @@ type PreDeployHookError struct {
|
|||||||
MachineName string
|
MachineName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *PreDeployHookError) Unwrap() error {
|
||||||
|
return e.error
|
||||||
|
}
|
||||||
|
|
||||||
// DefaultPreDeployTimeout is the maximum duration to wait for a pre-deploy hook container to complete.
|
// DefaultPreDeployTimeout is the maximum duration to wait for a pre-deploy hook container to complete.
|
||||||
const DefaultPreDeployTimeout = 5 * time.Minute
|
const DefaultPreDeployTimeout = 5 * time.Minute
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user