138 lines
3.5 KiB
Go
138 lines
3.5 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
|
"coopcloud.tech/abra/pkg/autocomplete"
|
|
"coopcloud.tech/abra/pkg/client"
|
|
"coopcloud.tech/abra/pkg/config"
|
|
"coopcloud.tech/abra/pkg/formatter"
|
|
"coopcloud.tech/abra/pkg/recipe"
|
|
abraService "coopcloud.tech/abra/pkg/service"
|
|
stack "coopcloud.tech/abra/pkg/upstream/stack"
|
|
"github.com/buger/goterm"
|
|
dockerFormatter "github.com/docker/cli/cli/command/formatter"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/filters"
|
|
dockerClient "github.com/docker/docker/client"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
var appPsCommand = cli.Command{
|
|
Name: "ps",
|
|
Aliases: []string{"p"},
|
|
Usage: "Check app status",
|
|
ArgsUsage: "<domain>",
|
|
Description: "Show a more detailed status output of a specific deployed app",
|
|
Flags: []cli.Flag{
|
|
internal.MachineReadableFlag,
|
|
internal.WatchFlag,
|
|
internal.DebugFlag,
|
|
},
|
|
Before: internal.SubCommandBefore,
|
|
BashComplete: autocomplete.AppNameComplete,
|
|
Action: func(c *cli.Context) error {
|
|
app := internal.ValidateApp(c)
|
|
|
|
cl, err := client.New(app.Server)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
isDeployed, _, err := stack.IsDeployed(context.Background(), cl, app.StackName())
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
if !isDeployed {
|
|
logrus.Fatalf("%s is not deployed?", app.Name)
|
|
}
|
|
|
|
if !internal.Watch {
|
|
showPSOutput(c, app, cl)
|
|
return nil
|
|
}
|
|
|
|
goterm.Clear()
|
|
for {
|
|
goterm.MoveCursor(1, 1)
|
|
showPSOutput(c, app, cl)
|
|
goterm.Flush()
|
|
time.Sleep(2 * time.Second)
|
|
}
|
|
},
|
|
}
|
|
|
|
// showPSOutput renders ps output.
|
|
func showPSOutput(c *cli.Context, app config.App, cl *dockerClient.Client) {
|
|
recipe, err := recipe.Get(app.Recipe, internal.Offline)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
return
|
|
}
|
|
|
|
var tablerows [][]string
|
|
allContainerStats := make(map[string]map[string]string)
|
|
for _, service := range recipe.Config.Services {
|
|
filters := filters.NewArgs()
|
|
filters.Add("name", fmt.Sprintf("^%s_%s", app.StackName(), service.Name))
|
|
|
|
containers, err := cl.ContainerList(context.Background(), types.ContainerListOptions{Filters: filters})
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
return
|
|
}
|
|
|
|
var containerStats map[string]string
|
|
|
|
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()
|
|
}
|
|
}
|