/
main.go
96 lines (83 loc) · 2.01 KB
/
main.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
95
96
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"text/template"
"time"
"github.com/trayio/bunny/Godeps/_workspace/src/github.com/aws/aws-sdk-go/aws"
"github.com/trayio/bunny/Godeps/_workspace/src/github.com/aws/aws-sdk-go/aws/credentials"
"github.com/trayio/bunny/nodes"
"github.com/trayio/bunny/temporary"
)
const (
configTemplate = `[{rabbit, [{cluster_nodes, {[{{range $index, $element := .}}{{if $index}}, {{end}}'rabbit@{{.Host}}'{{end}}], disc}}]}].`
)
func main() {
var (
rabbits []*nodes.Node
region string
config string
cfg *aws.Config
sigChan = make(chan os.Signal, 1)
tmpl = template.Must(template.New("config").Parse(configTemplate))
tickInit *time.Ticker
tick *time.Ticker
)
flag.StringVar(®ion, "region", "us-west-1", "AWS region")
flag.StringVar(&config, "destination", "/tmp/rabbitmq-cluster.conf", "Destination for generated configuration")
flag.Parse()
signal.Notify(
sigChan,
syscall.SIGINT,
syscall.SIGTERM,
)
cfg = auth(region)
tickInit = time.NewTicker(time.Second * 2)
tick = time.NewTicker(time.Minute * 5)
for {
select {
case <-tickInit.C:
rabbits = nodes.Collect(cfg)
if len(rabbits) < 2 {
continue
}
if config == "-" {
tmpl.Execute(os.Stdout, rabbits)
} else {
tmpFile, _ := temporary.NewFile()
tmpl.Execute(tmpFile.File, rabbits)
tmpFile.Move(config)
tickInit.Stop()
}
case <-tick.C:
if config == "-" {
tmpl.Execute(os.Stdout, rabbits)
} else {
rabbits = nodes.Collect(cfg)
tmpFile, _ := temporary.NewFile()
tmpl.Execute(tmpFile.File, rabbits)
tmpFile.Move(config)
}
case <-sigChan:
close(sigChan)
tickInit.Stop()
tick.Stop()
return
}
}
}
func auth(region string) *aws.Config {
credentials := credentials.NewChainCredentials(
[]credentials.Provider{
&credentials.SharedCredentialsProvider{},
&credentials.EnvProvider{},
&credentials.EC2RoleProvider{},
},
)
return &aws.Config{
Credentials: credentials,
Region: region,
}
}