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

Add support for notifying a service #247

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 GLOCKFILE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/BurntSushi/toml 056c9bc7be7190eaa7715723883caffa5f8fa3e4
github.com/fsouza/go-dockerclient e0d22d30691bcc996eca51f729a4777b8c7dc2a8
github.com/fsouza/go-dockerclient 4bc6a18363f79b32c26f92e2e6d8bfd30d79a770
6 changes: 6 additions & 0 deletions cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
notifyCmd string
notifyOutput bool
notifySigHUPContainerID string
notifySigHUPServiceID string
onlyExposed bool
onlyPublished bool
includeStopped bool
Expand Down Expand Up @@ -97,6 +98,7 @@ func initFlags() {
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`")
flag.StringVar(&notifySigHUPServiceID, "service-notify-sighup", "", "send HUP signal to all containers belong to a service.")
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 @@ -143,6 +145,7 @@ func main() {
NotifyCmd: notifyCmd,
NotifyOutput: notifyOutput,
NotifyContainers: make(map[string]docker.Signal),
NotifyServices: make(map[string]docker.Signal),
OnlyExposed: onlyExposed,
OnlyPublished: onlyPublished,
IncludeStopped: includeStopped,
Expand All @@ -152,6 +155,9 @@ func main() {
if notifySigHUPContainerID != "" {
config.NotifyContainers[notifySigHUPContainerID] = docker.SIGHUP
}
if notifySigHUPServiceID != "" {
config.NotifyServices[notifySigHUPServiceID] = docker.SIGHUP
}
configs = dockergen.ConfigFile{
Config: []dockergen.Config{config}}
}
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
NotifyCmd string
NotifyOutput bool
NotifyContainers map[string]docker.Signal
NotifyServices map[string]docker.Signal
OnlyExposed bool
OnlyPublished bool
IncludeStopped bool
Expand Down
8 changes: 4 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func (c *Context) Docker() Docker {
return dockerInfo
}

func SetServerInfo(d *docker.Env) {
func SetServerInfo(d *docker.DockerInfo) {
mu.Lock()
defer mu.Unlock()
dockerInfo = Docker{
Name: d.Get("Name"),
NumContainers: d.GetInt("Containers"),
NumImages: d.GetInt("Images"),
Name: d.Name,
NumContainers: d.Containers,
NumImages: d.Images,
Version: dockerEnv.Get("Version"),
ApiVersion: dockerEnv.Get("ApiVersion"),
GoVersion: dockerEnv.Get("GoVersion"),
Expand Down
34 changes: 34 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (g *generator) generateFromContainers() {
}
g.runNotifyCmd(config)
g.sendSignalToContainer(config)
g.sendSignalToService(config)
}
}

Expand Down Expand Up @@ -156,6 +157,7 @@ func (g *generator) generateAtInterval() {
GenerateFile(config, containers)
g.runNotifyCmd(config)
g.sendSignalToContainer(config)
g.sendSignalToService(config)
case sig := <-sigChan:
log.Printf("Received signal: %s\n", sig)
switch sig {
Expand Down Expand Up @@ -204,6 +206,7 @@ func (g *generator) generateFromEvents() {
}
g.runNotifyCmd(config)
g.sendSignalToContainer(config)
g.sendSignalToService(config)
}
}(config, make(chan *docker.APIEvents, 100))
}
Expand Down Expand Up @@ -341,6 +344,37 @@ func (g *generator) sendSignalToContainer(config Config) {
}
}

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

for service, signal := range config.NotifyServices {
log.Printf("Service '%s' needs notification", service)
taskOpts := docker.ListTasksOptions{
Filters: map[string][]string{
"service": []string{service},
},
}
tasks, err := g.Client.ListTasks(taskOpts)
if err != nil {
log.Printf("Error retrieving task list: %s", err)
}
for _, task := range tasks {
container := task.Status.ContainerStatus.ContainerID

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)
}
}
}
}

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