machine readable ps output + output of not running container #409

Merged
moritz merged 3 commits from ps into main 2024-05-15 22:04:53 +00:00

View File

@ -2,7 +2,8 @@ package app
import ( import (
"context" "context"
"strings" "encoding/json"
"fmt"
"time" "time"
"coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/cli/internal"
@ -10,11 +11,13 @@ import (
"coopcloud.tech/abra/pkg/client" "coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/formatter" "coopcloud.tech/abra/pkg/formatter"
"coopcloud.tech/abra/pkg/service" "coopcloud.tech/abra/pkg/recipe"
abraService "coopcloud.tech/abra/pkg/service"
stack "coopcloud.tech/abra/pkg/upstream/stack" stack "coopcloud.tech/abra/pkg/upstream/stack"
"github.com/buger/goterm" "github.com/buger/goterm"
dockerFormatter "github.com/docker/cli/cli/command/formatter" dockerFormatter "github.com/docker/cli/cli/command/formatter"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
dockerClient "github.com/docker/docker/client" dockerClient "github.com/docker/docker/client"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -27,6 +30,7 @@ var appPsCommand = cli.Command{
ArgsUsage: "<domain>", ArgsUsage: "<domain>",
Description: "Show a more detailed status output of a specific deployed app", Description: "Show a more detailed status output of a specific deployed app",
Flags: []cli.Flag{ Flags: []cli.Flag{
internal.MachineReadableFlag,
internal.WatchFlag, internal.WatchFlag,
internal.DebugFlag, internal.DebugFlag,
}, },
@ -40,7 +44,7 @@ var appPsCommand = cli.Command{
logrus.Fatal(err) logrus.Fatal(err)
} }
isDeployed, _, err := stack.IsDeployed(context.Background(), cl, app.StackName()) isDeployed, deployedVersion, err := stack.IsDeployed(context.Background(), cl, app.StackName())
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
@ -49,6 +53,15 @@ var appPsCommand = cli.Command{
logrus.Fatalf("%s is not deployed?", app.Name) logrus.Fatalf("%s is not deployed?", app.Name)
} }
statuses, err := config.GetAppStatuses([]config.App{app}, true)
if statusMeta, ok := statuses[app.StackName()]; ok {
if _, exists := statusMeta["chaos"]; !exists {
if err := recipe.EnsureVersion(app.Recipe, deployedVersion); err != nil {
logrus.Fatal(err)
}
}
}
if !internal.Watch { if !internal.Watch {
showPSOutput(c, app, cl) showPSOutput(c, app, cl)
return nil return nil
@ -66,36 +79,77 @@ var appPsCommand = cli.Command{
// showPSOutput renders ps output. // showPSOutput renders ps output.
func showPSOutput(c *cli.Context, app config.App, cl *dockerClient.Client) { func showPSOutput(c *cli.Context, app config.App, cl *dockerClient.Client) {
filters, err := app.Filters(true, true) composeFiles, err := config.GetComposeFiles(app.Recipe, app.Env)
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
return
} }
containers, err := cl.ContainerList(context.Background(), types.ContainerListOptions{Filters: filters}) deployOpts := stack.Deploy{
Composefiles: composeFiles,
Namespace: app.StackName(),
Prune: false,
ResolveImage: stack.ResolveImageAlways,
}
compose, err := config.GetAppComposeConfig(app.Name, deployOpts, app.Env)
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
return
} }
tableCol := []string{"service name", "image", "created", "status", "state", "ports"} var tablerows [][]string
table := formatter.CreateTable(tableCol) allContainerStats := make(map[string]map[string]string)
for _, service := range compose.Services {
filters := filters.NewArgs()
filters.Add("name", fmt.Sprintf("^%s_%s", app.StackName(), service.Name))
for _, container := range containers { containers, err := cl.ContainerList(context.Background(), types.ContainerListOptions{Filters: filters})
var containerNames []string if err != nil {
for _, containerName := range container.Names { logrus.Fatal(err)
trimmed := strings.TrimPrefix(containerName, "/") return
containerNames = append(containerNames, trimmed)
} }
tableRow := []string{ var containerStats map[string]string
service.ContainerToServiceName(container.Names, app.StackName()),
formatter.RemoveSha(container.Image), if len(containers) == 0 {
formatter.HumanDuration(container.Created), containerStats = map[string]string{
container.Status, "service name": service.Name,
container.State, "image": "unknown",
dockerFormatter.DisplayablePorts(container.Ports), "created": "unknown",
"status": "unknown",
"state": "unknown",
"ports": "unknown",
}
} else {
container := containers[0]
containerStats = map[string]string{
"service name": abraService.ContainerToServiceName(container.Names, app.StackName()),
"image": formatter.RemoveSha(container.Image),
"created": formatter.HumanDuration(container.Created),
"status": container.Status,
"state": container.State,
"ports": dockerFormatter.DisplayablePorts(container.Ports),
}
} }
table.Append(tableRow) allContainerStats[containerStats["service name"]] = containerStats
var tablerow []string = []string{containerStats["service name"], containerStats["image"], containerStats["created"], containerStats["status"], containerStats["state"], containerStats["ports"]}
tablerows = append(tablerows, tablerow)
}
if internal.MachineReadable {
jsonstring, err := json.Marshal(allContainerStats)
if err != nil {
logrus.Fatal(err)
} else {
fmt.Println(string(jsonstring))
}
return
} else {
tableCol := []string{"service name", "image", "created", "status", "state", "ports"}
table := formatter.CreateTable(tableCol)
for _, row := range tablerows {
table.Append(row)
}
table.Render()
} }
table.Render()
} }