-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlord.go
301 lines (249 loc) · 7.19 KB
/
overlord.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"flag"
"io"
"log"
"log/syslog"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"text/template"
"time"
"github.com/AirVantage/overlord/pkg/lookable"
"github.com/AirVantage/overlord/pkg/set"
"github.com/BurntSushi/toml"
"github.com/aws/aws-sdk-go/aws/awserr"
)
var (
resourcesDirName = "/etc/overlord/resources"
templatesDirName = "/etc/overlord/templates"
stateFileName = "/var/overlord/state.toml"
interval = flag.Duration("interval", 30*time.Second, "Interval between each lookup")
ipv6 = flag.Bool("ipv6", false, "Look for IPv6 addresses instead of IPv4")
)
type ResourceConfig struct {
Resource Resource `toml:"template"`
}
type Resource struct {
Src string
Dest string
Groups []lookable.AutoScalingGroup
Tags []lookable.Tag
Subnets []lookable.Subnet
ReloadCmd string `toml:"reload_cmd"`
}
// ResourceSet is a set of unique Resources.
type ResourceSet map[*Resource]struct{}
// Add a Resource to the set.
func (rs ResourceSet) Add(r *Resource) {
rs[r] = struct{}{}
}
// Changes keeps track of added/removed IPs for a Resource.
// We store IPs as strings to support both IPv4 and IPv6.
type Changes struct {
addedIPs set.Strings
removedIPs set.Strings
}
// NewChanges return a pointer to an initialized Changes struct.
func NewChanges() *Changes {
return &Changes{
addedIPs: set.NewStringSet(),
removedIPs: set.NewStringSet(),
}
}
// Formats an environment variable for one or more values.
func mkEnvVar(name string, values []string) string {
return name + "=" + strings.Join(values, " ")
}
func iterate() {
// log.Println("Start iteration")
resources := make(map[lookable.Lookable]ResourceSet)
//load resources definition files
resourcesDir, err := os.Open(resourcesDirName)
defer func() { resourcesDir.Close() }()
if err != nil {
log.Fatal(err)
}
resourcesFiles, err := resourcesDir.Readdir(0)
if err != nil {
log.Fatal(err)
}
for _, resourceFile := range resourcesFiles {
if filepath.Ext(resourceFile.Name()) != ".toml" || resourceFile.IsDir() {
continue
}
var rc *ResourceConfig
_, err := toml.DecodeFile(filepath.Join(resourcesDirName, resourceFile.Name()), &rc)
if err != nil {
log.Fatal(err)
}
// log.Println("Read File", resourceFile.Name(), ":", rc)
for _, group := range rc.Resource.Groups {
if resources[group] == nil {
resources[group] = make(ResourceSet)
}
resources[group].Add(&rc.Resource)
}
for _, tag := range rc.Resource.Tags {
if resources[tag] == nil {
resources[tag] = make(ResourceSet)
}
resources[tag].Add(&rc.Resource)
}
for _, subnet := range rc.Resource.Subnets {
if resources[subnet] == nil {
resources[subnet] = make(ResourceSet)
}
resources[subnet].Add(&rc.Resource)
}
}
state := make(map[string]set.Strings)
//load state file
err = os.MkdirAll(filepath.Dir(stateFileName), 0777)
if err != nil {
log.Fatal(err)
}
_, err = toml.DecodeFile(stateFileName, &state)
if err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
// log.Println("Load state from", stateFileName, ":", state)
// log.Println("Find Resources to update")
resourcesToUpdate := make(map[*Resource]*Changes)
newState := make(map[string]set.Strings)
//find group ips to update
for g, resourcesset := range resources {
group := g.String()
ips, err := g.LookupIPs(*ipv6)
// if some AWS API calls failed during the IPs lookup, stop here and exit
// it will keep the dest file unmodified and won't execute the reload command.
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
log.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
}
log.Fatal("AWS Error:", err.Error())
}
newState[group] = set.NewStringSet()
changes := NewChanges()
changed := false
if _, exists := state[group]; !exists {
state[group] = set.NewStringSet()
}
for _, ip := range ips {
newState[group].Add(ip)
if !state[group].Has(ip) {
changed = true
changes.addedIPs.Add(ip)
log.Println("For group", group, "new IP:", ip)
}
}
for _, oldIP := range state[group].ToSlice() {
if !newState[group].Has(oldIP) {
changed = true
changes.removedIPs.Add(oldIP)
log.Println("For group", group, "deprecated IP:", oldIP)
}
}
if changed {
for resource := range resourcesset {
log.Println("For group", group, "update ressource:", resource)
resourcesToUpdate[resource] = changes
}
}
}
//make list of ips
ips := make(map[string][]string)
for group, ipsSet := range newState {
ipsList := make([]string, 0, len(ipsSet))
for ip := range ipsSet {
ipsList = append(ipsList, ip)
}
sort.Strings(ipsList)
ips[group] = ipsList
}
// log.Println("Update resources and restart processes")
//generate resources
for resource, changes := range resourcesToUpdate {
tmpl, err := template.ParseFiles(filepath.Join(templatesDirName, resource.Src))
if err != nil {
log.Fatal(err)
}
err = os.MkdirAll(filepath.Dir(resource.Dest), 0777)
if err != nil {
log.Fatal(err)
}
// create the dest file and truncate it if it already exists
destFile, err := os.Create(resource.Dest)
defer func() { destFile.Close() }()
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(destFile, ips)
if err != nil {
log.Fatal(err)
}
log.Println("For resource", resource, "update file", resource.Dest)
if resource.ReloadCmd == "" {
continue
}
//cmdSplit := strings.Fields(resource.ReloadCmd)
//cmd := exec.Command(cmdSplit[0], cmdSplit[1:]...)
cmd := exec.Command("bash", "-c", resource.ReloadCmd)
if changes != nil {
cmd.Env = append(os.Environ(), mkEnvVar("IP_ADDED", changes.addedIPs.ToSlice()), mkEnvVar("IP_REMOVED", changes.removedIPs.ToSlice()))
}
log.Println(cmd)
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
log.Println("For resource", resource, "start reload cmd", resource.ReloadCmd)
err = cmd.Wait()
if err != nil {
log.Println("For resource", resource, "reload cmd", resource.ReloadCmd, "finished with error", err)
} else {
log.Println("For resource", resource, "reload cmd", resource.ReloadCmd, "finished successfuly")
}
}
//write state file
stateFile, err := os.Create(stateFileName)
defer func() { stateFile.Close() }()
if err != nil {
log.Fatal(err)
}
if err = toml.NewEncoder(stateFile).Encode(&newState); err != nil {
log.Fatal("Error writing state file:", stateFileName, ":", err)
}
state = newState
// log.Println("Log state", state, "in file", stateFileName)
// log.Println("Iteration done")
}
func main() {
log.SetFlags(0)
var syslogCfg = os.Getenv("SYSLOG_ADDRESS")
if len(syslogCfg) > 0 {
syslogWriter, err := syslog.Dial("udp", syslogCfg, syslog.LOG_INFO, "av-balancing")
if err != nil {
// Do not make this error fatal. A syslog server may
// not be available when deploying to a new region.
log.Println("warning: cannot send logs to syslog:", err)
} else {
log.SetOutput(io.MultiWriter(os.Stdout, syslogWriter))
}
}
flag.Parse()
// Remove the old and possibly incompatible state file.
os.Remove(stateFileName)
for {
iterate()
time.Sleep(*interval)
}
}
// func main() {
// for {
// log.Println(lookupIPs("qa-site-survey-instance"))
// time.Sleep(time.Duration(30)*time.Second)
// }
// }