Skip to content

Commit 5043565

Browse files
committed
Add server info
Based off of #128
1 parent 3b55980 commit 5043565

File tree

5 files changed

+168
-107
lines changed

5 files changed

+168
-107
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ type SwarmNode struct {
252252
Name string
253253
Address Address
254254
}
255+
256+
// Accessible from the root in templates as .Docker
257+
type Docker struct {
258+
Name string
259+
NumContainers int
260+
NumImages int
261+
Version string
262+
ApiVersion string
263+
GoVersion string
264+
OperatingSystem string
265+
Architecture string
266+
}
267+
268+
// Host environment variables accessible from root in templates as .Env
269+
255270
```
256271

257272
For example, this is a JSON version of an emitted RuntimeContainer struct:

context.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,51 @@
11
package dockergen
22

3-
import "os"
3+
import (
4+
"os"
5+
"sync"
6+
7+
"github.com/fsouza/go-dockerclient"
8+
)
9+
10+
var (
11+
mu sync.RWMutex
12+
dockerInfo Docker
13+
dockerEnv *docker.Env
14+
)
415

516
type Context []*RuntimeContainer
617

718
func (c *Context) Env() map[string]string {
819
return splitKeyValueSlice(os.Environ())
920
}
1021

22+
func (c *Context) Docker() Docker {
23+
mu.RLock()
24+
defer mu.RUnlock()
25+
return dockerInfo
26+
}
27+
28+
func SetServerInfo(d *docker.Env) {
29+
mu.Lock()
30+
defer mu.Unlock()
31+
dockerInfo = Docker{
32+
Name: d.Get("Name"),
33+
NumContainers: d.GetInt("Containers"),
34+
NumImages: d.GetInt("Images"),
35+
Version: dockerEnv.Get("Version"),
36+
ApiVersion: dockerEnv.Get("ApiVersion"),
37+
GoVersion: dockerEnv.Get("GoVersion"),
38+
OperatingSystem: dockerEnv.Get("Os"),
39+
Architecture: dockerEnv.Get("Arch"),
40+
}
41+
}
42+
43+
func SetDockerEnv(d *docker.Env) {
44+
mu.Lock()
45+
defer mu.Unlock()
46+
dockerEnv = d
47+
}
48+
1149
type Address struct {
1250
IP string
1351
IP6LinkLocal string
@@ -51,7 +89,6 @@ type RuntimeContainer struct {
5189
IP string
5290
IP6LinkLocal string
5391
IP6Global string
54-
Server Server
5592
}
5693

5794
func (r *RuntimeContainer) Equals(o RuntimeContainer) bool {
@@ -91,14 +128,10 @@ type SwarmNode struct {
91128
Address Address
92129
}
93130

94-
type Server struct {
95-
Name string
96-
NumContainers int
97-
NumImages int
98-
Docker Docker
99-
}
100-
101131
type Docker struct {
132+
Name string
133+
NumContainers int
134+
NumImages int
102135
Version string
103136
ApiVersion string
104137
GoVersion string

docker_client.go

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dockergen
33
import (
44
"errors"
55
"fmt"
6-
"log"
76
"os"
87
"strconv"
98
"strings"
@@ -120,99 +119,6 @@ func splitDockerImage(img string) (string, string, string) {
120119
return registry, repository, tag
121120
}
122121

123-
func GetContainers(client *docker.Client) ([]*RuntimeContainer, error) {
124-
apiContainers, err := client.ListContainers(docker.ListContainersOptions{
125-
All: false,
126-
Size: false,
127-
})
128-
if err != nil {
129-
return nil, err
130-
}
131-
132-
containers := []*RuntimeContainer{}
133-
for _, apiContainer := range apiContainers {
134-
container, err := client.InspectContainer(apiContainer.ID)
135-
if err != nil {
136-
log.Printf("error inspecting container: %s: %s\n", apiContainer.ID, err)
137-
continue
138-
}
139-
140-
registry, repository, tag := splitDockerImage(container.Config.Image)
141-
runtimeContainer := &RuntimeContainer{
142-
ID: container.ID,
143-
Image: DockerImage{
144-
Registry: registry,
145-
Repository: repository,
146-
Tag: tag,
147-
},
148-
Name: strings.TrimLeft(container.Name, "/"),
149-
Hostname: container.Config.Hostname,
150-
Gateway: container.NetworkSettings.Gateway,
151-
Addresses: []Address{},
152-
Networks: []Network{},
153-
Env: make(map[string]string),
154-
Volumes: make(map[string]Volume),
155-
Node: SwarmNode{},
156-
Labels: make(map[string]string),
157-
IP: container.NetworkSettings.IPAddress,
158-
IP6LinkLocal: container.NetworkSettings.LinkLocalIPv6Address,
159-
IP6Global: container.NetworkSettings.GlobalIPv6Address,
160-
}
161-
for k, v := range container.NetworkSettings.Ports {
162-
address := Address{
163-
IP: container.NetworkSettings.IPAddress,
164-
IP6LinkLocal: container.NetworkSettings.LinkLocalIPv6Address,
165-
IP6Global: container.NetworkSettings.GlobalIPv6Address,
166-
Port: k.Port(),
167-
Proto: k.Proto(),
168-
}
169-
if len(v) > 0 {
170-
address.HostPort = v[0].HostPort
171-
address.HostIP = v[0].HostIP
172-
}
173-
runtimeContainer.Addresses = append(runtimeContainer.Addresses,
174-
address)
175-
176-
}
177-
for k, v := range container.NetworkSettings.Networks {
178-
network := Network{
179-
IP: v.IPAddress,
180-
Name: k,
181-
Gateway: v.Gateway,
182-
EndpointID: v.EndpointID,
183-
IPv6Gateway: v.IPv6Gateway,
184-
GlobalIPv6Address: v.GlobalIPv6Address,
185-
MacAddress: v.MacAddress,
186-
GlobalIPv6PrefixLen: v.GlobalIPv6PrefixLen,
187-
IPPrefixLen: v.IPPrefixLen,
188-
}
189-
190-
runtimeContainer.Networks = append(runtimeContainer.Networks,
191-
network)
192-
}
193-
for k, v := range container.Volumes {
194-
runtimeContainer.Volumes[k] = Volume{
195-
Path: k,
196-
HostPath: v,
197-
ReadWrite: container.VolumesRW[k],
198-
}
199-
}
200-
if container.Node != nil {
201-
runtimeContainer.Node.ID = container.Node.ID
202-
runtimeContainer.Node.Name = container.Node.Name
203-
runtimeContainer.Node.Address = Address{
204-
IP: container.Node.IP,
205-
}
206-
}
207-
208-
runtimeContainer.Env = splitKeyValueSlice(container.Config.Env)
209-
runtimeContainer.Labels = container.Config.Labels
210-
containers = append(containers, runtimeContainer)
211-
}
212-
return containers, nil
213-
214-
}
215-
216122
// pathExists returns whether the given file or directory exists or not
217123
func pathExists(path string) (bool, error) {
218124
_, err := os.Stat(path)

generator.go

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ type generator struct {
1818
TLSVerify bool
1919
TLSCert, TLSCaCert, TLSKey string
2020

21-
dockerInfo Server
22-
2321
wg sync.WaitGroup
2422
}
2523

@@ -45,6 +43,14 @@ func NewGenerator(gc GeneratorConfig) (*generator, error) {
4543
return nil, fmt.Errorf("Unable to create docker client: %s", err)
4644
}
4745

46+
apiVersion, err := client.Version()
47+
if err != nil {
48+
log.Printf("error retrieving docker server version info: %s\n", err)
49+
}
50+
51+
// Grab the docker daemon info once and hold onto it
52+
SetDockerEnv(apiVersion)
53+
4854
return &generator{
4955
Client: client,
5056
Endpoint: gc.Endpoint,
@@ -66,7 +72,7 @@ func (g *generator) Generate() error {
6672
}
6773

6874
func (g *generator) generateFromContainers(client *docker.Client) {
69-
containers, err := GetContainers(client)
75+
containers, err := g.getContainers(client)
7076
if err != nil {
7177
log.Printf("error listing containers: %s\n", err)
7278
return
@@ -99,7 +105,7 @@ func (g *generator) generateAtInterval(client *docker.Client, configs ConfigFile
99105
for {
100106
select {
101107
case <-ticker.C:
102-
containers, err := GetContainers(client)
108+
containers, err := g.getContainers(client)
103109
if err != nil {
104110
log.Printf("Error listing containers: %s\n", err)
105111
continue
@@ -238,3 +244,103 @@ func (g *generator) sendSignalToContainer(client *docker.Client, config Config)
238244
}
239245
}
240246
}
247+
248+
func (g *generator) getContainers(client *docker.Client) ([]*RuntimeContainer, error) {
249+
apiInfo, err := client.Info()
250+
if err != nil {
251+
log.Printf("error retrieving docker server info: %s\n", err)
252+
}
253+
254+
SetServerInfo(apiInfo)
255+
256+
apiContainers, err := client.ListContainers(docker.ListContainersOptions{
257+
All: false,
258+
Size: false,
259+
})
260+
if err != nil {
261+
return nil, err
262+
}
263+
264+
containers := []*RuntimeContainer{}
265+
for _, apiContainer := range apiContainers {
266+
container, err := client.InspectContainer(apiContainer.ID)
267+
if err != nil {
268+
log.Printf("error inspecting container: %s: %s\n", apiContainer.ID, err)
269+
continue
270+
}
271+
272+
registry, repository, tag := splitDockerImage(container.Config.Image)
273+
runtimeContainer := &RuntimeContainer{
274+
ID: container.ID,
275+
Image: DockerImage{
276+
Registry: registry,
277+
Repository: repository,
278+
Tag: tag,
279+
},
280+
Name: strings.TrimLeft(container.Name, "/"),
281+
Hostname: container.Config.Hostname,
282+
Gateway: container.NetworkSettings.Gateway,
283+
Addresses: []Address{},
284+
Networks: []Network{},
285+
Env: make(map[string]string),
286+
Volumes: make(map[string]Volume),
287+
Node: SwarmNode{},
288+
Labels: make(map[string]string),
289+
IP: container.NetworkSettings.IPAddress,
290+
IP6LinkLocal: container.NetworkSettings.LinkLocalIPv6Address,
291+
IP6Global: container.NetworkSettings.GlobalIPv6Address,
292+
}
293+
for k, v := range container.NetworkSettings.Ports {
294+
address := Address{
295+
IP: container.NetworkSettings.IPAddress,
296+
IP6LinkLocal: container.NetworkSettings.LinkLocalIPv6Address,
297+
IP6Global: container.NetworkSettings.GlobalIPv6Address,
298+
Port: k.Port(),
299+
Proto: k.Proto(),
300+
}
301+
if len(v) > 0 {
302+
address.HostPort = v[0].HostPort
303+
address.HostIP = v[0].HostIP
304+
}
305+
runtimeContainer.Addresses = append(runtimeContainer.Addresses,
306+
address)
307+
308+
}
309+
for k, v := range container.NetworkSettings.Networks {
310+
network := Network{
311+
IP: v.IPAddress,
312+
Name: k,
313+
Gateway: v.Gateway,
314+
EndpointID: v.EndpointID,
315+
IPv6Gateway: v.IPv6Gateway,
316+
GlobalIPv6Address: v.GlobalIPv6Address,
317+
MacAddress: v.MacAddress,
318+
GlobalIPv6PrefixLen: v.GlobalIPv6PrefixLen,
319+
IPPrefixLen: v.IPPrefixLen,
320+
}
321+
322+
runtimeContainer.Networks = append(runtimeContainer.Networks,
323+
network)
324+
}
325+
for k, v := range container.Volumes {
326+
runtimeContainer.Volumes[k] = Volume{
327+
Path: k,
328+
HostPath: v,
329+
ReadWrite: container.VolumesRW[k],
330+
}
331+
}
332+
if container.Node != nil {
333+
runtimeContainer.Node.ID = container.Node.ID
334+
runtimeContainer.Node.Name = container.Node.Name
335+
runtimeContainer.Node.Address = Address{
336+
IP: container.Node.IP,
337+
}
338+
}
339+
340+
runtimeContainer.Env = splitKeyValueSlice(container.Config.Env)
341+
runtimeContainer.Labels = container.Config.Labels
342+
containers = append(containers, runtimeContainer)
343+
}
344+
return containers, nil
345+
346+
}

templates/etcd.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#!/bin/bash
33

44
# Genenerated by {{ .Env.USER }}
5+
# Docker Version {{ .Docker.Version }}
56

67
{{range $key, $value := .}}
78
{{ $addrLen := len $value.Addresses }}

0 commit comments

Comments
 (0)