From 5f699042cf87246ecf23da074fd0f6da25f836fc Mon Sep 17 00:00:00 2001 From: mpl Date: Wed, 9 Nov 2016 18:34:15 +0100 Subject: [PATCH] misc/docker: restore check that we run in docker Fixes #744 Change-Id: Ide28a48c229c71a4305a507bb5a6671910d6a1ab --- misc/docker/release/build-binaries.go | 30 +++++++++++++++++-- misc/docker/release/zip-source.go | 30 +++++++++++++++++-- misc/docker/server/build-camlistore-server.go | 30 +++++++++++++++++-- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/misc/docker/release/build-binaries.go b/misc/docker/release/build-binaries.go index 02dcc3fe4..3fb618455 100644 --- a/misc/docker/release/build-binaries.go +++ b/misc/docker/release/build-binaries.go @@ -22,6 +22,7 @@ package main // import "camlistore.org/misc/docker/release" import ( "archive/tar" + "bufio" "compress/gzip" "flag" "fmt" @@ -174,11 +175,36 @@ func checkArgs() { } } +func inDocker() bool { + r, err := os.Open("/proc/self/cgroup") + if err != nil { + log.Fatalf(`can't open "/proc/self/cgroup": %v`, err) + } + defer r.Close() + sc := bufio.NewScanner(r) + for sc.Scan() { + l := sc.Text() + fields := strings.SplitN(l, ":", 3) + if len(fields) != 3 { + log.Fatal(`unexpected line in "/proc/self/cgroup"`) + } + if !strings.HasPrefix(fields[2], "/docker/") { + return false + } + } + if err := sc.Err(); err != nil { + log.Fatal(err) + } + return true +} + func main() { flag.Usage = usage flag.Parse() - // TODO(mpl): find another way to detect whether we're on docker, as - // /.dockerinit does not exist anymore apparently? No time for now. + if !inDocker() { + fmt.Fprintf(os.Stderr, "Usage error: this program should be run within a docker container, and is meant to be called from misc/docker/dock.go\n") + usage() + } checkArgs() getCamliSrc() diff --git a/misc/docker/release/zip-source.go b/misc/docker/release/zip-source.go index 0d2d5bfcc..892712794 100644 --- a/misc/docker/release/zip-source.go +++ b/misc/docker/release/zip-source.go @@ -23,6 +23,7 @@ package main import ( "archive/tar" "archive/zip" + "bufio" "compress/gzip" "flag" "fmt" @@ -321,11 +322,36 @@ func checkArgs() { } } +func inDocker() bool { + r, err := os.Open("/proc/self/cgroup") + if err != nil { + log.Fatalf(`can't open "/proc/self/cgroup": %v`, err) + } + defer r.Close() + sc := bufio.NewScanner(r) + for sc.Scan() { + l := sc.Text() + fields := strings.SplitN(l, ":", 3) + if len(fields) != 3 { + log.Fatal(`unexpected line in "/proc/self/cgroup"`) + } + if !strings.HasPrefix(fields[2], "/docker/") { + return false + } + } + if err := sc.Err(); err != nil { + log.Fatal(err) + } + return true +} + func main() { flag.Usage = usage flag.Parse() - // TODO(mpl): find another way to detect whether we're on docker, as - // /.dockerinit does not exist anymore apparently? No time for now. + if !inDocker() { + fmt.Fprintf(os.Stderr, "Usage error: this program should be run within a docker container, and is meant to be called from misc/docker/dock.go\n") + usage() + } checkArgs() getCamliSrc() diff --git a/misc/docker/server/build-camlistore-server.go b/misc/docker/server/build-camlistore-server.go index 52b89a3b1..33e1711f1 100644 --- a/misc/docker/server/build-camlistore-server.go +++ b/misc/docker/server/build-camlistore-server.go @@ -23,6 +23,7 @@ package main // import "camlistore.org/misc/docker/server" import ( "archive/tar" + "bufio" "compress/gzip" "flag" "fmt" @@ -184,11 +185,36 @@ func checkArgs() { } } +func inDocker() bool { + r, err := os.Open("/proc/self/cgroup") + if err != nil { + log.Fatalf(`can't open "/proc/self/cgroup": %v`, err) + } + defer r.Close() + sc := bufio.NewScanner(r) + for sc.Scan() { + l := sc.Text() + fields := strings.SplitN(l, ":", 3) + if len(fields) != 3 { + log.Fatal(`unexpected line in "/proc/self/cgroup"`) + } + if !strings.HasPrefix(fields[2], "/docker/") { + return false + } + } + if err := sc.Err(); err != nil { + log.Fatal(err) + } + return true +} + func main() { flag.Usage = usage flag.Parse() - // TODO(mpl): find another way to detect whether we're on docker, as - // /.dockerinit does not exist anymore apparently? No time for now. + if !inDocker() { + fmt.Fprintf(os.Stderr, "Usage error: this program should be run within a docker container, and is meant to be called from misc/docker/dock.go\n") + usage() + } checkArgs() getCamliSrc()