/
cluster.go
83 lines (69 loc) · 1.96 KB
/
cluster.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
package commands
import (
"flag"
"log"
"os"
"os/signal"
"strings"
"github.com/hashicorp/consul-live/live"
"github.com/mitchellh/cli"
)
func ClusterCommandFactory() (cli.Command, error) {
return &Cluster{}, nil
}
type Cluster struct {
}
func (c *Cluster) Help() string {
helpText := `
Usage consul-live cluster <options>
Options:
-consul=<string> Consul executable, defaults to "consul" from PATH
-servers=<int> Number of servers, defaults to 3
-server-args=<string> Additional args to pass to servers, may be given multiple times
-clients=<int> Number of clients, defaults to 10
-client-args=<string> Additional args to pass to clients, may be given multiple times
-nice-ports=<bool> If true, uses the Consul default ports for the first agent, defaults to true
`
return strings.TrimSpace(helpText)
}
func (c *Cluster) Synopsis() string {
return "Starts up a cluster"
}
func (c *Cluster) Run(args []string) int {
cfg := &live.ClusterConfig{}
cmdFlags := flag.NewFlagSet("cluster", flag.ContinueOnError)
cmdFlags.Usage = func() { log.Println(c.Help()) }
cmdFlags.StringVar(&cfg.Executable, "consul", "consul", "")
cmdFlags.IntVar(&cfg.Servers, "servers", 3, "")
cmdFlags.Var(&stringsFlag{&cfg.ServerArgs}, "server-args", "")
cmdFlags.IntVar(&cfg.Clients, "clients", 10, "")
cmdFlags.Var(&stringsFlag{&cfg.ClientArgs}, "client-args", "")
cmdFlags.BoolVar(&cfg.NicePorts, "nice-ports", true, "")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
if err := c.run(cfg); err != nil {
log.Println(err)
return 1
}
return 0
}
func (c *Cluster) run(cfg *live.ClusterConfig) error {
cluster, err := live.NewCluster(cfg)
if err != nil {
return err
}
defer func() {
if err := cluster.Shutdown(); err != nil {
log.Println(err)
}
}()
if err := cluster.Start(); err != nil {
return err
}
wait := make(chan os.Signal, 1)
signal.Notify(wait, os.Interrupt)
<-wait
log.Println("Got interrupt, cleaning up...")
return nil
}