From 01f9815b55742654b2f35d13c3aba6a9e48634c7 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 24 Feb 2014 13:40:17 -0800 Subject: [PATCH] Fix tests with dockerinit lookup path Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- docker/docker.go | 2 +- execdriver/namespaces/driver.go | 3 ++- integration/runtime_test.go | 2 +- pkg/libcontainer/nsinit/nsinit/main.go | 4 +++- sysinit/sysinit.go | 24 +++++++++++++++++++++++- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/docker/docker.go b/docker/docker.go index 6c6ad66abf..66c4dbf02a 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -17,7 +17,7 @@ import ( ) func main() { - if selfPath := utils.SelfPath(); selfPath == "/sbin/init" || strings.Contains(selfPath, "/.dockerinit") { + if selfPath := utils.SelfPath(); selfPath == "/sbin/init" || strings.Contains(selfPath, ".dockerinit") { // Running in init mode sysinit.SysInit() return diff --git a/execdriver/namespaces/driver.go b/execdriver/namespaces/driver.go index 5657b8cc46..9f153bd3eb 100644 --- a/execdriver/namespaces/driver.go +++ b/execdriver/namespaces/driver.go @@ -86,7 +86,7 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba return -1, err } args := append([]string{c.Entrypoint}, c.Arguments...) - return nsinit.Exec(container, factory, stateWriter, term, "/nsinit.log", args) + return nsinit.Exec(container, factory, stateWriter, term, "", args) } func (d *driver) Kill(p *execdriver.Command, sig int) error { @@ -146,6 +146,7 @@ func (d *dockerCommandFactory) Create(container *libcontainer.Container, "-driver", DriverName, "-console", console, "-pipe", fmt.Sprint(syncFd), + "-log", logFile, }, args...) c.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: uintptr(nsinit.GetNamespaceFlags(container.Namespaces)), diff --git a/integration/runtime_test.go b/integration/runtime_test.go index ca2119ce1f..522e910562 100644 --- a/integration/runtime_test.go +++ b/integration/runtime_test.go @@ -85,7 +85,7 @@ func init() { os.Setenv("TEST", "1") // Hack to run sys init during unit testing - if selfPath := utils.SelfPath(); selfPath == "/sbin/init" || selfPath == "/.dockerinit" { + if selfPath := utils.SelfPath(); selfPath == "/sbin/init" || strings.Contains(selfPath, ".dockerinit") { sysinit.SysInit() return } diff --git a/pkg/libcontainer/nsinit/nsinit/main.go b/pkg/libcontainer/nsinit/nsinit/main.go index c299412c7b..786c9c1ea6 100644 --- a/pkg/libcontainer/nsinit/nsinit/main.go +++ b/pkg/libcontainer/nsinit/nsinit/main.go @@ -24,7 +24,7 @@ var ( ErrWrongArguments = errors.New("Wrong argument count") ) -func init() { +func registerFlags() { flag.StringVar(&console, "console", "", "console (pty slave) path") flag.StringVar(&logFile, "log", "none", "log options (none, stderr, or a file path)") flag.IntVar(&pipeFd, "pipe", 0, "sync pipe fd") @@ -33,6 +33,8 @@ func init() { } func main() { + registerFlags() + if flag.NArg() < 1 { log.Fatal(ErrWrongArguments) } diff --git a/sysinit/sysinit.go b/sysinit/sysinit.go index e10c0baad0..6d604fcccc 100644 --- a/sysinit/sysinit.go +++ b/sysinit/sysinit.go @@ -7,6 +7,7 @@ import ( "github.com/dotcloud/docker/execdriver" _ "github.com/dotcloud/docker/execdriver/chroot" _ "github.com/dotcloud/docker/execdriver/lxc" + "io" "io/ioutil" "log" "os" @@ -55,9 +56,14 @@ func SysInit() { driver = flag.String("driver", "", "exec driver") pipe = flag.Int("pipe", 0, "sync pipe fd") console = flag.String("console", "", "console (pty slave) path") + logFile = flag.String("log", "", "log file path") ) flag.Parse() + if err := setupLogging(*logFile); err != nil { + log.Fatalf("setup logging %s", err) + } + // Get env var env []string content, err := ioutil.ReadFile("/.dockerenv") @@ -67,7 +73,6 @@ func SysInit() { if err := json.Unmarshal(content, &env); err != nil { log.Fatalf("Unable to unmarshal environment variables: %v", err) } - // Propagate the plugin-specific container env variable env = append(env, "container="+os.Getenv("container")) @@ -89,3 +94,20 @@ func SysInit() { log.Fatal(err) } } + +func setupLogging(logFile string) (err error) { + var writer io.Writer + switch logFile { + case "stderr": + writer = os.Stderr + case "none", "": + writer = ioutil.Discard + default: + writer, err = os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0755) + if err != nil { + return err + } + } + log.SetOutput(writer) + return nil +}