machine readable ps output
This commit is contained in:
parent
03f94da2d8
commit
183ad8f576
@ -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,
|
||||||
},
|
},
|
||||||
@ -66,36 +70,68 @@ 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)
|
recipe, err := recipe.Get(app.Recipe, internal.Offline)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
containers, err := cl.ContainerList(context.Background(), types.ContainerListOptions{Filters: filters})
|
var tablerows [][]string
|
||||||
if err != nil {
|
allContainerStats := make(map[string]map[string]string)
|
||||||
logrus.Fatal(err)
|
for _, service := range recipe.Config.Services {
|
||||||
}
|
filters := filters.NewArgs()
|
||||||
|
filters.Add("name", fmt.Sprintf("^%s_%s", app.StackName(), service.Name))
|
||||||
|
|
||||||
tableCol := []string{"service name", "image", "created", "status", "state", "ports"}
|
containers, err := cl.ContainerList(context.Background(), types.ContainerListOptions{Filters: filters})
|
||||||
table := formatter.CreateTable(tableCol)
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
for _, container := range containers {
|
return
|
||||||
var containerNames []string
|
|
||||||
for _, containerName := range container.Names {
|
|
||||||
trimmed := strings.TrimPrefix(containerName, "/")
|
|
||||||
containerNames = append(containerNames, trimmed)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tableRow := []string{
|
var containerStats map[string]string
|
||||||
service.ContainerToServiceName(container.Names, app.StackName()),
|
|
||||||
formatter.RemoveSha(container.Image),
|
|
||||||
formatter.HumanDuration(container.Created),
|
|
||||||
container.Status,
|
|
||||||
container.State,
|
|
||||||
dockerFormatter.DisplayablePorts(container.Ports),
|
|
||||||
}
|
|
||||||
table.Append(tableRow)
|
|
||||||
}
|
|
||||||
|
|
||||||
table.Render()
|
if len(containers) == 0 {
|
||||||
|
containerStats = map[string]string{
|
||||||
|
"service name": service.Name,
|
||||||
|
"image": "unknown",
|
||||||
|
"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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
allContainerStats[containerStats["service name"]] = containerStats
|
||||||
|
|
||||||
|
var tablerow []string
|
||||||
|
for _, column := range containerStats {
|
||||||
|
tablerow = append(tablerow, column)
|
||||||
|
}
|
||||||
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user