fix: "abra app restart" improvements #427

Merged
decentral1se merged 2 commits from restart-improvements into main 2024-07-07 19:54:35 +00:00
4 changed files with 81 additions and 31 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/cli/internal"
appPkg "coopcloud.tech/abra/pkg/app"
"coopcloud.tech/abra/pkg/autocomplete" "coopcloud.tech/abra/pkg/autocomplete"
"coopcloud.tech/abra/pkg/client" "coopcloud.tech/abra/pkg/client"
upstream "coopcloud.tech/abra/pkg/upstream/service" upstream "coopcloud.tech/abra/pkg/upstream/service"
@ -18,23 +19,49 @@ var appRestartCommand = cli.Command{
Name: "restart", Name: "restart",
Aliases: []string{"re"}, Aliases: []string{"re"},
Usage: "Restart an app", Usage: "Restart an app",
ArgsUsage: "<domain>", ArgsUsage: "<domain> [<service>]",
Flags: []cli.Flag{ Flags: []cli.Flag{
internal.DebugFlag, internal.DebugFlag,
internal.OfflineFlag, internal.OfflineFlag,
internal.AllServicesFlag,
}, },
Before: internal.SubCommandBefore, Before: internal.SubCommandBefore,
Description: `This command restarts a service within a deployed app.`, Description: `
This command restarts services within a deployed app.
Run "abra app ps <domain>" 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, BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
app := internal.ValidateApp(c) app := internal.ValidateApp(c)
serviceNameShort := c.Args().Get(1) serviceName := c.Args().Get(1)
if serviceNameShort == "" { if serviceName == "" && !internal.AllServices {
err := errors.New("missing service?") err := errors.New("missing <service>")
internal.ShowSubcommandHelpAndError(c, err) internal.ShowSubcommandHelpAndError(c, err)
} }
if serviceName != "" && internal.AllServices {
logrus.Fatal("cannot use <service> 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) cl, err := client.New(app.Server)
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
@ -49,31 +76,33 @@ var appRestartCommand = cli.Command{
logrus.Fatalf("%s is not deployed?", app.Name) 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) logrus.Debugf("attempting to scale %s to 0", stackServiceName)
if err := upstream.RunServiceScale(context.Background(), cl, serviceName, 0); err != nil {
if err := upstream.RunServiceScale(context.Background(), cl, stackServiceName, 0); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
if err := stack.WaitOnService(context.Background(), cl, serviceName, app.Name); err != nil { if err := stack.WaitOnService(context.Background(), cl, stackServiceName, app.Name); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
logrus.Debugf("%s has been scaled to 0 (restart logic)", serviceName) logrus.Debugf("%s has been scaled to 0", stackServiceName)
logrus.Debugf("attempting to scale %s to 1", stackServiceName)
logrus.Debugf("attempting to scale %s to 1 (restart logic)", serviceName) if err := upstream.RunServiceScale(context.Background(), cl, stackServiceName, 1); err != nil {
if err := upstream.RunServiceScale(context.Background(), cl, serviceName, 1); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
if err := stack.WaitOnService(context.Background(), cl, serviceName, app.Name); err != nil { if err := stack.WaitOnService(context.Background(), cl, stackServiceName, app.Name); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
logrus.Debugf("%s has been scaled to 1 (restart logic)", serviceName) logrus.Debugf("%s has been scaled to 1", stackServiceName)
logrus.Infof("%s service successfully restarted", serviceName)
logrus.Infof("%s service successfully restarted", serviceNameShort) }
return nil return nil
}, },

View File

@ -278,6 +278,13 @@ var GitEmailFlag = &cli.StringFlag{
Destination: &GitEmail, 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). // SubCommandBefore wires up pre-action machinery (e.g. --debug handling).
func SubCommandBefore(c *cli.Context) error { func SubCommandBefore(c *cli.Context) error {
if Debug { if Debug {

View File

@ -1,4 +1,4 @@
package upstream package upstream //https://github.com/docker/cli/blob/master/cli/command/service/scale.go
import ( import (
"context" "context"

View File

@ -34,7 +34,7 @@ teardown(){
@test "error if service missing" { @test "error if service missing" {
run $ABRA app restart "$TEST_APP_DOMAIN" run $ABRA app restart "$TEST_APP_DOMAIN"
assert_failure assert_failure
assert_output --partial 'missing service' assert_output --partial 'missing <service>'
} }
@test "error if not deployed" { @test "error if not deployed" {
@ -53,3 +53,17 @@ teardown(){
assert_output --regexp 'attempting to scale .* to 1' assert_output --regexp 'attempting to scale .* to 1'
assert_output --partial 'service successfully restarted' assert_output --partial 'service successfully restarted'
} }
@test "cannot use <service> 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
}