Skip to content

include stopped containers #130

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
Feb 24, 2016
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ Options:
only include containers with exposed ports
-only-published
only include containers with published ports (implies -only-exposed)
-include-stopped
include stopped containers
-tlscacert string
path to TLS CA certificate file (default "/Users/jason/.docker/machine/machines/default/ca.pem")
-tlscert string
Expand Down Expand Up @@ -213,6 +215,7 @@ type RuntimeContainer struct {
IP6LinkLocal string
IP6Global string
Mounts []Mount
State State
}

type Address struct {
Expand Down Expand Up @@ -264,6 +267,10 @@ type SwarmNode struct {
Address Address
}

type State struct {
Running bool
}

// Accessible from the root in templates as .Docker
type Docker struct {
Name string
Expand Down
11 changes: 11 additions & 0 deletions cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
notifySigHUPContainerID string
onlyExposed bool
onlyPublished bool
includeStopped bool
configFiles stringslice
configs dockergen.ConfigFile
interval int
Expand Down Expand Up @@ -89,6 +90,7 @@ func initFlags() {

flag.BoolVar(&onlyPublished, "only-published", false,
"only include containers with published ports (implies -only-exposed)")
flag.BoolVar(&includeStopped, "include-stopped", false, "include stopped containers")
flag.BoolVar(&notifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command")
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
flag.StringVar(&notifySigHUPContainerID, "notify-sighup", "",
Expand Down Expand Up @@ -136,6 +138,7 @@ func main() {
NotifyContainers: make(map[string]docker.Signal),
OnlyExposed: onlyExposed,
OnlyPublished: onlyPublished,
IncludeStopped: includeStopped,
Interval: interval,
KeepBlankLines: keepBlankLines,
}
Expand All @@ -146,12 +149,20 @@ func main() {
Config: []dockergen.Config{config}}
}

all := true
for _, config := range configs.Config {
if config.IncludeStopped {
all = true
}
}

generator, err := dockergen.NewGenerator(dockergen.GeneratorConfig{
Endpoint: endpoint,
TLSKey: tlsKey,
TLSCert: tlsCert,
TLSCACert: tlsCaCert,
TLSVerify: tlsVerify,
All: all,
ConfigFile: configs,
})

Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Config struct {
NotifyContainers map[string]docker.Signal
OnlyExposed bool
OnlyPublished bool
IncludeStopped bool
Interval int
KeepBlankLines bool
}
Expand Down
5 changes: 5 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type Volume struct {
ReadWrite bool
}

type State struct {
Running bool
}

type RuntimeContainer struct {
ID string
Addresses []Address
Expand All @@ -90,6 +94,7 @@ type RuntimeContainer struct {
IP6LinkLocal string
IP6Global string
Mounts []Mount
State State
}

func (r *RuntimeContainer) Equals(o RuntimeContainer) bool {
Expand Down
8 changes: 7 additions & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type generator struct {
Endpoint string
TLSVerify bool
TLSCert, TLSCaCert, TLSKey string
All bool

wg sync.WaitGroup
}
Expand All @@ -31,6 +32,7 @@ type GeneratorConfig struct {
TLSKey string
TLSCACert string
TLSVerify bool
All bool

ConfigFile ConfigFile
}
Expand Down Expand Up @@ -61,6 +63,7 @@ func NewGenerator(gc GeneratorConfig) (*generator, error) {
TLSCert: gc.TLSCert,
TLSCaCert: gc.TLSCACert,
TLSKey: gc.TLSKey,
All: gc.All,
Configs: gc.ConfigFile,
}, nil
}
Expand Down Expand Up @@ -280,7 +283,7 @@ func (g *generator) getContainers() ([]*RuntimeContainer, error) {
SetServerInfo(apiInfo)

apiContainers, err := g.Client.ListContainers(docker.ListContainersOptions{
All: false,
All: g.All,
Size: false,
})
if err != nil {
Expand All @@ -303,6 +306,9 @@ func (g *generator) getContainers() ([]*RuntimeContainer, error) {
Repository: repository,
Tag: tag,
},
State: State{
Running: container.State.Running,
},
Name: strings.TrimLeft(container.Name, "/"),
Hostname: container.Config.Hostname,
Gateway: container.NetworkSettings.Gateway,
Expand Down
23 changes: 19 additions & 4 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func keys(input interface{}) (interface{}, error) {

vk := val.MapKeys()
k := make([]interface{}, val.Len())
for i, _ := range k {
for i := range k {
k[i] = vk[i].Interface()
}

Expand Down Expand Up @@ -425,22 +425,37 @@ func newTemplate(name string) *template.Template {
return tmpl
}

func filterRunning(config Config, containers Context) Context {
if config.IncludeStopped {
return containers
} else {
filteredContainers := Context{}
for _, container := range containers {
if container.State.Running {
filteredContainers = append(filteredContainers, container)
}
}
return filteredContainers
}
}

func GenerateFile(config Config, containers Context) bool {
filteredRunningContainers := filterRunning(config, containers)
filteredContainers := Context{}
if config.OnlyPublished {
for _, container := range containers {
for _, container := range filteredRunningContainers {
if len(container.PublishedAddresses()) > 0 {
filteredContainers = append(filteredContainers, container)
}
}
} else if config.OnlyExposed {
for _, container := range containers {
for _, container := range filteredRunningContainers {
if len(container.Addresses) > 0 {
filteredContainers = append(filteredContainers, container)
}
}
} else {
filteredContainers = containers
filteredContainers = filteredRunningContainers
}

contents := executeTemplate(config.Template, filteredContainers)
Expand Down