@@ -15,17 +15,18 @@ import (
15
15
)
16
16
17
17
var (
18
- buildVersion string
19
- version bool
20
- watch bool
21
- notifyCmd string
22
- onlyExposed bool
23
- onlyPublished bool
24
- configFile string
25
- configs ConfigFile
26
- interval int
27
- endpoint string
28
- wg sync.WaitGroup
18
+ buildVersion string
19
+ version bool
20
+ watch bool
21
+ notifyCmd string
22
+ notifySigHUPContainerID string
23
+ onlyExposed bool
24
+ onlyPublished bool
25
+ configFile string
26
+ configs ConfigFile
27
+ interval int
28
+ endpoint string
29
+ wg sync.WaitGroup
29
30
)
30
31
31
32
type Event struct {
@@ -66,13 +67,14 @@ func (i *DockerImage) String() string {
66
67
}
67
68
68
69
type Config struct {
69
- Template string
70
- Dest string
71
- Watch bool
72
- NotifyCmd string
73
- OnlyExposed bool
74
- OnlyPublished bool
75
- Interval int
70
+ Template string
71
+ Dest string
72
+ Watch bool
73
+ NotifyCmd string
74
+ NotifyContainers []docker.KillContainerOptions
75
+ OnlyExposed bool
76
+ OnlyPublished bool
77
+ Interval int
76
78
}
77
79
78
80
type ConfigFile struct {
@@ -120,7 +122,7 @@ func (r *RuntimeContainer) PublishedAddresses() []Address {
120
122
}
121
123
122
124
func usage () {
123
- println ("Usage: docker-gen [-config file] [-watch=false] [-notify=\" restart xyz\" ] [-interval=0] [-endpoint tcp|unix://..] <template> [<dest>]" )
125
+ println ("Usage: docker-gen [-config file] [-watch=false] [-notify=\" restart xyz\" ] [-notify-sighup= \" container-ID \" ] [- interval=0] [-endpoint tcp|unix://..] <template> [<dest>]" )
124
126
}
125
127
126
128
func generateFromContainers (client * docker.Client ) {
@@ -136,6 +138,7 @@ func generateFromContainers(client *docker.Client) {
136
138
continue
137
139
}
138
140
runNotifyCmd (config )
141
+ sendSignalToContainer (client , config )
139
142
}
140
143
}
141
144
@@ -154,6 +157,20 @@ func runNotifyCmd(config Config) {
154
157
}
155
158
}
156
159
160
+ func sendSignalToContainer (client * docker.Client , config Config ) {
161
+ if len (config .NotifyContainers ) < 1 {
162
+ return
163
+ }
164
+
165
+ for _ , killOpts := range config .NotifyContainers {
166
+ log .Printf ("Restarting container '%s'" , killOpts .ID )
167
+ err := client .KillContainer (killOpts )
168
+ if err != nil {
169
+ log .Printf ("Error restarting container: %s" , err )
170
+ }
171
+ }
172
+ }
173
+
157
174
func loadConfig (file string ) error {
158
175
_ , err := toml .DecodeFile (file , & configs )
159
176
if err != nil {
@@ -187,6 +204,7 @@ func generateAtInterval(client *docker.Client, configs ConfigFile) {
187
204
// ignore changed return value. always run notify command
188
205
generateFile (configCopy , containers )
189
206
runNotifyCmd (configCopy )
207
+ sendSignalToContainer (client , configCopy )
190
208
case <- quit :
191
209
ticker .Stop ()
192
210
return
@@ -226,6 +244,7 @@ func initFlags() {
226
244
flag .BoolVar (& onlyExposed , "only-exposed" , false , "only include containers with exposed ports" )
227
245
flag .BoolVar (& onlyPublished , "only-published" , false , "only include containers with published ports (implies -only-exposed)" )
228
246
flag .StringVar (& notifyCmd , "notify" , "" , "run command after template is regenerated" )
247
+ flag .StringVar (& notifySigHUPContainerID , "notify-sighup" , "" , "send HUP signal to container. Equivalent to `docker kill -s HUP container-ID`" )
229
248
flag .StringVar (& configFile , "config" , "" , "config file with template directives" )
230
249
flag .IntVar (& interval , "interval" , 0 , "notify command interval (s)" )
231
250
flag .StringVar (& endpoint , "endpoint" , "" , "docker api endpoint" )
@@ -253,13 +272,20 @@ func main() {
253
272
}
254
273
} else {
255
274
config := Config {
256
- Template : flag .Arg (0 ),
257
- Dest : flag .Arg (1 ),
258
- Watch : watch ,
259
- NotifyCmd : notifyCmd ,
260
- OnlyExposed : onlyExposed ,
261
- OnlyPublished : onlyPublished ,
262
- Interval : interval ,
275
+ Template : flag .Arg (0 ),
276
+ Dest : flag .Arg (1 ),
277
+ Watch : watch ,
278
+ NotifyCmd : notifyCmd ,
279
+ NotifyContainers : make ([]docker.KillContainerOptions , 0 ),
280
+ OnlyExposed : onlyExposed ,
281
+ OnlyPublished : onlyPublished ,
282
+ Interval : interval ,
283
+ }
284
+ if notifySigHUPContainerID != "" {
285
+ config .NotifyContainers = append (config .NotifyContainers , docker.KillContainerOptions {
286
+ ID : notifySigHUPContainerID ,
287
+ Signal : docker .SIGHUP ,
288
+ })
263
289
}
264
290
configs = ConfigFile {
265
291
Config : []Config {config }}
0 commit comments