@@ -14,6 +14,7 @@ import (
14
14
15
15
"github.com/BurntSushi/toml"
16
16
docker "github.com/fsouza/go-dockerclient"
17
+ "github.com/jwilder/docker-gen"
17
18
)
18
19
19
20
type stringslice []string
28
29
onlyExposed bool
29
30
onlyPublished bool
30
31
configFiles stringslice
31
- configs ConfigFile
32
+ configs dockergen. ConfigFile
32
33
interval int
33
34
keepBlankLines bool
34
35
endpoint string
@@ -46,63 +47,6 @@ type Event struct {
46
47
Image string `json:"from"`
47
48
}
48
49
49
- type Address struct {
50
- IP string
51
- IP6LinkLocal string
52
- IP6Global string
53
- Port string
54
- HostPort string
55
- Proto string
56
- HostIP string
57
- }
58
-
59
- type Network struct {
60
- IP string
61
- Name string
62
- Gateway string
63
- EndpointID string
64
- IPv6Gateway string
65
- GlobalIPv6Address string
66
- MacAddress string
67
- GlobalIPv6PrefixLen int
68
- IPPrefixLen int
69
- }
70
-
71
- type Volume struct {
72
- Path string
73
- HostPath string
74
- ReadWrite bool
75
- }
76
-
77
- type RuntimeContainer struct {
78
- ID string
79
- Addresses []Address
80
- Networks []Network
81
- Gateway string
82
- Name string
83
- Hostname string
84
- Image DockerImage
85
- Env map [string ]string
86
- Volumes map [string ]Volume
87
- Node SwarmNode
88
- Labels map [string ]string
89
- IP string
90
- IP6LinkLocal string
91
- IP6Global string
92
- }
93
-
94
- type DockerImage struct {
95
- Registry string
96
- Repository string
97
- Tag string
98
- }
99
-
100
- type SwarmNode struct {
101
- ID string
102
- Name string
103
- Address Address
104
- }
105
-
106
50
func (strings * stringslice ) String () string {
107
51
return "[]"
108
52
}
@@ -113,67 +57,6 @@ func (strings *stringslice) Set(value string) error {
113
57
return nil
114
58
}
115
59
116
- func (i * DockerImage ) String () string {
117
- ret := i .Repository
118
- if i .Registry != "" {
119
- ret = i .Registry + "/" + i .Repository
120
- }
121
- if i .Tag != "" {
122
- ret = ret + ":" + i .Tag
123
- }
124
- return ret
125
- }
126
-
127
- type Config struct {
128
- Template string
129
- Dest string
130
- Watch bool
131
- NotifyCmd string
132
- NotifyOutput bool
133
- NotifyContainers map [string ]docker.Signal
134
- OnlyExposed bool
135
- OnlyPublished bool
136
- Interval int
137
- KeepBlankLines bool
138
- }
139
-
140
- type ConfigFile struct {
141
- Config []Config
142
- }
143
-
144
- type Context []* RuntimeContainer
145
-
146
- func (c * Context ) Env () map [string ]string {
147
- return splitKeyValueSlice (os .Environ ())
148
- }
149
-
150
- func (c * ConfigFile ) filterWatches () ConfigFile {
151
- configWithWatches := []Config {}
152
-
153
- for _ , config := range c .Config {
154
- if config .Watch {
155
- configWithWatches = append (configWithWatches , config )
156
- }
157
- }
158
- return ConfigFile {
159
- Config : configWithWatches ,
160
- }
161
- }
162
-
163
- func (r * RuntimeContainer ) Equals (o RuntimeContainer ) bool {
164
- return r .ID == o .ID && r .Image == o .Image
165
- }
166
-
167
- func (r * RuntimeContainer ) PublishedAddresses () []Address {
168
- mapped := []Address {}
169
- for _ , address := range r .Addresses {
170
- if address .HostPort != "" {
171
- mapped = append (mapped , address )
172
- }
173
- }
174
- return mapped
175
- }
176
-
177
60
func usage () {
178
61
println (`Usage: docker-gen [options] template [dest]
179
62
@@ -221,13 +104,13 @@ func NewDockerClient(endpoint string) (*docker.Client, error) {
221
104
}
222
105
223
106
func generateFromContainers (client * docker.Client ) {
224
- containers , err := getContainers (client )
107
+ containers , err := dockergen . GetContainers (client )
225
108
if err != nil {
226
109
log .Printf ("error listing containers: %s\n " , err )
227
110
return
228
111
}
229
112
for _ , config := range configs .Config {
230
- changed := generateFile (config , containers )
113
+ changed := dockergen . GenerateFile (config , containers )
231
114
if ! changed {
232
115
log .Printf ("Contents of %s did not change. Skipping notification '%s'" , config .Dest , config .NotifyCmd )
233
116
continue
@@ -237,7 +120,7 @@ func generateFromContainers(client *docker.Client) {
237
120
}
238
121
}
239
122
240
- func runNotifyCmd (config Config ) {
123
+ func runNotifyCmd (config dockergen. Config ) {
241
124
if config .NotifyCmd == "" {
242
125
return
243
126
}
@@ -257,7 +140,7 @@ func runNotifyCmd(config Config) {
257
140
}
258
141
}
259
142
260
- func sendSignalToContainer (client * docker.Client , config Config ) {
143
+ func sendSignalToContainer (client * docker.Client , config dockergen. Config ) {
261
144
if len (config .NotifyContainers ) < 1 {
262
145
return
263
146
}
@@ -282,7 +165,7 @@ func loadConfig(file string) error {
282
165
return nil
283
166
}
284
167
285
- func generateAtInterval (client * docker.Client , configs ConfigFile ) {
168
+ func generateAtInterval (client * docker.Client , configs dockergen. ConfigFile ) {
286
169
for _ , config := range configs .Config {
287
170
288
171
if config .Interval == 0 {
@@ -299,13 +182,13 @@ func generateAtInterval(client *docker.Client, configs ConfigFile) {
299
182
for {
300
183
select {
301
184
case <- ticker .C :
302
- containers , err := getContainers (client )
185
+ containers , err := dockergen . GetContainers (client )
303
186
if err != nil {
304
187
log .Printf ("Error listing containers: %s\n " , err )
305
188
continue
306
189
}
307
190
// ignore changed return value. always run notify command
308
- generateFile (configCopy , containers )
191
+ dockergen . GenerateFile (configCopy , containers )
309
192
runNotifyCmd (configCopy )
310
193
sendSignalToContainer (client , configCopy )
311
194
case <- quit :
@@ -317,8 +200,8 @@ func generateAtInterval(client *docker.Client, configs ConfigFile) {
317
200
}
318
201
}
319
202
320
- func generateFromEvents (client * docker.Client , configs ConfigFile ) {
321
- configs = configs .filterWatches ()
203
+ func generateFromEvents (client * docker.Client , configs dockergen. ConfigFile ) {
204
+ configs = configs .FilterWatches ()
322
205
if len (configs .Config ) == 0 {
323
206
return
324
207
}
@@ -329,7 +212,7 @@ func generateFromEvents(client *docker.Client, configs ConfigFile) {
329
212
for {
330
213
if client == nil {
331
214
var err error
332
- endpoint , err := getEndpoint ( )
215
+ endpoint , err := dockergen . GetEndpoint ( endpoint )
333
216
if err != nil {
334
217
log .Printf ("Bad endpoint: %s" , err )
335
218
time .Sleep (10 * time .Second )
@@ -452,7 +335,7 @@ func main() {
452
335
}
453
336
}
454
337
} else {
455
- config := Config {
338
+ config := dockergen. Config {
456
339
Template : flag .Arg (0 ),
457
340
Dest : flag .Arg (1 ),
458
341
Watch : watch ,
@@ -467,11 +350,11 @@ func main() {
467
350
if notifySigHUPContainerID != "" {
468
351
config .NotifyContainers [notifySigHUPContainerID ] = docker .SIGHUP
469
352
}
470
- configs = ConfigFile {
471
- Config : []Config {config }}
353
+ configs = dockergen. ConfigFile {
354
+ Config : []dockergen. Config {config }}
472
355
}
473
356
474
- endpoint , err := getEndpoint ( )
357
+ endpoint , err := dockergen . GetEndpoint ( endpoint )
475
358
if err != nil {
476
359
log .Fatalf ("Bad endpoint: %s" , err )
477
360
}
@@ -486,3 +369,15 @@ func main() {
486
369
generateFromEvents (client , configs )
487
370
wg .Wait ()
488
371
}
372
+
373
+ // pathExists returns whether the given file or directory exists or not
374
+ func pathExists (path string ) (bool , error ) {
375
+ _ , err := os .Stat (path )
376
+ if err == nil {
377
+ return true , nil
378
+ }
379
+ if os .IsNotExist (err ) {
380
+ return false , nil
381
+ }
382
+ return false , err
383
+ }
0 commit comments