Skip to content
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Options:
show version
-watch
watch for container changes
-wait
minimum (and/or maximum) duration to wait after each container change before triggering

Arguments:
template - path to a template to generate
Expand Down Expand Up @@ -153,6 +155,9 @@ path to a template to generate
watch = true
watch for container changes

wait = "500ms:2s"
debounce changes with a min:max duration. Only applicable if watch = true


[config.NotifyContainers]
Starts a notify container section
Expand Down Expand Up @@ -180,6 +185,7 @@ watch = true
template = "/etc/docker-gen/templates/nginx.tmpl"
dest = "/etc/nginx/conf.d/default.conf"
watch = true
wait = "500ms:2s"

[config.NotifyContainers]
nginx = 1 # 1 is a signal number to be sent; here SIGINT
Expand Down
7 changes: 7 additions & 0 deletions cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
buildVersion string
version bool
watch bool
wait string
notifyCmd string
notifyOutput bool
notifySigHUPContainerID string
Expand Down Expand Up @@ -85,6 +86,7 @@ func initFlags() {
}
flag.BoolVar(&version, "version", false, "show version")
flag.BoolVar(&watch, "watch", false, "watch for container changes")
flag.StringVar(&wait, "wait", "", "minimum and maximum durations to wait (e.g. \"500ms:2s\") before triggering generate")
flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports")

flag.BoolVar(&onlyPublished, "only-published", false,
Expand Down Expand Up @@ -127,10 +129,15 @@ func main() {
}
}
} else {
w, err := dockergen.ParseWait(wait)
if err != nil {
log.Fatalf("error parsing wait interval: %s\n", err)
}
config := dockergen.Config{
Template: flag.Arg(0),
Dest: flag.Arg(1),
Watch: watch,
Wait: w,
NotifyCmd: notifyCmd,
NotifyOutput: notifyOutput,
NotifyContainers: make(map[string]docker.Signal),
Expand Down
53 changes: 52 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package dockergen

import "github.com/fsouza/go-dockerclient"
import (
"errors"
"strings"
"time"

"github.com/fsouza/go-dockerclient"
)

type Config struct {
Template string
Dest string
Watch bool
Wait *Wait
NotifyCmd string
NotifyOutput bool
NotifyContainers map[string]docker.Signal
Expand All @@ -31,3 +38,47 @@ func (c *ConfigFile) FilterWatches() ConfigFile {
Config: configWithWatches,
}
}

type Wait struct {
Min time.Duration
Max time.Duration
}

func (w *Wait) UnmarshalText(text []byte) error {
wait, err := ParseWait(string(text))
if err == nil {
w.Min, w.Max = wait.Min, wait.Max
}
return err
}

func ParseWait(s string) (*Wait, error) {
if len(strings.TrimSpace(s)) < 1 {
return &Wait{0, 0}, nil
}

parts := strings.Split(s, ":")

var (
min time.Duration
max time.Duration
err error
)
min, err = time.ParseDuration(strings.TrimSpace(parts[0]))
if err != nil {
return nil, err
}
if len(parts) > 1 {
max, err = time.ParseDuration(strings.TrimSpace(parts[1]))
if err != nil {
return nil, err
}
if max < min {
return nil, errors.New("Invalid wait interval: max must be larger than min")
}
} else {
max = 4 * min
}

return &Wait{min, max}, nil
}
Loading