Browse code

Remove logger from nsinit struct Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/05/01 07:24:18
Showing 6 changed files
... ...
@@ -3,9 +3,7 @@ package native
3 3
 import (
4 4
 	"encoding/json"
5 5
 	"fmt"
6
-	"io"
7 6
 	"io/ioutil"
8
-	"log"
9 7
 	"os"
10 8
 	"os/exec"
11 9
 	"path/filepath"
... ...
@@ -31,7 +29,7 @@ func init() {
31 31
 	execdriver.RegisterInitFunc(DriverName, func(args *execdriver.InitArgs) error {
32 32
 		var (
33 33
 			container *libcontainer.Container
34
-			ns        = nsinit.NewNsInit(&nsinit.DefaultCommandFactory{}, &nsinit.DefaultStateWriter{args.Root}, createLogger(""))
34
+			ns        = nsinit.NewNsInit(&nsinit.DefaultCommandFactory{}, &nsinit.DefaultStateWriter{args.Root})
35 35
 		)
36 36
 		f, err := os.Open(filepath.Join(args.Root, "container.json"))
37 37
 		if err != nil {
... ...
@@ -102,7 +100,7 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba
102 102
 			c:        c,
103 103
 			dsw:      &nsinit.DefaultStateWriter{filepath.Join(d.root, c.ID)},
104 104
 		}
105
-		ns   = nsinit.NewNsInit(factory, stateWriter, createLogger(os.Getenv("DEBUG")))
105
+		ns   = nsinit.NewNsInit(factory, stateWriter)
106 106
 		args = append([]string{c.Entrypoint}, c.Arguments...)
107 107
 	)
108 108
 	if err := d.createContainerRoot(c.ID); err != nil {
... ...
@@ -287,14 +285,3 @@ func (d *dockerStateWriter) WritePid(pid int, started string) error {
287 287
 func (d *dockerStateWriter) DeletePid() error {
288 288
 	return d.dsw.DeletePid()
289 289
 }
290
-
291
-func createLogger(debug string) *log.Logger {
292
-	var w io.Writer
293
-	// if we are in debug mode set the logger to stderr
294
-	if debug != "" {
295
-		w = os.Stderr
296
-	} else {
297
-		w = ioutil.Discard
298
-	}
299
-	return log.New(w, "[libcontainer] ", log.LstdFlags)
300
-}
... ...
@@ -30,10 +30,8 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [
30 30
 	if err != nil {
31 31
 		return -1, err
32 32
 	}
33
-	ns.logger.Printf("created sync pipe parent fd %d child fd %d\n", syncPipe.parent.Fd(), syncPipe.child.Fd())
34 33
 
35 34
 	if container.Tty {
36
-		ns.logger.Println("creating master and console")
37 35
 		master, console, err = system.CreateMasterAndConsole()
38 36
 		if err != nil {
39 37
 			return -1, err
... ...
@@ -42,13 +40,11 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [
42 42
 	}
43 43
 
44 44
 	command := ns.commandFactory.Create(container, console, syncPipe.child, args)
45
-	ns.logger.Println("attach terminal to command")
46 45
 	if err := term.Attach(command); err != nil {
47 46
 		return -1, err
48 47
 	}
49 48
 	defer term.Close()
50 49
 
51
-	ns.logger.Println("starting command")
52 50
 	if err := command.Start(); err != nil {
53 51
 		return -1, err
54 52
 	}
... ...
@@ -57,19 +53,14 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [
57 57
 	if err != nil {
58 58
 		return -1, err
59 59
 	}
60
-	ns.logger.Printf("writing pid %d to file\n", command.Process.Pid)
61 60
 	if err := ns.stateWriter.WritePid(command.Process.Pid, started); err != nil {
62 61
 		command.Process.Kill()
63 62
 		return -1, err
64 63
 	}
65
-	defer func() {
66
-		ns.logger.Println("removing pid file")
67
-		ns.stateWriter.DeletePid()
68
-	}()
64
+	defer ns.stateWriter.DeletePid()
69 65
 
70 66
 	// Do this before syncing with child so that no children
71 67
 	// can escape the cgroup
72
-	ns.logger.Println("setting cgroups")
73 68
 	activeCgroup, err := ns.SetupCgroups(container, command.Process.Pid)
74 69
 	if err != nil {
75 70
 		command.Process.Kill()
... ...
@@ -79,13 +70,11 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [
79 79
 		defer activeCgroup.Cleanup()
80 80
 	}
81 81
 
82
-	ns.logger.Println("setting up network")
83 82
 	if err := ns.InitializeNetworking(container, command.Process.Pid, syncPipe); err != nil {
84 83
 		command.Process.Kill()
85 84
 		return -1, err
86 85
 	}
87 86
 
88
-	ns.logger.Println("closing sync pipe with child")
89 87
 	// Sync with child
90 88
 	syncPipe.Close()
91 89
 
... ...
@@ -95,7 +84,6 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [
95 95
 		}
96 96
 	}
97 97
 	status := command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
98
-	ns.logger.Printf("process exited with status %d\n", status)
99 98
 	return status, err
100 99
 }
101 100
 
... ...
@@ -4,14 +4,15 @@ package nsinit
4 4
 
5 5
 import (
6 6
 	"fmt"
7
-	"github.com/dotcloud/docker/pkg/label"
8
-	"github.com/dotcloud/docker/pkg/libcontainer"
9
-	"github.com/dotcloud/docker/pkg/libcontainer/mount"
10
-	"github.com/dotcloud/docker/pkg/system"
11 7
 	"os"
12 8
 	"path/filepath"
13 9
 	"strconv"
14 10
 	"syscall"
11
+
12
+	"github.com/dotcloud/docker/pkg/label"
13
+	"github.com/dotcloud/docker/pkg/libcontainer"
14
+	"github.com/dotcloud/docker/pkg/libcontainer/mount"
15
+	"github.com/dotcloud/docker/pkg/system"
15 16
 )
16 17
 
17 18
 // 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
42 42
 	// foreach namespace fd, use setns to join an existing container's namespaces
43 43
 	for _, fd := range fds {
44 44
 		if fd > 0 {
45
-			ns.logger.Printf("setns on %d\n", fd)
46 45
 			if err := system.Setns(fd, 0); err != nil {
47 46
 				closeFds()
48 47
 				return -1, fmt.Errorf("setns %s", err)
... ...
@@ -54,7 +54,6 @@ func (ns *linuxNs) ExecIn(container *libcontainer.Container, nspid int, args []s
54 54
 	// if the container has a new pid and mount namespace we need to
55 55
 	// remount proc and sys to pick up the changes
56 56
 	if container.Namespaces.Contains("NEWNS") && container.Namespaces.Contains("NEWPID") {
57
-		ns.logger.Println("forking to remount /proc and /sys")
58 57
 		pid, err := system.Fork()
59 58
 		if err != nil {
60 59
 			return -1, err
... ...
@@ -29,17 +29,14 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol
29 29
 	}
30 30
 
31 31
 	// We always read this as it is a way to sync with the parent as well
32
-	ns.logger.Printf("reading from sync pipe fd %d\n", syncPipe.child.Fd())
33 32
 	context, err := syncPipe.ReadFromParent()
34 33
 	if err != nil {
35 34
 		syncPipe.Close()
36 35
 		return err
37 36
 	}
38
-	ns.logger.Println("received context from parent")
39 37
 	syncPipe.Close()
40 38
 
41 39
 	if consolePath != "" {
42
-		ns.logger.Printf("setting up %s as console\n", consolePath)
43 40
 		if err := console.OpenAndDup(consolePath); err != nil {
44 41
 			return err
45 42
 		}
... ...
@@ -57,7 +54,6 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol
57 57
 	}
58 58
 
59 59
 	label.Init()
60
-	ns.logger.Println("setup mount namespace")
61 60
 	if err := mount.InitializeMountNamespace(rootfs, consolePath, container); err != nil {
62 61
 		return fmt.Errorf("setup mount namespace %s", err)
63 62
 	}
... ...
@@ -69,7 +65,6 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol
69 69
 	}
70 70
 
71 71
 	if profile := container.Context["apparmor_profile"]; profile != "" {
72
-		ns.logger.Printf("setting apparmor profile %s\n", profile)
73 72
 		if err := apparmor.ApplyProfile(os.Getpid(), profile); err != nil {
74 73
 			return err
75 74
 		}
... ...
@@ -79,7 +74,6 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol
79 79
 	if err := label.SetProcessLabel(container.Context["process_label"]); err != nil {
80 80
 		return fmt.Errorf("set process label %s", err)
81 81
 	}
82
-	ns.logger.Printf("execing %s\n", args[0])
83 82
 	return system.Execv(args[0], args[0:], container.Env)
84 83
 }
85 84
 
... ...
@@ -1,9 +1,6 @@
1 1
 package nsinit
2 2
 
3
-import (
4
-	"github.com/dotcloud/docker/pkg/libcontainer"
5
-	"log"
6
-)
3
+import "github.com/dotcloud/docker/pkg/libcontainer"
7 4
 
8 5
 // NsInit is an interface with the public facing methods to provide high level
9 6
 // exec operations on a container
... ...
@@ -17,13 +14,11 @@ type linuxNs struct {
17 17
 	root           string
18 18
 	commandFactory CommandFactory
19 19
 	stateWriter    StateWriter
20
-	logger         *log.Logger
21 20
 }
22 21
 
23
-func NewNsInit(command CommandFactory, state StateWriter, logger *log.Logger) NsInit {
22
+func NewNsInit(command CommandFactory, state StateWriter) NsInit {
24 23
 	return &linuxNs{
25 24
 		commandFactory: command,
26 25
 		stateWriter:    state,
27
-		logger:         logger,
28 26
 	}
29 27
 }
... ...
@@ -3,7 +3,6 @@ package main
3 3
 import (
4 4
 	"encoding/json"
5 5
 	"flag"
6
-	"io"
7 6
 	"io/ioutil"
8 7
 	"log"
9 8
 	"os"
... ...
@@ -38,12 +37,8 @@ func main() {
38 38
 	if err != nil {
39 39
 		log.Fatalf("Unable to load container: %s", err)
40 40
 	}
41
-	l, err := getLogger("[exec] ")
42
-	if err != nil {
43
-		log.Fatal(err)
44
-	}
45 41
 
46
-	ns, err := newNsInit(l)
42
+	ns, err := newNsInit()
47 43
 	if err != nil {
48 44
 		log.Fatalf("Unable to initialize nsinit: %s", err)
49 45
 	}
... ...
@@ -54,7 +49,7 @@ func main() {
54 54
 		nspid, err := readPid()
55 55
 		if err != nil {
56 56
 			if !os.IsNotExist(err) {
57
-				l.Fatalf("Unable to read pid: %s", err)
57
+				log.Fatalf("Unable to read pid: %s", err)
58 58
 			}
59 59
 		}
60 60
 		if nspid > 0 {
... ...
@@ -64,26 +59,26 @@ func main() {
64 64
 			exitCode, err = ns.Exec(container, term, flag.Args()[1:])
65 65
 		}
66 66
 		if err != nil {
67
-			l.Fatalf("Failed to exec: %s", err)
67
+			log.Fatalf("Failed to exec: %s", err)
68 68
 		}
69 69
 		os.Exit(exitCode)
70 70
 	case "init": // this is executed inside of the namespace to setup the container
71 71
 		cwd, err := os.Getwd()
72 72
 		if err != nil {
73
-			l.Fatal(err)
73
+			log.Fatal(err)
74 74
 		}
75 75
 		if flag.NArg() < 2 {
76
-			l.Fatalf("wrong number of arguments %d", flag.NArg())
76
+			log.Fatalf("wrong number of arguments %d", flag.NArg())
77 77
 		}
78 78
 		syncPipe, err := nsinit.NewSyncPipeFromFd(0, uintptr(pipeFd))
79 79
 		if err != nil {
80
-			l.Fatalf("Unable to create sync pipe: %s", err)
80
+			log.Fatalf("Unable to create sync pipe: %s", err)
81 81
 		}
82 82
 		if err := ns.Init(container, cwd, console, syncPipe, flag.Args()[1:]); err != nil {
83
-			l.Fatalf("Unable to initialize for container: %s", err)
83
+			log.Fatalf("Unable to initialize for container: %s", err)
84 84
 		}
85 85
 	default:
86
-		l.Fatalf("command not supported for nsinit %s", flag.Arg(0))
86
+		log.Fatalf("command not supported for nsinit %s", flag.Arg(0))
87 87
 	}
88 88
 }
89 89
 
... ...
@@ -113,23 +108,6 @@ func readPid() (int, error) {
113 113
 	return pid, nil
114 114
 }
115 115
 
116
-func newNsInit(l *log.Logger) (nsinit.NsInit, error) {
117
-	return nsinit.NewNsInit(&nsinit.DefaultCommandFactory{root}, &nsinit.DefaultStateWriter{root}, l), nil
118
-}
119
-
120
-func getLogger(prefix string) (*log.Logger, error) {
121
-	var w io.Writer
122
-	switch logs {
123
-	case "", "none":
124
-		w = ioutil.Discard
125
-	case "stderr":
126
-		w = os.Stderr
127
-	default: // we have a filepath
128
-		f, err := os.OpenFile(logs, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0755)
129
-		if err != nil {
130
-			return nil, err
131
-		}
132
-		w = f
133
-	}
134
-	return log.New(w, prefix, log.LstdFlags), nil
116
+func newNsInit() (nsinit.NsInit, error) {
117
+	return nsinit.NewNsInit(&nsinit.DefaultCommandFactory{root}, &nsinit.DefaultStateWriter{root}), nil
135 118
 }