forked from toolshed/abra
refactor: break up cli pkg into nice small chunks
This commit is contained in:
32
cli/app/app.go
Normal file
32
cli/app/app.go
Normal file
@ -0,0 +1,32 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var AppCommand = &cli.Command{
|
||||
Name: "app",
|
||||
Usage: "Manage your apps",
|
||||
Description: `
|
||||
This command provides all the functionality you need to manage the lifecycle of
|
||||
your apps. From initial deployment to day-2 operations (e.g. backup/restore) to
|
||||
scaling apps up and spinning them down.
|
||||
`,
|
||||
Subcommands: []*cli.Command{
|
||||
appNewCommand,
|
||||
appConfigCommand,
|
||||
appDeployCommand,
|
||||
appUndeployCommand,
|
||||
appBackupCommand,
|
||||
appRestoreCommand,
|
||||
appRemoveCommand,
|
||||
appCheckCommand,
|
||||
appListCommand,
|
||||
appPsCommand,
|
||||
appLogsCommand,
|
||||
appCpCommand,
|
||||
appRunCommand,
|
||||
appRollbackCommand,
|
||||
appSecretCommand,
|
||||
},
|
||||
}
|
11
cli/app/backup.go
Normal file
11
cli/app/backup.go
Normal file
@ -0,0 +1,11 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var appBackupCommand = &cli.Command{
|
||||
Name: "backup",
|
||||
Flags: []cli.Flag{internal.AllFlag},
|
||||
}
|
7
cli/app/check.go
Normal file
7
cli/app/check.go
Normal file
@ -0,0 +1,7 @@
|
||||
package app
|
||||
|
||||
import "github.com/urfave/cli/v2"
|
||||
|
||||
var appCheckCommand = &cli.Command{
|
||||
Name: "check",
|
||||
}
|
7
cli/app/config.go
Normal file
7
cli/app/config.go
Normal file
@ -0,0 +1,7 @@
|
||||
package app
|
||||
|
||||
import "github.com/urfave/cli/v2"
|
||||
|
||||
var appConfigCommand = &cli.Command{
|
||||
Name: "config",
|
||||
}
|
8
cli/app/cp.go
Normal file
8
cli/app/cp.go
Normal file
@ -0,0 +1,8 @@
|
||||
package app
|
||||
|
||||
import "github.com/urfave/cli/v2"
|
||||
|
||||
var appCpCommand = &cli.Command{
|
||||
Name: "cp",
|
||||
ArgsUsage: "<src> <dst>",
|
||||
}
|
16
cli/app/deploy.go
Normal file
16
cli/app/deploy.go
Normal file
@ -0,0 +1,16 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var appDeployCommand = &cli.Command{
|
||||
Name: "deploy",
|
||||
Flags: []cli.Flag{
|
||||
internal.UpdateFlag,
|
||||
internal.ForceFlag,
|
||||
internal.SkipVersionCheckFlag,
|
||||
internal.NoDomainPollFlag,
|
||||
},
|
||||
}
|
70
cli/app/list.go
Normal file
70
cli/app/list.go
Normal file
@ -0,0 +1,70 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/config"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var appListCommand = &cli.Command{
|
||||
Name: "list",
|
||||
Usage: "List all managed apps",
|
||||
Description: `
|
||||
This command looks at your local file system listing of apps and servers (e.g.
|
||||
in ~/.abra/) to generate a report of all your apps.
|
||||
|
||||
By passing the "--status/-S" flag, you can query all your servers for the
|
||||
actual live deployment status. Depending on how many servers you manage, this
|
||||
can take some time.
|
||||
`,
|
||||
Aliases: []string{"ls"},
|
||||
Flags: []cli.Flag{internal.StatusFlag, internal.ServerFlag, internal.TypeFlag},
|
||||
Action: func(c *cli.Context) error {
|
||||
appFiles, err := config.LoadAppFiles(internal.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
apps, err := config.GetApps(appFiles)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
sort.Sort(config.ByServerAndType(apps))
|
||||
|
||||
statuses := map[string]string{}
|
||||
tableCol := []string{"Server", "Type", "Domain"}
|
||||
if internal.Status {
|
||||
tableCol = append(tableCol, "Status")
|
||||
statuses, err = config.GetAppStatuses(appFiles)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
table := abraFormatter.CreateTable(tableCol)
|
||||
table.SetAutoMergeCellsByColumnIndex([]int{0})
|
||||
|
||||
for _, app := range apps {
|
||||
var tableRow []string
|
||||
if app.Type == internal.Type || internal.Type == "" {
|
||||
// If type flag is set, check for it, if not, Type == ""
|
||||
tableRow = []string{app.File.Server, app.Type, app.Domain}
|
||||
if internal.Status {
|
||||
if status, ok := statuses[app.StackName()]; ok {
|
||||
tableRow = append(tableRow, status)
|
||||
} else {
|
||||
tableRow = append(tableRow, "unknown")
|
||||
}
|
||||
}
|
||||
}
|
||||
table.Append(tableRow)
|
||||
}
|
||||
|
||||
table.Render()
|
||||
return nil
|
||||
},
|
||||
}
|
8
cli/app/logs.go
Normal file
8
cli/app/logs.go
Normal file
@ -0,0 +1,8 @@
|
||||
package app
|
||||
|
||||
import "github.com/urfave/cli/v2"
|
||||
|
||||
var appLogsCommand = &cli.Command{
|
||||
Name: "logs",
|
||||
ArgsUsage: "[<service>]",
|
||||
}
|
153
cli/app/new.go
Normal file
153
cli/app/new.go
Normal file
@ -0,0 +1,153 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"coopcloud.tech/abra/catalogue"
|
||||
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/config"
|
||||
"coopcloud.tech/abra/secret"
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var appNewCommand = &cli.Command{
|
||||
Name: "new",
|
||||
Usage: "Create a new app",
|
||||
Description: `
|
||||
This command takes an app recipe and uses it to create a new app. This new app
|
||||
configuration is stored in your ~/.abra directory under the appropriate server.
|
||||
|
||||
This command does not deploy your app for you. You will need to run "abra app
|
||||
deploy <app>" to do so.
|
||||
|
||||
You can see what apps can be created (i.e. values for the <type> argument) by
|
||||
running "abra recipe ls".
|
||||
|
||||
Passing the "--secrets/-S" flag will automatically generate secrets for your
|
||||
app and store them encrypted at rest on the chosen target server. These
|
||||
generated secrets are only visible at generation time, so please take care to
|
||||
store them somewhere safe.
|
||||
|
||||
You can use the "--pass/-P" to store these generated passwords locally in a
|
||||
pass store (see passwordstore.org for more). The pass command must be available
|
||||
on your $PATH.
|
||||
`,
|
||||
Flags: []cli.Flag{
|
||||
internal.ServerFlag,
|
||||
internal.DomainFlag,
|
||||
internal.AppNameFlag,
|
||||
internal.PassFlag,
|
||||
internal.SecretsFlag,
|
||||
},
|
||||
ArgsUsage: "<type>",
|
||||
Action: func(c *cli.Context) error {
|
||||
appType := c.Args().First()
|
||||
if appType == "" {
|
||||
internal.ShowSubcommandHelpAndError(c, errors.New("no app type provided"))
|
||||
return nil
|
||||
}
|
||||
|
||||
config.EnsureAbraDirExists()
|
||||
|
||||
appFiles, err := config.LoadAppFiles(internal.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
catl, err := catalogue.ReadAppsCatalogue()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
app := catl[appType]
|
||||
app.EnsureExists()
|
||||
|
||||
latestVersion := app.LatestVersion()
|
||||
if err := app.EnsureVersion(latestVersion); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
servers := appFiles.GetServers()
|
||||
if internal.Server == "" {
|
||||
prompt := &survey.Select{
|
||||
Message: "Select app server:",
|
||||
Options: servers,
|
||||
}
|
||||
if err := survey.AskOne(prompt, &internal.Server); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if internal.Domain == "" {
|
||||
prompt := &survey.Input{
|
||||
Message: "Specify app domain",
|
||||
}
|
||||
if err := survey.AskOne(prompt, &internal.Domain); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if internal.AppName == "" {
|
||||
prompt := &survey.Input{
|
||||
Message: "Specify app name:",
|
||||
Default: strings.ReplaceAll(internal.Domain, ".", "_"),
|
||||
}
|
||||
if err := survey.AskOne(prompt, &internal.AppName); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
sanitisedAppName := strings.ReplaceAll(internal.AppName, ".", "_")
|
||||
if len(sanitisedAppName) > 45 {
|
||||
logrus.Fatal(fmt.Errorf("'%s' cannot be longer than 45 characters", sanitisedAppName))
|
||||
}
|
||||
|
||||
if err := config.CopyAppEnvSample(appType, internal.AppName, internal.Server); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
secrets := make(map[string]string)
|
||||
if internal.Secrets {
|
||||
appEnvPath := path.Join(config.ABRA_DIR, "servers", internal.Server, fmt.Sprintf("%s.env", sanitisedAppName))
|
||||
appEnv, err := config.ReadEnv(appEnvPath)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
secretEnvVars := secret.ReadSecretEnvVars(appEnv)
|
||||
secrets, err = secret.GenerateSecrets(secretEnvVars, sanitisedAppName, internal.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
if internal.Pass {
|
||||
for secretName := range secrets {
|
||||
secretValue := secrets[secretName]
|
||||
if err := secret.PassInsertSecret(secretValue, secretName, sanitisedAppName, internal.Server); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tableCol := []string{"Name", "Domain", "Type", "Server"}
|
||||
table := abraFormatter.CreateTable(tableCol)
|
||||
table.Append([]string{sanitisedAppName, internal.Domain, appType, internal.Server})
|
||||
table.Render()
|
||||
|
||||
if internal.Secrets {
|
||||
secretCols := []string{"Name", "Value"}
|
||||
secretTable := abraFormatter.CreateTable(secretCols)
|
||||
for secret := range secrets {
|
||||
secretTable.Append([]string{secret, secrets[secret]})
|
||||
}
|
||||
secretTable.Render()
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
61
cli/app/ps.go
Normal file
61
cli/app/ps.go
Normal file
@ -0,0 +1,61 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/client"
|
||||
"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/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var appPsCommand = &cli.Command{
|
||||
Name: "ps",
|
||||
Action: func(c *cli.Context) error {
|
||||
ctx := context.Background()
|
||||
cl, err := client.NewClientWithContext(internal.Context)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
tasks, err := cl.TaskList(ctx, types.TaskListOptions{})
|
||||
if err != nil {
|
||||
logrus.Fatal(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 {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
table := abraFormatter.CreateTable([]string{"ID", "Image", "Command", "Created", "Status", "Ports", "Names"})
|
||||
var conTable [][]string
|
||||
for _, container := range containers {
|
||||
conRow := []string{
|
||||
abraFormatter.ShortenID(container.ID),
|
||||
abraFormatter.RemoveSha(container.Image),
|
||||
abraFormatter.Truncate(container.Command),
|
||||
abraFormatter.HumanDuration(container.Created),
|
||||
container.Status,
|
||||
formatter.DisplayablePorts(container.Ports),
|
||||
strings.Join(container.Names, ","),
|
||||
}
|
||||
conTable = append(conTable, conRow)
|
||||
}
|
||||
table.AppendBulk(conTable)
|
||||
table.Render()
|
||||
return nil
|
||||
},
|
||||
}
|
11
cli/app/remove.go
Normal file
11
cli/app/remove.go
Normal file
@ -0,0 +1,11 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var appRemoveCommand = &cli.Command{
|
||||
Name: "remove",
|
||||
Flags: []cli.Flag{internal.VolumesFlag, internal.SecretsFlag},
|
||||
}
|
12
cli/app/restore.go
Normal file
12
cli/app/restore.go
Normal file
@ -0,0 +1,12 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var appRestoreCommand = &cli.Command{
|
||||
Name: "restore",
|
||||
Flags: []cli.Flag{internal.AllFlag},
|
||||
ArgsUsage: "<service> [<backup file>]",
|
||||
}
|
8
cli/app/rollback.go
Normal file
8
cli/app/rollback.go
Normal file
@ -0,0 +1,8 @@
|
||||
package app
|
||||
|
||||
import "github.com/urfave/cli/v2"
|
||||
|
||||
var appRollbackCommand = &cli.Command{
|
||||
Name: "rollback",
|
||||
ArgsUsage: "[<version>]",
|
||||
}
|
15
cli/app/run.go
Normal file
15
cli/app/run.go
Normal file
@ -0,0 +1,15 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var appRunCommand = &cli.Command{
|
||||
Name: "run",
|
||||
Flags: []cli.Flag{
|
||||
internal.NoTTYFlag,
|
||||
internal.UserFlag,
|
||||
},
|
||||
ArgsUsage: "<service> <args>...",
|
||||
}
|
25
cli/app/secret.go
Normal file
25
cli/app/secret.go
Normal file
@ -0,0 +1,25 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/secret"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// 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{internal.AllFlag, internal.PassFlag},
|
||||
Action: func(c *cli.Context) error {
|
||||
password, err := secret.GeneratePassphrases(1)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
fmt.Println(password)
|
||||
return nil
|
||||
},
|
||||
}
|
7
cli/app/undeploy.go
Normal file
7
cli/app/undeploy.go
Normal file
@ -0,0 +1,7 @@
|
||||
package app
|
||||
|
||||
import "github.com/urfave/cli/v2"
|
||||
|
||||
var appUndeployCommand = &cli.Command{
|
||||
Name: "undeploy",
|
||||
}
|
Reference in New Issue
Block a user