Skip to content

Commit 351114f

Browse files
committed
include stopped containers
1 parent 94e2d51 commit 351114f

File tree

6 files changed

+50
-5
lines changed

6 files changed

+50
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ Options:
101101
only include containers with exposed ports
102102
-only-published
103103
only include containers with published ports (implies -only-exposed)
104+
-include-stopped
105+
include stopped containers
104106
-tlscacert string
105107
path to TLS CA certificate file (default "/Users/jason/.docker/machine/machines/default/ca.pem")
106108
-tlscert string
@@ -213,6 +215,7 @@ type RuntimeContainer struct {
213215
IP6LinkLocal string
214216
IP6Global string
215217
Mounts []Mount
218+
State State
216219
}
217220

218221
type Address struct {
@@ -264,6 +267,10 @@ type SwarmNode struct {
264267
Address Address
265268
}
266269

270+
type State struct {
271+
Running bool
272+
}
273+
267274
// Accessible from the root in templates as .Docker
268275
type Docker struct {
269276
Name string

cmd/docker-gen/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
notifySigHUPContainerID string
2525
onlyExposed bool
2626
onlyPublished bool
27+
includeStopped bool
2728
configFiles stringslice
2829
configs dockergen.ConfigFile
2930
interval int
@@ -89,6 +90,7 @@ func initFlags() {
8990

9091
flag.BoolVar(&onlyPublished, "only-published", false,
9192
"only include containers with published ports (implies -only-exposed)")
93+
flag.BoolVar(&includeStopped, "include-stopped", false, "include stopped containers")
9294
flag.BoolVar(&notifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command")
9395
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
9496
flag.StringVar(&notifySigHUPContainerID, "notify-sighup", "",
@@ -136,6 +138,7 @@ func main() {
136138
NotifyContainers: make(map[string]docker.Signal),
137139
OnlyExposed: onlyExposed,
138140
OnlyPublished: onlyPublished,
141+
IncludeStopped: includeStopped,
139142
Interval: interval,
140143
KeepBlankLines: keepBlankLines,
141144
}
@@ -146,12 +149,20 @@ func main() {
146149
Config: []dockergen.Config{config}}
147150
}
148151

152+
all := true
153+
for _, config := range configs.Config {
154+
if config.IncludeStopped {
155+
all = true
156+
}
157+
}
158+
149159
generator, err := dockergen.NewGenerator(dockergen.GeneratorConfig{
150160
Endpoint: endpoint,
151161
TLSKey: tlsKey,
152162
TLSCert: tlsCert,
153163
TLSCACert: tlsCaCert,
154164
TLSVerify: tlsVerify,
165+
All: all,
155166
ConfigFile: configs,
156167
})
157168

config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Config struct {
1111
NotifyContainers map[string]docker.Signal
1212
OnlyExposed bool
1313
OnlyPublished bool
14+
IncludeStopped bool
1415
Interval int
1516
KeepBlankLines bool
1617
}

context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ type Volume struct {
7474
ReadWrite bool
7575
}
7676

77+
type State struct {
78+
Running bool
79+
}
80+
7781
type RuntimeContainer struct {
7882
ID string
7983
Addresses []Address
@@ -90,6 +94,7 @@ type RuntimeContainer struct {
9094
IP6LinkLocal string
9195
IP6Global string
9296
Mounts []Mount
97+
State State
9398
}
9499

95100
func (r *RuntimeContainer) Equals(o RuntimeContainer) bool {

generator.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type generator struct {
2020
Endpoint string
2121
TLSVerify bool
2222
TLSCert, TLSCaCert, TLSKey string
23+
All bool
2324

2425
wg sync.WaitGroup
2526
}
@@ -31,6 +32,7 @@ type GeneratorConfig struct {
3132
TLSKey string
3233
TLSCACert string
3334
TLSVerify bool
35+
All bool
3436

3537
ConfigFile ConfigFile
3638
}
@@ -61,6 +63,7 @@ func NewGenerator(gc GeneratorConfig) (*generator, error) {
6163
TLSCert: gc.TLSCert,
6264
TLSCaCert: gc.TLSCACert,
6365
TLSKey: gc.TLSKey,
66+
All: gc.All,
6467
Configs: gc.ConfigFile,
6568
}, nil
6669
}
@@ -280,7 +283,7 @@ func (g *generator) getContainers() ([]*RuntimeContainer, error) {
280283
SetServerInfo(apiInfo)
281284

282285
apiContainers, err := g.Client.ListContainers(docker.ListContainersOptions{
283-
All: false,
286+
All: g.All,
284287
Size: false,
285288
})
286289
if err != nil {
@@ -303,6 +306,9 @@ func (g *generator) getContainers() ([]*RuntimeContainer, error) {
303306
Repository: repository,
304307
Tag: tag,
305308
},
309+
State: State{
310+
Running: container.State.Running,
311+
},
306312
Name: strings.TrimLeft(container.Name, "/"),
307313
Hostname: container.Config.Hostname,
308314
Gateway: container.NetworkSettings.Gateway,

template.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func keys(input interface{}) (interface{}, error) {
233233

234234
vk := val.MapKeys()
235235
k := make([]interface{}, val.Len())
236-
for i, _ := range k {
236+
for i := range k {
237237
k[i] = vk[i].Interface()
238238
}
239239

@@ -425,22 +425,37 @@ func newTemplate(name string) *template.Template {
425425
return tmpl
426426
}
427427

428+
func filterRunning(config Config, containers Context) Context {
429+
if config.IncludeStopped {
430+
return containers
431+
} else {
432+
filteredContainers := Context{}
433+
for _, container := range containers {
434+
if container.State.Running {
435+
filteredContainers = append(filteredContainers, container)
436+
}
437+
}
438+
return filteredContainers
439+
}
440+
}
441+
428442
func GenerateFile(config Config, containers Context) bool {
443+
filteredRunningContainers := filterRunning(config, containers)
429444
filteredContainers := Context{}
430445
if config.OnlyPublished {
431-
for _, container := range containers {
446+
for _, container := range filteredRunningContainers {
432447
if len(container.PublishedAddresses()) > 0 {
433448
filteredContainers = append(filteredContainers, container)
434449
}
435450
}
436451
} else if config.OnlyExposed {
437-
for _, container := range containers {
452+
for _, container := range filteredRunningContainers {
438453
if len(container.Addresses) > 0 {
439454
filteredContainers = append(filteredContainers, container)
440455
}
441456
}
442457
} else {
443-
filteredContainers = containers
458+
filteredContainers = filteredRunningContainers
444459
}
445460

446461
contents := executeTemplate(config.Template, filteredContainers)

0 commit comments

Comments
 (0)