Skip to content

Added flag -restart-container to allow restarting of docker containers. #27

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

Merged
merged 1 commit into from
Oct 7, 2014
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $ docker-gen
Usage: docker-gen [options] <template> [<dest>]
```

[-config file] [-watch=false] [-notify="restart xyz"] [-interval=0] [-endpoint tcp|unix://..]
[-config file] [-watch=false] [-notify="restart xyz"] [-notify-sighup="nginx-proxy"] [-interval=0] [-endpoint tcp|unix://..]

*Options:*
```
Expand All @@ -50,6 +50,7 @@ Usage: docker-gen [options] <template> [<dest>]
-interval=0:run notify command interval (s). Useful for service registration use cases.
-notify="": run command after template is regenerated ["restart xyz"]. Useful for restarting nginx,
reloading haproxy, etc..
-notify-sighup="": send HUP signal to container. Equivalent to `docker kill -s HUP container-ID`
-only-exposed=false: only include containers with exposed ports
-only-published=false: only include containers with published ports (implies -only-exposed)
-version=false: show version
Expand Down
78 changes: 52 additions & 26 deletions docker-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ import (
)

var (
buildVersion string
version bool
watch bool
notifyCmd string
onlyExposed bool
onlyPublished bool
configFile string
configs ConfigFile
interval int
endpoint string
wg sync.WaitGroup
buildVersion string
version bool
watch bool
notifyCmd string
notifySigHUPContainerID string
onlyExposed bool
onlyPublished bool
configFile string
configs ConfigFile
interval int
endpoint string
wg sync.WaitGroup
)

type Event struct {
Expand Down Expand Up @@ -66,13 +67,14 @@ func (i *DockerImage) String() string {
}

type Config struct {
Template string
Dest string
Watch bool
NotifyCmd string
OnlyExposed bool
OnlyPublished bool
Interval int
Template string
Dest string
Watch bool
NotifyCmd string
NotifyContainers map[string]docker.Signal
OnlyExposed bool
OnlyPublished bool
Interval int
}

type ConfigFile struct {
Expand Down Expand Up @@ -120,7 +122,7 @@ func (r *RuntimeContainer) PublishedAddresses() []Address {
}

func usage() {
println("Usage: docker-gen [-config file] [-watch=false] [-notify=\"restart xyz\"] [-interval=0] [-endpoint tcp|unix://..] <template> [<dest>]")
println("Usage: docker-gen [-config file] [-watch=false] [-notify=\"restart xyz\"] [-notify-sighup=\"container-ID\"] [-interval=0] [-endpoint tcp|unix://..] <template> [<dest>]")
}

func generateFromContainers(client *docker.Client) {
Expand All @@ -136,6 +138,7 @@ func generateFromContainers(client *docker.Client) {
continue
}
runNotifyCmd(config)
sendSignalToContainer(client, config)
}
}

Expand All @@ -154,6 +157,23 @@ func runNotifyCmd(config Config) {
}
}

func sendSignalToContainer(client *docker.Client, 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 := client.KillContainer(killOpts); err != nil {
log.Printf("Error sending signal to container: %s", err)
}
}
}

func loadConfig(file string) error {
_, err := toml.DecodeFile(file, &configs)
if err != nil {
Expand Down Expand Up @@ -187,6 +207,7 @@ func generateAtInterval(client *docker.Client, configs ConfigFile) {
// ignore changed return value. always run notify command
generateFile(configCopy, containers)
runNotifyCmd(configCopy)
sendSignalToContainer(client, configCopy)
case <-quit:
ticker.Stop()
return
Expand Down Expand Up @@ -226,6 +247,7 @@ func initFlags() {
flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports")
flag.BoolVar(&onlyPublished, "only-published", false, "only include containers with published ports (implies -only-exposed)")
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated")
flag.StringVar(&notifySigHUPContainerID, "notify-sighup", "", "send HUP signal to container. Equivalent to `docker kill -s HUP container-ID`")
flag.StringVar(&configFile, "config", "", "config file with template directives")
flag.IntVar(&interval, "interval", 0, "notify command interval (s)")
flag.StringVar(&endpoint, "endpoint", "", "docker api endpoint")
Expand Down Expand Up @@ -253,13 +275,17 @@ func main() {
}
} else {
config := Config{
Template: flag.Arg(0),
Dest: flag.Arg(1),
Watch: watch,
NotifyCmd: notifyCmd,
OnlyExposed: onlyExposed,
OnlyPublished: onlyPublished,
Interval: interval,
Template: flag.Arg(0),
Dest: flag.Arg(1),
Watch: watch,
NotifyCmd: notifyCmd,
NotifyContainers: make(map[string]docker.Signal),
OnlyExposed: onlyExposed,
OnlyPublished: onlyPublished,
Interval: interval,
}
if notifySigHUPContainerID != "" {
config.NotifyContainers[notifySigHUPContainerID] = docker.SIGHUP
}
configs = ConfigFile{
Config: []Config{config}}
Expand Down