-
Notifications
You must be signed in to change notification settings - Fork 32
/
retire.go
52 lines (43 loc) · 1.31 KB
/
retire.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
52
package hosts
import (
"strings"
"github.com/Songmu/prompter"
"github.com/mackerelio/mkr/logger"
"github.com/mackerelio/mkr/mackerelclient"
"github.com/urfave/cli"
)
var CommandRetire = cli.Command{
Name: "retire",
Usage: "Retire hosts",
ArgsUsage: "[--force] hostIds...",
Description: `
Retire host identified by <hostId>. Be careful because this is an irreversible operation.
Requests POST /api/v0/hosts/<hostId>/retire parallelly. See https://mackerel.io/api-docs/entry/hosts#retire .
`,
Action: doRetire,
Flags: []cli.Flag{
cli.BoolFlag{Name: "force", Usage: "Force retirement without confirmation."},
},
}
func doRetire(c *cli.Context) error {
confFile := c.GlobalString("conf")
force := c.Bool("force")
argHostIDs := c.Args()
if len(argHostIDs) < 1 {
argHostIDs = make([]string, 1)
if argHostIDs[0] = mackerelclient.LoadHostIDFromConfig(confFile); argHostIDs[0] == "" {
cli.ShowCommandHelpAndExit(c, "retire", 1)
}
}
if !force && !prompter.YN("Retire following hosts.\n "+strings.Join(argHostIDs, "\n ")+"\nAre you sure?", true) {
logger.Log("", "retirement is canceled.")
return nil
}
client := mackerelclient.NewFromContext(c)
for _, hostID := range argHostIDs {
err := client.RetireHost(hostID)
logger.DieIf(err)
logger.Log("retired", hostID)
}
return nil
}