Skip to content

Improve notify-sigup to allow for the use of filters (such as labels) #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func initFlags() {
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", "",
"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")
Expand Down
43 changes: 31 additions & 12 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (g *generator) generateFromContainers() {
continue
}
g.runNotifyCmd(config)
g.sendSignalToContainer(config)
g.sendSignalToContainers(config)
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -203,7 +203,7 @@ func (g *generator) generateFromEvents() {
continue
}
g.runNotifyCmd(config)
g.sendSignalToContainer(config)
g.sendSignalToContainers(config)
}
}(config, make(chan *docker.APIEvents, 100))
}
Expand Down Expand Up @@ -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.SplitN(container, "=", 2); 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)
}
}
}
Expand Down