From 0ab9320ab28afbf62a8b81e5f151db908897df86 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Mon, 12 Dec 2016 09:28:32 +0100 Subject: [PATCH 1/6] Move utils.TestDirectory to pkg/testutil/tempfile Signed-off-by: Vincent Demeester --- distribution/registry_unit_test.go | 41 ++++++++++++++++++++++++++-- pkg/testutil/tempfile/tempfile.go | 3 +- utils/utils.go | 44 ------------------------------ 3 files changed, 40 insertions(+), 48 deletions(-) diff --git a/distribution/registry_unit_test.go b/distribution/registry_unit_test.go index 23edc095e1..7270258735 100644 --- a/distribution/registry_unit_test.go +++ b/distribution/registry_unit_test.go @@ -1,19 +1,23 @@ package distribution import ( + "fmt" + "io/ioutil" "net/http" "net/http/httptest" "net/url" "os" + "runtime" "strings" "testing" "github.com/Sirupsen/logrus" "github.com/docker/docker/api/types" registrytypes "github.com/docker/docker/api/types/registry" + "github.com/docker/docker/pkg/archive" + "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/reference" "github.com/docker/docker/registry" - "github.com/docker/docker/utils" "golang.org/x/net/context" ) @@ -38,7 +42,7 @@ func (h *tokenPassThruHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) } func testTokenPassThru(t *testing.T, ts *httptest.Server) { - tmp, err := utils.TestDirectory("") + tmp, err := testDirectory("") if err != nil { t.Fatal(err) } @@ -131,3 +135,36 @@ func TestTokenPassThruDifferentHost(t *testing.T) { t.Fatal("Redirect should not forward Authorization header to another host") } } + +// TestDirectory creates a new temporary directory and returns its path. +// The contents of directory at path `templateDir` is copied into the +// new directory. +func testDirectory(templateDir string) (dir string, err error) { + testID := stringid.GenerateNonCryptoID()[:4] + prefix := fmt.Sprintf("docker-test%s-%s-", testID, getCallerName(2)) + if prefix == "" { + prefix = "docker-test-" + } + dir, err = ioutil.TempDir("", prefix) + if err = os.Remove(dir); err != nil { + return + } + if templateDir != "" { + if err = archive.CopyWithTar(templateDir, dir); err != nil { + return + } + } + return +} + +// getCallerName introspects the call stack and returns the name of the +// function `depth` levels down in the stack. +func getCallerName(depth int) string { + // Use the caller function name as a prefix. + // This helps trace temp directories back to their test. + pc, _, _, _ := runtime.Caller(depth + 1) + callerLongName := runtime.FuncForPC(pc).Name() + parts := strings.Split(callerLongName, ".") + callerShortName := parts[len(parts)-1] + return callerShortName +} diff --git a/pkg/testutil/tempfile/tempfile.go b/pkg/testutil/tempfile/tempfile.go index 0e09d99dae..48c654d1f4 100644 --- a/pkg/testutil/tempfile/tempfile.go +++ b/pkg/testutil/tempfile/tempfile.go @@ -1,10 +1,9 @@ package tempfile import ( + "github.com/docker/docker/pkg/testutil/assert" "io/ioutil" "os" - - "github.com/docker/docker/pkg/testutil/assert" ) // TempFile is a temporary file that can be used with unit tests. TempFile diff --git a/utils/utils.go b/utils/utils.go index d3dd00abf4..9a7acca419 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,53 +1,9 @@ package utils import ( - "fmt" - "io/ioutil" - "os" - "runtime" "strings" - - "github.com/docker/docker/pkg/archive" - "github.com/docker/docker/pkg/stringid" ) -var globalTestID string - -// TestDirectory creates a new temporary directory and returns its path. -// The contents of directory at path `templateDir` is copied into the -// new directory. -func TestDirectory(templateDir string) (dir string, err error) { - if globalTestID == "" { - globalTestID = stringid.GenerateNonCryptoID()[:4] - } - prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2)) - if prefix == "" { - prefix = "docker-test-" - } - dir, err = ioutil.TempDir("", prefix) - if err = os.Remove(dir); err != nil { - return - } - if templateDir != "" { - if err = archive.CopyWithTar(templateDir, dir); err != nil { - return - } - } - return -} - -// GetCallerName introspects the call stack and returns the name of the -// function `depth` levels down in the stack. -func GetCallerName(depth int) string { - // Use the caller function name as a prefix. - // This helps trace temp directories back to their test. - pc, _, _, _ := runtime.Caller(depth + 1) - callerLongName := runtime.FuncForPC(pc).Name() - parts := strings.Split(callerLongName, ".") - callerShortName := parts[len(parts)-1] - return callerShortName -} - // ReplaceOrAppendEnvValues returns the defaults with the overrides either // replaced by env key or appended to the list func ReplaceOrAppendEnvValues(defaults, overrides []string) []string { From 8c1ac816657a1371597c4b2d1a758bee0114e0d7 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Mon, 12 Dec 2016 09:28:41 +0100 Subject: [PATCH 2/6] Move process functions to pkg/system Signed-off-by: Vincent Demeester --- libcontainerd/remote_unix.go | 19 +++++++++---------- {utils => pkg/system}/process_unix.go | 2 +- {utils => pkg/system}/process_windows.go | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) rename {utils => pkg/system}/process_unix.go (96%) rename {utils => pkg/system}/process_windows.go (96%) diff --git a/libcontainerd/remote_unix.go b/libcontainerd/remote_unix.go index 64a28646be..eebbc886c6 100644 --- a/libcontainerd/remote_unix.go +++ b/libcontainerd/remote_unix.go @@ -21,8 +21,7 @@ import ( "github.com/Sirupsen/logrus" containerd "github.com/docker/containerd/api/grpc/types" "github.com/docker/docker/pkg/locker" - sysinfo "github.com/docker/docker/pkg/system" - "github.com/docker/docker/utils" + "github.com/docker/docker/pkg/system" "github.com/golang/protobuf/ptypes" "github.com/golang/protobuf/ptypes/timestamp" "golang.org/x/net/context" @@ -81,7 +80,7 @@ func New(stateDir string, options ...RemoteOption) (_ Remote, err error) { } } - if err := sysinfo.MkdirAll(stateDir, 0700); err != nil { + if err := system.MkdirAll(stateDir, 0700); err != nil { return nil, err } @@ -164,8 +163,8 @@ func (r *remote) handleConnectionChange() { transientFailureCount++ if transientFailureCount >= maxConnectionRetryCount { transientFailureCount = 0 - if utils.IsProcessAlive(r.daemonPid) { - utils.KillProcess(r.daemonPid) + if system.IsProcessAlive(r.daemonPid) { + system.KillProcess(r.daemonPid) } <-r.daemonWaitCh if err := r.runContainerdDaemon(); err != nil { //FIXME: Handle error @@ -188,13 +187,13 @@ func (r *remote) Cleanup() { // Wait up to 15secs for it to stop for i := time.Duration(0); i < containerdShutdownTimeout; i += time.Second { - if !utils.IsProcessAlive(r.daemonPid) { + if !system.IsProcessAlive(r.daemonPid) { break } time.Sleep(time.Second) } - if utils.IsProcessAlive(r.daemonPid) { + if system.IsProcessAlive(r.daemonPid) { logrus.Warnf("libcontainerd: containerd (%d) didn't stop within 15 secs, killing it\n", r.daemonPid) syscall.Kill(r.daemonPid, syscall.SIGKILL) } @@ -354,7 +353,7 @@ func (r *remote) runContainerdDaemon() error { if err != nil { return err } - if utils.IsProcessAlive(int(pid)) { + if system.IsProcessAlive(int(pid)) { logrus.Infof("libcontainerd: previous instance of containerd still alive (%d)", pid) r.daemonPid = int(pid) return nil @@ -417,11 +416,11 @@ func (r *remote) runContainerdDaemon() error { } logrus.Infof("libcontainerd: new containerd process, pid: %d", cmd.Process.Pid) if err := setOOMScore(cmd.Process.Pid, r.oomScore); err != nil { - utils.KillProcess(cmd.Process.Pid) + system.KillProcess(cmd.Process.Pid) return err } if _, err := f.WriteString(fmt.Sprintf("%d", cmd.Process.Pid)); err != nil { - utils.KillProcess(cmd.Process.Pid) + system.KillProcess(cmd.Process.Pid) return err } diff --git a/utils/process_unix.go b/pkg/system/process_unix.go similarity index 96% rename from utils/process_unix.go rename to pkg/system/process_unix.go index fc0b1c8b74..58411f9541 100644 --- a/utils/process_unix.go +++ b/pkg/system/process_unix.go @@ -1,6 +1,6 @@ // +build linux freebsd solaris -package utils +package system import ( "syscall" diff --git a/utils/process_windows.go b/pkg/system/process_windows.go similarity index 96% rename from utils/process_windows.go rename to pkg/system/process_windows.go index 03cb855197..e8113f97e3 100644 --- a/utils/process_windows.go +++ b/pkg/system/process_windows.go @@ -1,4 +1,4 @@ -package utils +package system // IsProcessAlive returns true if process with a given pid is running. func IsProcessAlive(pid int) bool { From ce375503477c82a76ce8530e73655fbbf5046834 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Mon, 12 Dec 2016 09:33:58 +0100 Subject: [PATCH 3/6] Move debug functions to cli/debug package Signed-off-by: Vincent Demeester --- cli/command/system/info.go | 4 ++-- {utils => cli/debug}/debug.go | 14 +++++++------- {utils => cli/debug}/debug_test.go | 20 ++++++++++---------- cmd/docker/docker.go | 4 ++-- cmd/docker/docker_test.go | 4 ++-- cmd/dockerd/daemon.go | 12 ++++++------ daemon/info.go | 4 ++-- 7 files changed, 31 insertions(+), 31 deletions(-) rename {utils => cli/debug}/debug.go (55%) rename {utils => cli/debug}/debug_test.go (74%) diff --git a/cli/command/system/info.go b/cli/command/system/info.go index e0b8767377..6c3487de8a 100644 --- a/cli/command/system/info.go +++ b/cli/command/system/info.go @@ -12,8 +12,8 @@ import ( "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/cli" "github.com/docker/docker/cli/command" + "github.com/docker/docker/cli/debug" "github.com/docker/docker/pkg/ioutils" - "github.com/docker/docker/utils" "github.com/docker/docker/utils/templates" "github.com/docker/go-units" "github.com/spf13/cobra" @@ -206,7 +206,7 @@ func prettyPrintInfo(dockerCli *command.DockerCli, info types.Info) error { ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Name: %s\n", info.Name) ioutils.FprintfIfNotEmpty(dockerCli.Out(), "ID: %s\n", info.ID) fmt.Fprintf(dockerCli.Out(), "Docker Root Dir: %s\n", info.DockerRootDir) - fmt.Fprintf(dockerCli.Out(), "Debug Mode (client): %v\n", utils.IsDebugEnabled()) + fmt.Fprintf(dockerCli.Out(), "Debug Mode (client): %v\n", debug.IsEnabled()) fmt.Fprintf(dockerCli.Out(), "Debug Mode (server): %v\n", info.Debug) if info.Debug { diff --git a/utils/debug.go b/cli/debug/debug.go similarity index 55% rename from utils/debug.go rename to cli/debug/debug.go index d203891129..51dfab2a97 100644 --- a/utils/debug.go +++ b/cli/debug/debug.go @@ -1,4 +1,4 @@ -package utils +package debug import ( "os" @@ -6,21 +6,21 @@ import ( "github.com/Sirupsen/logrus" ) -// EnableDebug sets the DEBUG env var to true +// Enable sets the DEBUG env var to true // and makes the logger to log at debug level. -func EnableDebug() { +func Enable() { os.Setenv("DEBUG", "1") logrus.SetLevel(logrus.DebugLevel) } -// DisableDebug sets the DEBUG env var to false +// Disable sets the DEBUG env var to false // and makes the logger to log at info level. -func DisableDebug() { +func Disable() { os.Setenv("DEBUG", "") logrus.SetLevel(logrus.InfoLevel) } -// IsDebugEnabled checks whether the debug flag is set or not. -func IsDebugEnabled() bool { +// IsEnabled checks whether the debug flag is set or not. +func IsEnabled() bool { return os.Getenv("DEBUG") != "" } diff --git a/utils/debug_test.go b/cli/debug/debug_test.go similarity index 74% rename from utils/debug_test.go rename to cli/debug/debug_test.go index 6f9c4dfbb0..ad8412a944 100644 --- a/utils/debug_test.go +++ b/cli/debug/debug_test.go @@ -1,4 +1,4 @@ -package utils +package debug import ( "os" @@ -7,12 +7,12 @@ import ( "github.com/Sirupsen/logrus" ) -func TestEnableDebug(t *testing.T) { +func TestEnable(t *testing.T) { defer func() { os.Setenv("DEBUG", "") logrus.SetLevel(logrus.InfoLevel) }() - EnableDebug() + Enable() if os.Getenv("DEBUG") != "1" { t.Fatalf("expected DEBUG=1, got %s\n", os.Getenv("DEBUG")) } @@ -21,8 +21,8 @@ func TestEnableDebug(t *testing.T) { } } -func TestDisableDebug(t *testing.T) { - DisableDebug() +func TestDisable(t *testing.T) { + Disable() if os.Getenv("DEBUG") != "" { t.Fatalf("expected DEBUG=\"\", got %s\n", os.Getenv("DEBUG")) } @@ -31,13 +31,13 @@ func TestDisableDebug(t *testing.T) { } } -func TestDebugEnabled(t *testing.T) { - EnableDebug() - if !IsDebugEnabled() { +func TestEnabled(t *testing.T) { + Enable() + if !IsEnabled() { t.Fatal("expected debug enabled, got false") } - DisableDebug() - if IsDebugEnabled() { + Disable() + if IsEnabled() { t.Fatal("expected debug disabled, got true") } } diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index d4847a90ee..f4033738b7 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -10,11 +10,11 @@ import ( "github.com/docker/docker/cli" "github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command/commands" + "github.com/docker/docker/cli/debug" cliflags "github.com/docker/docker/cli/flags" "github.com/docker/docker/cliconfig" "github.com/docker/docker/dockerversion" "github.com/docker/docker/pkg/term" - "github.com/docker/docker/utils" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -130,7 +130,7 @@ func dockerPreRun(opts *cliflags.ClientOptions) { } if opts.Common.Debug { - utils.EnableDebug() + debug.Enable() } } diff --git a/cmd/docker/docker_test.go b/cmd/docker/docker_test.go index 8738f6005d..f8a5297ed4 100644 --- a/cmd/docker/docker_test.go +++ b/cmd/docker/docker_test.go @@ -7,12 +7,12 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/docker/cli/command" + "github.com/docker/docker/cli/debug" "github.com/docker/docker/pkg/testutil/assert" - "github.com/docker/docker/utils" ) func TestClientDebugEnabled(t *testing.T) { - defer utils.DisableDebug() + defer debug.Disable() cmd := newDockerCommand(&command.DockerCli{}) cmd.Flags().Set("debug", "true") diff --git a/cmd/dockerd/daemon.go b/cmd/dockerd/daemon.go index be8025fd98..a223467821 100644 --- a/cmd/dockerd/daemon.go +++ b/cmd/dockerd/daemon.go @@ -26,6 +26,7 @@ import ( systemrouter "github.com/docker/docker/api/server/router/system" "github.com/docker/docker/api/server/router/volume" "github.com/docker/docker/builder/dockerfile" + "github.com/docker/docker/cli/debug" cliflags "github.com/docker/docker/cli/flags" "github.com/docker/docker/cliconfig" "github.com/docker/docker/daemon" @@ -44,7 +45,6 @@ import ( "github.com/docker/docker/plugin" "github.com/docker/docker/registry" "github.com/docker/docker/runconfig" - "github.com/docker/docker/utils" "github.com/docker/go-connections/tlsconfig" "github.com/spf13/pflag" ) @@ -137,7 +137,7 @@ func (cli *DaemonCli) start(opts daemonOptions) (err error) { } if cli.Config.Debug { - utils.EnableDebug() + debug.Enable() } if cli.Config.Experimental { @@ -351,13 +351,13 @@ func (cli *DaemonCli) reloadConfig() { } if config.IsValueSet("debug") { - debugEnabled := utils.IsDebugEnabled() + debugEnabled := debug.IsEnabled() switch { case debugEnabled && !config.Debug: // disable debug - utils.DisableDebug() + debug.Disable() cli.api.DisableProfiler() case config.Debug && !debugEnabled: // enable debug - utils.EnableDebug() + debug.Enable() cli.api.EnableProfiler() } @@ -488,7 +488,7 @@ func initRouter(s *apiserver.Server, d *daemon.Daemon, c *cluster.Cluster) { } } - s.InitRouter(utils.IsDebugEnabled(), routers...) + s.InitRouter(debug.IsEnabled(), routers...) } func (cli *DaemonCli) initMiddlewares(s *apiserver.Server, cfg *apiserver.Config) error { diff --git a/daemon/info.go b/daemon/info.go index 1ab9f29592..4ee5b1c377 100644 --- a/daemon/info.go +++ b/daemon/info.go @@ -10,6 +10,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/docker/api" "github.com/docker/docker/api/types" + "github.com/docker/docker/cli/debug" "github.com/docker/docker/container" "github.com/docker/docker/dockerversion" "github.com/docker/docker/pkg/fileutils" @@ -19,7 +20,6 @@ import ( "github.com/docker/docker/pkg/sysinfo" "github.com/docker/docker/pkg/system" "github.com/docker/docker/registry" - "github.com/docker/docker/utils" "github.com/docker/docker/volume/drivers" "github.com/docker/go-connections/sockets" ) @@ -102,7 +102,7 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) { IPv4Forwarding: !sysInfo.IPv4ForwardingDisabled, BridgeNfIptables: !sysInfo.BridgeNFCallIPTablesDisabled, BridgeNfIP6tables: !sysInfo.BridgeNFCallIP6TablesDisabled, - Debug: utils.IsDebugEnabled(), + Debug: debug.IsEnabled(), NFd: fileutils.GetTotalUsedFds(), NGoroutines: runtime.NumGoroutine(), SystemTime: time.Now().Format(time.RFC3339Nano), From 04f7a03359ac009ad2cd548fa93124a66e880ae5 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Mon, 12 Dec 2016 09:34:03 +0100 Subject: [PATCH 4/6] Move templates to pkg/templates Signed-off-by: Vincent Demeester --- cli/command/container/list.go | 2 +- cli/command/formatter/formatter.go | 2 +- cli/command/inspect/inspector.go | 2 +- cli/command/inspect/inspector_test.go | 2 +- cli/command/system/events.go | 2 +- cli/command/system/info.go | 2 +- cli/command/system/version.go | 2 +- daemon/logger/loggerutils/log_tag.go | 2 +- {utils => pkg}/templates/templates.go | 0 {utils => pkg}/templates/templates_test.go | 0 profiles/apparmor/apparmor.go | 2 +- 11 files changed, 9 insertions(+), 9 deletions(-) rename {utils => pkg}/templates/templates.go (100%) rename {utils => pkg}/templates/templates_test.go (100%) diff --git a/cli/command/container/list.go b/cli/command/container/list.go index 60c2462986..5104e9b6c0 100644 --- a/cli/command/container/list.go +++ b/cli/command/container/list.go @@ -10,7 +10,7 @@ import ( "github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command/formatter" "github.com/docker/docker/opts" - "github.com/docker/docker/utils/templates" + "github.com/docker/docker/pkg/templates" "github.com/spf13/cobra" ) diff --git a/cli/command/formatter/formatter.go b/cli/command/formatter/formatter.go index e859a1ca26..4345f7c3bc 100644 --- a/cli/command/formatter/formatter.go +++ b/cli/command/formatter/formatter.go @@ -8,7 +8,7 @@ import ( "text/tabwriter" "text/template" - "github.com/docker/docker/utils/templates" + "github.com/docker/docker/pkg/templates" ) // Format keys used to specify certain kinds of output formats diff --git a/cli/command/inspect/inspector.go b/cli/command/inspect/inspector.go index 1d81643fb1..1e53671f84 100644 --- a/cli/command/inspect/inspector.go +++ b/cli/command/inspect/inspector.go @@ -9,7 +9,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/docker/cli" - "github.com/docker/docker/utils/templates" + "github.com/docker/docker/pkg/templates" ) // Inspector defines an interface to implement to process elements diff --git a/cli/command/inspect/inspector_test.go b/cli/command/inspect/inspector_test.go index 1ce1593ab7..9085230ac5 100644 --- a/cli/command/inspect/inspector_test.go +++ b/cli/command/inspect/inspector_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - "github.com/docker/docker/utils/templates" + "github.com/docker/docker/pkg/templates" ) type testElement struct { diff --git a/cli/command/system/events.go b/cli/command/system/events.go index 087523051a..441ef91d33 100644 --- a/cli/command/system/events.go +++ b/cli/command/system/events.go @@ -17,7 +17,7 @@ import ( "github.com/docker/docker/cli/command" "github.com/docker/docker/opts" "github.com/docker/docker/pkg/jsonlog" - "github.com/docker/docker/utils/templates" + "github.com/docker/docker/pkg/templates" "github.com/spf13/cobra" ) diff --git a/cli/command/system/info.go b/cli/command/system/info.go index 6c3487de8a..e11aff77b4 100644 --- a/cli/command/system/info.go +++ b/cli/command/system/info.go @@ -14,7 +14,7 @@ import ( "github.com/docker/docker/cli/command" "github.com/docker/docker/cli/debug" "github.com/docker/docker/pkg/ioutils" - "github.com/docker/docker/utils/templates" + "github.com/docker/docker/pkg/templates" "github.com/docker/go-units" "github.com/spf13/cobra" ) diff --git a/cli/command/system/version.go b/cli/command/system/version.go index ded4f4d118..569da21886 100644 --- a/cli/command/system/version.go +++ b/cli/command/system/version.go @@ -11,7 +11,7 @@ import ( "github.com/docker/docker/cli" "github.com/docker/docker/cli/command" "github.com/docker/docker/dockerversion" - "github.com/docker/docker/utils/templates" + "github.com/docker/docker/pkg/templates" "github.com/spf13/cobra" ) diff --git a/daemon/logger/loggerutils/log_tag.go b/daemon/logger/loggerutils/log_tag.go index 4752679c72..79fcf585c9 100644 --- a/daemon/logger/loggerutils/log_tag.go +++ b/daemon/logger/loggerutils/log_tag.go @@ -4,7 +4,7 @@ import ( "bytes" "github.com/docker/docker/daemon/logger" - "github.com/docker/docker/utils/templates" + "github.com/docker/docker/pkg/templates" ) // DefaultTemplate defines the defaults template logger should use. diff --git a/utils/templates/templates.go b/pkg/templates/templates.go similarity index 100% rename from utils/templates/templates.go rename to pkg/templates/templates.go diff --git a/utils/templates/templates_test.go b/pkg/templates/templates_test.go similarity index 100% rename from utils/templates/templates_test.go rename to pkg/templates/templates_test.go diff --git a/profiles/apparmor/apparmor.go b/profiles/apparmor/apparmor.go index 36eb10cdf9..19475680c9 100644 --- a/profiles/apparmor/apparmor.go +++ b/profiles/apparmor/apparmor.go @@ -11,7 +11,7 @@ import ( "strings" "github.com/docker/docker/pkg/aaparser" - "github.com/docker/docker/utils/templates" + "github.com/docker/docker/pkg/templates" ) var ( From 7164b66cfc70b43bad98e156e72e305b66aa8ca4 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Wed, 21 Dec 2016 22:42:39 +0100 Subject: [PATCH 5/6] Move ReplaceOrAppendEnvValues to container package Signed-off-by: Vincent Demeester --- container/container_unix.go | 3 +-- container/container_windows.go | 3 +-- utils/utils.go => container/env.go | 2 +- utils/utils_test.go => container/env_test.go | 2 +- daemon/exec.go | 15 +++++++-------- 5 files changed, 11 insertions(+), 14 deletions(-) rename utils/utils.go => container/env.go (98%) rename utils/utils_test.go => container/env_test.go (95%) diff --git a/container/container_unix.go b/container/container_unix.go index f92d586204..d311b6c0bf 100644 --- a/container/container_unix.go +++ b/container/container_unix.go @@ -16,7 +16,6 @@ import ( "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/symlink" "github.com/docker/docker/pkg/system" - "github.com/docker/docker/utils" "github.com/docker/docker/volume" "github.com/opencontainers/runc/libcontainer/label" "golang.org/x/sys/unix" @@ -69,7 +68,7 @@ func (container *Container) CreateDaemonEnvironment(tty bool, linkedEnv []string // because the env on the container can override certain default values // we need to replace the 'env' keys where they match and append anything // else. - env = utils.ReplaceOrAppendEnvValues(env, container.Config.Env) + env = ReplaceOrAppendEnvValues(env, container.Config.Env) return env } diff --git a/container/container_windows.go b/container/container_windows.go index b24aa3d845..0ade8461c2 100644 --- a/container/container_windows.go +++ b/container/container_windows.go @@ -8,7 +8,6 @@ import ( "path/filepath" containertypes "github.com/docker/docker/api/types/container" - "github.com/docker/docker/utils" ) // Container holds fields specific to the Windows implementation. See @@ -30,7 +29,7 @@ func (container *Container) CreateDaemonEnvironment(_ bool, linkedEnv []string) // because the env on the container can override certain default values // we need to replace the 'env' keys where they match and append anything // else. - return utils.ReplaceOrAppendEnvValues(linkedEnv, container.Config.Env) + return ReplaceOrAppendEnvValues(linkedEnv, container.Config.Env) } // UnmountIpcMounts unmounts Ipc related mounts. diff --git a/utils/utils.go b/container/env.go similarity index 98% rename from utils/utils.go rename to container/env.go index 9a7acca419..896a384c4d 100644 --- a/utils/utils.go +++ b/container/env.go @@ -1,4 +1,4 @@ -package utils +package container import ( "strings" diff --git a/utils/utils_test.go b/container/env_test.go similarity index 95% rename from utils/utils_test.go rename to container/env_test.go index ab3911e8b3..fb304fe361 100644 --- a/utils/utils_test.go +++ b/container/env_test.go @@ -1,4 +1,4 @@ -package utils +package container import "testing" diff --git a/daemon/exec.go b/daemon/exec.go index 8197426a33..ab5315a745 100644 --- a/daemon/exec.go +++ b/daemon/exec.go @@ -18,7 +18,6 @@ import ( "github.com/docker/docker/pkg/pools" "github.com/docker/docker/pkg/signal" "github.com/docker/docker/pkg/term" - "github.com/docker/docker/utils" ) // Seconds to wait after sending TERM before trying KILL @@ -94,7 +93,7 @@ func (d *Daemon) getActiveContainer(name string) (*container.Container, error) { // ContainerExecCreate sets up an exec in a running container. func (d *Daemon) ContainerExecCreate(name string, config *types.ExecConfig) (string, error) { - container, err := d.getActiveContainer(name) + cntr, err := d.getActiveContainer(name) if err != nil { return "", err } @@ -115,7 +114,7 @@ func (d *Daemon) ContainerExecCreate(name string, config *types.ExecConfig) (str execConfig.OpenStdin = config.AttachStdin execConfig.OpenStdout = config.AttachStdout execConfig.OpenStderr = config.AttachStderr - execConfig.ContainerID = container.ID + execConfig.ContainerID = cntr.ID execConfig.DetachKeys = keys execConfig.Entrypoint = entrypoint execConfig.Args = args @@ -123,18 +122,18 @@ func (d *Daemon) ContainerExecCreate(name string, config *types.ExecConfig) (str execConfig.Privileged = config.Privileged execConfig.User = config.User - linkedEnv, err := d.setupLinkedContainers(container) + linkedEnv, err := d.setupLinkedContainers(cntr) if err != nil { return "", err } - execConfig.Env = utils.ReplaceOrAppendEnvValues(container.CreateDaemonEnvironment(config.Tty, linkedEnv), config.Env) + execConfig.Env = container.ReplaceOrAppendEnvValues(cntr.CreateDaemonEnvironment(config.Tty, linkedEnv), config.Env) if len(execConfig.User) == 0 { - execConfig.User = container.Config.User + execConfig.User = cntr.Config.User } - d.registerExecCommand(container, execConfig) + d.registerExecCommand(cntr, execConfig) - d.LogContainerEvent(container, "exec_create: "+execConfig.Entrypoint+" "+strings.Join(execConfig.Args, " ")) + d.LogContainerEvent(cntr, "exec_create: "+execConfig.Entrypoint+" "+strings.Join(execConfig.Args, " ")) return execConfig.ID, nil } From dba271a42ab4841dbcf2e953491e9ee728cd8e16 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Wed, 21 Dec 2016 22:42:47 +0100 Subject: [PATCH 6/6] Move names to package api Signed-off-by: Vincent Demeester --- {utils => api}/names.go | 2 +- daemon/checkpoint.go | 6 +++--- daemon/names.go | 6 +++--- volume/local/local.go | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) rename {utils => api}/names.go (96%) diff --git a/utils/names.go b/api/names.go similarity index 96% rename from utils/names.go rename to api/names.go index 632062819c..f147d1f4ce 100644 --- a/utils/names.go +++ b/api/names.go @@ -1,4 +1,4 @@ -package utils +package api import "regexp" diff --git a/daemon/checkpoint.go b/daemon/checkpoint.go index 27181743f5..b8c4b8c87b 100644 --- a/daemon/checkpoint.go +++ b/daemon/checkpoint.go @@ -7,13 +7,13 @@ import ( "os" "path/filepath" + "github.com/docker/docker/api" "github.com/docker/docker/api/types" - "github.com/docker/docker/utils" ) var ( - validCheckpointNameChars = utils.RestrictedNameChars - validCheckpointNamePattern = utils.RestrictedNamePattern + validCheckpointNameChars = api.RestrictedNameChars + validCheckpointNamePattern = api.RestrictedNamePattern ) // CheckpointCreate checkpoints the process running in a container with CRIU diff --git a/daemon/names.go b/daemon/names.go index 273d551513..613d45e365 100644 --- a/daemon/names.go +++ b/daemon/names.go @@ -5,16 +5,16 @@ import ( "strings" "github.com/Sirupsen/logrus" + "github.com/docker/docker/api" "github.com/docker/docker/container" "github.com/docker/docker/pkg/namesgenerator" "github.com/docker/docker/pkg/registrar" "github.com/docker/docker/pkg/stringid" - "github.com/docker/docker/utils" ) var ( - validContainerNameChars = utils.RestrictedNameChars - validContainerNamePattern = utils.RestrictedNamePattern + validContainerNameChars = api.RestrictedNameChars + validContainerNamePattern = api.RestrictedNamePattern ) func (daemon *Daemon) registerName(container *container.Container) error { diff --git a/volume/local/local.go b/volume/local/local.go index 62c45e69ea..e32c5c124d 100644 --- a/volume/local/local.go +++ b/volume/local/local.go @@ -16,9 +16,9 @@ import ( "github.com/pkg/errors" "github.com/Sirupsen/logrus" + "github.com/docker/docker/api" "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/mount" - "github.com/docker/docker/utils" "github.com/docker/docker/volume" ) @@ -36,7 +36,7 @@ var ( // volumeNameRegex ensures the name assigned for the volume is valid. // This name is used to create the bind directory, so we need to avoid characters that // would make the path to escape the root directory. - volumeNameRegex = utils.RestrictedNamePattern + volumeNameRegex = api.RestrictedNamePattern ) type validationError struct { @@ -269,7 +269,7 @@ func (r *Root) validateName(name string) error { return validationError{fmt.Errorf("volume name is too short, names should be at least two alphanumeric characters")} } if !volumeNameRegex.MatchString(name) { - return validationError{fmt.Errorf("%q includes invalid characters for a local volume name, only %q are allowed. If you intented to pass a host directory, use absolute path", name, utils.RestrictedNameChars)} + return validationError{fmt.Errorf("%q includes invalid characters for a local volume name, only %q are allowed. If you intented to pass a host directory, use absolute path", name, api.RestrictedNameChars)} } return nil }