Fix the inability to start with logger in docker contianer (#178)
* check for docker/lxc in isInteractive() for linux * more clear logic and function name * add some tests for linux part * fix test * cover coverage
This commit is contained in:
committed by
Daniel Theophanes
parent
28e7e9edbb
commit
4df36c9fc1
+34
-1
@@ -5,10 +5,13 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var cgroupFile = "/proc/1/cgroup"
|
||||
|
||||
type linuxSystemService struct {
|
||||
name string
|
||||
detect func() bool
|
||||
@@ -62,7 +65,37 @@ func init() {
|
||||
|
||||
func isInteractive() (bool, error) {
|
||||
// TODO: This is not true for user services.
|
||||
return os.Getppid() != 1, nil
|
||||
inContainer, err := isInContainer(cgroupFile)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return os.Getppid() != 1 || inContainer, nil
|
||||
}
|
||||
|
||||
// isInContainer checks if the service is being executed in docker or lxc
|
||||
// container.
|
||||
func isInContainer(cgroupPath string) (bool, error) {
|
||||
const maxlines = 5 // maximum lines to scan
|
||||
|
||||
f, err := os.Open(cgroupPath)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer f.Close()
|
||||
scan := bufio.NewScanner(f)
|
||||
|
||||
lines := 0
|
||||
for scan.Scan() && !(lines > maxlines) {
|
||||
if strings.Contains(scan.Text(), "docker") || strings.Contains(scan.Text(), "lxc") {
|
||||
return true, nil
|
||||
}
|
||||
lines++
|
||||
}
|
||||
if err := scan.Err(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var tf = map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user