|
@@ -0,0 +1,180 @@
|
|
1
|
+package main
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "fmt"
|
|
5
|
+ "github.com/docker/distribution/context"
|
|
6
|
+ "github.com/docker/docker/api/types"
|
|
7
|
+ "github.com/docker/docker/api/types/filters"
|
|
8
|
+ "github.com/docker/docker/client"
|
|
9
|
+ "os"
|
|
10
|
+ "os/signal"
|
|
11
|
+ "sort"
|
|
12
|
+ "strings"
|
|
13
|
+ "syscall"
|
|
14
|
+ "text/template"
|
|
15
|
+ "time"
|
|
16
|
+)
|
|
17
|
+
|
|
18
|
+type Container struct {
|
|
19
|
+ Id string
|
|
20
|
+ Name string
|
|
21
|
+ Labels map[string]string
|
|
22
|
+}
|
|
23
|
+
|
|
24
|
+type Context struct {
|
|
25
|
+ Containers map[string]Container
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+var funcMap = template.FuncMap{
|
|
29
|
+ "replace": func(from, to, input string) string { return strings.Replace(input, from, to, -1) },
|
|
30
|
+ "split": func(sep, input string) []string { return strings.Split(input, sep) },
|
|
31
|
+ "join": func(sep string, input []string) string { return strings.Join(input, sep) },
|
|
32
|
+ "sortlines": func(input string) string {
|
|
33
|
+ lines := strings.Split(input, "\n")
|
|
34
|
+ sort.Strings(lines)
|
|
35
|
+ return strings.Join(lines, "\n")
|
|
36
|
+ },
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+func main() {
|
|
40
|
+ sigs := make(chan os.Signal, 1)
|
|
41
|
+ done := make(chan bool, 1)
|
|
42
|
+ containerChan := make(chan Container, 1)
|
|
43
|
+ expiryChan := make(chan string, 1)
|
|
44
|
+
|
|
45
|
+ signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
46
|
+
|
|
47
|
+ cli, err := client.NewEnvClient()
|
|
48
|
+ if err != nil {
|
|
49
|
+ panic(err)
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ go func() {
|
|
53
|
+ sig := <-sigs
|
|
54
|
+ fmt.Printf("Received %s signal\n", sig)
|
|
55
|
+ err := cli.Close()
|
|
56
|
+ if err != nil {
|
|
57
|
+ panic(err)
|
|
58
|
+ }
|
|
59
|
+ done <- true
|
|
60
|
+ }()
|
|
61
|
+
|
|
62
|
+ go func() {
|
|
63
|
+ const deletionTime = 10 * time.Second
|
|
64
|
+
|
|
65
|
+ expiryTimes := make(map[string]time.Time)
|
|
66
|
+ expiryTimer := time.NewTimer(time.Hour)
|
|
67
|
+ nextExpiry := time.Now()
|
|
68
|
+ expiryTimer.Stop()
|
|
69
|
+
|
|
70
|
+ args := filters.NewArgs()
|
|
71
|
+ args.Add("type", "container")
|
|
72
|
+ args.Add("event", "create")
|
|
73
|
+ args.Add("event", "destroy")
|
|
74
|
+ eventsChan, errChan := cli.Events(context.Background(), types.EventsOptions{Filters: args})
|
|
75
|
+
|
|
76
|
+ containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
|
|
77
|
+ if err != nil {
|
|
78
|
+ panic(err)
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ for _, container := range containers {
|
|
82
|
+ containerChan <- Container{
|
|
83
|
+ Id: container.ID,
|
|
84
|
+ Name: container.Names[0][1:],
|
|
85
|
+ Labels: container.Labels,
|
|
86
|
+ }
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ for {
|
|
90
|
+ select {
|
|
91
|
+ case event := <-eventsChan:
|
|
92
|
+ if event.Action == "create" {
|
|
93
|
+ container, err := cli.ContainerInspect(context.Background(), event.Actor.ID)
|
|
94
|
+ if err != nil {
|
|
95
|
+ panic(err)
|
|
96
|
+ }
|
|
97
|
+ containerChan <- Container{
|
|
98
|
+ Id: container.ID,
|
|
99
|
+ Name: container.Name[1:],
|
|
100
|
+ Labels: container.Config.Labels,
|
|
101
|
+ }
|
|
102
|
+ } else {
|
|
103
|
+ now := time.Now()
|
|
104
|
+ expiryTime := now.Add(deletionTime)
|
|
105
|
+ expiryTimes[event.Actor.Attributes["name"]] = expiryTime
|
|
106
|
+ fmt.Printf("Scheduling expiry timer for %s\n", event.Actor.Attributes["name"])
|
|
107
|
+ if nextExpiry.Before(now) || nextExpiry.After(expiryTime) {
|
|
108
|
+ fmt.Printf("Starting expiry timer with default duration\n")
|
|
109
|
+ expiryTimer.Reset(deletionTime + 1*time.Second)
|
|
110
|
+ nextExpiry = expiryTime
|
|
111
|
+ }
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ case <-expiryTimer.C:
|
|
115
|
+ now := time.Now()
|
|
116
|
+ next := 0 * time.Second
|
|
117
|
+
|
|
118
|
+ for name, expiryTime := range expiryTimes {
|
|
119
|
+ if expiryTime.Before(now) {
|
|
120
|
+ fmt.Printf("Expiring %s\n", name)
|
|
121
|
+ delete(expiryTimes, name)
|
|
122
|
+ expiryChan <- name
|
|
123
|
+ } else if next == 0 || expiryTime.Sub(now) < next {
|
|
124
|
+ next = expiryTime.Sub(now)
|
|
125
|
+ }
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ if next > 0 {
|
|
129
|
+ fmt.Printf("Starting expiry timer with duration %s\n", next)
|
|
130
|
+ expiryTimer.Reset(next + 1*time.Second)
|
|
131
|
+ nextExpiry = now.Add(next)
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ case err := <-errChan:
|
|
135
|
+ panic(err)
|
|
136
|
+ }
|
|
137
|
+ }
|
|
138
|
+ }()
|
|
139
|
+
|
|
140
|
+ go func() {
|
|
141
|
+ var templates []*template.Template
|
|
142
|
+ containers := make(map[string]Container)
|
|
143
|
+ timer := time.NewTimer(time.Hour)
|
|
144
|
+ timer.Stop()
|
|
145
|
+
|
|
146
|
+ tmpl, err := template.New("domains.txt.tpl").Funcs(funcMap).ParseFiles("./templates/domains.txt.tpl")
|
|
147
|
+ if err != nil {
|
|
148
|
+ panic(err)
|
|
149
|
+ }
|
|
150
|
+ templates = append(templates, tmpl)
|
|
151
|
+
|
|
152
|
+ tmpl, err = template.New("haproxy.cfg.tpl").Funcs(funcMap).ParseFiles("./templates/haproxy.cfg.tpl")
|
|
153
|
+ if err != nil {
|
|
154
|
+ panic(err)
|
|
155
|
+ }
|
|
156
|
+ templates = append(templates, tmpl)
|
|
157
|
+
|
|
158
|
+ for {
|
|
159
|
+ select {
|
|
160
|
+ case container := <-containerChan:
|
|
161
|
+ containers[container.Name] = container
|
|
162
|
+ timer.Reset(100 * time.Millisecond)
|
|
163
|
+ case name := <-expiryChan:
|
|
164
|
+ delete(containers, name)
|
|
165
|
+ timer.Reset(100 * time.Millisecond)
|
|
166
|
+ case <-timer.C:
|
|
167
|
+ for _, tmpl := range templates {
|
|
168
|
+ fmt.Printf("--- Writing %s ---\n", tmpl.Name())
|
|
169
|
+ err = tmpl.Execute(os.Stdout, Context{Containers: containers})
|
|
170
|
+ fmt.Printf("--- / writing %s ---\n", tmpl.Name())
|
|
171
|
+ if err != nil {
|
|
172
|
+ panic(err)
|
|
173
|
+ }
|
|
174
|
+ }
|
|
175
|
+ }
|
|
176
|
+ }
|
|
177
|
+ }()
|
|
178
|
+
|
|
179
|
+ <-done
|
|
180
|
+}
|