From a8efaab225833ae1a4b0fd46191179079c95b35f Mon Sep 17 00:00:00 2001 From: Tjeerd Jan van der Molen Date: Thu, 23 May 2019 15:39:17 +0200 Subject: [PATCH] Working on jwilder/docker-gen#77 --- cmd/docker-gen/main.go | 2 +- generator.go | 43 ++++++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/cmd/docker-gen/main.go b/cmd/docker-gen/main.go index f84546a9..0803c73c 100644 --- a/cmd/docker-gen/main.go +++ b/cmd/docker-gen/main.go @@ -96,7 +96,7 @@ func initFlags() { flag.BoolVar(¬ifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command") flag.StringVar(¬ifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)") flag.StringVar(¬ifySigHUPContainerID, "notify-sighup", "", - "send HUP signal to container. Equivalent to docker kill -s HUP `container-ID`") + "send HUP signal to container. You can either pass a container name/id or a filter key=value to send to multiple containers. Equivalent to docker kill -s HUP `container-ID`") 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") diff --git a/generator.go b/generator.go index bf68be66..3510cd62 100644 --- a/generator.go +++ b/generator.go @@ -126,7 +126,7 @@ func (g *generator) generateFromContainers() { continue } g.runNotifyCmd(config) - g.sendSignalToContainer(config) + g.sendSignalToContainers(config) } } @@ -155,7 +155,7 @@ func (g *generator) generateAtInterval() { // ignore changed return value. always run notify command GenerateFile(config, containers) g.runNotifyCmd(config) - g.sendSignalToContainer(config) + g.sendSignalToContainers(config) case sig := <-sigChan: log.Printf("Received signal: %s\n", sig) switch sig { @@ -203,7 +203,7 @@ func (g *generator) generateFromEvents() { continue } g.runNotifyCmd(config) - g.sendSignalToContainer(config) + g.sendSignalToContainers(config) } }(config, make(chan *docker.APIEvents, 100)) } @@ -323,20 +323,39 @@ func (g *generator) runNotifyCmd(config Config) { } } } +func (g *generator) sendSignalToContainer(containerID string, signal docker.Signal) { + log.Printf("Sending container '%s' signal '%v'", containerID, signal) + killOpts := docker.KillContainerOptions{ + ID: containerID, + Signal: signal, + } + if err := g.Client.KillContainer(killOpts); err != nil { + log.Printf("Error sending signal to container: %s", err) + } +} -func (g *generator) sendSignalToContainer(config Config) { +func (g *generator) sendSignalToContainers(config Config) { if len(config.NotifyContainers) < 1 { return } - for container, signal := range config.NotifyContainers { - log.Printf("Sending container '%s' signal '%v'", container, signal) - killOpts := docker.KillContainerOptions{ - ID: container, - Signal: signal, - } - if err := g.Client.KillContainer(killOpts); err != nil { - log.Printf("Error sending signal to container: %s", err) + // Check if the input has a = (filter input requires key/value input) or default back to the old functionality + if filterSplit := strings.Split(container, "="); len(filterSplit) > 1 { + filters := map[string][]string{} + filters[filterSplit[0]] = []string{filterSplit[1]} + listOpts := docker.ListContainersOptions{ + Filters: filters, + } + containers, err := g.Client.ListContainers(listOpts) + if err != nil { + log.Printf("Error finding containers to send signal to: %s %s", container, err) + } + for _, container := range containers { + g.sendSignalToContainer(container.ID, signal) + } + + } else { + g.sendSignalToContainer(container, signal) } } }