detached bca075e

Add test function to determine if Gordon is running inside a container

commit
bca075e2159fbcf17f616ce19dd9c6284c3fa329
author
bnema · · 2023-11-03 08:39 UTC
parent
6b3cda7
signature
unsigned
4 files · +80 -5
M cmd/cli/main.go +9 -0
@@ -8,6 +8,7 @@ import (
88 "github.com/bnema/gordon/internal/cli/cmd"
99 "github.com/bnema/gordon/internal/common"
1010 "github.com/bnema/gordon/internal/server"
11+ "github.com/bnema/gordon/pkg/docker"
1112 "github.com/spf13/cobra"
1213 )
1314
@@ -44,5 +45,13 @@ func main() {
4445 // Set the BuildVersion
4546 s.Config.Build.BuildVersion = build
4647
48+ inContainer, err := docker.IsRunningInContainer()
49+ if err != nil {
50+ fmt.Printf("Error checking if running in a container: %s\n", err)
51+ } else if inContainer {
52+ fmt.Println("Running inside a container.")
53+ } else {
54+ fmt.Println("Not running inside a container.")
55+ }
4756 Execute(a, s)
4857 }
@@ -8,6 +8,7 @@ import (
8 "github.com/bnema/gordon/internal/cli/cmd"8 "github.com/bnema/gordon/internal/cli/cmd"
9 "github.com/bnema/gordon/internal/common"9 "github.com/bnema/gordon/internal/common"
10 "github.com/bnema/gordon/internal/server"10 "github.com/bnema/gordon/internal/server"
11+ "github.com/bnema/gordon/pkg/docker"
11 "github.com/spf13/cobra"12 "github.com/spf13/cobra"
12 )13 )
13 14
@@ -44,5 +45,13 @@ func main() {
44 // Set the BuildVersion45 // Set the BuildVersion
45 s.Config.Build.BuildVersion = build46 s.Config.Build.BuildVersion = build
46 47
48+ inContainer, err := docker.IsRunningInContainer()
49+ if err != nil {
50+ fmt.Printf("Error checking if running in a container: %s\n", err)
51+ } else if inContainer {
52+ fmt.Println("Running inside a container.")
53+ } else {
54+ fmt.Println("Not running inside a container.")
55+ }
47 Execute(a, s)56 Execute(a, s)
48 }57 }
M internal/common/initconfig.go +5 -5
@@ -165,12 +165,12 @@ func (config *Config) GetToken() (string, error) {
165165
166166 }
167167
168-// GetBuildVersion returns the build version
169-func (config *BuildConfig) GetBuildVersion() string {
170- return config.BuildVersion
171-}
172-
173168 // GetRunEnv returns the run environment
174169 func (config *BuildConfig) GetRunEnv() string {
175170 return config.RunEnv
176171 }
172+
173+// GetVersion returns the version
174+func (config *Config) GetVersion() string {
175+ return config.Build.BuildVersion
176+}
@@ -165,12 +165,12 @@ func (config *Config) GetToken() (string, error) {
165 165
166 }166 }
167 167
168-// GetBuildVersion returns the build version
169-func (config *BuildConfig) GetBuildVersion() string {
170- return config.BuildVersion
171-}
172-
173 // GetRunEnv returns the run environment168 // GetRunEnv returns the run environment
174 func (config *BuildConfig) GetRunEnv() string {169 func (config *BuildConfig) GetRunEnv() string {
175 return config.RunEnv170 return config.RunEnv
176 }171 }
172+
173+// GetVersion returns the version
174+func (config *Config) GetVersion() string {
175+ return config.Build.BuildVersion
176+}
A internal/common/version.go +16 -0
@@ -0,0 +1,16 @@
1+package common
2+
3+import "time"
4+
5+const checkVersionURL = "https://gordon.bnema.dev/version"
6+
7+var timer = time.NewTicker(1 * time.Minute)
8+
9+type CurrentVersion struct {
10+ isDocker bool
11+ Version string `json:"version"`
12+ Commit string `json:"commit"`
13+ Sha256ID string `json:"sha256id"`
14+}
15+
16+type Response interface{}
@@ -0,0 +1,16 @@
1+package common
2+
3+import "time"
4+
5+const checkVersionURL = "https://gordon.bnema.dev/version"
6+
7+var timer = time.NewTicker(1 * time.Minute)
8+
9+type CurrentVersion struct {
10+ isDocker bool
11+ Version string `json:"version"`
12+ Commit string `json:"commit"`
13+ Sha256ID string `json:"sha256id"`
14+}
15+
16+type Response interface{}
A pkg/docker/detect.go +50 -0
@@ -0,0 +1,50 @@
1+package docker
2+
3+import (
4+ "bufio"
5+ "os"
6+ "strings"
7+)
8+
9+func fileExists(filepath string) bool {
10+ _, err := os.Stat(filepath)
11+ return !os.IsNotExist(err)
12+}
13+
14+func checkCgroup() (bool, error) {
15+ // Open the cgroup file for the init process
16+ file, err := os.Open("/proc/1/cgroup")
17+ if err != nil {
18+ return false, err
19+ }
20+ defer file.Close()
21+
22+ // Scan through the cgroup file looking for docker or kube pod paths
23+ scanner := bufio.NewScanner(file)
24+ for scanner.Scan() {
25+ line := scanner.Text()
26+ if strings.Contains(line, "docker") || strings.Contains(line, "kubepods") {
27+ return true, nil
28+ }
29+ }
30+ if err := scanner.Err(); err != nil {
31+ return false, err
32+ }
33+
34+ return false, nil
35+}
36+
37+func IsRunningInContainer() (bool, error) {
38+ // Check for .dockerenv file
39+ if fileExists("/.dockerenv") {
40+ return true, nil
41+ }
42+
43+ // Check cgroup for docker or kube pod paths
44+ inContainer, err := checkCgroup()
45+ if err != nil {
46+ return false, err
47+ }
48+
49+ return inContainer, nil
50+}
@@ -0,0 +1,50 @@
1+package docker
2+
3+import (
4+ "bufio"
5+ "os"
6+ "strings"
7+)
8+
9+func fileExists(filepath string) bool {
10+ _, err := os.Stat(filepath)
11+ return !os.IsNotExist(err)
12+}
13+
14+func checkCgroup() (bool, error) {
15+ // Open the cgroup file for the init process
16+ file, err := os.Open("/proc/1/cgroup")
17+ if err != nil {
18+ return false, err
19+ }
20+ defer file.Close()
21+
22+ // Scan through the cgroup file looking for docker or kube pod paths
23+ scanner := bufio.NewScanner(file)
24+ for scanner.Scan() {
25+ line := scanner.Text()
26+ if strings.Contains(line, "docker") || strings.Contains(line, "kubepods") {
27+ return true, nil
28+ }
29+ }
30+ if err := scanner.Err(); err != nil {
31+ return false, err
32+ }
33+
34+ return false, nil
35+}
36+
37+func IsRunningInContainer() (bool, error) {
38+ // Check for .dockerenv file
39+ if fileExists("/.dockerenv") {
40+ return true, nil
41+ }
42+
43+ // Check cgroup for docker or kube pod paths
44+ inContainer, err := checkCgroup()
45+ if err != nil {
46+ return false, err
47+ }
48+
49+ return inContainer, nil
50+}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close