|
@@ -2,6 +2,8 @@ package main
|
2
|
2
|
|
3
|
3
|
import (
|
4
|
4
|
"fmt"
|
|
5
|
+ "github.com/csmith/dotege/docker"
|
|
6
|
+ "github.com/csmith/dotege/model"
|
5
|
7
|
"github.com/docker/docker/client"
|
6
|
8
|
"go.uber.org/zap"
|
7
|
9
|
"go.uber.org/zap/zapcore"
|
|
@@ -12,27 +14,6 @@ import (
|
12
|
14
|
"time"
|
13
|
15
|
)
|
14
|
16
|
|
15
|
|
-type Container struct {
|
16
|
|
- Id string
|
17
|
|
- Name string
|
18
|
|
- Labels map[string]string
|
19
|
|
-}
|
20
|
|
-
|
21
|
|
-type LabelConfig struct {
|
22
|
|
- Hostnames string
|
23
|
|
-}
|
24
|
|
-
|
25
|
|
-type Hostname struct {
|
26
|
|
- Name string
|
27
|
|
- Alternatives map[string]bool
|
28
|
|
- Containers []Container
|
29
|
|
-}
|
30
|
|
-
|
31
|
|
-type Config struct {
|
32
|
|
- Templates []TemplateConfig
|
33
|
|
- Labels LabelConfig
|
34
|
|
-}
|
35
|
|
-
|
36
|
17
|
func monitorSignals() <-chan bool {
|
37
|
18
|
signals := make(chan os.Signal, 1)
|
38
|
19
|
done := make(chan bool, 1)
|
|
@@ -59,9 +40,9 @@ func main() {
|
59
|
40
|
sugar.Info("Dotege is starting")
|
60
|
41
|
|
61
|
42
|
done := monitorSignals()
|
62
|
|
- containerChan := make(chan Container, 1)
|
|
43
|
+ containerChan := make(chan model.Container, 1)
|
63
|
44
|
expiryChan := make(chan string, 1)
|
64
|
|
- labelConfig := LabelConfig{
|
|
45
|
+ labelConfig := model.LabelConfig{
|
65
|
46
|
Hostnames: "com.chameth.vhost",
|
66
|
47
|
}
|
67
|
48
|
|
|
@@ -71,20 +52,20 @@ func main() {
|
71
|
52
|
}
|
72
|
53
|
|
73
|
54
|
templateGenerator := NewTemplateGenerator(sugar)
|
74
|
|
- templateGenerator.AddTemplate(TemplateConfig{
|
|
55
|
+ templateGenerator.AddTemplate(model.TemplateConfig{
|
75
|
56
|
Source: "./templates/domains.txt.tpl",
|
76
|
57
|
Destination: "domains.txt",
|
77
|
58
|
})
|
78
|
|
- templateGenerator.AddTemplate(TemplateConfig{
|
|
59
|
+ templateGenerator.AddTemplate(model.TemplateConfig{
|
79
|
60
|
Source: "./templates/haproxy.cfg.tpl",
|
80
|
61
|
Destination: "haproxy.cfg",
|
81
|
62
|
})
|
82
|
63
|
|
83
|
|
- monitor := NewContainerMonitor(sugar, cli, containerChan, expiryChan)
|
|
64
|
+ monitor := docker.NewContainerMonitor(sugar, cli, containerChan, expiryChan)
|
84
|
65
|
go monitor.Monitor()
|
85
|
66
|
|
86
|
67
|
go func() {
|
87
|
|
- containers := make(map[string]Container)
|
|
68
|
+ containers := make(map[string]model.Container)
|
88
|
69
|
timer := time.NewTimer(time.Hour)
|
89
|
70
|
timer.Stop()
|
90
|
71
|
|
|
@@ -113,18 +94,18 @@ func main() {
|
113
|
94
|
}
|
114
|
95
|
}
|
115
|
96
|
|
116
|
|
-func getHostnames(containers map[string]Container, config LabelConfig) (hostnames map[string]Hostname) {
|
117
|
|
- hostnames = make(map[string]Hostname)
|
|
97
|
+func getHostnames(containers map[string]model.Container, config model.LabelConfig) (hostnames map[string]model.Hostname) {
|
|
98
|
+ hostnames = make(map[string]model.Hostname)
|
118
|
99
|
for _, container := range containers {
|
119
|
100
|
if label, ok := container.Labels[config.Hostnames]; ok {
|
120
|
101
|
names := strings.Split(strings.Replace(label, ",", " ", -1), " ")
|
121
|
102
|
if hostname, ok := hostnames[names[0]]; ok {
|
122
|
103
|
hostname.Containers = append(hostname.Containers, container)
|
123
|
104
|
} else {
|
124
|
|
- hostnames[names[0]] = Hostname{
|
|
105
|
+ hostnames[names[0]] = model.Hostname{
|
125
|
106
|
Name: names[0],
|
126
|
107
|
Alternatives: make(map[string]bool),
|
127
|
|
- Containers: []Container{container},
|
|
108
|
+ Containers: []model.Container{container},
|
128
|
109
|
}
|
129
|
110
|
}
|
130
|
111
|
addAlternatives(hostnames[names[0]], names[1:])
|
|
@@ -133,7 +114,7 @@ func getHostnames(containers map[string]Container, config LabelConfig) (hostname
|
133
|
114
|
return
|
134
|
115
|
}
|
135
|
116
|
|
136
|
|
-func addAlternatives(hostname Hostname, alternatives []string) {
|
|
117
|
+func addAlternatives(hostname model.Hostname, alternatives []string) {
|
137
|
118
|
for _, alternative := range alternatives {
|
138
|
119
|
hostname.Alternatives[alternative] = true
|
139
|
120
|
}
|