Skip to content

Commit d48e1f3

Browse files
committed
Merge pull request #130 from AXA-GROUP-SOLUTIONS/include_stopped
include stopped containers
2 parents 0de7d92 + 351114f commit d48e1f3

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
@@ -219,6 +221,7 @@ type RuntimeContainer struct {
219221
IP6LinkLocal string
220222
IP6Global string
221223
Mounts []Mount
224+
State State
222225
}
223226

224227
type Address struct {
@@ -270,6 +273,10 @@ type SwarmNode struct {
270273
Address Address
271274
}
272275

276+
type State struct {
277+
Running bool
278+
}
279+
273280
// Accessible from the root in templates as .Docker
274281
type Docker struct {
275282
Name string

cmd/docker-gen/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
notifySigHUPContainerID string
2626
onlyExposed bool
2727
onlyPublished bool
28+
includeStopped bool
2829
configFiles stringslice
2930
configs dockergen.ConfigFile
3031
interval int
@@ -91,6 +92,7 @@ func initFlags() {
9192

9293
flag.BoolVar(&onlyPublished, "only-published", false,
9394
"only include containers with published ports (implies -only-exposed)")
95+
flag.BoolVar(&includeStopped, "include-stopped", false, "include stopped containers")
9496
flag.BoolVar(&notifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command")
9597
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
9698
flag.StringVar(&notifySigHUPContainerID, "notify-sighup", "",
@@ -143,6 +145,7 @@ func main() {
143145
NotifyContainers: make(map[string]docker.Signal),
144146
OnlyExposed: onlyExposed,
145147
OnlyPublished: onlyPublished,
148+
IncludeStopped: includeStopped,
146149
Interval: interval,
147150
KeepBlankLines: keepBlankLines,
148151
}
@@ -153,12 +156,20 @@ func main() {
153156
Config: []dockergen.Config{config}}
154157
}
155158

159+
all := true
160+
for _, config := range configs.Config {
161+
if config.IncludeStopped {
162+
all = true
163+
}
164+
}
165+
156166
generator, err := dockergen.NewGenerator(dockergen.GeneratorConfig{
157167
Endpoint: endpoint,
158168
TLSKey: tlsKey,
159169
TLSCert: tlsCert,
160170
TLSCACert: tlsCaCert,
161171
TLSVerify: tlsVerify,
172+
All: all,
162173
ConfigFile: configs,
163174
})
164175

config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Config struct {
1818
NotifyContainers map[string]docker.Signal
1919
OnlyExposed bool
2020
OnlyPublished bool
21+
IncludeStopped bool
2122
Interval int
2223
KeepBlankLines bool
2324
}

context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ type Volume struct {
7777
ReadWrite bool
7878
}
7979

80+
type State struct {
81+
Running bool
82+
}
83+
8084
type RuntimeContainer struct {
8185
ID string
8286
Addresses []Address
@@ -93,6 +97,7 @@ type RuntimeContainer struct {
9397
IP6LinkLocal string
9498
IP6Global string
9599
Mounts []Mount
100+
State State
96101
}
97102

98103
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
retry bool
@@ -32,6 +33,7 @@ type GeneratorConfig struct {
3233
TLSKey string
3334
TLSCACert string
3435
TLSVerify bool
36+
All bool
3537

3638
ConfigFile ConfigFile
3739
}
@@ -62,6 +64,7 @@ func NewGenerator(gc GeneratorConfig) (*generator, error) {
6264
TLSCert: gc.TLSCert,
6365
TLSCaCert: gc.TLSCACert,
6466
TLSKey: gc.TLSKey,
67+
All: gc.All,
6568
Configs: gc.ConfigFile,
6669
retry: true,
6770
}, nil
@@ -347,7 +350,7 @@ func (g *generator) getContainers() ([]*RuntimeContainer, error) {
347350
}
348351

349352
apiContainers, err := g.Client.ListContainers(docker.ListContainersOptions{
350-
All: false,
353+
All: g.All,
351354
Size: false,
352355
})
353356
if err != nil {
@@ -370,6 +373,9 @@ func (g *generator) getContainers() ([]*RuntimeContainer, error) {
370373
Repository: repository,
371374
Tag: tag,
372375
},
376+
State: State{
377+
Running: container.State.Running,
378+
},
373379
Name: strings.TrimLeft(container.Name, "/"),
374380
Hostname: container.Config.Hostname,
375381
Gateway: container.NetworkSettings.Gateway,

template.go

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

260260
vk := val.MapKeys()
261261
k := make([]interface{}, val.Len())
262-
for i, _ := range k {
262+
for i := range k {
263263
k[i] = vk[i].Interface()
264264
}
265265

@@ -452,22 +452,37 @@ func newTemplate(name string) *template.Template {
452452
return tmpl
453453
}
454454

455+
func filterRunning(config Config, containers Context) Context {
456+
if config.IncludeStopped {
457+
return containers
458+
} else {
459+
filteredContainers := Context{}
460+
for _, container := range containers {
461+
if container.State.Running {
462+
filteredContainers = append(filteredContainers, container)
463+
}
464+
}
465+
return filteredContainers
466+
}
467+
}
468+
455469
func GenerateFile(config Config, containers Context) bool {
470+
filteredRunningContainers := filterRunning(config, containers)
456471
filteredContainers := Context{}
457472
if config.OnlyPublished {
458-
for _, container := range containers {
473+
for _, container := range filteredRunningContainers {
459474
if len(container.PublishedAddresses()) > 0 {
460475
filteredContainers = append(filteredContainers, container)
461476
}
462477
}
463478
} else if config.OnlyExposed {
464-
for _, container := range containers {
479+
for _, container := range filteredRunningContainers {
465480
if len(container.Addresses) > 0 {
466481
filteredContainers = append(filteredContainers, container)
467482
}
468483
}
469484
} else {
470-
filteredContainers = containers
485+
filteredContainers = filteredRunningContainers
471486
}
472487

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

0 commit comments

Comments
 (0)