Skip to content

Commit 3e4c18f

Browse files
committed
Added flag -restart-container to allow restarting of docker containers.
1 parent 57bb735 commit 3e4c18f

File tree

2 files changed

+59
-47
lines changed

2 files changed

+59
-47
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $ docker-gen
4040
Usage: docker-gen [options] <template> [<dest>]
4141
```
4242

43-
[-config file] [-watch=false] [-notify="restart xyz"] [-interval=0] [-endpoint tcp|unix://..]
43+
[-config file] [-watch=false] [-notify="restart xyz"] [-restart-container="nginx-proxy"] [-interval=0] [-endpoint tcp|unix://..]
4444

4545
*Options:*
4646
```
@@ -50,6 +50,7 @@ Usage: docker-gen [options] <template> [<dest>]
5050
-interval=0:run notify command interval (s). Useful for service registration use cases.
5151
-notify="": run command after template is regenerated ["restart xyz"]. Useful for restarting nginx,
5252
reloading haproxy, etc..
53+
-restart-container="": restart this container after template is regenerated
5354
-only-exposed=false: only include containers with exposed ports
5455
-only-published=false: only include containers with published ports (implies -only-exposed)
5556
-version=false: show version

docker-gen.go

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ import (
1515
)
1616

1717
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+
restartContainerID string
23+
onlyExposed bool
24+
onlyPublished bool
25+
configFile string
26+
configs ConfigFile
27+
interval int
28+
endpoint string
29+
wg sync.WaitGroup
2930
)
3031

3132
type Event struct {
@@ -66,13 +67,14 @@ func (i *DockerImage) String() string {
6667
}
6768

6869
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+
RestartContainerID string
75+
OnlyExposed bool
76+
OnlyPublished bool
77+
Interval int
7678
}
7779

7880
type ConfigFile struct {
@@ -120,7 +122,7 @@ func (r *RuntimeContainer) PublishedAddresses() []Address {
120122
}
121123

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

126128
func generateFromContainers(client *docker.Client) {
@@ -135,33 +137,39 @@ func generateFromContainers(client *docker.Client) {
135137
log.Printf("Contents of %s did not change. Skipping notification '%s'", config.Dest, config.NotifyCmd)
136138
continue
137139
}
138-
runNotifyCmd(client, config)
140+
runNotifyCmd(config)
141+
restartContainer(client, config)
139142
}
140143
}
141144

142-
func runNotifyCmd(client *docker.Client, config Config) {
145+
func runNotifyCmd(config Config) {
143146
if config.NotifyCmd == "" {
144147
return
145148
}
146149

150+
log.Printf("Running '%s'", config.NotifyCmd)
147151
args := strings.Split(config.NotifyCmd, " ")
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-
}
152+
cmd := exec.Command(args[0], args[1:]...)
153+
out, err := cmd.CombinedOutput()
154+
if err != nil {
155+
log.Printf("error running notify command: %s, %s\n", config.NotifyCmd, err)
156+
log.Print(string(out))
157+
}
158+
159+
}
160+
161+
func restartContainer(client *docker.Client, config Config) {
162+
if config.RestartContainerID == "" {
163+
return
164+
}
165+
166+
log.Printf("Restarting container '%s'", config.RestartContainerID)
167+
err := client.KillContainer(docker.KillContainerOptions{
168+
ID: config.RestartContainerID,
169+
Signal: docker.SIGHUP,
170+
})
171+
if err != nil {
172+
log.Printf("Error restarting container: %s", err)
165173
}
166174
}
167175

@@ -197,7 +205,8 @@ func generateAtInterval(client *docker.Client, configs ConfigFile) {
197205
}
198206
// ignore changed return value. always run notify command
199207
generateFile(configCopy, containers)
200-
runNotifyCmd(client, configCopy)
208+
runNotifyCmd(configCopy)
209+
restartContainer(client, configCopy)
201210
case <-quit:
202211
ticker.Stop()
203212
return
@@ -237,6 +246,7 @@ func initFlags() {
237246
flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports")
238247
flag.BoolVar(&onlyPublished, "only-published", false, "only include containers with published ports (implies -only-exposed)")
239248
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated")
249+
flag.StringVar(&restartContainerID, "restart-container", "", "restart this container after template is regenerated")
240250
flag.StringVar(&configFile, "config", "", "config file with template directives")
241251
flag.IntVar(&interval, "interval", 0, "notify command interval (s)")
242252
flag.StringVar(&endpoint, "endpoint", "", "docker api endpoint")
@@ -264,13 +274,14 @@ func main() {
264274
}
265275
} else {
266276
config := Config{
267-
Template: flag.Arg(0),
268-
Dest: flag.Arg(1),
269-
Watch: watch,
270-
NotifyCmd: notifyCmd,
271-
OnlyExposed: onlyExposed,
272-
OnlyPublished: onlyPublished,
273-
Interval: interval,
277+
Template: flag.Arg(0),
278+
Dest: flag.Arg(1),
279+
Watch: watch,
280+
NotifyCmd: notifyCmd,
281+
RestartContainerID: restartContainerID,
282+
OnlyExposed: onlyExposed,
283+
OnlyPublished: onlyPublished,
284+
Interval: interval,
274285
}
275286
configs = ConfigFile{
276287
Config: []Config{config}}

0 commit comments

Comments
 (0)