@@ -14,6 +14,7 @@ import (
1414
1515 "github.com/BurntSushi/toml"
1616 docker "github.com/fsouza/go-dockerclient"
17+ "github.com/jwilder/docker-gen"
1718)
1819
1920type stringslice []string
2829 onlyExposed bool
2930 onlyPublished bool
3031 configFiles stringslice
31- configs ConfigFile
32+ configs dockergen. ConfigFile
3233 interval int
3334 keepBlankLines bool
3435 endpoint string
@@ -46,63 +47,6 @@ type Event struct {
4647 Image string `json:"from"`
4748}
4849
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-
10650func (strings * stringslice ) String () string {
10751 return "[]"
10852}
@@ -113,67 +57,6 @@ func (strings *stringslice) Set(value string) error {
11357 return nil
11458}
11559
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-
17760func usage () {
17861 println (`Usage: docker-gen [options] template [dest]
17962
@@ -221,13 +104,13 @@ func NewDockerClient(endpoint string) (*docker.Client, error) {
221104}
222105
223106func generateFromContainers (client * docker.Client ) {
224- containers , err := getContainers (client )
107+ containers , err := dockergen . GetContainers (client )
225108 if err != nil {
226109 log .Printf ("error listing containers: %s\n " , err )
227110 return
228111 }
229112 for _ , config := range configs .Config {
230- changed := generateFile (config , containers )
113+ changed := dockergen . GenerateFile (config , containers )
231114 if ! changed {
232115 log .Printf ("Contents of %s did not change. Skipping notification '%s'" , config .Dest , config .NotifyCmd )
233116 continue
@@ -237,7 +120,7 @@ func generateFromContainers(client *docker.Client) {
237120 }
238121}
239122
240- func runNotifyCmd (config Config ) {
123+ func runNotifyCmd (config dockergen. Config ) {
241124 if config .NotifyCmd == "" {
242125 return
243126 }
@@ -257,7 +140,7 @@ func runNotifyCmd(config Config) {
257140 }
258141}
259142
260- func sendSignalToContainer (client * docker.Client , config Config ) {
143+ func sendSignalToContainer (client * docker.Client , config dockergen. Config ) {
261144 if len (config .NotifyContainers ) < 1 {
262145 return
263146 }
@@ -282,7 +165,7 @@ func loadConfig(file string) error {
282165 return nil
283166}
284167
285- func generateAtInterval (client * docker.Client , configs ConfigFile ) {
168+ func generateAtInterval (client * docker.Client , configs dockergen. ConfigFile ) {
286169 for _ , config := range configs .Config {
287170
288171 if config .Interval == 0 {
@@ -299,13 +182,13 @@ func generateAtInterval(client *docker.Client, configs ConfigFile) {
299182 for {
300183 select {
301184 case <- ticker .C :
302- containers , err := getContainers (client )
185+ containers , err := dockergen . GetContainers (client )
303186 if err != nil {
304187 log .Printf ("Error listing containers: %s\n " , err )
305188 continue
306189 }
307190 // ignore changed return value. always run notify command
308- generateFile (configCopy , containers )
191+ dockergen . GenerateFile (configCopy , containers )
309192 runNotifyCmd (configCopy )
310193 sendSignalToContainer (client , configCopy )
311194 case <- quit :
@@ -317,8 +200,8 @@ func generateAtInterval(client *docker.Client, configs ConfigFile) {
317200 }
318201}
319202
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 ()
322205 if len (configs .Config ) == 0 {
323206 return
324207 }
@@ -329,7 +212,7 @@ func generateFromEvents(client *docker.Client, configs ConfigFile) {
329212 for {
330213 if client == nil {
331214 var err error
332- endpoint , err := getEndpoint ( )
215+ endpoint , err := dockergen . GetEndpoint ( endpoint )
333216 if err != nil {
334217 log .Printf ("Bad endpoint: %s" , err )
335218 time .Sleep (10 * time .Second )
@@ -452,7 +335,7 @@ func main() {
452335 }
453336 }
454337 } else {
455- config := Config {
338+ config := dockergen. Config {
456339 Template : flag .Arg (0 ),
457340 Dest : flag .Arg (1 ),
458341 Watch : watch ,
@@ -467,11 +350,11 @@ func main() {
467350 if notifySigHUPContainerID != "" {
468351 config .NotifyContainers [notifySigHUPContainerID ] = docker .SIGHUP
469352 }
470- configs = ConfigFile {
471- Config : []Config {config }}
353+ configs = dockergen. ConfigFile {
354+ Config : []dockergen. Config {config }}
472355 }
473356
474- endpoint , err := getEndpoint ( )
357+ endpoint , err := dockergen . GetEndpoint ( endpoint )
475358 if err != nil {
476359 log .Fatalf ("Bad endpoint: %s" , err )
477360 }
@@ -486,3 +369,15 @@ func main() {
486369 generateFromEvents (client , configs )
487370 wg .Wait ()
488371}
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