Roxie Gibson
16514b3151
All checks were successful
continuous-integration/drone/push Build is passing
200 lines
4.4 KiB
Go
200 lines
4.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"coopcloud.tech/abra/client"
|
|
"coopcloud.tech/abra/config"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
"github.com/docker/cli/cli/command/idresolver"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/schultz-is/passgen"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
var appNewCommand = &cli.Command{
|
|
Name: "new",
|
|
Flags: []cli.Flag{
|
|
ServerFlag,
|
|
DomainFlag,
|
|
AppNameFlag,
|
|
PassFlag,
|
|
SecretsFlag,
|
|
},
|
|
}
|
|
var appDeployCommand = &cli.Command{
|
|
Name: "deploy",
|
|
Flags: []cli.Flag{
|
|
UpdateFlag,
|
|
ForceFlag,
|
|
SkipVersionCheckFlag,
|
|
NoDomainPollFlag,
|
|
},
|
|
}
|
|
var appUndeployCommand = &cli.Command{
|
|
Name: "undeploy",
|
|
}
|
|
var appBackupCommand = &cli.Command{
|
|
Name: "backup",
|
|
Flags: []cli.Flag{AllFlag},
|
|
}
|
|
var appRestoreCommand = &cli.Command{
|
|
Name: "restore",
|
|
Flags: []cli.Flag{AllFlag},
|
|
ArgsUsage: "<service> [<backup file>]",
|
|
}
|
|
var appListCommand = &cli.Command{
|
|
Name: "list",
|
|
Aliases: []string{"ls"},
|
|
Flags: []cli.Flag{StatusFlag, ServerFlag, TypeFlag},
|
|
Action: func(c *cli.Context) error {
|
|
// FIXME: Needs status flag implementing
|
|
// TODO: Sorting of output to make servers in alphabetical
|
|
// Looks like sorting a 2d slice of strings might be messy though
|
|
apps, err := config.LoadAppFiles(Server)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
tableCol := []string{"Name", "Type", "Server"}
|
|
table := createTable(tableCol)
|
|
for name, appFile := range apps {
|
|
app, err := config.GetApp(apps, name)
|
|
if err != nil {
|
|
logrus.Fatal(err.Error())
|
|
}
|
|
if app.Type == Type || Type == "" {
|
|
// If type flag is set, check for it, if not, Type == ""
|
|
tableRow := []string{name, app.Type, appFile.Server}
|
|
table.Append(tableRow)
|
|
}
|
|
|
|
}
|
|
table.Render()
|
|
return nil
|
|
},
|
|
}
|
|
var appCheckCommand = &cli.Command{
|
|
Name: "check",
|
|
}
|
|
var appCpCommand = &cli.Command{
|
|
Name: "cp",
|
|
ArgsUsage: "<src> <dst>",
|
|
}
|
|
var appConfigCommand = &cli.Command{
|
|
Name: "config",
|
|
}
|
|
var appLogsCommand = &cli.Command{
|
|
Name: "logs",
|
|
ArgsUsage: "[<service>]",
|
|
}
|
|
|
|
var appPsCommand = &cli.Command{
|
|
Name: "ps",
|
|
Action: func(c *cli.Context) error {
|
|
ctx := context.Background()
|
|
fmt.Println(Context)
|
|
cl := client.NewClientWithContext(Context)
|
|
tasks, err := cl.TaskList(ctx, types.TaskListOptions{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
for _, task := range tasks {
|
|
resolver := idresolver.New(cl, false)
|
|
serviceName, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("%#v\n", serviceName)
|
|
}
|
|
containers, err := cl.ContainerList(ctx, types.ContainerListOptions{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
table := createTable([]string{"ID", "Image", "Command", "Created", "Status", "Ports", "Names"})
|
|
var conTable [][]string
|
|
for _, container := range containers {
|
|
conRow := []string{
|
|
shortenID(container.ID),
|
|
removeSha(container.Image),
|
|
truncate(container.Command),
|
|
humanDuration(container.Created),
|
|
container.Status,
|
|
formatter.DisplayablePorts(container.Ports),
|
|
strings.Join(container.Names, ","),
|
|
}
|
|
conTable = append(conTable, conRow)
|
|
}
|
|
table.AppendBulk(conTable)
|
|
table.Render()
|
|
return nil
|
|
},
|
|
}
|
|
var appRemoveCommand = &cli.Command{
|
|
Name: "remove",
|
|
Flags: []cli.Flag{VolumesFlag, SecretsFlag},
|
|
}
|
|
var appRunCommand = &cli.Command{
|
|
Name: "run",
|
|
Flags: []cli.Flag{
|
|
NoTTYFlag,
|
|
UserFlag,
|
|
},
|
|
ArgsUsage: "<service> <args>...",
|
|
}
|
|
|
|
var appRollbackCommand = &cli.Command{
|
|
Name: "rollback",
|
|
ArgsUsage: "[<version>]",
|
|
}
|
|
|
|
// TODO: Replicating what the bash abra does might be hard
|
|
// with the mix of subcommands and flags
|
|
var appSecretCommand = &cli.Command{
|
|
Name: "secret",
|
|
Flags: []cli.Flag{AllFlag, PassFlag},
|
|
Action: func(c *cli.Context) error {
|
|
passwords, err := passgen.GeneratePassphrases(
|
|
1,
|
|
passgen.PassphraseWordCountDefault,
|
|
rune('-'),
|
|
passgen.PassphraseCasingDefault,
|
|
passgen.WordListDefault,
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for _, password := range passwords {
|
|
fmt.Println(password)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var AppCommand = &cli.Command{
|
|
Name: "app",
|
|
HideHelp: true,
|
|
Subcommands: []*cli.Command{
|
|
appNewCommand,
|
|
appConfigCommand,
|
|
appDeployCommand,
|
|
appUndeployCommand,
|
|
appBackupCommand,
|
|
appRestoreCommand,
|
|
appRemoveCommand,
|
|
appCheckCommand,
|
|
appListCommand,
|
|
appPsCommand,
|
|
appLogsCommand,
|
|
appCpCommand,
|
|
appRunCommand,
|
|
appRollbackCommand,
|
|
appSecretCommand,
|
|
},
|
|
}
|