feat(deploy): print last logs from new container when fails to become healthy

This commit is contained in:
Pasha Sviderski
2026-04-22 13:45:09 +10:00
parent 8afe52367f
commit 303c8e4506
3 changed files with 76 additions and 24 deletions
+41 -16
View File
@@ -9,6 +9,7 @@ import (
"charm.land/lipgloss/v2"
composecli "github.com/compose-spec/compose-go/v2/cli"
"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/logs"
"github.com/psviderski/uncloud/internal/cli/tui"
@@ -20,10 +21,6 @@ import (
"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 {
cli.BuildServicesOptions
@@ -238,34 +235,46 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
}, uncli.ProgressOut(), title)
if err != nil {
fmt.Println()
tail := failedContainerLogsTail()
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()
}
return err
}
return nil
}
// printPreDeployHookLogs fetches the last log lines from the failed pre-deploy hook container and prints them using
// the standard log formatter.
func printPreDeployHookLogs(ctx context.Context, cli *client.Client, hookErr *operation.PreDeployHookError) {
_, ch, err := cli.ServiceLogs(ctx, hookErr.ServiceName, api.ServiceLogsOptions{
Containers: []string{hookErr.ContainerID},
Machines: []string{hookErr.MachineName},
Tail: failedContainerLogsTail,
// printFailedContainerLogs fetches the last tail log lines from a container that failed during deployment and prints
// them using the standard log formatter under the provided header.
func printFailedContainerLogs(
ctx context.Context, cli *client.Client, serviceName, containerID, machineName string, tail int, header string,
) {
_, ch, err := cli.ServiceLogs(ctx, serviceName, api.ServiceLogsOptions{
Containers: []string{containerID},
Machines: []string{machineName},
Tail: tail,
})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to fetch pre-deploy hook logs: %v\n", err)
fmt.Fprintf(os.Stderr, "You can try manually with: uc logs %s\n", hookErr.ServiceName)
shortCtrID := stringid.TruncateID(containerID)
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
}
header := fmt.Sprintf("Last %d log lines from failed pre-deploy hook:", failedContainerLogsTail)
fmt.Println(tui.BoldRed.Render(header))
logsEmpty := true
formatter := logs.NewFormatter([]string{hookErr.MachineName}, []string{hookErr.ServiceName}, false)
formatter := logs.NewFormatter([]string{machineName}, []string{serviceName}, false)
for entry := range ch {
logsEmpty = false
formatter.PrintEntry(entry)
@@ -275,3 +284,19 @@ func printPreDeployHookLogs(ctx context.Context, cli *client.Client, hookErr *op
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
}