Browse Source

Begin refactoring into packages

master
Chris Smith 5 years ago
parent
commit
8eb3db4442
6 changed files with 64 additions and 48 deletions
  1. 0
    1
      .pre-commit-config.yaml
  2. 1
    0
      certificate_manager.go
  3. 12
    5
      docker/monitor.go
  4. 13
    32
      dotege.go
  5. 32
    0
      model/model.go
  6. 6
    10
      template_generator.go

+ 0
- 1
.pre-commit-config.yaml View File

9
     rev: v1.1.6
9
     rev: v1.1.6
10
     hooks:
10
     hooks:
11
       - id: remove-crlf
11
       - id: remove-crlf
12
-      - id: forbid-tabs
13
   - repo: https://github.com/dnephin/pre-commit-golang
12
   - repo: https://github.com/dnephin/pre-commit-golang
14
     rev: v0.3.2
13
     rev: v0.3.2
15
     hooks:
14
     hooks:

+ 1
- 0
certificate_manager.go View File

1
+package main

container_monitor.go → docker/monitor.go View File

1
-package main
1
+package docker
2
 
2
 
3
 import (
3
 import (
4
 	"context"
4
 	"context"
5
+	"github.com/csmith/dotege/model"
5
 	"github.com/docker/docker/api/types"
6
 	"github.com/docker/docker/api/types"
6
 	"github.com/docker/docker/api/types/filters"
7
 	"github.com/docker/docker/api/types/filters"
7
 	"github.com/docker/docker/client"
8
 	"github.com/docker/docker/client"
9
 	"time"
10
 	"time"
10
 )
11
 )
11
 
12
 
13
+// ContainerMonitor watches for newly created and destroyed containers, and emits their information on a channel.
14
+// Destroyed containers are given a grace period before being announced, to allow for restarts etc to be less
15
+// disruptive.
12
 type ContainerMonitor struct {
16
 type ContainerMonitor struct {
13
 	logger             *zap.SugaredLogger
17
 	logger             *zap.SugaredLogger
14
-	newContainers      chan<- Container
18
+	newContainers      chan<- model.Container
15
 	goneContainerNames chan<- string
19
 	goneContainerNames chan<- string
16
 	client             *client.Client
20
 	client             *client.Client
17
 	expiryTimes        map[string]time.Time
21
 	expiryTimes        map[string]time.Time
20
 	expiryTimer        *time.Timer
24
 	expiryTimer        *time.Timer
21
 }
25
 }
22
 
26
 
23
-func NewContainerMonitor(logger *zap.SugaredLogger, client *client.Client, newContainerChannel chan<- Container, goneContainerChannel chan<- string) *ContainerMonitor {
27
+// NewContainerMonitor creates a new container monitor
28
+func NewContainerMonitor(logger *zap.SugaredLogger, client *client.Client, newContainerChannel chan<- model.Container, goneContainerChannel chan<- string) *ContainerMonitor {
24
 	timer := time.NewTimer(time.Hour)
29
 	timer := time.NewTimer(time.Hour)
25
 	timer.Stop()
30
 	timer.Stop()
26
 
31
 
36
 	}
41
 	}
37
 }
42
 }
38
 
43
 
44
+// Monitor starts monitoring for changes, and publishes info on any pre-existing containers. It blocks indefinitely,
45
+// and should be run from a goroutine.
39
 func (c *ContainerMonitor) Monitor() {
46
 func (c *ContainerMonitor) Monitor() {
40
 	args := filters.NewArgs()
47
 	args := filters.NewArgs()
41
 	args.Add("type", "container")
48
 	args.Add("type", "container")
71
 
78
 
72
 	for _, container := range containers {
79
 	for _, container := range containers {
73
 		c.logger.Infof("Found existing container %s", container.Names[0][1:])
80
 		c.logger.Infof("Found existing container %s", container.Names[0][1:])
74
-		c.newContainers <- Container{
81
+		c.newContainers <- model.Container{
75
 			Id:     container.ID,
82
 			Id:     container.ID,
76
 			Name:   container.Names[0][1:],
83
 			Name:   container.Names[0][1:],
77
 			Labels: container.Labels,
84
 			Labels: container.Labels,
84
 	if err != nil {
91
 	if err != nil {
85
 		c.logger.Fatal("Error received trying to inspect container", err)
92
 		c.logger.Fatal("Error received trying to inspect container", err)
86
 	}
93
 	}
87
-	c.newContainers <- Container{
94
+	c.newContainers <- model.Container{
88
 		Id:     container.ID,
95
 		Id:     container.ID,
89
 		Name:   container.Name[1:],
96
 		Name:   container.Name[1:],
90
 		Labels: container.Config.Labels,
97
 		Labels: container.Config.Labels,

+ 13
- 32
dotege.go View File

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

+ 32
- 0
model/model.go View File

1
+package model
2
+
3
+// Container models a docker container that is running on the system.
4
+type Container struct {
5
+	Id     string
6
+	Name   string
7
+	Labels map[string]string
8
+}
9
+
10
+// LabelConfig describes the labels used for various properties.
11
+type LabelConfig struct {
12
+	Hostnames string
13
+}
14
+
15
+// Hostname describes a DNS name used for proxying, retrieving certificates, etc.
16
+type Hostname struct {
17
+	Name         string
18
+	Alternatives map[string]bool
19
+	Containers   []Container
20
+}
21
+
22
+// Config is the user-definable configuration for Dotege.
23
+type Config struct {
24
+	Templates []TemplateConfig
25
+	Labels    LabelConfig
26
+}
27
+
28
+// TemplateConfig configures a single template for the generator.
29
+type TemplateConfig struct {
30
+	Source      string
31
+	Destination string
32
+}

+ 6
- 10
template_generator.go View File

1
 package main
1
 package main
2
 
2
 
3
 import (
3
 import (
4
+	"github.com/csmith/dotege/model"
4
 	"go.uber.org/zap"
5
 	"go.uber.org/zap"
5
 	"io/ioutil"
6
 	"io/ioutil"
6
 	"path"
7
 	"path"
10
 )
11
 )
11
 
12
 
12
 type Context struct {
13
 type Context struct {
13
-	Containers map[string]Container
14
-	Hostnames map[string]Hostname
15
-}
16
-
17
-type TemplateConfig struct {
18
-	Source      string
19
-	Destination string
14
+	Containers map[string]model.Container
15
+	Hostnames  map[string]model.Hostname
20
 }
16
 }
21
 
17
 
22
 type Template struct {
18
 type Template struct {
23
-	config   TemplateConfig
19
+	config   model.TemplateConfig
24
 	content  string
20
 	content  string
25
 	template *template.Template
21
 	template *template.Template
26
 }
22
 }
27
 
23
 
28
 type TemplateGenerator struct {
24
 type TemplateGenerator struct {
29
-	logger *zap.SugaredLogger
25
+	logger    *zap.SugaredLogger
30
 	templates []*Template
26
 	templates []*Template
31
 }
27
 }
32
 
28
 
47
 	}
43
 	}
48
 }
44
 }
49
 
45
 
50
-func (t *TemplateGenerator) AddTemplate(config TemplateConfig) {
46
+func (t *TemplateGenerator) AddTemplate(config model.TemplateConfig) {
51
 	t.logger.Infof("Adding template from %s, writing to %s", config.Source, config.Destination)
47
 	t.logger.Infof("Adding template from %s, writing to %s", config.Source, config.Destination)
52
 	tmpl, err := template.New(path.Base(config.Source)).Funcs(funcMap).ParseFiles(config.Source)
48
 	tmpl, err := template.New(path.Base(config.Source)).Funcs(funcMap).ParseFiles(config.Source)
53
 	if err != nil {
49
 	if err != nil {

Loading…
Cancel
Save