Skip to content

Commit

Permalink
Merge pull request #283 from NickBolles/master
Browse files Browse the repository at this point in the history
Allow more notify and restart options
  • Loading branch information
jwilder authored Apr 3, 2021
2 parents e51c234 + 997abf7 commit 83d32ba
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dist-clean:
rm -f docker-gen-darwin-*.tar.gz

dist: dist-clean
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
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
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
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
mkdir -p dist/linux/amd64 && GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o dist/linux/amd64/docker-gen ./cmd/docker-gen
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,13 @@ Options:
run command after template is regenerated (e.g restart xyz)
-notify-output
log the output(stdout/stderr) of notify command
-notify-container container-ID
container to send a signal to
-notify-signal signal
signal to send to the -notify-container. -1 to call docker restart. Defaults to 1 aka. HUP.
All available signals available on the [dockerclient](https://github.com/fsouza/go-dockerclient/blob/01804dec8a84d0a77e63611f2b62d33e9bb2b64a/signal.go)
-notify-sighup container-ID
send HUP signal to container. Equivalent to 'docker kill -s HUP container-ID'
send HUP signal to container. Equivalent to 'docker kill -s HUP container-ID', or `-notify-container container-ID -notify-signal 1`
-only-exposed
only include containers with exposed ports
-only-published
Expand Down
55 changes: 30 additions & 25 deletions cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,28 @@ import (
type stringslice []string

var (
buildVersion string
version bool
watch bool
wait string
notifyCmd string
notifyOutput bool
notifySigHUPContainerID string
onlyExposed bool
onlyPublished bool
includeStopped bool
configFiles stringslice
configs dockergen.ConfigFile
interval int
keepBlankLines bool
endpoint string
tlsCert string
tlsKey string
tlsCaCert string
tlsVerify bool
tlsCertPath string
wg sync.WaitGroup
buildVersion string
version bool
watch bool
wait string
notifyCmd string
notifyOutput bool
notifyContainerID string
notifyContainerSignal int
onlyExposed bool
onlyPublished bool
includeStopped bool
configFiles stringslice
configs dockergen.ConfigFile
interval int
keepBlankLines bool
endpoint string
tlsCert string
tlsKey string
tlsCaCert string
tlsVerify bool
tlsCertPath string
wg sync.WaitGroup
)

func (strings *stringslice) String() string {
Expand Down Expand Up @@ -95,8 +96,12 @@ func initFlags() {
flag.BoolVar(&includeStopped, "include-stopped", false, "include stopped containers")
flag.BoolVar(&notifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command")
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
flag.StringVar(&notifySigHUPContainerID, "notify-sighup", "",
flag.StringVar(&notifyContainerID, "notify-sighup", "",
"send HUP signal to container. Equivalent to docker kill -s HUP `container-ID`")
flag.StringVar(&notifyContainerID, "notify-container", "",
"container to send a signal to")
flag.IntVar(&notifyContainerSignal, "notify-signal", int(docker.SIGHUP),
"signal to send to the notify-container. Defaults to SIGHUP")
flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.")
flag.IntVar(&interval, "interval", 0, "notify command interval (secs)")
flag.BoolVar(&keepBlankLines, "keep-blank-lines", false, "keep blank lines in the output file")
Expand Down Expand Up @@ -142,15 +147,15 @@ func main() {
Wait: w,
NotifyCmd: notifyCmd,
NotifyOutput: notifyOutput,
NotifyContainers: make(map[string]docker.Signal),
NotifyContainers: make(map[string]int),
OnlyExposed: onlyExposed,
OnlyPublished: onlyPublished,
IncludeStopped: includeStopped,
Interval: interval,
KeepBlankLines: keepBlankLines,
}
if notifySigHUPContainerID != "" {
config.NotifyContainers[notifySigHUPContainerID] = docker.SIGHUP
if notifyContainerID != "" {
config.NotifyContainers[notifyContainerID] = notifyContainerSignal
}
configs = dockergen.ConfigFile{
Config: []dockergen.Config{config}}
Expand Down
4 changes: 1 addition & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"errors"
"strings"
"time"

"github.com/fsouza/go-dockerclient"
)

type Config struct {
Expand All @@ -15,7 +13,7 @@ type Config struct {
Wait *Wait
NotifyCmd string
NotifyOutput bool
NotifyContainers map[string]docker.Signal
NotifyContainers map[string]int
OnlyExposed bool
OnlyPublished bool
IncludeStopped bool
Expand Down
10 changes: 9 additions & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,17 @@ func (g *generator) sendSignalToContainer(config Config) {

for container, signal := range config.NotifyContainers {
log.Printf("Sending container '%s' signal '%v'", container, signal)

if signal == -1 {
if err := g.Client.RestartContainer(container, 10); err != nil {
log.Printf("Error sending restarting container: %s", err)
}
return
}

killOpts := docker.KillContainerOptions{
ID: container,
Signal: signal,
Signal: docker.Signal(signal),
}
if err := g.Client.KillContainer(killOpts); err != nil {
log.Printf("Error sending signal to container: %s", err)
Expand Down

0 comments on commit 83d32ba

Please sign in to comment.