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