Skip to content

Commit 8e479fe

Browse files
tarasov65536buchdag
authored andcommitted
feat: -notify-filter argument
1 parent c10343f commit 8e479fe

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

cmd/docker-gen/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/signal"
99
"path/filepath"
10+
"strings"
1011
"syscall"
1112

1213
"github.com/BurntSushi/toml"
@@ -16,6 +17,7 @@ import (
1617
)
1718

1819
type stringslice []string
20+
type notifyfilter map[string][]string
1921

2022
var (
2123
buildVersion string
@@ -26,6 +28,7 @@ var (
2628
notifyOutput bool
2729
notifyContainerID string
2830
notifyContainerSignal int
31+
notifyContainerFilter notifyfilter = make(notifyfilter)
2932
onlyExposed bool
3033
onlyPublished bool
3134
includeStopped bool
@@ -50,6 +53,16 @@ func (strings *stringslice) Set(value string) error {
5053
return nil
5154
}
5255

56+
func (filter *notifyfilter) String() string {
57+
return "[string][]string"
58+
}
59+
60+
func (filter *notifyfilter) Set(value string) error {
61+
name, value, _ := strings.Cut(value, "=")
62+
(*filter)[name] = append((*filter)[name], value)
63+
return nil
64+
}
65+
5366
func usage() {
5467
println(`Usage: docker-gen [options] template [dest]
5568
@@ -102,6 +115,8 @@ func initFlags() {
102115
"container to send a signal to")
103116
flag.IntVar(&notifyContainerSignal, "notify-signal", int(docker.SIGHUP),
104117
"signal to send to the notify-container. Defaults to SIGHUP")
118+
flag.Var(&notifyContainerFilter, "notify-filter",
119+
"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")
105120
flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.")
106121
flag.IntVar(&interval, "interval", 0, "notify command interval (secs)")
107122
flag.BoolVar(&keepBlankLines, "keep-blank-lines", false, "keep blank lines in the output file")
@@ -161,6 +176,10 @@ func main() {
161176
if notifyContainerID != "" {
162177
cfg.NotifyContainers[notifyContainerID] = notifyContainerSignal
163178
}
179+
if len(notifyContainerFilter) > 0 {
180+
cfg.NotifyContainersFilter = notifyContainerFilter
181+
cfg.NotifyContainersSignal = notifyContainerSignal
182+
}
164183
configs = config.ConfigFile{
165184
Config: []config.Config{cfg}}
166185
}

internal/config/config.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ import (
77
)
88

99
type Config struct {
10-
Template string
11-
Dest string
12-
Watch bool
13-
Wait *Wait
14-
NotifyCmd string
15-
NotifyOutput bool
16-
NotifyContainers map[string]int
17-
OnlyExposed bool
18-
OnlyPublished bool
19-
IncludeStopped bool
20-
Interval int
21-
KeepBlankLines bool
10+
Template string
11+
Dest string
12+
Watch bool
13+
Wait *Wait
14+
NotifyCmd string
15+
NotifyOutput bool
16+
NotifyContainers map[string]int
17+
NotifyContainersFilter map[string][]string
18+
NotifyContainersSignal int
19+
OnlyExposed bool
20+
OnlyPublished bool
21+
IncludeStopped bool
22+
Interval int
23+
KeepBlankLines bool
2224
}
2325

2426
type ConfigFile struct {

internal/generator/generator.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func (g *generator) generateFromContainers() {
133133
}
134134
g.runNotifyCmd(config)
135135
g.sendSignalToContainer(config)
136+
g.sendSignalToContainers(config)
136137
}
137138
}
138139

@@ -163,6 +164,7 @@ func (g *generator) generateAtInterval() {
163164
template.GenerateFile(cfg, containers)
164165
g.runNotifyCmd(cfg)
165166
g.sendSignalToContainer(cfg)
167+
g.sendSignalToContainers(cfg)
166168
case sig := <-sigChan:
167169
log.Printf("Received signal: %s\n", sig)
168170
switch sig {
@@ -211,6 +213,7 @@ func (g *generator) generateFromEvents() {
211213
}
212214
g.runNotifyCmd(cfg)
213215
g.sendSignalToContainer(cfg)
216+
g.sendSignalToContainers(cfg)
214217
}
215218
}(cfg)
216219
}
@@ -357,6 +360,35 @@ func (g *generator) sendSignalToContainer(config config.Config) {
357360
}
358361
}
359362

363+
func (g *generator) sendSignalToContainers(config config.Config) {
364+
if len(config.NotifyContainersFilter) < 1 {
365+
return
366+
}
367+
368+
containers, err := g.Client.ListContainers(docker.ListContainersOptions{
369+
Filters: config.NotifyContainersFilter,
370+
})
371+
if err != nil {
372+
log.Printf("Error getting containers: %s", err)
373+
return
374+
}
375+
for _, container := range containers {
376+
if config.NotifyContainersSignal == -1 {
377+
if err := g.Client.RestartContainer(container.ID, 10); err != nil {
378+
log.Printf("Error sending restarting container: %s", err)
379+
}
380+
} else {
381+
killOpts := docker.KillContainerOptions{
382+
ID: container.ID,
383+
Signal: docker.Signal(config.NotifyContainersSignal),
384+
}
385+
if err := g.Client.KillContainer(killOpts); err != nil {
386+
log.Printf("Error sending signal to container: %s", err)
387+
}
388+
}
389+
}
390+
}
391+
360392
func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
361393
apiInfo, err := g.Client.Info()
362394
if err != nil {

0 commit comments

Comments
 (0)