Skip to content

Filter containers to notify #549

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 3 commits into from
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
19 changes: 19 additions & 0 deletions cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

"github.com/BurntSushi/toml"
Expand All @@ -16,6 +17,7 @@ import (
)

type stringslice []string
type notifyfilter map[string][]string

var (
buildVersion string
Expand All @@ -26,6 +28,7 @@ var (
notifyOutput bool
notifyContainerID string
notifyContainerSignal int
notifyContainerFilter notifyfilter = make(notifyfilter)
onlyExposed bool
onlyPublished bool
includeStopped bool
Expand All @@ -50,6 +53,16 @@ func (strings *stringslice) Set(value string) error {
return nil
}

func (filter *notifyfilter) String() string {
return "[string][]string"
}

func (filter *notifyfilter) Set(value string) error {
name, value, _ := strings.Cut(value, "=")
(*filter)[name] = append((*filter)[name], value)
return nil
}

func usage() {
println(`Usage: docker-gen [options] template [dest]

Expand Down Expand Up @@ -102,6 +115,8 @@ func initFlags() {
"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(&notifyContainerFilter, "notify-filter",
"container filter for notification (e.g -notify-filter name=foo). You can have multiple of these. https://docs.docker.com/engine/reference/commandline/ps/#filter")
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 @@ -161,6 +176,10 @@ func main() {
if notifyContainerID != "" {
cfg.NotifyContainers[notifyContainerID] = notifyContainerSignal
}
if len(notifyContainerFilter) > 0 {
cfg.NotifyContainersFilter = notifyContainerFilter
cfg.NotifyContainersSignal = notifyContainerSignal
}
configs = config.ConfigFile{
Config: []config.Config{cfg}}
}
Expand Down
26 changes: 14 additions & 12 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import (
)

type Config struct {
Template string
Dest string
Watch bool
Wait *Wait
NotifyCmd string
NotifyOutput bool
NotifyContainers map[string]int
OnlyExposed bool
OnlyPublished bool
IncludeStopped bool
Interval int
KeepBlankLines bool
Template string
Dest string
Watch bool
Wait *Wait
NotifyCmd string
NotifyOutput bool
NotifyContainers map[string]int
NotifyContainersFilter map[string][]string
NotifyContainersSignal int
OnlyExposed bool
OnlyPublished bool
IncludeStopped bool
Interval int
KeepBlankLines bool
}

type ConfigFile struct {
Expand Down
33 changes: 33 additions & 0 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (g *generator) generateFromContainers() {
}
g.runNotifyCmd(config)
g.sendSignalToContainer(config)
g.sendSignalToContainers(config)
}
}

Expand Down Expand Up @@ -163,6 +164,7 @@ func (g *generator) generateAtInterval() {
template.GenerateFile(cfg, containers)
g.runNotifyCmd(cfg)
g.sendSignalToContainer(cfg)
g.sendSignalToContainers(cfg)
case sig := <-sigChan:
log.Printf("Received signal: %s\n", sig)
switch sig {
Expand Down Expand Up @@ -211,6 +213,7 @@ func (g *generator) generateFromEvents() {
}
g.runNotifyCmd(cfg)
g.sendSignalToContainer(cfg)
g.sendSignalToContainers(cfg)
}
}(cfg)
}
Expand Down Expand Up @@ -357,6 +360,36 @@ func (g *generator) sendSignalToContainer(config config.Config) {
}
}

func (g *generator) sendSignalToContainers(config config.Config) {
if len(config.NotifyContainersFilter) < 1 {
return
}

containers, err := g.Client.ListContainers(docker.ListContainersOptions{
Filters: config.NotifyContainersFilter,
})
if err != nil {
log.Printf("Error getting containers: %s", err)
return
}
for _, container := range containers {
log.Printf("Sending container '%s' signal '%v'", container.ID, config.NotifyContainersSignal)
if config.NotifyContainersSignal == -1 {
if err := g.Client.RestartContainer(container.ID, 10); err != nil {
log.Printf("Error sending restarting container: %s", err)
}
} else {
killOpts := docker.KillContainerOptions{
ID: container.ID,
Signal: docker.Signal(config.NotifyContainersSignal),
}
if err := g.Client.KillContainer(killOpts); err != nil {
log.Printf("Error sending signal to container: %s", err)
}
}
}
}

func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
apiInfo, err := g.Client.Info()
if err != nil {
Expand Down