forked from toolshed/abra
		
	fix: "abra app restart" docs + --all-services
See coop-cloud/organising#605
This commit is contained in:
		| @ -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: "<domain>", | ||||
| 	ArgsUsage: "<domain> [<service>]", | ||||
| 	Flags: []cli.Flag{ | ||||
| 		internal.DebugFlag, | ||||
| 		internal.OfflineFlag, | ||||
| 		internal.AllServicesFlag, | ||||
| 	}, | ||||
| 	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, | ||||
| 	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 <service>") | ||||
| 			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) | ||||
| 		if err != nil { | ||||
| 			logrus.Fatal(err) | ||||
| @ -49,31 +76,33 @@ 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.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, serviceName, app.Name); err != nil { | ||||
| 			if err := stack.WaitOnService(context.Background(), cl, stackServiceName, app.Name); err != nil { | ||||
| 				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, serviceName, 1); err != nil { | ||||
| 			if err := upstream.RunServiceScale(context.Background(), cl, stackServiceName, 1); err != nil { | ||||
| 				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.Debugf("%s has been scaled to 1 (restart logic)", serviceName) | ||||
|  | ||||
| 		logrus.Infof("%s service successfully restarted", serviceNameShort) | ||||
| 			logrus.Debugf("%s has been scaled to 1", stackServiceName) | ||||
| 			logrus.Infof("%s service successfully restarted", serviceName) | ||||
| 		} | ||||
|  | ||||
| 		return nil | ||||
| 	}, | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
| @ -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 <service>' | ||||
| } | ||||
|  | ||||
| @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 <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 | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user