Skip to content
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

feat: allow multiple -notify-sighup / -notify-container #622

Merged
merged 3 commits into from
May 17, 2024
Merged
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: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,33 @@ Options:
run command after template is regenerated (e.g restart xyz)
-notify-output
log the output(stdout/stderr) of notify command
-notify-sighup container-ID
send HUP signal to container.
Equivalent to 'docker kill -s HUP container-ID', or `-notify-container container-ID -notify-signal 1`.
You can pass this option multiple times to send HUP to multiple containers.
-notify-container container-ID
container to send a signal to
send -notify-signal signal (defaults to 1 / HUP) to container.
You can pass this option multiple times to notify multiple containers.
-notify-filter key=value
container filter for notification (e.g -notify-filter name=foo).
You can have multiple of these.
You can pass this option multiple times to combine filters with AND.
https://docs.docker.com/engine/reference/commandline/ps/#filter
-notify-signal signal
signal to send to the -notify-container and -notify-filter. -1 to call docker restart. Defaults to 1 aka. HUP.
All available signals available on the dockerclient
https://github.com/fsouza/go-dockerclient/blob/01804dec8a84d0a77e63611f2b62d33e9bb2b64a/signal.go
-notify-sighup container-ID
send HUP signal to container. Equivalent to 'docker kill -s HUP container-ID', or `-notify-container container-ID -notify-signal 1`
https://github.com/fsouza/go-dockerclient/blob/main/signal.go
-only-exposed
only include containers with exposed ports
-only-published
only include containers with published ports (implies -only-exposed)
-include-stopped
include stopped containers
-tlscacert string
path to TLS CA certificate file (default "/Users/jason/.docker/machine/machines/default/ca.pem")
path to TLS CA certificate file (default "~/.docker/machine/machines/default/ca.pem")
-tlscert string
path to TLS client certificate file (default "/Users/jason/.docker/machine/machines/default/cert.pem")
path to TLS client certificate file (default "~/.docker/machine/machines/default/cert.pem")
-tlskey string
path to TLS client key file (default "/Users/jason/.docker/machine/machines/default/key.pem")
path to TLS client key file (default "~/.docker/machine/machines/default/key.pem")
-tlsverify
verify docker daemon's TLS certicate (default true)
-version
Expand Down
30 changes: 17 additions & 13 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"
"slices"
"strings"
"syscall"

Expand All @@ -26,8 +27,8 @@ var (
wait string
notifyCmd string
notifyOutput bool
sighupContainerID string
notifyContainerID string
sighupContainerID stringslice
notifyContainerID stringslice
notifyContainerSignal int
notifyContainerFilter mapstringslice = make(mapstringslice)
onlyExposed bool
Expand All @@ -49,7 +50,6 @@ func (strings *stringslice) String() string {
}

func (strings *stringslice) Set(value string) error {
// TODO: Throw an error for duplicate `dest`
*strings = append(*strings, value)
return nil
}
Expand Down Expand Up @@ -112,12 +112,12 @@ func initFlags() {
flag.BoolVar(&includeStopped, "include-stopped", false, "include stopped containers")
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(&sighupContainerID, "notify-sighup", "",
"send HUP signal to container. Equivalent to docker kill -s HUP `container-ID`")
flag.StringVar(&notifyContainerID, "notify-container", "",
"container to send a signal to")
flag.Var(&sighupContainerID, "notify-sighup",
"send HUP signal to container. Equivalent to docker kill -s HUP `container-ID`. You can pass this option multiple times to send HUP to multiple containers.")
flag.Var(&notifyContainerID, "notify-container",
"send -notify-signal signal (defaults to 1 / HUP) to container. You can pass this option multiple times to notify multiple containers.")
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")
"container filter for notification (e.g -notify-filter name=foo). You can pass this option multiple times to combine filters with AND. https://docs.docker.com/engine/reference/commandline/ps/#filter")
flag.IntVar(&notifyContainerSignal, "notify-signal", int(docker.SIGHUP),
"signal to send to the notify-container and notify-filter. Defaults to SIGHUP")
flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.")
Expand Down Expand Up @@ -150,6 +150,9 @@ func main() {
os.Exit(1)
}

slices.Sort(configFiles)
configFiles = slices.Compact(configFiles)

if len(configFiles) > 0 {
for _, configFile := range configFiles {
err := loadConfig(configFile)
Expand All @@ -176,18 +179,19 @@ func main() {
Interval: interval,
KeepBlankLines: keepBlankLines,
}
if sighupContainerID != "" {
cfg.NotifyContainers[sighupContainerID] = int(syscall.SIGHUP)
for _, id := range sighupContainerID {
cfg.NotifyContainers[id] = int(syscall.SIGHUP)
}
if notifyContainerID != "" {
cfg.NotifyContainers[notifyContainerID] = notifyContainerSignal
for _, id := range notifyContainerID {
cfg.NotifyContainers[id] = notifyContainerSignal
}
if len(notifyContainerFilter) > 0 {
cfg.NotifyContainersFilter = notifyContainerFilter
cfg.NotifyContainersSignal = notifyContainerSignal
}
configs = config.ConfigFile{
Config: []config.Config{cfg}}
Config: []config.Config{cfg},
}
}

all := false
Expand Down