Skip to content

Commit 57bb735

Browse files
committed
Add option to restart a docker container in -notify
1 parent 2847e7b commit 57bb735

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

docker-gen.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (r *RuntimeContainer) PublishedAddresses() []Address {
120120
}
121121

122122
func usage() {
123-
println("Usage: docker-gen [-config file] [-watch=false] [-notify=\"restart xyz\"] [-interval=0] [-endpoint tcp|unix://..] <template> [<dest>]")
123+
println("Usage: docker-gen [-config file] [-watch=false] [-notify=\"restart xyz\"][-notify=\"restart-container container-ID\"] [-interval=0] [-endpoint tcp|unix://..] <template> [<dest>]")
124124
}
125125

126126
func generateFromContainers(client *docker.Client) {
@@ -135,22 +135,33 @@ func generateFromContainers(client *docker.Client) {
135135
log.Printf("Contents of %s did not change. Skipping notification '%s'", config.Dest, config.NotifyCmd)
136136
continue
137137
}
138-
runNotifyCmd(config)
138+
runNotifyCmd(client, config)
139139
}
140140
}
141141

142-
func runNotifyCmd(config Config) {
142+
func runNotifyCmd(client *docker.Client, config Config) {
143143
if config.NotifyCmd == "" {
144144
return
145145
}
146146

147-
log.Printf("Running '%s'", config.NotifyCmd)
148147
args := strings.Split(config.NotifyCmd, " ")
149-
cmd := exec.Command(args[0], args[1:]...)
150-
out, err := cmd.CombinedOutput()
151-
if err != nil {
152-
log.Printf("error running notify command: %s, %s\n", config.NotifyCmd, err)
153-
log.Print(string(out))
148+
if args[0] == "restart-container" {
149+
log.Printf("Restarting container '%s'", args[1])
150+
err := client.KillContainer(docker.KillContainerOptions{
151+
ID: args[1],
152+
Signal: docker.SIGHUP,
153+
})
154+
if err != nil {
155+
log.Printf("Error restarting container: %s", err)
156+
}
157+
} else {
158+
log.Printf("Running '%s'", config.NotifyCmd)
159+
cmd := exec.Command(args[0], args[1:]...)
160+
out, err := cmd.CombinedOutput()
161+
if err != nil {
162+
log.Printf("error running notify command: %s, %s\n", config.NotifyCmd, err)
163+
log.Print(string(out))
164+
}
154165
}
155166
}
156167

@@ -186,7 +197,7 @@ func generateAtInterval(client *docker.Client, configs ConfigFile) {
186197
}
187198
// ignore changed return value. always run notify command
188199
generateFile(configCopy, containers)
189-
runNotifyCmd(configCopy)
200+
runNotifyCmd(client, configCopy)
190201
case <-quit:
191202
ticker.Stop()
192203
return

0 commit comments

Comments
 (0)