Skip to content

Commit

Permalink
Merge pull request #27 from JustAdam/container-restart
Browse files Browse the repository at this point in the history
Added flag -restart-container to allow restarting of docker containers.
  • Loading branch information
jwilder committed Oct 7, 2014
2 parents 1b6ec01 + 9455238 commit a3948f8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
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 @@ -153,6 +156,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 @@ -186,6 +206,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 @@ -225,6 +246,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 @@ -252,13 +274,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

0 comments on commit a3948f8

Please sign in to comment.