@@ -2,11 +2,14 @@ package dockergen
2
2
3
3
import (
4
4
"bufio"
5
+ "bytes"
6
+ "fmt"
7
+ "io"
5
8
"os"
6
9
"regexp"
7
10
"sync"
8
- "fmt"
9
- "github.com/fsouza/go-dockerclient"
11
+
12
+ docker "github.com/fsouza/go-dockerclient"
10
13
)
11
14
12
15
var (
@@ -158,57 +161,85 @@ type Docker struct {
158
161
CurrentContainerID string
159
162
}
160
163
161
- func GetCurrentContainerID () string {
162
- filepaths := []string {"/proc/self/cgroup" , "/proc/self/mountinfo" }
164
+ // GetCurrentContainerID attempts to extract the current container ID from the provided file paths.
165
+ // If no files paths are provided, it will default to /proc/1/cpuset, /proc/self/cgroup and /proc/self/mountinfo.
166
+ // It attempts to match the HOSTNAME first then use the fallback method, and returns with the first valid match.
167
+ func GetCurrentContainerID (filepaths ... string ) (id string ) {
168
+ if len (filepaths ) == 0 {
169
+ filepaths = []string {"/proc/1/cpuset" , "/proc/self/cgroup" , "/proc/self/mountinfo" }
170
+ }
171
+
172
+ var files []io.Reader
163
173
164
174
for _ , filepath := range filepaths {
165
175
file , err := os .Open (filepath )
166
176
if err != nil {
167
177
continue
168
178
}
169
- reader := bufio .NewReader (file )
170
- scanner := bufio .NewScanner (reader )
171
- scanner .Split (bufio .ScanLines )
172
- for scanner .Scan () {
173
- _ , lines , err := bufio .ScanLines ([]byte (scanner .Text ()), true )
174
- if err == nil {
175
- strLines := string (lines )
176
- if id := matchDockerCurrentContainerID (strLines ); id != "" {
177
- return id
178
- } else if id := matchECSCurrentContainerID (strLines ); id != "" {
179
- return id
180
- }
179
+ defer file .Close ()
180
+ files = append (files , file )
181
+ }
182
+
183
+ reader := io .MultiReader (files ... )
184
+ var buffer bytes.Buffer
185
+ tee := io .TeeReader (reader , & buffer )
186
+
187
+ // We try to match a 64 character hex string starting with the hostname first
188
+ scanner := bufio .NewScanner (tee )
189
+ scanner .Split (bufio .ScanLines )
190
+ for scanner .Scan () {
191
+ _ , lines , err := bufio .ScanLines ([]byte (scanner .Text ()), true )
192
+ if err == nil {
193
+ strLines := string (lines )
194
+ if id = matchContainerIDWithHostname (strLines ); len (id ) == 64 {
195
+ return
181
196
}
182
197
}
183
198
}
184
199
185
- return ""
200
+ // If we didn't get any ID that matches the hostname, fall back to matching the first 64 character hex string
201
+ scanner = bufio .NewScanner (& buffer )
202
+ scanner .Split (bufio .ScanLines )
203
+ for scanner .Scan () {
204
+ _ , lines , err := bufio .ScanLines ([]byte (scanner .Text ()), true )
205
+ if err == nil {
206
+ strLines := string (lines )
207
+ if id = matchContainerID (strLines ); len (id ) == 64 {
208
+ return
209
+ }
210
+ }
211
+ }
212
+
213
+ return
186
214
}
187
215
188
- func matchDockerCurrentContainerID (lines string ) string {
216
+ func matchContainerIDWithHostname (lines string ) string {
189
217
hostname := os .Getenv ("HOSTNAME" )
190
- regex := fmt .Sprintf ("(%s[[:alnum:]]{52})" , hostname )
191
- re := regexp .MustCompilePOSIX (regex )
218
+ re := regexp .MustCompilePOSIX ("^[[:alnum:]]{12}$" )
192
219
193
- if re .MatchString (lines ) {
194
- submatches := re . FindStringSubmatch ( string ( lines ) )
195
- containerID := submatches [ 1 ]
220
+ if re .MatchString (hostname ) {
221
+ regex := fmt . Sprintf ( "(%s[[:alnum:]]{52})" , hostname )
222
+ re := regexp . MustCompilePOSIX ( regex )
196
223
197
- return containerID
224
+ if re .MatchString (lines ) {
225
+ submatches := re .FindStringSubmatch (string (lines ))
226
+ containerID := submatches [1 ]
227
+
228
+ return containerID
229
+ }
198
230
}
199
231
return ""
200
232
}
201
233
202
- func matchECSCurrentContainerID (lines string ) string {
203
- regex := "/ecs \\ /[^ \\ /]+ \\ /(.+)$ "
234
+ func matchContainerID (lines string ) string {
235
+ regex := "([[:alnum:]]{64}) "
204
236
re := regexp .MustCompilePOSIX (regex )
205
237
206
- if re .MatchString (string ( lines ) ) {
238
+ if re .MatchString (lines ) {
207
239
submatches := re .FindStringSubmatch (string (lines ))
208
240
containerID := submatches [1 ]
209
241
210
242
return containerID
211
243
}
212
-
213
244
return ""
214
245
}
0 commit comments