-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync.go
executable file
·144 lines (112 loc) · 3.09 KB
/
sync.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
package main
import "github.com/docker/go-dockercloud/dockercloud"
import "github.com/aws/aws-sdk-go/service/ec2"
import "github.com/aws/aws-sdk-go/aws/session"
import "log"
import "os"
import "strings"
/*
* Return an environment variable. If it's not set - crash
*/
func getEnv(name string) string {
value := os.Getenv(name)
if len(value) == 0 {
panic("Please set " + name + " variable")
}
return value
}
/*
* Returns an array of public IP addresses
*/
func getNodeIps() []string {
nodeList, err := dockercloud.ListNodes()
if err != nil {
panic(err)
}
log.Println("Received public IP list from Docker Cloud")
nodeIps := make([]string, 0)
if len(nodeList.Objects) == 0 {
log.Println("There are no nodes in your Docker Cloud account yet")
}
for i := 0; i < len(nodeList.Objects); i++ {
if len(nodeList.Objects[i].Public_ip) > 0 {
nodeIps = append(nodeIps, nodeList.Objects[i].Public_ip + "/32")
}
}
return nodeIps
}
/*
* Infinite loop - listening to Docker Cloud events
*/
func listenToEvents() {
log.Println("Listening to Docker Cloud events")
c := make(chan dockercloud.Event)
e := make(chan error)
done := make(chan bool)
go dockercloud.Events(c, e, done)
for {
select {
case event := <-c:
log.Println(event)
case err := <-e:
log.Println(err)
}
}
}
/*
* Rewrite inbound rules for the security group
*/
func modifySecurityGroup(groupId string, ips []string) {
var newRules ec2.AuthorizeSecurityGroupIngressInput
var oldRules ec2.RevokeSecurityGroupIngressInput
var allProtocol string = "-1"
log.Println("Flushing security group... ")
svc := ec2.New(session.New())
newRules.GroupId = &groupId
oldRules.GroupId = &groupId
params := &ec2.DescribeSecurityGroupsInput{ GroupIds: []*string{ &groupId }}
resp, err := svc.DescribeSecurityGroups(params)
for i := 0; i < len(resp.SecurityGroups[0].IpPermissions); i++ {
existing := resp.SecurityGroups[0].IpPermissions[i]
oldRules.IpPermissions = append(oldRules.IpPermissions, existing)
}
_, err = svc.RevokeSecurityGroupIngress(&oldRules)
if err == nil {
log.Println("success")
}
log.Println("Adding current node IPs to the group... ")
// Add AWS internal network
ips = append(ips, "10.0.0.0/8")
for i := 0; i < len(ips); i++ {
entry := new(ec2.IpPermission)
entry.IpProtocol = &allProtocol
entry.IpRanges = []*ec2.IpRange{{CidrIp: &ips[i]}}
newRules.IpPermissions = append(newRules.IpPermissions, entry)
}
_, err = svc.AuthorizeSecurityGroupIngress(&newRules)
if err != nil {
panic(err)
}
log.Println("success")
}
/*
* Initialize Docker Cloud SDK
*/
func initDockerCloud() {
dockercloud.User = getEnv("DOCKER_CLOUD_USER")
dockercloud.ApiKey = getEnv("DOCKER_CLOUD_KEY")
if len(os.Getenv("DOCKER_CLOUD_NAMESPACE")) > 0 {
dockercloud.Namespace = os.Getenv("DOCKER_CLOUD_NAMESPACE")
}
}
/*
* Main block
*/
func main() {
initDockerCloud()
groups := strings.Split(getEnv("AWS_SG_ID"), ",")
for i := 0; i < len(groups); i++ {
modifySecurityGroup(groups[i], getNodeIps())
}
listenToEvents()
}