forked from nginx-proxy/docker-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
212 lines (180 loc) · 4.03 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package dockergen
import (
"bufio"
"os"
"regexp"
"sync"
"github.com/fsouza/go-dockerclient"
)
var (
mu sync.RWMutex
dockerInfo Docker
dockerEnv *docker.Env
)
type Context []*RuntimeContainer
func (c *Context) Env() map[string]string {
return splitKeyValueSlice(os.Environ())
}
func (c *Context) Docker() Docker {
mu.RLock()
defer mu.RUnlock()
return dockerInfo
}
func SetServerInfo(d *docker.DockerInfo) {
mu.Lock()
defer mu.Unlock()
dockerInfo = Docker{
Name: d.Name,
NumContainers: d.Containers,
NumImages: d.Images,
Version: dockerEnv.Get("Version"),
ApiVersion: dockerEnv.Get("ApiVersion"),
GoVersion: dockerEnv.Get("GoVersion"),
OperatingSystem: dockerEnv.Get("Os"),
Architecture: dockerEnv.Get("Arch"),
CurrentContainerID: GetCurrentContainerID(),
}
}
func SetDockerEnv(d *docker.Env) {
mu.Lock()
defer mu.Unlock()
dockerEnv = d
}
type Address struct {
IP string
IP6LinkLocal string
IP6Global string
Port string
HostPort string
Proto string
HostIP string
}
type Network struct {
IP string
Name string
Gateway string
EndpointID string
IPv6Gateway string
GlobalIPv6Address string
MacAddress string
GlobalIPv6PrefixLen int
IPPrefixLen int
}
type Volume struct {
Path string
HostPath string
ReadWrite bool
}
type State struct {
Running bool
}
type RuntimeContainer struct {
ID string
Addresses []Address
Networks []Network
Gateway string
Name string
Hostname string
Image DockerImage
Env map[string]string
Volumes map[string]Volume
Node SwarmNode
Labels map[string]string
IP string
IP6LinkLocal string
IP6Global string
Mounts []Mount
State State
}
func (r *RuntimeContainer) Equals(o RuntimeContainer) bool {
return r.ID == o.ID && r.Image == o.Image
}
func (r *RuntimeContainer) PublishedAddresses() []Address {
mapped := []Address{}
for _, address := range r.Addresses {
if address.HostPort != "" {
mapped = append(mapped, address)
}
}
return mapped
}
type DockerImage struct {
Registry string
Repository string
Tag string
}
func (i *DockerImage) String() string {
ret := i.Repository
if i.Registry != "" {
ret = i.Registry + "/" + i.Repository
}
if i.Tag != "" {
ret = ret + ":" + i.Tag
}
return ret
}
type SwarmNode struct {
ID string
Name string
Address Address
}
type Mount struct {
Name string
Source string
Destination string
Driver string
Mode string
RW bool
}
type Docker struct {
Name string
NumContainers int
NumImages int
Version string
ApiVersion string
GoVersion string
OperatingSystem string
Architecture string
CurrentContainerID string
}
func GetCurrentContainerID() string {
file, err := os.Open("/proc/self/cgroup")
if err != nil {
return ""
}
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
_, lines, err := bufio.ScanLines([]byte(scanner.Text()), true)
if err == nil {
strLines := string(lines)
if id := matchDockerCurrentContainerID(strLines); id != "" {
return id
} else if id := matchECSCurrentContainerID(strLines); id != "" {
return id
}
}
}
return ""
}
func matchDockerCurrentContainerID(lines string) string {
regex := "/docker[/-]([[:alnum:]]{64})(\\.scope)?$"
re := regexp.MustCompilePOSIX(regex)
if re.MatchString(lines) {
submatches := re.FindStringSubmatch(string(lines))
containerID := submatches[1]
return containerID
}
return ""
}
func matchECSCurrentContainerID(lines string) string {
regex := "/ecs\\/[^\\/]+\\/(.+)$"
re := regexp.MustCompilePOSIX(regex)
if re.MatchString(string(lines)) {
submatches := re.FindStringSubmatch(string(lines))
containerID := submatches[1]
return containerID
}
return ""
}