Browse Source

Pull out hostnames more nicely

master
Chris Smith 5 years ago
parent
commit
980e19b59d
3 changed files with 47 additions and 5 deletions
  1. 44
    1
      dotege.go
  2. 1
    0
      template_generator.go
  3. 2
    4
      templates/domains.txt.tpl

+ 44
- 1
dotege.go View File

@@ -5,6 +5,7 @@ import (
5 5
 	"github.com/docker/docker/client"
6 6
 	"os"
7 7
 	"os/signal"
8
+	"strings"
8 9
 	"syscall"
9 10
 	"time"
10 11
 )
@@ -15,6 +16,16 @@ type Container struct {
15 16
 	Labels map[string]string
16 17
 }
17 18
 
19
+type LabelConfig struct {
20
+	Hostnames string
21
+}
22
+
23
+type Hostname struct {
24
+	Name         string
25
+	Alternatives map[string]bool
26
+	Containers   []Container
27
+}
28
+
18 29
 func monitorSignals() <-chan bool {
19 30
 	signals := make(chan os.Signal, 1)
20 31
 	done := make(chan bool, 1)
@@ -34,6 +45,9 @@ func main() {
34 45
 	done := monitorSignals()
35 46
 	containerChan := make(chan Container, 1)
36 47
 	expiryChan := make(chan string, 1)
48
+	labelConfig := LabelConfig{
49
+		Hostnames: "com.chameth.vhost",
50
+	}
37 51
 
38 52
 	cli, err := client.NewEnvClient()
39 53
 	if err != nil {
@@ -63,7 +77,10 @@ func main() {
63 77
 				delete(containers, name)
64 78
 				timer.Reset(100 * time.Millisecond)
65 79
 			case <-timer.C:
66
-				templateGenerator.Generate(Context{Containers: containers})
80
+				templateGenerator.Generate(Context{
81
+					Containers: containers,
82
+					Hostnames:  getHostnames(containers, labelConfig),
83
+				})
67 84
 			}
68 85
 		}
69 86
 	}()
@@ -75,3 +92,29 @@ func main() {
75 92
 		panic(err)
76 93
 	}
77 94
 }
95
+
96
+func getHostnames(containers map[string]Container, config LabelConfig) (hostnames map[string]Hostname) {
97
+	hostnames = make(map[string]Hostname)
98
+	for _, container := range containers {
99
+		if label, ok := container.Labels[config.Hostnames]; ok {
100
+			names := strings.Split(strings.Replace(label, ",", " ", -1), " ")
101
+			if hostname, ok := hostnames[names[0]]; ok {
102
+				hostname.Containers = append(hostname.Containers, container)
103
+			} else {
104
+				hostnames[names[0]] = Hostname{
105
+					Name:         names[0],
106
+					Alternatives: make(map[string]bool),
107
+					Containers:   []Container{container},
108
+				}
109
+			}
110
+			addAlternatives(hostnames[names[0]], names[1:])
111
+		}
112
+	}
113
+	return
114
+}
115
+
116
+func addAlternatives(hostname Hostname, alternatives []string) {
117
+	for _, alternative := range alternatives {
118
+		hostname.Alternatives[alternative] = true
119
+	}
120
+}

+ 1
- 0
template_generator.go View File

@@ -11,6 +11,7 @@ import (
11 11
 
12 12
 type Context struct {
13 13
 	Containers map[string]Container
14
+	Hostnames map[string]Hostname
14 15
 }
15 16
 
16 17
 type TemplateConfig struct {

+ 2
- 4
templates/domains.txt.tpl View File

@@ -1,5 +1,3 @@
1
-{{ range .Containers }}
2
-    {{- with index .Labels "com.chameth.vhost" -}}
3
-        {{ . | replace "," " " }}{{ "\n" }}
4
-    {{- end -}}
1
+{{ range .Hostnames }}
2
+    {{- .Name }}{{ range $san, $true := .Alternatives }} {{ $san }}{{ end }}
5 3
 {{ end }}

Loading…
Cancel
Save