forked from rochacon/elastic-nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elnginx.go
290 lines (237 loc) · 6.62 KB
/
elnginx.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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/rochacon/elastic-nginx/config"
"io/ioutil"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/ec2"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
)
const VERSION = "0.5"
var AWSAuth = aws.Auth{}
var AWSRegion = ""
var ConfigPath = ""
var Config = &config.Config{}
var Region = aws.Region{}
type Message struct {
Event string
InstanceId string `json:"EC2InstanceId"`
AutoScalingGroupARN string
}
type JSONInput struct {
Type string
TopicArn string
Message string
SubscribeURL string
}
func init() {
flag.StringVar(&AWSRegion, "aws-region", "us-east-1", "AWS Region of choice.")
flag.StringVar(&ConfigPath, "config", "/etc/elastic-nginx.json", "Elastic NGINX config file.")
}
func main() {
listen := flag.String("listen", "127.0.0.1:5000", "Address to listen to.")
show_version := flag.Bool("version", false, "Print version and exit.")
flag.Parse()
Region = aws.Region{
Name: AWSRegion,
EC2Endpoint: fmt.Sprintf("https://ec2.%s.amazonaws.com", AWSRegion),
SNSEndpoint: fmt.Sprintf("https://sns.%s.amazonaws.com", AWSRegion),
}
if *show_version {
fmt.Println("elastic-nginx version", VERSION)
return
}
var err error
AWSAuth, err = aws.EnvAuth()
if err != nil {
log.Fatal(err)
}
Config, err = config.ReadFile(ConfigPath)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/", readMessage)
log.Println("Listening on", *listen)
log.Println("Monitoring events on topic:", Config.TopicArn)
log.Println("AWS Region:", AWSRegion)
for i, u := range Config.Upstreams {
log.Printf("Upstream %d: %s", i, u.Name)
log.Println(" ContainerFolder:", u.ContainerFolder)
log.Println(" File:", u.File)
}
err = http.ListenAndServe(*listen, nil)
if err != nil {
log.Fatal(err)
}
}
func getUpstreamFilenameForInstance(u config.Upstream, i *ec2.Instance) string {
return filepath.Join(u.ContainerFolder, i.InstanceId+".upstream")
}
func addInstance(u config.Upstream, i *ec2.Instance) error {
filename := getUpstreamFilenameForInstance(u, i)
u.Lock()
defer u.Unlock()
upstream := fmt.Sprintf("server %s:80 max_fails=3 fail_timeout=60s; # %s\n", i.PrivateDNSName, i.InstanceId)
buf := []byte(upstream)
if err := ioutil.WriteFile(filename, buf, 0640); err != nil {
return err
}
return nil
}
func rmInstance(u config.Upstream, i *ec2.Instance) error {
filename := getUpstreamFilenameForInstance(u, i)
err := os.Remove(filename)
if err != nil && os.IsNotExist(err) {
return fmt.Errorf("Instance \"%s\" not found in config.", i.InstanceId)
}
if err != nil {
return err
}
return nil
}
func getInstance(id string) (ec2.Instance, error) {
ec2conn := ec2.New(AWSAuth, Region)
ec2resp, err := ec2conn.Instances([]string{id}, nil)
if err != nil {
return ec2.Instance{}, err
}
for _, r := range ec2resp.Reservations {
for _, i := range r.Instances {
return i, nil
}
}
return ec2.Instance{}, fmt.Errorf("WTFBBQ?!")
}
func reconfigure(u config.Upstream) error {
u.Lock()
defer u.Unlock()
upstream, err := os.Create(u.File)
if err != nil {
return err
}
defer upstream.Close()
upstream.WriteString(fmt.Sprintf("upstream %s {\n", u.Name))
upstream_filenames, err := filepath.Glob(filepath.Join(u.ContainerFolder, "*.upstream"))
if err != nil {
return err
}
for _, upstream_filename := range upstream_filenames {
content, err := ioutil.ReadFile(upstream_filename)
if err != nil {
return err
}
upstream.WriteString(fmt.Sprintf(" %s", string(content)))
}
upstream.WriteString("}\n")
return nil
}
func reload() ([]byte, error) {
return exec.Command("sudo", "service", "nginx", "reload").CombinedOutput()
}
func readMessage(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
log.Println(fmt.Sprintf("Received Payload: %s", body))
data := JSONInput{}
if err := json.Unmarshal(body, &data); err != nil {
log.Println("Invalid JSON.")
http.Error(w, "Invalid JSON.", http.StatusBadRequest)
return
}
if data.TopicArn != Config.TopicArn {
output := fmt.Sprintf("No handler for the specified ARN (\"%s\") found.", data.TopicArn)
log.Println(output)
http.Error(w, output, http.StatusNotFound)
return
}
if data.Type == "SubscriptionConfirmation" {
if Config.AutoSubscribe {
go http.Get(data.SubscribeURL)
w.WriteHeader(http.StatusAccepted)
output := fmt.Sprintf(`Subscribed to "%s".`, data.TopicArn)
log.Println(output)
fmt.Fprintf(w, output)
}
return
}
// Load message
message := Message{}
if err := json.Unmarshal([]byte(data.Message), &message); err != nil {
log.Println("Invalid Message field JSON.")
http.Error(w, "Invalid Message field JSON.", http.StatusBadRequest)
return
}
for _, u := range Config.Upstreams {
if message.AutoScalingGroupARN == u.AutoScalingGroupARN {
switch message.Event {
case "autoscaling:EC2_INSTANCE_LAUNCH":
launch(w, u, message.InstanceId)
case "autoscaling:EC2_INSTANCE_TERMINATE":
terminate(w, u, message.InstanceId)
default:
log.Println("Invaild Event.")
http.Error(w, "Invalid Event.", http.StatusBadRequest)
return
}
return
}
}
output := fmt.Sprintf(`Invalid Auto Scaling Group ARN "%s".`, message.AutoScalingGroupARN)
log.Println(output)
http.Error(w, output, http.StatusBadRequest)
return
}
func launch(w http.ResponseWriter, u config.Upstream, instanceId string) {
// Get EC2 Instance
instance, err := getInstance(instanceId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = addInstance(u, &instance)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := reconfigure(u); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := reload(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
output := fmt.Sprintf(`Added instance "%s".`, instance.InstanceId)
log.Println(output)
fmt.Fprintf(w, output)
}
func terminate(w http.ResponseWriter, u config.Upstream, instanceId string) {
// Get EC2 Instance
instance, err := getInstance(instanceId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = rmInstance(u, &instance)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := reconfigure(u); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := reload(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
output := fmt.Sprintf(`Removed instance "%s".`, instance.InstanceId)
log.Println(output)
fmt.Fprintf(w, output)
}