From 1097daa69fa95a72460f7f49baf90c6ee906a7ac Mon Sep 17 00:00:00 2001 From: decentral1se Date: Sun, 7 Jul 2024 21:52:24 +0200 Subject: [PATCH] fix: "abra app restart" docs + --all-services See https://git.coopcloud.tech/coop-cloud/organising/issues/605 --- cli/app/restart.go | 87 ++++++++++++++++++++---------- cli/internal/cli.go | 7 +++ tests/integration/app_restart.bats | 16 +++++- 3 files changed, 80 insertions(+), 30 deletions(-) diff --git a/cli/app/restart.go b/cli/app/restart.go index 15b4cfd0..d5ada105 100644 --- a/cli/app/restart.go +++ b/cli/app/restart.go @@ -6,6 +6,7 @@ import ( "fmt" "coopcloud.tech/abra/cli/internal" + appPkg "coopcloud.tech/abra/pkg/app" "coopcloud.tech/abra/pkg/autocomplete" "coopcloud.tech/abra/pkg/client" upstream "coopcloud.tech/abra/pkg/upstream/service" @@ -18,23 +19,49 @@ var appRestartCommand = cli.Command{ Name: "restart", Aliases: []string{"re"}, Usage: "Restart an app", - ArgsUsage: "", + ArgsUsage: " []", Flags: []cli.Flag{ internal.DebugFlag, internal.OfflineFlag, + internal.AllServicesFlag, }, - Before: internal.SubCommandBefore, - Description: `This command restarts a service within a deployed app.`, + Before: internal.SubCommandBefore, + Description: ` +This command restarts services within a deployed app. + +Run "abra app ps " to see a list of service names. + +Pass "--all-services/-a" to restart all services. + +Example: + + abra app restart example.com app +`, BashComplete: autocomplete.AppNameComplete, Action: func(c *cli.Context) error { app := internal.ValidateApp(c) - serviceNameShort := c.Args().Get(1) - if serviceNameShort == "" { - err := errors.New("missing service?") + serviceName := c.Args().Get(1) + if serviceName == "" && !internal.AllServices { + err := errors.New("missing ") internal.ShowSubcommandHelpAndError(c, err) } + if serviceName != "" && internal.AllServices { + logrus.Fatal("cannot use and --all-services together") + } + + var serviceNames []string + if internal.AllServices { + var err error + serviceNames, err = appPkg.GetAppServiceNames(app.Name) + if err != nil { + logrus.Fatal(err) + } + } else { + serviceNames = append(serviceNames, serviceName) + } + cl, err := client.New(app.Server) if err != nil { logrus.Fatal(err) @@ -49,32 +76,34 @@ var appRestartCommand = cli.Command{ logrus.Fatalf("%s is not deployed?", app.Name) } - serviceName := fmt.Sprintf("%s_%s", app.StackName(), serviceNameShort) + for _, serviceName := range serviceNames { + stackServiceName := fmt.Sprintf("%s_%s", app.StackName(), serviceName) - logrus.Debugf("attempting to scale %s to 0 (restart logic)", serviceName) - if err := upstream.RunServiceScale(context.Background(), cl, serviceName, 0); err != nil { - logrus.Fatal(err) + logrus.Debugf("attempting to scale %s to 0", stackServiceName) + + if err := upstream.RunServiceScale(context.Background(), cl, stackServiceName, 0); err != nil { + logrus.Fatal(err) + } + + if err := stack.WaitOnService(context.Background(), cl, stackServiceName, app.Name); err != nil { + logrus.Fatal(err) + } + + logrus.Debugf("%s has been scaled to 0", stackServiceName) + logrus.Debugf("attempting to scale %s to 1", stackServiceName) + + if err := upstream.RunServiceScale(context.Background(), cl, stackServiceName, 1); err != nil { + logrus.Fatal(err) + } + + if err := stack.WaitOnService(context.Background(), cl, stackServiceName, app.Name); err != nil { + logrus.Fatal(err) + } + + logrus.Debugf("%s has been scaled to 1", stackServiceName) + logrus.Infof("%s service successfully restarted", serviceName) } - if err := stack.WaitOnService(context.Background(), cl, serviceName, app.Name); err != nil { - logrus.Fatal(err) - } - - logrus.Debugf("%s has been scaled to 0 (restart logic)", serviceName) - - logrus.Debugf("attempting to scale %s to 1 (restart logic)", serviceName) - if err := upstream.RunServiceScale(context.Background(), cl, serviceName, 1); err != nil { - logrus.Fatal(err) - } - - if err := stack.WaitOnService(context.Background(), cl, serviceName, app.Name); err != nil { - logrus.Fatal(err) - } - - logrus.Debugf("%s has been scaled to 1 (restart logic)", serviceName) - - logrus.Infof("%s service successfully restarted", serviceNameShort) - return nil }, } diff --git a/cli/internal/cli.go b/cli/internal/cli.go index 53b7082e..edaa71cc 100644 --- a/cli/internal/cli.go +++ b/cli/internal/cli.go @@ -278,6 +278,13 @@ var GitEmailFlag = &cli.StringFlag{ Destination: &GitEmail, } +var AllServices bool +var AllServicesFlag = &cli.BoolFlag{ + Name: "all-services, a", + Usage: "Restart all services", + Destination: &AllServices, +} + // SubCommandBefore wires up pre-action machinery (e.g. --debug handling). func SubCommandBefore(c *cli.Context) error { if Debug { diff --git a/tests/integration/app_restart.bats b/tests/integration/app_restart.bats index ceac66d9..b41c9689 100644 --- a/tests/integration/app_restart.bats +++ b/tests/integration/app_restart.bats @@ -34,7 +34,7 @@ teardown(){ @test "error if service missing" { run $ABRA app restart "$TEST_APP_DOMAIN" assert_failure - assert_output --partial 'missing service' + assert_output --partial 'missing ' } @test "error if not deployed" { @@ -53,3 +53,17 @@ teardown(){ assert_output --regexp 'attempting to scale .* to 1' assert_output --partial 'service successfully restarted' } + +@test "cannot use and --all-services together" { + run $ABRA app restart "$TEST_APP_DOMAIN" app --all-services + assert_failure + assert_output --regexp "cannot use .* together" +} + +# bats test_tags=slow +@test "all services are restarted" { + _deploy_app + + run $ABRA app restart "$TEST_APP_DOMAIN" --all-services --debug + assert_success +}