forked from rancher/cli
/
restart.go
51 lines (46 loc) · 1.44 KB
/
restart.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cmd
import (
"strings"
"github.com/rancher/go-rancher/v2"
"github.com/urfave/cli"
)
var (
restartTypes = []string{"service", "container"}
)
func RestartCommand() cli.Command {
return cli.Command{
Name: "restart",
Usage: "Restart " + strings.Join(restartTypes, ", "),
Description: "\nRestart resources by ID or name in the current $RANCHER_ENVIRONMENT. Use `--env <envID>` or `--env <envName>` to select a different environment.\n\nExample:\n\t$ rancher restart 1s70\n\t$ rancher --env 1a5 restart stackName/serviceName \n",
ArgsUsage: "[ID NAME...]",
Action: restartResources,
Flags: []cli.Flag{
typesStringFlag(restartTypes),
cli.IntFlag{
Name: "batch-size",
Usage: "Number of containers to restart at a time",
Value: 1,
},
cli.IntFlag{
Name: "interval",
Usage: "Interval in millisecond to wait between restarts",
Value: 1000,
},
},
}
}
func restartResources(ctx *cli.Context) error {
return forEachResource(ctx, restartTypes, func(c *client.RancherClient, resource *client.Resource) (string, error) {
action, err := pickAction(resource, "restart")
if err != nil {
return "", err
}
err = c.Action(resource.Type, action, resource, &client.ServiceRestart{
RollingRestartStrategy: client.RollingRestartStrategy{
BatchSize: int64(ctx.Int("batch-size")),
IntervalMillis: int64(ctx.Int("interval")),
},
}, resource)
return resource.Id, err
})
}