refactor: use app getting instead of boilerplate
continuous-integration/drone/push Build is failing Details

This commit is contained in:
decentral1se 2021-09-05 23:17:35 +02:00
parent 48bcc9cb36
commit d4333c2dc0
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
18 changed files with 111 additions and 266 deletions

View File

@ -31,23 +31,13 @@ var appBackupCommand = &cli.Command{
Flags: []cli.Flag{backupAllServicesFlag},
ArgsUsage: "<service>",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
if c.Args().Get(1) != "" && backupAllServices {
internal.ShowSubcommandHelpAndError(c, errors.New("cannot use '<service>' and '--all' together"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
abraSh := path.Join(config.ABRA_DIR, "apps", appEnv.Type, "abra.sh")
abraSh := path.Join(config.ABRA_DIR, "apps", app.Type, "abra.sh")
if _, err := os.Stat(abraSh); err != nil {
if os.IsNotExist(err) {
logrus.Fatalf("'%s' does not exist?", abraSh)
@ -71,7 +61,7 @@ var appBackupCommand = &cli.Command{
logrus.Fatal(err)
}
if !strings.Contains(string(bytes), execCmd) {
logrus.Fatalf("%s doesn't have a '%s' function", appEnv.Type, execCmd)
logrus.Fatalf("%s doesn't have a '%s' function", app.Type, execCmd)
}
sourceAndExec := fmt.Sprintf("%s; %s", sourceCmd, execCmd)

View File

@ -17,19 +17,9 @@ var appCheckCommand = &cli.Command{
Aliases: []string{"c"},
ArgsUsage: "<service>",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
envSamplePath := path.Join(config.ABRA_DIR, "apps", appEnv.Type, ".env.sample")
envSamplePath := path.Join(config.ABRA_DIR, "apps", app.Type, ".env.sample")
if _, err := os.Stat(envSamplePath); err != nil {
if os.IsNotExist(err) {
logrus.Fatalf("'%s' does not exist?", envSamplePath)
@ -44,15 +34,14 @@ var appCheckCommand = &cli.Command{
var missing []string
for k := range envSample {
if _, ok := appEnv.Env[k]; !ok {
if _, ok := app.Env[k]; !ok {
missing = append(missing, k)
}
}
if len(missing) > 0 {
path := appFiles[appName].Path
missingEnvVars := strings.Join(missing, ", ")
logrus.Fatalf("%s is missing %s", path, missingEnvVars)
logrus.Fatalf("%s is missing %s", app.Path, missingEnvVars)
}
logrus.Info("All necessary environment variables defined")

View File

@ -5,7 +5,6 @@ import (
"os/exec"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/config"
"github.com/AlecAivazis/survey/v2"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
@ -16,16 +15,7 @@ var appConfigCommand = &cli.Command{
Aliases: []string{"c"},
Usage: "Edit app config",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
if _, ok := appFiles[appName]; !ok {
logrus.Fatalf("'%s' doesn't exist?", appName)
}
app := internal.ValidateApp(c)
ed, ok := os.LookupEnv("EDITOR")
if !ok {
@ -38,7 +28,7 @@ var appConfigCommand = &cli.Command{
}
}
cmd := exec.Command(ed, appFiles[appName].Path)
cmd := exec.Command(ed, app.Path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

View File

@ -22,7 +22,7 @@ var appCpCommand = &cli.Command{
ArgsUsage: "<src> <dst>",
Usage: "Copy files to/from a running app service",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
src := c.Args().Get(1)
dst := c.Args().Get(2)
@ -67,14 +67,13 @@ var appCpCommand = &cli.Command{
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
appEnv, err := config.GetApp(appFiles, app.Name)
if err != nil {
logrus.Fatal(err)
}
ctx := context.Background()
server := appFiles[appName].Server
cl, err := client.New(server)
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}

View File

@ -16,45 +16,34 @@ var appDeployCommand = &cli.Command{
Aliases: []string{"d"},
Usage: "Deploy an app",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
appFiles, err := config.LoadAppFiles("")
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
server := appFiles[appName].Server
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}
abraShPath := fmt.Sprintf("%s/%s/%s", config.APPS_DIR, appEnv.Type, "abra.sh")
abraShPath := fmt.Sprintf("%s/%s/%s", config.APPS_DIR, app.Type, "abra.sh")
abraShEnv, err := config.ReadAbraShEnvVars(abraShPath)
if err != nil {
logrus.Fatal(err)
}
for k, v := range abraShEnv {
appEnv.Env[k] = v
app.Env[k] = v
}
appEnv.Env["STACK_NAME"] = appEnv.StackName()
app.Env["STACK_NAME"] = app.StackName()
composeFiles, err := config.GetAppComposeFiles(appEnv.Type, appEnv.Env)
composeFiles, err := config.GetAppComposeFiles(app.Type, app.Env)
if err != nil {
logrus.Fatal(err)
}
deployOpts := stack.Deploy{
Composefiles: composeFiles,
Namespace: appEnv.StackName(),
Namespace: app.StackName(),
Prune: false,
ResolveImage: stack.ResolveImageAlways,
}
compose, err := config.GetAppComposeConfig(appName, deployOpts, appEnv.Env)
compose, err := config.GetAppComposeConfig(app.Name, deployOpts, app.Env)
if err != nil {
logrus.Fatal(err)
}

View File

@ -82,7 +82,7 @@ can take some time.
var tableRow []string
if app.Type == appType || appType == "" {
// If type flag is set, check for it, if not, Type == ""
tableRow = []string{app.File.Server, app.Type, app.Domain}
tableRow = []string{app.Server, app.Type, app.Domain}
if status {
if status, ok := statuses[app.StackName()]; ok {
tableRow = append(tableRow, status)

View File

@ -9,7 +9,6 @@ import (
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/config"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
dockerClient "github.com/docker/docker/client"
@ -63,31 +62,20 @@ var appLogsCommand = &cli.Command{
ArgsUsage: "[<service>]",
Usage: "Tail app logs",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
app := internal.ValidateApp(c)
ctx := context.Background()
server := appFiles[appName].Server
cl, err := client.New(server)
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}
serviceName := c.Args().Get(1)
if serviceName == "" {
stackLogs(appEnv.StackName(), cl)
stackLogs(app.StackName(), cl)
}
service := fmt.Sprintf("%s_%s", appEnv.StackName(), serviceName)
service := fmt.Sprintf("%s_%s", app.StackName(), serviceName)
filters := filters.NewArgs()
filters.Add("name", service)
serviceOpts := types.ServiceListOptions{Filters: filters}

View File

@ -7,7 +7,6 @@ import (
abraFormatter "coopcloud.tech/abra/cli/formatter"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/config"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
@ -20,27 +19,16 @@ var appPsCommand = &cli.Command{
Usage: "Check app status",
Aliases: []string{"p"},
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
server := appFiles[appName].Server
ctx := context.Background()
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}
filters := filters.NewArgs()
filters.Add("name", appEnv.StackName())
filters.Add("name", app.StackName())
containers, err := cl.ContainerList(ctx, types.ContainerListOptions{Filters: filters})
if err != nil {

View File

@ -34,12 +34,12 @@ var appRemoveCommand = &cli.Command{
internal.ForceFlag,
},
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
if !internal.Force {
response := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("About to delete %s, are you sure", appName),
Message: fmt.Sprintf("About to delete %s, are you sure", app.Name),
}
if err := survey.AskOne(prompt, &response); err != nil {
logrus.Fatal(err)
@ -54,10 +54,8 @@ var appRemoveCommand = &cli.Command{
logrus.Fatal(err)
}
appPath := appFiles[appName].Path
server := appFiles[appName].Server
ctx := context.Background()
cl, err := client.New(server)
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}
@ -67,13 +65,13 @@ var appRemoveCommand = &cli.Command{
if err != nil {
logrus.Fatal(err)
}
if statuses[appName] == "deployed" {
logrus.Fatalf("'%s' is still deployed. Run \"abra app %s undeploy\" or pass --force", appName, appName)
if statuses[app.Name] == "deployed" {
logrus.Fatalf("'%s' is still deployed. Run \"abra app %s undeploy\" or pass --force", app.Name, app.Name)
}
}
fs := filters.NewArgs()
fs.Add("name", appName)
fs.Add("name", app.Name)
secretList, err := cl.SecretList(ctx, types.SecretListOptions{Filters: fs})
if err != nil {
logrus.Fatal(err)
@ -149,11 +147,11 @@ var appRemoveCommand = &cli.Command{
logrus.Info("No volumes to remove")
}
err = os.Remove(appPath)
err = os.Remove(app.Path)
if err != nil {
logrus.Fatal(err)
}
logrus.Info(fmt.Sprintf("File: %s removed", appPath))
logrus.Info(fmt.Sprintf("File: %s removed", app.Path))
return nil
},

View File

@ -31,23 +31,13 @@ var appRestoreCommand = &cli.Command{
Flags: []cli.Flag{restoreAllServicesFlag},
ArgsUsage: "<service> [<backup file>]",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
if c.Args().Len() > 1 && restoreAllServices {
internal.ShowSubcommandHelpAndError(c, errors.New("cannot use <service>/<backup file> and '--all' together"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
abraSh := path.Join(config.ABRA_DIR, "apps", appEnv.Type, "abra.sh")
abraSh := path.Join(config.ABRA_DIR, "apps", app.Type, "abra.sh")
if _, err := os.Stat(abraSh); err != nil {
if os.IsNotExist(err) {
logrus.Fatalf("'%s' does not exist?", abraSh)
@ -70,7 +60,7 @@ var appRestoreCommand = &cli.Command{
logrus.Fatal(err)
}
if !strings.Contains(string(bytes), execCmd) {
logrus.Fatalf("%s doesn't have a '%s' function", appEnv.Type, execCmd)
logrus.Fatalf("%s doesn't have a '%s' function", app.Type, execCmd)
}
backupFile := c.Args().Get(2)

View File

@ -8,7 +8,6 @@ import (
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/client/container"
"coopcloud.tech/abra/pkg/config"
"github.com/docker/cli/cli/command"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
@ -40,32 +39,21 @@ var appRunCommand = &cli.Command{
ArgsUsage: "<service> <args>...",
Usage: "Run a command in a service container",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
if c.Args().Len() < 2 {
internal.ShowSubcommandHelpAndError(c, errors.New("no <service> provided"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
server := appFiles[appName].Server
ctx := context.Background()
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}
serviceName := c.Args().Get(1)
filters := filters.NewArgs()
filters.Add("name", fmt.Sprintf("%s_%s", appEnv.StackName(), serviceName))
filters.Add("name", fmt.Sprintf("%s_%s", app.StackName(), serviceName))
containers, err := cl.ContainerList(ctx, types.ContainerListOptions{Filters: filters})
if err != nil {

View File

@ -9,7 +9,6 @@ import (
abraFormatter "coopcloud.tech/abra/cli/formatter"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/secret"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
@ -32,25 +31,14 @@ var appSecretGenerateCommand = &cli.Command{
ArgsUsage: "<secret> <version>",
Flags: []cli.Flag{allSecretsFlag, internal.PassFlag},
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
if c.Args().Get(1) != "" && allSecrets {
internal.ShowSubcommandHelpAndError(c, errors.New("cannot use '<secret> <version>' and '--all' together"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
secretsToCreate := make(map[string]string)
server := appFiles[appName].Server
secretEnvVars := secret.ReadSecretEnvVars(appEnv.Env)
secretEnvVars := secret.ReadSecretEnvVars(app.Env)
if allSecrets {
secretsToCreate = secretEnvVars
} else {
@ -64,14 +52,14 @@ var appSecretGenerateCommand = &cli.Command{
}
}
secretVals, err := secret.GenerateSecrets(secretsToCreate, appEnv.StackName(), server)
secretVals, err := secret.GenerateSecrets(secretsToCreate, app.StackName(), app.Server)
if err != nil {
logrus.Fatal(err)
}
if internal.Pass {
for name, data := range secretVals {
if err := secret.PassInsertSecret(data, name, appEnv.StackName(), server); err != nil {
if err := secret.PassInsertSecret(data, name, app.StackName(), app.Server); err != nil {
logrus.Fatal(err)
}
}
@ -88,7 +76,7 @@ var appSecretInsertCommand = &cli.Command{
Flags: []cli.Flag{internal.PassFlag},
ArgsUsage: "<secret> <version> <data>",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
if c.Args().Len() != 4 {
internal.ShowSubcommandHelpAndError(c, errors.New("missing arguments?"))
@ -98,24 +86,13 @@ var appSecretInsertCommand = &cli.Command{
version := c.Args().Get(2)
data := c.Args().Get(3)
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
server := appFiles[appName].Server
secretName := fmt.Sprintf("%s_%s_%s", appEnv.StackName(), name, version)
if err := client.StoreSecret(secretName, data, server); err != nil {
secretName := fmt.Sprintf("%s_%s_%s", app.StackName(), name, version)
if err := client.StoreSecret(secretName, data, app.Server); err != nil {
logrus.Fatal(err)
}
if internal.Pass {
if err := secret.PassInsertSecret(data, name, appEnv.StackName(), server); err != nil {
if err := secret.PassInsertSecret(data, name, app.StackName(), app.Server); err != nil {
logrus.Fatal(err)
}
}
@ -131,7 +108,7 @@ var appSecretRmCommand = &cli.Command{
Flags: []cli.Flag{allSecretsFlag, internal.PassFlag},
ArgsUsage: "<secret>",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
if c.Args().Get(1) != "" && allSecrets {
internal.ShowSubcommandHelpAndError(c, errors.New("cannot use '<secret>' and '--all' together"))
@ -141,25 +118,14 @@ var appSecretRmCommand = &cli.Command{
internal.ShowSubcommandHelpAndError(c, errors.New("no secret(s) specified?"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
server := appFiles[appName].Server
ctx := context.Background()
cl, err := client.New(server)
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}
filters := filters.NewArgs()
filters.Add("name", appEnv.StackName())
filters.Add("name", app.StackName())
secretList, err := cl.SecretList(ctx, types.SecretListOptions{Filters: filters})
if err != nil {
logrus.Fatal(err)
@ -168,13 +134,13 @@ var appSecretRmCommand = &cli.Command{
secretToRm := c.Args().Get(1)
for _, cont := range secretList {
secretName := cont.Spec.Annotations.Name
parsed := secret.ParseGeneratedSecretName(secretName, appEnv)
parsed := secret.ParseGeneratedSecretName(secretName, app)
if allSecrets {
if err := cl.SecretRemove(ctx, secretName); err != nil {
logrus.Fatal(err)
}
if internal.Pass {
if err := secret.PassRmSecret(parsed, appEnv.StackName(), server); err != nil {
if err := secret.PassRmSecret(parsed, app.StackName(), app.Server); err != nil {
logrus.Fatal(err)
}
}
@ -184,7 +150,7 @@ var appSecretRmCommand = &cli.Command{
logrus.Fatal(err)
}
if internal.Pass {
if err := secret.PassRmSecret(parsed, appEnv.StackName(), server); err != nil {
if err := secret.PassRmSecret(parsed, app.StackName(), app.Server); err != nil {
logrus.Fatal(err)
}
}
@ -201,32 +167,20 @@ var appSecretLsCommand = &cli.Command{
Usage: "List all secrets",
Aliases: []string{"ls"},
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
secrets := secret.ReadSecretEnvVars(appEnv.Env)
app := internal.ValidateApp(c)
secrets := secret.ReadSecretEnvVars(app.Env)
tableCol := []string{"Name", "Version", "Generated Name", "Created On Server"}
table := abraFormatter.CreateTable(tableCol)
server := appFiles[appName].Server
ctx := context.Background()
cl, err := client.New(server)
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}
filters := filters.NewArgs()
filters.Add("name", appEnv.StackName())
filters.Add("name", app.StackName())
secretList, err := cl.SecretList(ctx, types.SecretListOptions{Filters: filters})
if err != nil {
logrus.Fatal(err)
@ -244,7 +198,7 @@ var appSecretLsCommand = &cli.Command{
if err != nil {
logrus.Fatal(err)
}
secretRemoteName := fmt.Sprintf("%s_%s_%s", appEnv.StackName(), secretName, secVal.Version)
secretRemoteName := fmt.Sprintf("%s_%s_%s", app.StackName(), secretName, secVal.Version)
if _, ok := remoteSecretNames[secretRemoteName]; ok {
createdRemote = true
}

View File

@ -6,7 +6,6 @@ import (
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/client"
stack "coopcloud.tech/abra/pkg/client/stack"
"coopcloud.tech/abra/pkg/config"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
@ -21,26 +20,15 @@ vigilant, as your swarm installation will consider any previously attached
volumes as eligiblef or pruning once undeployed.
`,
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
app := internal.ValidateApp(c)
ctx := context.Background()
host := appFiles[appName].Server
cl, err := client.New(host)
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}
rmOpts := stack.Remove{Namespaces: []string{appEnv.StackName()}}
rmOpts := stack.Remove{Namespaces: []string{app.StackName()}}
if err := stack.RunRemove(ctx, cl, rmOpts); err != nil {
logrus.Fatal(err)
}

View File

@ -45,33 +45,21 @@ var appVersionCommand = &cli.Command{
Aliases: []string{"v"},
Usage: "Show version of all services in app",
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
app := appFiles[appName]
composeFiles, err := config.GetAppComposeFiles(appEnv.Type, appEnv.Env)
composeFiles, err := config.GetAppComposeFiles(app.Type, app.Env)
if err != nil {
logrus.Fatal(err)
}
opts := stack.Deploy{Composefiles: composeFiles}
compose, err := config.GetAppComposeConfig(appEnv.Type, opts, appEnv.Env)
compose, err := config.GetAppComposeConfig(app.Type, opts, app.Env)
if err != nil {
logrus.Fatal(err)
}
ch := make(chan stack.StackStatus, len(compose.Services))
for _, service := range compose.Services {
label := fmt.Sprintf("coop-cloud.%s.%s.version", appEnv.StackName(), service.Name)
label := fmt.Sprintf("coop-cloud.%s.%s.version", app.StackName(), service.Name)
go func(s string, l string) {
ch <- stack.GetDeployedServicesByLabel(s, l)
}(app.Server, label)
@ -96,7 +84,7 @@ var appVersionCommand = &cli.Command{
for _, service := range compose.Services {
if status, ok := statuses[service.Name]; ok {
statusService := status.Services[0]
label := fmt.Sprintf("coop-cloud.%s.%s.version", appEnv.StackName(), service.Name)
label := fmt.Sprintf("coop-cloud.%s.%s.version", app.StackName(), service.Name)
version, digest := parseVersionLabel(statusService.Spec.Labels[label])
image, err := getImagePath(statusService.Spec.Labels["com.docker.stack.image"])
if err != nil {

View File

@ -6,41 +6,24 @@ import (
abraFormatter "coopcloud.tech/abra/cli/formatter"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/config"
"github.com/AlecAivazis/survey/v2"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
// getAppServer retrieves the server of an app.
func getAppServer(appName string) string {
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
var server string
if app, ok := appFiles[appName]; ok {
server = app.Server
} else {
logrus.Fatalf(`app "%s" does not exist`, appName)
}
return server
}
var appVolumeListCommand = &cli.Command{
Name: "list",
Usage: "list volumes associated with an app",
Aliases: []string{"ls"},
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
ctx := context.Background()
server := getAppServer(appName)
volumeList, err := client.GetVolumes(ctx, server, appName)
volumeList, err := client.GetVolumes(ctx, app.Server, app.Name)
if err != nil {
logrus.Fatal(err)
}
table := abraFormatter.CreateTable([]string{"DRIVER", "VOLUME NAME"})
var volTable [][]string
for _, volume := range volumeList {
@ -50,8 +33,10 @@ var appVolumeListCommand = &cli.Command{
}
volTable = append(volTable, volRow)
}
table.AppendBulk(volTable)
table.Render()
return nil
},
}
@ -64,10 +49,10 @@ var appVolumeRemoveCommand = &cli.Command{
internal.ForceFlag,
},
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
server := getAppServer(appName)
app := internal.ValidateApp(c)
ctx := context.Background()
volumeList, err := client.GetVolumes(ctx, server, appName)
volumeList, err := client.GetVolumes(ctx, app.Server, app.Name)
if err != nil {
logrus.Fatal(err)
}
@ -87,11 +72,13 @@ var appVolumeRemoveCommand = &cli.Command{
volumesToRemove = volumeNames
}
err = client.RemoveVolumes(ctx, server, volumesToRemove, internal.Force)
err = client.RemoveVolumes(ctx, app.Server, volumesToRemove, internal.Force)
if err != nil {
logrus.Fatal(err)
}
logrus.Info("Volumes removed successfully.")
return nil
},
}

View File

@ -3,6 +3,7 @@ package internal
import (
"errors"
"coopcloud.tech/abra/pkg/app"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/recipe"
"github.com/sirupsen/logrus"
@ -25,21 +26,17 @@ func ValidateRecipeArg(c *cli.Context) string {
}
// ValidateAppNameArg ensures the app name arg is valid.
func ValidateAppNameArg(c *cli.Context) string {
func ValidateApp(c *cli.Context) config.App {
appName := c.Args().First()
if appName == "" {
ShowSubcommandHelpAndError(c, errors.New("no app provided"))
}
appFiles, err := config.LoadAppFiles("")
app, err := app.Get(appName)
if err != nil {
logrus.Fatal(err)
}
if _, ok := appFiles[appName]; !ok {
logrus.Fatalf("'%s' doesn't exist?", appName)
}
return appName
return app
}

20
pkg/app/app.go Normal file
View File

@ -0,0 +1,20 @@
package app
import (
"coopcloud.tech/abra/pkg/config"
)
// Get retrieves an app
func Get(appName string) (config.App, error) {
files, err := config.LoadAppFiles("")
if err != nil {
return config.App{}, err
}
app, err := config.GetApp(files, appName)
if err != nil {
return config.App{}, err
}
return app, nil
}

View File

@ -40,7 +40,8 @@ type App struct {
Type string
Domain string
Env AppEnv
File AppFile
Server string
Path string
}
// StackName gets what the docker safe stack name is for the app
@ -56,7 +57,7 @@ type ByServer []App
func (a ByServer) Len() int { return len(a) }
func (a ByServer) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByServer) Less(i, j int) bool {
return strings.ToLower(a[i].File.Server) < strings.ToLower(a[j].File.Server)
return strings.ToLower(a[i].Server) < strings.ToLower(a[j].Server)
}
// ByServerAndType sort a slice of Apps
@ -65,10 +66,10 @@ type ByServerAndType []App
func (a ByServerAndType) Len() int { return len(a) }
func (a ByServerAndType) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByServerAndType) Less(i, j int) bool {
if a[i].File.Server == a[j].File.Server {
if a[i].Server == a[j].Server {
return strings.ToLower(a[i].Type) < strings.ToLower(a[j].Type)
}
return strings.ToLower(a[i].File.Server) < strings.ToLower(a[j].File.Server)
return strings.ToLower(a[i].Server) < strings.ToLower(a[j].Server)
}
// ByType sort a slice of Apps
@ -114,7 +115,8 @@ func newApp(env AppEnv, name string, appFile AppFile) (App, error) {
Domain: domain,
Type: apptype,
Env: env,
File: appFile,
Server: appFile.Server,
Path: appFile.Path,
}, nil
}