Skip to content

Commit

Permalink
Add -notify-container and notify-signal to enable more workflows and …
Browse files Browse the repository at this point in the history
…allow calling docker restart on a container
  • Loading branch information
NickBolles committed Apr 13, 2018
1 parent d25795e commit 2ff5971
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
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
56 changes: 30 additions & 26 deletions cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ import (

"github.com/BurntSushi/toml"
docker "github.com/fsouza/go-dockerclient"
"github.com/jwilder/docker-gen"
)

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 +95,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 +146,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
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 2ff5971

Please sign in to comment.