Skip to content

Commit 83d32ba

Browse files
authored
Merge pull request #283 from NickBolles/master
Allow more notify and restart options
2 parents e51c234 + 997abf7 commit 83d32ba

File tree

5 files changed

+47
-31
lines changed

5 files changed

+47
-31
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ dist-clean:
1717
rm -f docker-gen-darwin-*.tar.gz
1818

1919
dist: dist-clean
20-
mkdir -p dist/alpine-linux/amd64 && GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -a -tags netgo -installsuffix netgo -o dist/alpine-linux/amd64/docker-gen ./cmd/docker-gen
20+
mkdir -p dist/alpine-linux/amd64 && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" -a -tags netgo -installsuffix netgo -o dist/alpine-linux/amd64/docker-gen ./cmd/docker-gen
2121
mkdir -p dist/alpine-linux/arm64 && GOOS=linux GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -a -tags netgo -installsuffix netgo -o dist/alpine-linux/arm64/docker-gen ./cmd/docker-gen
2222
mkdir -p dist/alpine-linux/armhf && GOOS=linux GOARCH=arm GOARM=6 go build -ldflags "$(LDFLAGS)" -a -tags netgo -installsuffix netgo -o dist/alpine-linux/armhf/docker-gen ./cmd/docker-gen
2323
mkdir -p dist/linux/amd64 && GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o dist/linux/amd64/docker-gen ./cmd/docker-gen

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 & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,28 @@ import (
1616
type stringslice []string
1717

1818
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
19+
buildVersion string
20+
version bool
21+
watch bool
22+
wait string
23+
notifyCmd string
24+
notifyOutput bool
25+
notifyContainerID string
26+
notifyContainerSignal int
27+
onlyExposed bool
28+
onlyPublished bool
29+
includeStopped bool
30+
configFiles stringslice
31+
configs dockergen.ConfigFile
32+
interval int
33+
keepBlankLines bool
34+
endpoint string
35+
tlsCert string
36+
tlsKey string
37+
tlsCaCert string
38+
tlsVerify bool
39+
tlsCertPath string
40+
wg sync.WaitGroup
4041
)
4142

4243
func (strings *stringslice) String() string {
@@ -95,8 +96,12 @@ func initFlags() {
9596
flag.BoolVar(&includeStopped, "include-stopped", false, "include stopped containers")
9697
flag.BoolVar(&notifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command")
9798
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
98-
flag.StringVar(&notifySigHUPContainerID, "notify-sighup", "",
99+
flag.StringVar(&notifyContainerID, "notify-sighup", "",
99100
"send HUP signal to container. Equivalent to docker kill -s HUP `container-ID`")
101+
flag.StringVar(&notifyContainerID, "notify-container", "",
102+
"container to send a signal to")
103+
flag.IntVar(&notifyContainerSignal, "notify-signal", int(docker.SIGHUP),
104+
"signal to send to the notify-container. Defaults to SIGHUP")
100105
flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.")
101106
flag.IntVar(&interval, "interval", 0, "notify command interval (secs)")
102107
flag.BoolVar(&keepBlankLines, "keep-blank-lines", false, "keep blank lines in the output file")
@@ -142,15 +147,15 @@ func main() {
142147
Wait: w,
143148
NotifyCmd: notifyCmd,
144149
NotifyOutput: notifyOutput,
145-
NotifyContainers: make(map[string]docker.Signal),
150+
NotifyContainers: make(map[string]int),
146151
OnlyExposed: onlyExposed,
147152
OnlyPublished: onlyPublished,
148153
IncludeStopped: includeStopped,
149154
Interval: interval,
150155
KeepBlankLines: keepBlankLines,
151156
}
152-
if notifySigHUPContainerID != "" {
153-
config.NotifyContainers[notifySigHUPContainerID] = docker.SIGHUP
157+
if notifyContainerID != "" {
158+
config.NotifyContainers[notifyContainerID] = notifyContainerSignal
154159
}
155160
configs = dockergen.ConfigFile{
156161
Config: []dockergen.Config{config}}

config.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"errors"
55
"strings"
66
"time"
7-
8-
"github.com/fsouza/go-dockerclient"
97
)
108

119
type Config struct {
@@ -15,7 +13,7 @@ type Config struct {
1513
Wait *Wait
1614
NotifyCmd string
1715
NotifyOutput bool
18-
NotifyContainers map[string]docker.Signal
16+
NotifyContainers map[string]int
1917
OnlyExposed bool
2018
OnlyPublished bool
2119
IncludeStopped bool

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)