2021-08-02 01:10:41 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
"coopcloud.tech/abra/cli/internal"
|
2021-09-05 19:37:03 +00:00
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-11-08 14:37:23 +00:00
|
|
|
"coopcloud.tech/abra/pkg/context"
|
2021-12-28 00:24:23 +00:00
|
|
|
"coopcloud.tech/abra/pkg/formatter"
|
2021-10-22 08:40:28 +00:00
|
|
|
"github.com/docker/cli/cli/connhelper/ssh"
|
2021-08-02 01:10:41 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-18 13:13:20 +00:00
|
|
|
"github.com/urfave/cli"
|
2021-08-02 01:10:41 +00:00
|
|
|
)
|
|
|
|
|
2023-01-23 13:56:34 +00:00
|
|
|
var problemsFilter bool
|
|
|
|
|
|
|
|
var problemsFilterFlag = &cli.BoolFlag{
|
|
|
|
Name: "problems, p",
|
|
|
|
Usage: "Show only servers with potential connection problems",
|
|
|
|
Destination: &problemsFilter,
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
var serverListCommand = cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Aliases: []string{"ls"},
|
|
|
|
Usage: "List managed servers",
|
|
|
|
Flags: []cli.Flag{
|
2023-01-23 13:56:34 +00:00
|
|
|
problemsFilterFlag,
|
2022-01-18 13:13:20 +00:00
|
|
|
internal.DebugFlag,
|
2023-01-11 02:51:38 +00:00
|
|
|
internal.MachineReadableFlag,
|
2022-01-18 13:13:20 +00:00
|
|
|
},
|
|
|
|
Before: internal.SubCommandBefore,
|
2021-08-02 01:10:41 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2021-11-08 14:37:23 +00:00
|
|
|
dockerContextStore := context.NewDefaultDockerContextStore()
|
2021-08-02 01:10:41 +00:00
|
|
|
contexts, err := dockerContextStore.Store.List()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
2021-10-22 08:40:28 +00:00
|
|
|
tableColumns := []string{"name", "host", "user", "port"}
|
2021-08-02 01:10:41 +00:00
|
|
|
table := formatter.CreateTable(tableColumns)
|
2023-01-11 02:51:38 +00:00
|
|
|
if internal.MachineReadable {
|
|
|
|
defer table.JSONRender()
|
|
|
|
} else {
|
|
|
|
defer table.Render()
|
|
|
|
}
|
2021-08-02 01:10:41 +00:00
|
|
|
serverNames, err := config.ReadServerNames()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
for _, serverName := range serverNames {
|
2021-08-02 01:10:41 +00:00
|
|
|
var row []string
|
|
|
|
for _, ctx := range contexts {
|
2021-11-08 14:37:23 +00:00
|
|
|
endpoint, err := context.GetContextEndpoint(ctx)
|
2021-08-02 01:10:41 +00:00
|
|
|
if err != nil && strings.Contains(err.Error(), "does not exist") {
|
|
|
|
// No local context found, we can continue safely
|
|
|
|
continue
|
|
|
|
}
|
2023-01-23 13:56:34 +00:00
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
if ctx.Name == serverName {
|
2021-10-22 08:40:28 +00:00
|
|
|
sp, err := ssh.ParseURL(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
row = []string{serverName, sp.Host, sp.User, sp.Port}
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-23 13:56:34 +00:00
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
if len(row) == 0 {
|
2021-10-02 20:40:08 +00:00
|
|
|
if serverName == "default" {
|
2021-10-22 08:40:28 +00:00
|
|
|
row = []string{serverName, "local", "n/a", "n/a"}
|
2021-10-02 20:40:08 +00:00
|
|
|
} else {
|
2021-10-22 08:40:28 +00:00
|
|
|
row = []string{serverName, "unknown", "unknown", "unknown"}
|
2021-10-02 20:40:08 +00:00
|
|
|
}
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|
2023-01-23 13:56:34 +00:00
|
|
|
|
|
|
|
if problemsFilter {
|
|
|
|
if row[1] == "unknown" {
|
|
|
|
table.Append(row)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
table.Append(row)
|
|
|
|
}
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
return nil
|
2021-08-02 01:10:41 +00:00
|
|
|
},
|
|
|
|
}
|