2021-08-02 01:10:41 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-10-14 09:51:40 +00:00
|
|
|
"time"
|
2021-08-02 01:10:41 +00:00
|
|
|
|
|
|
|
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
2021-12-11 23:17:39 +00:00
|
|
|
"coopcloud.tech/abra/pkg/autocomplete"
|
2021-09-05 19:37:03 +00:00
|
|
|
"coopcloud.tech/abra/pkg/client"
|
2021-12-27 03:00:37 +00:00
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-12-27 02:55:42 +00:00
|
|
|
stack "coopcloud.tech/abra/pkg/upstream/stack"
|
|
|
|
"github.com/buger/goterm"
|
2021-08-02 01:10:41 +00:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
|
|
"github.com/docker/docker/api/types"
|
2021-08-28 14:00:16 +00:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2021-12-27 03:00:37 +00:00
|
|
|
dockerClient "github.com/docker/docker/client"
|
2021-08-02 01:10:41 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var appPsCommand = &cli.Command{
|
2021-11-21 13:07:06 +00:00
|
|
|
Name: "ps",
|
|
|
|
Usage: "Check app status",
|
|
|
|
Description: "This command shows a more detailed status output of a specific deployed app.",
|
|
|
|
Aliases: []string{"p"},
|
2021-10-14 09:51:40 +00:00
|
|
|
Flags: []cli.Flag{
|
2021-12-24 01:23:46 +00:00
|
|
|
internal.WatchFlag,
|
2021-10-14 09:51:40 +00:00
|
|
|
},
|
2021-12-11 23:17:39 +00:00
|
|
|
BashComplete: autocomplete.AppNameComplete,
|
2021-08-02 01:10:41 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2021-12-27 03:00:37 +00:00
|
|
|
app := internal.ValidateApp(c)
|
|
|
|
|
|
|
|
cl, err := client.New(app.Server)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isDeployed, _, err := stack.IsDeployed(c.Context, cl, app.StackName())
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isDeployed {
|
|
|
|
logrus.Fatalf("%s is not deployed?", app.Name)
|
|
|
|
}
|
|
|
|
|
2021-12-24 01:23:46 +00:00
|
|
|
if !internal.Watch {
|
2021-12-27 03:00:37 +00:00
|
|
|
showPSOutput(c, app, cl)
|
2021-10-14 09:51:40 +00:00
|
|
|
return nil
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|
2021-08-28 14:00:16 +00:00
|
|
|
|
2021-12-27 02:55:42 +00:00
|
|
|
goterm.Clear()
|
2021-10-14 09:51:40 +00:00
|
|
|
for {
|
2021-12-27 02:55:42 +00:00
|
|
|
goterm.MoveCursor(1, 1)
|
2021-12-27 03:00:37 +00:00
|
|
|
showPSOutput(c, app, cl)
|
2021-12-27 02:55:42 +00:00
|
|
|
goterm.Flush()
|
2021-10-14 09:51:40 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2021-10-14 09:51:40 +00:00
|
|
|
|
|
|
|
// showPSOutput renders ps output.
|
2021-12-27 03:00:37 +00:00
|
|
|
func showPSOutput(c *cli.Context, app config.App, cl *dockerClient.Client) {
|
2021-10-14 09:51:40 +00:00
|
|
|
filters := filters.NewArgs()
|
|
|
|
filters.Add("name", app.StackName())
|
|
|
|
|
|
|
|
containers, err := cl.ContainerList(c.Context, types.ContainerListOptions{Filters: filters})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-12-23 20:14:15 +00:00
|
|
|
tableCol := []string{"image", "created", "status", "state", "ports"}
|
2021-10-14 09:51:40 +00:00
|
|
|
table := abraFormatter.CreateTable(tableCol)
|
|
|
|
|
|
|
|
for _, container := range containers {
|
|
|
|
var containerNames []string
|
|
|
|
for _, containerName := range container.Names {
|
|
|
|
trimmed := strings.TrimPrefix(containerName, "/")
|
|
|
|
containerNames = append(containerNames, trimmed)
|
|
|
|
}
|
|
|
|
|
|
|
|
tableRow := []string{
|
|
|
|
abraFormatter.RemoveSha(container.Image),
|
|
|
|
abraFormatter.HumanDuration(container.Created),
|
|
|
|
container.Status,
|
2021-12-23 20:14:15 +00:00
|
|
|
container.State,
|
2021-10-14 09:51:40 +00:00
|
|
|
formatter.DisplayablePorts(container.Ports),
|
|
|
|
}
|
|
|
|
table.Append(tableRow)
|
|
|
|
}
|
|
|
|
|
|
|
|
table.Render()
|
|
|
|
}
|