Skip to content

Commit cb69dac

Browse files
committed
Merge pull request #146 from jwilder/jw-prs
Updates for new docker APIs
2 parents baf8e2c + df69486 commit cb69dac

17 files changed

+789
-624
lines changed

GLOCKFILE

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
github.com/BurntSushi/toml f87ce853111478914f0bcffa34d43a93643e6eda
2-
github.com/Sirupsen/logrus 6ebb4e7b3c24b9fef150d7693e728cb1ebadf1f5
3-
github.com/docker/docker 2606a2e4d3bf810ec82e373a6cd334e22e504e83
4-
github.com/fsouza/go-dockerclient 54afc1babbc57f075f958f84904251332bd8fd73
1+
github.com/BurntSushi/toml 056c9bc7be7190eaa7715723883caffa5f8fa3e4
2+
github.com/fsouza/go-dockerclient e0d22d30691bcc996eca51f729a4777b8c7dc2a8

Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ all: docker-gen
88

99
docker-gen:
1010
echo "Building docker-gen"
11-
go build -ldflags "$(LDFLAGS)"
11+
go build -ldflags "$(LDFLAGS)" ./cmd/docker-gen
1212

1313
dist-clean:
1414
rm -rf dist
1515
rm -f docker-gen-linux-*.tar.gz
1616
rm -f docker-gen-darwin-*.tar.gz
1717

1818
dist: dist-clean
19-
mkdir -p dist/linux/amd64 && GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o dist/linux/amd64/docker-gen
20-
mkdir -p dist/linux/i386 && GOOS=linux GOARCH=386 go build -ldflags "$(LDFLAGS)" -o dist/linux/i386/docker-gen
21-
mkdir -p dist/linux/armel && GOOS=linux GOARCH=arm GOARM=5 go build -ldflags "$(LDFLAGS)" -o dist/linux/armel/docker-gen
22-
mkdir -p dist/linux/armhf && GOOS=linux GOARCH=arm GOARM=6 go build -ldflags "$(LDFLAGS)" -o dist/linux/armhf/docker-gen
23-
mkdir -p dist/darwin/amd64 && GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o dist/darwin/amd64/docker-gen
24-
mkdir -p dist/darwin/i386 && GOOS=darwin GOARCH=386 go build -ldflags "$(LDFLAGS)" -o dist/darwin/i386/docker-gen
19+
mkdir -p dist/linux/amd64 && GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o dist/linux/amd64/docker-gen ./cmd/docker-gen
20+
mkdir -p dist/linux/i386 && GOOS=linux GOARCH=386 go build -ldflags "$(LDFLAGS)" -o dist/linux/i386/docker-gen ./cmd/docker-gen
21+
mkdir -p dist/linux/armel && GOOS=linux GOARCH=arm GOARM=5 go build -ldflags "$(LDFLAGS)" -o dist/linux/armel/docker-gen ./cmd/docker-gen
22+
mkdir -p dist/linux/armhf && GOOS=linux GOARCH=arm GOARM=6 go build -ldflags "$(LDFLAGS)" -o dist/linux/armhf/docker-gen ./cmd/docker-gen
23+
mkdir -p dist/darwin/amd64 && GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o dist/darwin/amd64/docker-gen ./cmd/docker-gen
24+
mkdir -p dist/darwin/i386 && GOOS=darwin GOARCH=386 go build -ldflags "$(LDFLAGS)" -o dist/darwin/i386/docker-gen ./cmd/docker-gen
2525

2626

2727
release: dist

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ type RuntimeContainer struct {
211211
IP string
212212
IP6LinkLocal string
213213
IP6Global string
214+
Mounts []Mount
214215
}
215216

216217
type Address struct {
@@ -241,6 +242,15 @@ type DockerImage struct {
241242
Tag string
242243
}
243244

245+
type Mount struct {
246+
Name string
247+
Source string
248+
Destination string
249+
Driver string
250+
Mode string
251+
RW bool
252+
}
253+
244254
type Volume struct {
245255
Path string
246256
HostPath string
@@ -252,6 +262,21 @@ type SwarmNode struct {
252262
Name string
253263
Address Address
254264
}
265+
266+
// Accessible from the root in templates as .Docker
267+
type Docker struct {
268+
Name string
269+
NumContainers int
270+
NumImages int
271+
Version string
272+
ApiVersion string
273+
GoVersion string
274+
OperatingSystem string
275+
Architecture string
276+
}
277+
278+
// Host environment variables accessible from root in templates as .Env
279+
255280
```
256281

257282
For example, this is a JSON version of an emitted RuntimeContainer struct:

cmd/docker-gen/main.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"sync"
10+
11+
"github.com/BurntSushi/toml"
12+
docker "github.com/fsouza/go-dockerclient"
13+
"github.com/jwilder/docker-gen"
14+
)
15+
16+
type stringslice []string
17+
18+
var (
19+
buildVersion string
20+
version bool
21+
watch bool
22+
notifyCmd string
23+
notifyOutput bool
24+
notifySigHUPContainerID string
25+
onlyExposed bool
26+
onlyPublished bool
27+
configFiles stringslice
28+
configs dockergen.ConfigFile
29+
interval int
30+
keepBlankLines bool
31+
endpoint string
32+
tlsCert string
33+
tlsKey string
34+
tlsCaCert string
35+
tlsVerify bool
36+
tlsCertPath string
37+
wg sync.WaitGroup
38+
)
39+
40+
func (strings *stringslice) String() string {
41+
return "[]"
42+
}
43+
44+
func (strings *stringslice) Set(value string) error {
45+
// TODO: Throw an error for duplicate `dest`
46+
*strings = append(*strings, value)
47+
return nil
48+
}
49+
50+
func usage() {
51+
println(`Usage: docker-gen [options] template [dest]
52+
53+
Generate files from docker container meta-data
54+
55+
Options:`)
56+
flag.PrintDefaults()
57+
58+
println(`
59+
Arguments:
60+
template - path to a template to generate
61+
dest - path to a write the template. If not specfied, STDOUT is used`)
62+
63+
println(`
64+
Environment Variables:
65+
DOCKER_HOST - default value for -endpoint
66+
DOCKER_CERT_PATH - directory path containing key.pem, cert.pem and ca.pem
67+
DOCKER_TLS_VERIFY - enable client TLS verification
68+
`)
69+
println(`For more information, see https://github.com/jwilder/docker-gen`)
70+
}
71+
72+
func loadConfig(file string) error {
73+
_, err := toml.DecodeFile(file, &configs)
74+
if err != nil {
75+
return err
76+
}
77+
return nil
78+
}
79+
80+
func initFlags() {
81+
82+
certPath := filepath.Join(os.Getenv("DOCKER_CERT_PATH"))
83+
if certPath == "" {
84+
certPath = filepath.Join(os.Getenv("HOME"), ".docker")
85+
}
86+
flag.BoolVar(&version, "version", false, "show version")
87+
flag.BoolVar(&watch, "watch", false, "watch for container changes")
88+
flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports")
89+
90+
flag.BoolVar(&onlyPublished, "only-published", false,
91+
"only include containers with published ports (implies -only-exposed)")
92+
flag.BoolVar(&notifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command")
93+
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
94+
flag.StringVar(&notifySigHUPContainerID, "notify-sighup", "",
95+
"send HUP signal to container. Equivalent to `docker kill -s HUP container-ID`")
96+
flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.")
97+
flag.IntVar(&interval, "interval", 0, "notify command interval (secs)")
98+
flag.BoolVar(&keepBlankLines, "keep-blank-lines", false, "keep blank lines in the output file")
99+
flag.StringVar(&endpoint, "endpoint", "", "docker api endpoint (tcp|unix://..). Default unix:///var/run/docker.sock")
100+
flag.StringVar(&tlsCert, "tlscert", filepath.Join(certPath, "cert.pem"), "path to TLS client certificate file")
101+
flag.StringVar(&tlsKey, "tlskey", filepath.Join(certPath, "key.pem"), "path to TLS client key file")
102+
flag.StringVar(&tlsCaCert, "tlscacert", filepath.Join(certPath, "ca.pem"), "path to TLS CA certificate file")
103+
flag.BoolVar(&tlsVerify, "tlsverify", os.Getenv("DOCKER_TLS_VERIFY") != "", "verify docker daemon's TLS certicate")
104+
105+
flag.Usage = usage
106+
flag.Parse()
107+
}
108+
109+
func main() {
110+
initFlags()
111+
112+
if version {
113+
fmt.Println(buildVersion)
114+
return
115+
}
116+
117+
if flag.NArg() < 1 && len(configFiles) == 0 {
118+
usage()
119+
os.Exit(1)
120+
}
121+
122+
if len(configFiles) > 0 {
123+
for _, configFile := range configFiles {
124+
err := loadConfig(configFile)
125+
if err != nil {
126+
log.Fatalf("error loading config %s: %s\n", configFile, err)
127+
}
128+
}
129+
} else {
130+
config := dockergen.Config{
131+
Template: flag.Arg(0),
132+
Dest: flag.Arg(1),
133+
Watch: watch,
134+
NotifyCmd: notifyCmd,
135+
NotifyOutput: notifyOutput,
136+
NotifyContainers: make(map[string]docker.Signal),
137+
OnlyExposed: onlyExposed,
138+
OnlyPublished: onlyPublished,
139+
Interval: interval,
140+
KeepBlankLines: keepBlankLines,
141+
}
142+
if notifySigHUPContainerID != "" {
143+
config.NotifyContainers[notifySigHUPContainerID] = docker.SIGHUP
144+
}
145+
configs = dockergen.ConfigFile{
146+
Config: []dockergen.Config{config}}
147+
}
148+
149+
generator, err := dockergen.NewGenerator(dockergen.GeneratorConfig{
150+
Endpoint: endpoint,
151+
TLSKey: tlsKey,
152+
TLSCert: tlsCert,
153+
TLSCACert: tlsCaCert,
154+
TLSVerify: tlsVerify,
155+
ConfigFile: configs,
156+
})
157+
158+
if err != nil {
159+
log.Fatalf("error creating generator: %v", err)
160+
}
161+
162+
if err := generator.Generate(); err != nil {
163+
log.Fatalf("error running generate: %v", err)
164+
}
165+
}

config.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dockergen
2+
3+
import "github.com/fsouza/go-dockerclient"
4+
5+
type Config struct {
6+
Template string
7+
Dest string
8+
Watch bool
9+
NotifyCmd string
10+
NotifyOutput bool
11+
NotifyContainers map[string]docker.Signal
12+
OnlyExposed bool
13+
OnlyPublished bool
14+
Interval int
15+
KeepBlankLines bool
16+
}
17+
18+
type ConfigFile struct {
19+
Config []Config
20+
}
21+
22+
func (c *ConfigFile) FilterWatches() ConfigFile {
23+
configWithWatches := []Config{}
24+
25+
for _, config := range c.Config {
26+
if config.Watch {
27+
configWithWatches = append(configWithWatches, config)
28+
}
29+
}
30+
return ConfigFile{
31+
Config: configWithWatches,
32+
}
33+
}

0 commit comments

Comments
 (0)