Skip to content

Commit 2ff5971

Browse files
committed
Add -notify-container and notify-signal to enable more workflows and allow calling docker restart on a container
1 parent d25795e commit 2ff5971

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,13 @@ Options:
9595
run command after template is regenerated (e.g restart xyz)
9696
-notify-output
9797
log the output(stdout/stderr) of notify command
98+
-notify-container container-ID
99+
container to send a signal to
100+
-notify-signal signal
101+
signal to send to the -notify-container. -1 to call docker restart. Defaults to 1 aka. HUP.
102+
All available signals available on the [dockerclient](https://github.com/fsouza/go-dockerclient/blob/01804dec8a84d0a77e63611f2b62d33e9bb2b64a/signal.go)
98103
-notify-sighup container-ID
99-
send HUP signal to container. Equivalent to 'docker kill -s HUP container-ID'
104+
send HUP signal to container. Equivalent to 'docker kill -s HUP container-ID', or `-notify-container container-ID -notify-signal 1`
100105
-only-exposed
101106
only include containers with exposed ports
102107
-only-published

cmd/docker-gen/main.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ import (
1010

1111
"github.com/BurntSushi/toml"
1212
docker "github.com/fsouza/go-dockerclient"
13-
"github.com/jwilder/docker-gen"
1413
)
1514

1615
type stringslice []string
1716

1817
var (
19-
buildVersion string
20-
version bool
21-
watch bool
22-
wait string
23-
notifyCmd string
24-
notifyOutput bool
25-
notifySigHUPContainerID string
26-
onlyExposed bool
27-
onlyPublished bool
28-
includeStopped bool
29-
configFiles stringslice
30-
configs dockergen.ConfigFile
31-
interval int
32-
keepBlankLines bool
33-
endpoint string
34-
tlsCert string
35-
tlsKey string
36-
tlsCaCert string
37-
tlsVerify bool
38-
tlsCertPath string
39-
wg sync.WaitGroup
18+
buildVersion string
19+
version bool
20+
watch bool
21+
wait string
22+
notifyCmd string
23+
notifyOutput bool
24+
notifyContainerID string
25+
notifyContainerSignal int
26+
onlyExposed bool
27+
onlyPublished bool
28+
includeStopped bool
29+
configFiles stringslice
30+
configs dockergen.ConfigFile
31+
interval int
32+
keepBlankLines bool
33+
endpoint string
34+
tlsCert string
35+
tlsKey string
36+
tlsCaCert string
37+
tlsVerify bool
38+
tlsCertPath string
39+
wg sync.WaitGroup
4040
)
4141

4242
func (strings *stringslice) String() string {
@@ -95,8 +95,12 @@ func initFlags() {
9595
flag.BoolVar(&includeStopped, "include-stopped", false, "include stopped containers")
9696
flag.BoolVar(&notifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command")
9797
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
98-
flag.StringVar(&notifySigHUPContainerID, "notify-sighup", "",
98+
flag.StringVar(&notifyContainerID, "notify-sighup", "",
9999
"send HUP signal to container. Equivalent to docker kill -s HUP `container-ID`")
100+
flag.StringVar(&notifyContainerID, "notify-container", "",
101+
"container to send a signal to")
102+
flag.IntVar(&notifyContainerSignal, "notify-signal", int(docker.SIGHUP),
103+
"signal to send to the notify-container. Defaults to SIGHUP")
100104
flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.")
101105
flag.IntVar(&interval, "interval", 0, "notify command interval (secs)")
102106
flag.BoolVar(&keepBlankLines, "keep-blank-lines", false, "keep blank lines in the output file")
@@ -142,15 +146,15 @@ func main() {
142146
Wait: w,
143147
NotifyCmd: notifyCmd,
144148
NotifyOutput: notifyOutput,
145-
NotifyContainers: make(map[string]docker.Signal),
149+
NotifyContainers: make(map[string]int),
146150
OnlyExposed: onlyExposed,
147151
OnlyPublished: onlyPublished,
148152
IncludeStopped: includeStopped,
149153
Interval: interval,
150154
KeepBlankLines: keepBlankLines,
151155
}
152-
if notifySigHUPContainerID != "" {
153-
config.NotifyContainers[notifySigHUPContainerID] = docker.SIGHUP
156+
if notifyContainerID != "" {
157+
config.NotifyContainers[notifyContainerID] = notifyContainerSignal
154158
}
155159
configs = dockergen.ConfigFile{
156160
Config: []dockergen.Config{config}}

generator.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,17 @@ func (g *generator) sendSignalToContainer(config Config) {
331331

332332
for container, signal := range config.NotifyContainers {
333333
log.Printf("Sending container '%s' signal '%v'", container, signal)
334+
335+
if signal == -1 {
336+
if err := g.Client.RestartContainer(container, 10); err != nil {
337+
log.Printf("Error sending restarting container: %s", err)
338+
}
339+
return
340+
}
341+
334342
killOpts := docker.KillContainerOptions{
335343
ID: container,
336-
Signal: signal,
344+
Signal: docker.Signal(signal),
337345
}
338346
if err := g.Client.KillContainer(killOpts); err != nil {
339347
log.Printf("Error sending signal to container: %s", err)

0 commit comments

Comments
 (0)