From 5fa2e4d4f2be7787ad29b1e6ffd9c026ea0c1925 Mon Sep 17 00:00:00 2001 From: John Howard Date: Sun, 1 Nov 2015 07:53:15 -0800 Subject: [PATCH] Refactor ProcessConfig Signed-off-by: John Howard --- daemon/container_unix.go | 8 +++++--- daemon/container_windows.go | 11 +++++------ daemon/exec.go | 16 ++++++---------- daemon/exec_unix.go | 20 ++++++++++++++++++++ daemon/exec_windows.go | 11 +++++++++++ daemon/execdriver/driver.go | 17 +++++++---------- daemon/execdriver/driver_unix.go | 11 +++++++++++ daemon/execdriver/driver_windows.go | 9 +++++++++ daemon/execdriver/windows/checkoptions.go | 21 --------------------- daemon/execdriver/windows/run.go | 6 ------ 10 files changed, 74 insertions(+), 56 deletions(-) create mode 100644 daemon/exec_unix.go create mode 100644 daemon/exec_windows.go delete mode 100644 daemon/execdriver/windows/checkoptions.go diff --git a/daemon/container_unix.go b/daemon/container_unix.go index c797059eec..6c57561aab 100644 --- a/daemon/container_unix.go +++ b/daemon/container_unix.go @@ -300,10 +300,12 @@ func (daemon *Daemon) populateCommand(c *Container, env []string) error { } processConfig := execdriver.ProcessConfig{ + CommonProcessConfig: execdriver.CommonProcessConfig{ + Entrypoint: c.Path, + Arguments: c.Args, + Tty: c.Config.Tty, + }, Privileged: c.hostConfig.Privileged, - Entrypoint: c.Path, - Arguments: c.Args, - Tty: c.Config.Tty, User: c.Config.User, } diff --git a/daemon/container_windows.go b/daemon/container_windows.go index fd2dcb9bdf..6a20781421 100644 --- a/daemon/container_windows.go +++ b/daemon/container_windows.go @@ -86,13 +86,12 @@ func (daemon *Daemon) populateCommand(c *Container, env []string) error { }, } - // TODO Windows. Further refactoring required (privileged/user) processConfig := execdriver.ProcessConfig{ - Privileged: c.hostConfig.Privileged, - Entrypoint: c.Path, - Arguments: c.Args, - Tty: c.Config.Tty, - User: c.Config.User, + CommonProcessConfig: execdriver.CommonProcessConfig{ + Entrypoint: c.Path, + Arguments: c.Args, + Tty: c.Config.Tty, + }, ConsoleSize: c.hostConfig.ConsoleSize, } diff --git a/daemon/exec.go b/daemon/exec.go index 299aaf6082..412220cf59 100644 --- a/daemon/exec.go +++ b/daemon/exec.go @@ -156,18 +156,14 @@ func (d *Daemon) ContainerExecCreate(config *runconfig.ExecConfig) (string, erro cmd := stringutils.NewStrSlice(config.Cmd...) entrypoint, args := d.getEntrypointAndArgs(stringutils.NewStrSlice(), cmd) - user := config.User - if len(user) == 0 { - user = container.Config.User - } - processConfig := &execdriver.ProcessConfig{ - Tty: config.Tty, - Entrypoint: entrypoint, - Arguments: args, - User: user, - Privileged: config.Privileged, + CommonProcessConfig: execdriver.CommonProcessConfig{ + Tty: config.Tty, + Entrypoint: entrypoint, + Arguments: args, + }, } + setPlatformSpecificExecProcessConfig(config, container, processConfig) ExecConfig := &ExecConfig{ ID: stringid.GenerateNonCryptoID(), diff --git a/daemon/exec_unix.go b/daemon/exec_unix.go new file mode 100644 index 0000000000..985112fe9d --- /dev/null +++ b/daemon/exec_unix.go @@ -0,0 +1,20 @@ +// +build linux freebsd + +package daemon + +import ( + "github.com/docker/docker/daemon/execdriver" + "github.com/docker/docker/runconfig" +) + +// setPlatformSpecificExecProcessConfig sets platform-specific fields in the +// ProcessConfig structure. +func setPlatformSpecificExecProcessConfig(config *runconfig.ExecConfig, container *Container, pc *execdriver.ProcessConfig) { + user := config.User + if len(user) == 0 { + user = container.Config.User + } + + pc.User = user + pc.Privileged = config.Privileged +} diff --git a/daemon/exec_windows.go b/daemon/exec_windows.go new file mode 100644 index 0000000000..bdab49b194 --- /dev/null +++ b/daemon/exec_windows.go @@ -0,0 +1,11 @@ +package daemon + +import ( + "github.com/docker/docker/daemon/execdriver" + "github.com/docker/docker/runconfig" +) + +// setPlatformSpecificExecProcessConfig sets platform-specific fields in the +// ProcessConfig structure. This is a no-op on Windows +func setPlatformSpecificExecProcessConfig(config *runconfig.ExecConfig, container *Container, pc *execdriver.ProcessConfig) { +} diff --git a/daemon/execdriver/driver.go b/daemon/execdriver/driver.go index 859eaa74f5..4a9d7d6e06 100644 --- a/daemon/execdriver/driver.go +++ b/daemon/execdriver/driver.go @@ -112,18 +112,15 @@ type ResourceStats struct { SystemUsage uint64 `json:"system_usage"` } -// ProcessConfig describes a process that will be run inside a container. -type ProcessConfig struct { +// CommonProcessConfig is the common platform agnostic part of the ProcessConfig +// structure that describes a process that will be run inside a container. +type CommonProcessConfig struct { exec.Cmd `json:"-"` - Privileged bool `json:"privileged"` - User string `json:"user"` - Tty bool `json:"tty"` - Entrypoint string `json:"entrypoint"` - Arguments []string `json:"arguments"` - Terminal Terminal `json:"-"` // standard or tty terminal (Unix) - Console string `json:"-"` // dev/console path (Unix) - ConsoleSize [2]int `json:"-"` // h,w of initial console size (Windows) + Tty bool `json:"tty"` + Entrypoint string `json:"entrypoint"` + Arguments []string `json:"arguments"` + Terminal Terminal `json:"-"` // standard or tty terminal } // CommonCommand is the common platform agnostic part of the Command structure diff --git a/daemon/execdriver/driver_unix.go b/daemon/execdriver/driver_unix.go index 2e800c088e..17a46ba177 100644 --- a/daemon/execdriver/driver_unix.go +++ b/daemon/execdriver/driver_unix.go @@ -47,6 +47,17 @@ type Resources struct { MemorySwappiness int64 `json:"memory_swappiness"` } +// ProcessConfig is the platform specific structure that describes a process +// that will be run inside a container. +type ProcessConfig struct { + CommonProcessConfig + + // Fields below here are platform specific + Privileged bool `json:"privileged"` + User string `json:"user"` + Console string `json:"-"` // dev/console path +} + // Ipc settings of the container // It is for IPC namespace setting. Usually different containers // have their own IPC namespace, however this specifies to use diff --git a/daemon/execdriver/driver_windows.go b/daemon/execdriver/driver_windows.go index 1336ba97f0..26e1105a17 100644 --- a/daemon/execdriver/driver_windows.go +++ b/daemon/execdriver/driver_windows.go @@ -17,6 +17,15 @@ type Resources struct { // Fields below here are platform specific } +// ProcessConfig is the platform specific structure that describes a process +// that will be run inside a container. +type ProcessConfig struct { + CommonProcessConfig + + // Fields below here are platform specific + ConsoleSize [2]int `json:"-"` // h,w of initial console size +} + // Network settings of the container type Network struct { Interface *NetworkInterface `json:"interface"` diff --git a/daemon/execdriver/windows/checkoptions.go b/daemon/execdriver/windows/checkoptions.go deleted file mode 100644 index 6b5482345f..0000000000 --- a/daemon/execdriver/windows/checkoptions.go +++ /dev/null @@ -1,21 +0,0 @@ -// +build windows - -package windows - -import ( - "errors" - - "github.com/docker/docker/daemon/execdriver" -) - -func checkSupportedOptions(c *execdriver.Command) error { - // Windows doesn't support username - if c.ProcessConfig.User != "" { - return errors.New("Windows does not support the username option") - } - - // TODO Windows: Validate other fields which Windows doesn't support, factor - // out where applicable per platform. - - return nil -} diff --git a/daemon/execdriver/windows/run.go b/daemon/execdriver/windows/run.go index 058109f2ef..0b09a36e75 100644 --- a/daemon/execdriver/windows/run.go +++ b/daemon/execdriver/windows/run.go @@ -94,12 +94,6 @@ func (d *Driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, hooks execd err error ) - // Make sure the client isn't asking for options which aren't supported - err = checkSupportedOptions(c) - if err != nil { - return execdriver.ExitStatus{ExitCode: -1}, err - } - cu := &containerInit{ SystemType: "Container", Name: c.ID,