Skip to content

Commit

Permalink
Working on nginx-proxy#77
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjeerd Jan van der Molen committed May 23, 2019
1 parent 4edc190 commit a8efaab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
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.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)
}
}
}
Expand Down

0 comments on commit a8efaab

Please sign in to comment.