From 32e9beb86ef14102ebf80947e5d91dce97e2c81f Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 30 Apr 2014 15:24:18 -0700 Subject: [PATCH] Remove logger from nsinit struct Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- libcontainer/nsinit/exec.go | 14 +--------- libcontainer/nsinit/execin.go | 11 ++++---- libcontainer/nsinit/init.go | 6 ----- libcontainer/nsinit/nsinit.go | 9 ++----- libcontainer/nsinit/nsinit/main.go | 42 +++++++----------------------- 5 files changed, 18 insertions(+), 64 deletions(-) diff --git a/libcontainer/nsinit/exec.go b/libcontainer/nsinit/exec.go index 430dd89..17201b6 100644 --- a/libcontainer/nsinit/exec.go +++ b/libcontainer/nsinit/exec.go @@ -30,10 +30,8 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [ if err != nil { return -1, err } - ns.logger.Printf("created sync pipe parent fd %d child fd %d\n", syncPipe.parent.Fd(), syncPipe.child.Fd()) if container.Tty { - ns.logger.Println("creating master and console") master, console, err = system.CreateMasterAndConsole() if err != nil { return -1, err @@ -42,13 +40,11 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [ } command := ns.commandFactory.Create(container, console, syncPipe.child, args) - ns.logger.Println("attach terminal to command") if err := term.Attach(command); err != nil { return -1, err } defer term.Close() - ns.logger.Println("starting command") if err := command.Start(); err != nil { return -1, err } @@ -57,19 +53,14 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [ if err != nil { return -1, err } - ns.logger.Printf("writing pid %d to file\n", command.Process.Pid) if err := ns.stateWriter.WritePid(command.Process.Pid, started); err != nil { command.Process.Kill() return -1, err } - defer func() { - ns.logger.Println("removing pid file") - ns.stateWriter.DeletePid() - }() + defer ns.stateWriter.DeletePid() // Do this before syncing with child so that no children // can escape the cgroup - ns.logger.Println("setting cgroups") activeCgroup, err := ns.SetupCgroups(container, command.Process.Pid) if err != nil { command.Process.Kill() @@ -79,13 +70,11 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [ defer activeCgroup.Cleanup() } - ns.logger.Println("setting up network") if err := ns.InitializeNetworking(container, command.Process.Pid, syncPipe); err != nil { command.Process.Kill() return -1, err } - ns.logger.Println("closing sync pipe with child") // Sync with child syncPipe.Close() @@ -95,7 +84,6 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [ } } status := command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() - ns.logger.Printf("process exited with status %d\n", status) return status, err } diff --git a/libcontainer/nsinit/execin.go b/libcontainer/nsinit/execin.go index b798810..c4ddb78 100644 --- a/libcontainer/nsinit/execin.go +++ b/libcontainer/nsinit/execin.go @@ -4,14 +4,15 @@ package nsinit import ( "fmt" - "github.com/dotcloud/docker/pkg/label" - "github.com/dotcloud/docker/pkg/libcontainer" - "github.com/dotcloud/docker/pkg/libcontainer/mount" - "github.com/dotcloud/docker/pkg/system" "os" "path/filepath" "strconv" "syscall" + + "github.com/dotcloud/docker/pkg/label" + "github.com/dotcloud/docker/pkg/libcontainer" + "github.com/dotcloud/docker/pkg/libcontainer/mount" + "github.com/dotcloud/docker/pkg/system" ) // ExecIn uses an existing pid and joins the pid's namespaces with the new command. @@ -42,7 +43,6 @@ func (ns *linuxNs) ExecIn(container *libcontainer.Container, nspid int, args []s // foreach namespace fd, use setns to join an existing container's namespaces for _, fd := range fds { if fd > 0 { - ns.logger.Printf("setns on %d\n", fd) if err := system.Setns(fd, 0); err != nil { closeFds() return -1, fmt.Errorf("setns %s", err) @@ -54,7 +54,6 @@ func (ns *linuxNs) ExecIn(container *libcontainer.Container, nspid int, args []s // if the container has a new pid and mount namespace we need to // remount proc and sys to pick up the changes if container.Namespaces.Contains("NEWNS") && container.Namespaces.Contains("NEWPID") { - ns.logger.Println("forking to remount /proc and /sys") pid, err := system.Fork() if err != nil { return -1, err diff --git a/libcontainer/nsinit/init.go b/libcontainer/nsinit/init.go index 4ca026d..dd4fdde 100644 --- a/libcontainer/nsinit/init.go +++ b/libcontainer/nsinit/init.go @@ -29,17 +29,14 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol } // We always read this as it is a way to sync with the parent as well - ns.logger.Printf("reading from sync pipe fd %d\n", syncPipe.child.Fd()) context, err := syncPipe.ReadFromParent() if err != nil { syncPipe.Close() return err } - ns.logger.Println("received context from parent") syncPipe.Close() if consolePath != "" { - ns.logger.Printf("setting up %s as console\n", consolePath) if err := console.OpenAndDup(consolePath); err != nil { return err } @@ -57,7 +54,6 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol } label.Init() - ns.logger.Println("setup mount namespace") if err := mount.InitializeMountNamespace(rootfs, consolePath, container); err != nil { return fmt.Errorf("setup mount namespace %s", err) } @@ -69,7 +65,6 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol } if profile := container.Context["apparmor_profile"]; profile != "" { - ns.logger.Printf("setting apparmor profile %s\n", profile) if err := apparmor.ApplyProfile(os.Getpid(), profile); err != nil { return err } @@ -79,7 +74,6 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol if err := label.SetProcessLabel(container.Context["process_label"]); err != nil { return fmt.Errorf("set process label %s", err) } - ns.logger.Printf("execing %s\n", args[0]) return system.Execv(args[0], args[0:], container.Env) } diff --git a/libcontainer/nsinit/nsinit.go b/libcontainer/nsinit/nsinit.go index c308692..6aed9c9 100644 --- a/libcontainer/nsinit/nsinit.go +++ b/libcontainer/nsinit/nsinit.go @@ -1,9 +1,6 @@ package nsinit -import ( - "github.com/dotcloud/docker/pkg/libcontainer" - "log" -) +import "github.com/dotcloud/docker/pkg/libcontainer" // NsInit is an interface with the public facing methods to provide high level // exec operations on a container @@ -17,13 +14,11 @@ type linuxNs struct { root string commandFactory CommandFactory stateWriter StateWriter - logger *log.Logger } -func NewNsInit(command CommandFactory, state StateWriter, logger *log.Logger) NsInit { +func NewNsInit(command CommandFactory, state StateWriter) NsInit { return &linuxNs{ commandFactory: command, stateWriter: state, - logger: logger, } } diff --git a/libcontainer/nsinit/nsinit/main.go b/libcontainer/nsinit/nsinit/main.go index 0965c1c..615e31d 100644 --- a/libcontainer/nsinit/nsinit/main.go +++ b/libcontainer/nsinit/nsinit/main.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "flag" - "io" "io/ioutil" "log" "os" @@ -38,12 +37,8 @@ func main() { if err != nil { log.Fatalf("Unable to load container: %s", err) } - l, err := getLogger("[exec] ") - if err != nil { - log.Fatal(err) - } - ns, err := newNsInit(l) + ns, err := newNsInit() if err != nil { log.Fatalf("Unable to initialize nsinit: %s", err) } @@ -54,7 +49,7 @@ func main() { nspid, err := readPid() if err != nil { if !os.IsNotExist(err) { - l.Fatalf("Unable to read pid: %s", err) + log.Fatalf("Unable to read pid: %s", err) } } if nspid > 0 { @@ -64,26 +59,26 @@ func main() { exitCode, err = ns.Exec(container, term, flag.Args()[1:]) } if err != nil { - l.Fatalf("Failed to exec: %s", err) + log.Fatalf("Failed to exec: %s", err) } os.Exit(exitCode) case "init": // this is executed inside of the namespace to setup the container cwd, err := os.Getwd() if err != nil { - l.Fatal(err) + log.Fatal(err) } if flag.NArg() < 2 { - l.Fatalf("wrong number of arguments %d", flag.NArg()) + log.Fatalf("wrong number of arguments %d", flag.NArg()) } syncPipe, err := nsinit.NewSyncPipeFromFd(0, uintptr(pipeFd)) if err != nil { - l.Fatalf("Unable to create sync pipe: %s", err) + log.Fatalf("Unable to create sync pipe: %s", err) } if err := ns.Init(container, cwd, console, syncPipe, flag.Args()[1:]); err != nil { - l.Fatalf("Unable to initialize for container: %s", err) + log.Fatalf("Unable to initialize for container: %s", err) } default: - l.Fatalf("command not supported for nsinit %s", flag.Arg(0)) + log.Fatalf("command not supported for nsinit %s", flag.Arg(0)) } } @@ -113,23 +108,6 @@ func readPid() (int, error) { return pid, nil } -func newNsInit(l *log.Logger) (nsinit.NsInit, error) { - return nsinit.NewNsInit(&nsinit.DefaultCommandFactory{root}, &nsinit.DefaultStateWriter{root}, l), nil -} - -func getLogger(prefix string) (*log.Logger, error) { - var w io.Writer - switch logs { - case "", "none": - w = ioutil.Discard - case "stderr": - w = os.Stderr - default: // we have a filepath - f, err := os.OpenFile(logs, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0755) - if err != nil { - return nil, err - } - w = f - } - return log.New(w, prefix, log.LstdFlags), nil +func newNsInit() (nsinit.NsInit, error) { + return nsinit.NewNsInit(&nsinit.DefaultCommandFactory{root}, &nsinit.DefaultStateWriter{root}), nil }