Skip to content

Commit 940fd42

Browse files
committed
Add service registration w/ etcd example
1 parent 895f883 commit 940fd42

File tree

5 files changed

+176
-8
lines changed

5 files changed

+176
-8
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This is can be used to generate config files for:
1919
docker-gen
2020
Usage: docker-gen [-config file] [-watch=false] [-notify="restart xyz"] <template> [<dest>]
2121
```
22-
22+
2323
*Options:*
2424
* `-watch` - runs continuously and monitors docker container events. When containers are started
2525
or stopped, the template is regenerated.
@@ -29,14 +29,23 @@ haproxy, etc..
2929

3030
If no `<dest>` file is specified, the output is send to stdout. Mainly useful for debugging.
3131

32-
### Examples
32+
#### NGINX Reverse Proxy Config
3333

34-
[Docker Log Management With Fluentd](http://jasonwilder.com/blog/2014/03/17/docker-log-management-using-fluentd/)
34+
`docker-gen -only-exposed -watch -notify "/etc/init.d/nginx reload" templates/nginx.tmpl /etc/nginx/sites-enabled/default`
3535

3636
[Automated Nginx Reverse Proxy for Docker](http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/)
3737

38+
#### Fluentd Log Management
39+
40+
`docker-gen -watch -notify "restart fluentd" templates/fluentd.tmpl /etc/fluent/fluent.conf`
41+
42+
[Docker Log Management With Fluentd](http://jasonwilder.com/blog/2014/03/17/docker-log-management-using-fluentd/)
43+
44+
#### Register Containers in Etcd
45+
46+
`docker-gen -notify "/bin/bash /tmp/etcd.sh" -interval 10 templates/etcd.tmpl /tmp/etcd.sh`
47+
3848

3949
### TODO
4050

4151
* Add a way to filter out containers in templates
42-
* Add a notify interval option

docker-gen.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,49 @@ type Address struct {
4242
type RuntimeContainer struct {
4343
ID string
4444
Addresses []Address
45-
Image string
45+
Image DockerImage
4646
Env map[string]string
4747
}
4848

49+
type DockerImage struct {
50+
Registry string
51+
Repository string
52+
Tag string
53+
}
54+
55+
func (i *DockerImage) String() string {
56+
ret := i.Repository
57+
if i.Registry != "" {
58+
ret = i.Registry + "/" + i.Repository
59+
}
60+
if i.Tag != "" {
61+
ret = ret + ":" + i.Tag
62+
}
63+
return ret
64+
}
65+
66+
func splitDockerImage(img string) (string, string, string) {
67+
68+
index := 0
69+
repository := img
70+
var registry, tag string
71+
if strings.Contains(img, "/") {
72+
separator := strings.Index(img, "/")
73+
registry = img[index:separator]
74+
index = separator + 1
75+
repository = img[index:]
76+
}
77+
78+
if strings.Contains(img, ":") {
79+
separator := strings.Index(img, ":")
80+
repository = img[index:separator]
81+
index = separator + 1
82+
tag = img[index:]
83+
}
84+
85+
return registry, repository, tag
86+
}
87+
4988
type Config struct {
5089
Template string
5190
Dest string
@@ -161,9 +200,14 @@ func getContainers(client *docker.Client) ([]*RuntimeContainer, error) {
161200
continue
162201
}
163202

203+
registry, repository, tag := splitDockerImage(container.Config.Image)
164204
runtimeContainer := &RuntimeContainer{
165-
ID: container.ID,
166-
Image: container.Config.Image,
205+
ID: container.ID,
206+
Image: DockerImage{
207+
Registry: registry,
208+
Repository: repository,
209+
Tag: tag,
210+
},
167211
Addresses: []Address{},
168212
Env: make(map[string]string),
169213
}

docker-gen_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSplitDockerImageRepository(t *testing.T) {
8+
registry, repository, tag := splitDockerImage("ubuntu")
9+
10+
if registry != "" {
11+
t.Fail()
12+
}
13+
if repository != "ubuntu" {
14+
t.Fail()
15+
}
16+
if tag != "" {
17+
t.Fail()
18+
}
19+
20+
dockerImage := DockerImage{
21+
Registry: registry,
22+
Repository: repository,
23+
Tag: tag,
24+
}
25+
if "ubuntu" != dockerImage.String() {
26+
t.Fail()
27+
}
28+
}
29+
30+
func TestSplitDockerImageWithRegistry(t *testing.T) {
31+
registry, repository, tag := splitDockerImage("custom.registry/ubuntu")
32+
33+
if registry != "custom.registry" {
34+
t.Fail()
35+
}
36+
if repository != "ubuntu" {
37+
t.Fail()
38+
}
39+
if tag != "" {
40+
t.Fail()
41+
}
42+
dockerImage := DockerImage{
43+
Registry: registry,
44+
Repository: repository,
45+
Tag: tag,
46+
}
47+
if "custom.registry/ubuntu" != dockerImage.String() {
48+
t.Fail()
49+
}
50+
51+
}
52+
53+
func TestSplitDockerImageWithRegistryAndTag(t *testing.T) {
54+
registry, repository, tag := splitDockerImage("custom.registry/ubuntu:12.04")
55+
56+
if registry != "custom.registry" {
57+
t.Fail()
58+
}
59+
if repository != "ubuntu" {
60+
t.Fail()
61+
}
62+
if tag != "12.04" {
63+
t.Fail()
64+
}
65+
dockerImage := DockerImage{
66+
Registry: registry,
67+
Repository: repository,
68+
Tag: tag,
69+
}
70+
if "custom.registry/ubuntu:12.04" != dockerImage.String() {
71+
t.Fail()
72+
}
73+
74+
}
75+
76+
func TestSplitDockerImageWithRepositoryAndTag(t *testing.T) {
77+
registry, repository, tag := splitDockerImage("ubuntu:12.04")
78+
79+
if registry != "" {
80+
t.Fail()
81+
}
82+
83+
if repository != "ubuntu" {
84+
t.Fail()
85+
}
86+
87+
if tag != "12.04" {
88+
t.Fail()
89+
}
90+
dockerImage := DockerImage{
91+
Registry: registry,
92+
Repository: repository,
93+
Tag: tag,
94+
}
95+
if "ubuntu:12.04" != dockerImage.String() {
96+
t.Fail()
97+
}
98+
99+
}

example.conf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ notifycmd = "/etc/init.d/nginx reload"
88
template = "templates/fluentd.conf.tmpl"
99
dest = "/tmp/fluentd.conf"
1010
watch = true
11-
notifycmd = "echo test"
11+
notifycmd = "echo test"
12+
13+
[[config]]
14+
template = "templates/etcd.tmpl"
15+
dest = "/tmp/etcd.sh"
16+
watch = true
17+
notifycmd = "/bin/bash /tmp/etcd.sh"
18+
interval = 10

templates/etcd.tmpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
#!/bin/bash
3+
4+
5+
{{range $key, $value := .}}
6+
{{ with $address := index $value.Addresses 0 }}
7+
curl -L http://127.0.0.1:4001/v2/keys/backends/{{ $value.Image.Repository}}/{{printf "%.*s" 12 $value.ID}} -XPUT -d value="{{ $address.IP }}:{{ $address.Port }}" -d ttl=15
8+
{{ end }}
9+
{{end}}

0 commit comments

Comments
 (0)