2021-11-10 06:52:45 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
|
|
|
"coopcloud.tech/abra/pkg/client"
|
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-12-11 23:04:37 +00:00
|
|
|
containerPkg "coopcloud.tech/abra/pkg/container"
|
2021-11-10 06:52:45 +00:00
|
|
|
"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: "<service>",
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-12-09 13:12:06 +00:00
|
|
|
serviceFilter := fmt.Sprintf("%s_%s", app.StackName(), serviceName)
|
2021-11-10 06:52:45 +00:00
|
|
|
filters := filters.NewArgs()
|
|
|
|
filters.Add("name", serviceFilter)
|
2021-12-11 23:04:37 +00:00
|
|
|
|
|
|
|
targetContainer, err := containerPkg.GetContainer(c.Context, cl, filters, true)
|
2021-11-10 06:52:45 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("attempting to restart %s", serviceFilter)
|
|
|
|
|
|
|
|
timeout := 30 * time.Second
|
2021-12-11 23:04:37 +00:00
|
|
|
if err := cl.ContainerRestart(c.Context, targetContainer.ID, &timeout); err != nil {
|
2021-11-10 06:52:45 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|