forked from vulcand/vulcand
/
host.go
94 lines (85 loc) · 2.2 KB
/
host.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package command
import (
"fmt"
"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/codegangsta/cli"
)
func NewHostCommand(cmd *Command) cli.Command {
return cli.Command{
Name: "host",
Usage: "Operations with vulcan hosts",
Subcommands: []cli.Command{
{
Name: "ls",
Usage: "List all hosts",
Flags: []cli.Flag{},
Action: cmd.printHostsAction,
},
{
Name: "show",
Usage: "Show host details",
Flags: []cli.Flag{
cli.StringFlag{Name: "name", Usage: "hostname"},
},
Action: cmd.printHostAction,
},
{
Name: "add",
Flags: []cli.Flag{
cli.StringFlag{Name: "name", Usage: "hostname"},
},
Usage: "Add a new host to vulcan proxy",
Action: cmd.addHostAction,
},
{
Name: "rm",
Flags: []cli.Flag{
cli.StringFlag{Name: "name", Usage: "hostname"},
},
Usage: "Remove a host from vulcan",
Action: cmd.deleteHostAction,
},
{
Name: "set_keypair",
Flags: []cli.Flag{
cli.StringFlag{Name: "name", Usage: "hostname"},
cli.StringFlag{Name: "privateKey", Usage: "Path to a private key"},
cli.StringFlag{Name: "cert", Usage: "Path to a certificate"},
},
Usage: "Set host key pair",
Action: cmd.updateHostKeyPairAction,
},
},
}
}
func (cmd *Command) printHostsAction(c *cli.Context) {
hosts, err := cmd.client.GetHosts()
if err != nil {
cmd.printError(err)
return
}
cmd.printHosts(hosts)
}
func (cmd *Command) printHostAction(c *cli.Context) {
host, err := cmd.client.GetHost(c.String("name"))
if err != nil {
cmd.printError(err)
return
}
cmd.printHost(host)
}
func (cmd *Command) addHostAction(c *cli.Context) {
host, err := cmd.client.AddHost(c.String("name"))
cmd.printResult("%s added", host, err)
}
func (cmd *Command) updateHostKeyPairAction(c *cli.Context) {
keyPair, err := readKeyPair(c.String("cert"), c.String("privateKey"))
if err != nil {
cmd.printError(fmt.Errorf("failed to read key pair: %s", err))
return
}
host, err := cmd.client.UpdateHostKeyPair(c.String("name"), keyPair)
cmd.printResult("%s key pair updated", host, err)
}
func (cmd *Command) deleteHostAction(c *cli.Context) {
cmd.printStatus(cmd.client.DeleteHost(c.String("name")))
}