package app import ( "errors" "fmt" "time" "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" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) var appRestartCommand = &cli.Command{ Name: "restart", Usage: "Restart an app", Aliases: []string{"R"}, ArgsUsage: "", Description: `This command restarts a service within a deployed app.`, Action: func(c *cli.Context) error { app := internal.ValidateApp(c) serviceName := c.Args().Get(1) if serviceName == "" { err := errors.New("missing service?") internal.ShowSubcommandHelpAndError(c, err) } cl, err := client.New(app.Server) if err != nil { logrus.Fatal(err) } serviceFilter := fmt.Sprintf("%s_%s", app.StackName(), serviceName) filters := filters.NewArgs() filters.Add("name", serviceFilter) containerOpts := types.ContainerListOptions{Filters: filters} containers, err := cl.ContainerList(c.Context, containerOpts) if err != nil { logrus.Fatal(err) } if len(containers) != 1 { logrus.Fatalf("expected 1 service but got %v", len(containers)) } logrus.Debugf("attempting to restart %s", serviceFilter) timeout := 30 * time.Second if err := cl.ContainerRestart(c.Context, containers[0].ID, &timeout); err != nil { logrus.Fatal(err) } logrus.Infof("%s service restarted", serviceFilter) return nil }, BashComplete: func(c *cli.Context) { appNames, err := config.GetAppNames() if err != nil { logrus.Warn(err) } if c.NArg() > 0 { return } for _, a := range appNames { fmt.Println(a) } }, }