Skip to content

Commit

Permalink
Merge pull request #336 from btcpayserver/quwn3e
Browse files Browse the repository at this point in the history
Fix docker-gen breaking on new docker version (See #335)
  • Loading branch information
jwilder committed Apr 3, 2021
2 parents faaa781 + 5fb960a commit e51c234
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"regexp"
"sync"

"fmt"
"github.com/fsouza/go-dockerclient"
)

Expand Down Expand Up @@ -159,24 +159,25 @@ type Docker struct {
}

func GetCurrentContainerID() string {
file, err := os.Open("/proc/self/cgroup")

if err != nil {
return ""
}
filepaths := []string{"/proc/self/cgroup", "/proc/self/mountinfo"}

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
for _, filepath := range filepaths {
file, err := os.Open(filepath)
if err != nil {
continue
}
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
}
}
}
}
Expand All @@ -185,7 +186,8 @@ func GetCurrentContainerID() string {
}

func matchDockerCurrentContainerID(lines string) string {
regex := "/docker[/-]([[:alnum:]]{64})(\\.scope)?$"
hostname := os.Getenv("HOSTNAME")
regex := fmt.Sprintf("(%s[[:alnum:]]{52})", hostname)
re := regexp.MustCompilePOSIX(regex)

if re.MatchString(lines) {
Expand Down

0 comments on commit e51c234

Please sign in to comment.