Skip to content

Commit

Permalink
Add -only-published filter option
Browse files Browse the repository at this point in the history
Adds a more restrictive form of -only-exposed where only contiainers
that have an exposed port published to the host will be included
in the template metadata.
  • Loading branch information
jwilder committed May 20, 2014
1 parent dfc75c9 commit 613bd8d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
44 changes: 24 additions & 20 deletions docker-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (
)

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

type Event struct {
Expand Down Expand Up @@ -63,12 +64,13 @@ func (i *DockerImage) String() string {
}

type Config struct {
Template string
Dest string
Watch bool
NotifyCmd string
OnlyExposed bool
Interval int
Template string
Dest string
Watch bool
NotifyCmd string
OnlyExposed bool
OnlyPublished bool
Interval int
}

type ConfigFile struct {
Expand Down Expand Up @@ -198,6 +200,7 @@ func generateFromEvents(client *docker.Client, configs ConfigFile) {
func initFlags() {
flag.BoolVar(&watch, "watch", false, "watch for container changes")
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(&configFile, "config", "", "config file with template directives")
flag.IntVar(&interval, "interval", 0, "notify command interval (s)")
Expand All @@ -221,12 +224,13 @@ func main() {
}
} else {
config := Config{
Template: flag.Arg(0),
Dest: flag.Arg(1),
Watch: watch,
NotifyCmd: notifyCmd,
OnlyExposed: onlyExposed,
Interval: interval,
Template: flag.Arg(0),
Dest: flag.Arg(1),
Watch: watch,
NotifyCmd: notifyCmd,
OnlyExposed: onlyExposed,
OnlyPublished: onlyPublished,
Interval: interval,
}
configs = ConfigFile{
Config: []Config{config}}
Expand Down
8 changes: 7 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ func generateFile(config Config, containers []*RuntimeContainer) bool {
}

filteredContainers := []*RuntimeContainer{}
if config.OnlyExposed {
if config.OnlyPublished {
for _, container := range containers {
if len(container.PublishedAddresses()) > 0 {
filteredContainers = append(filteredContainers, container)
}
}
} else if config.OnlyExposed {
for _, container := range containers {
if len(container.Addresses) > 0 {
filteredContainers = append(filteredContainers, container)
}
}
} else {
filteredContainers = containers
}
Expand Down

0 comments on commit 613bd8d

Please sign in to comment.