From 433c8e9c7da7cd3cd952c3dce3763db70fc450e5 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sat, 26 Oct 2013 17:19:35 -0700 Subject: [PATCH 01/13] Separate a) initialization of the http api and b) actually serving the api into 2 distinct jobs --- config.go | 2 -- docker/docker.go | 8 ++++++-- server.go | 31 +++++++++++++++---------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/config.go b/config.go index 40c47e692c..d5f852c26f 100644 --- a/config.go +++ b/config.go @@ -9,7 +9,6 @@ import ( type DaemonConfig struct { Pidfile string Root string - ProtoAddresses []string AutoRestart bool EnableCors bool Dns []string @@ -36,7 +35,6 @@ func ConfigFromJob(job *engine.Job) *DaemonConfig { } else { config.BridgeIface = DefaultNetworkBridge } - config.ProtoAddresses = job.GetenvList("ProtoAddresses") config.DefaultIp = net.ParseIP(job.Getenv("DefaultIp")) config.InterContainerCommunication = job.GetenvBool("InterContainerCommunication") return &config diff --git a/docker/docker.go b/docker/docker.go index c500633a71..2fc864adf4 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -71,7 +71,8 @@ func main() { if err != nil { log.Fatal(err) } - job := eng.Job("serveapi") + // Load plugin: httpapi + job := eng.Job("initapi") job.Setenv("Pidfile", *pidfile) job.Setenv("Root", *flRoot) job.SetenvBool("AutoRestart", *flAutoRestart) @@ -79,12 +80,15 @@ func main() { job.Setenv("Dns", *flDns) job.SetenvBool("EnableIptables", *flEnableIptables) job.Setenv("BridgeIface", *bridgeName) - job.SetenvList("ProtoAddresses", flHosts) job.Setenv("DefaultIp", *flDefaultIp) job.SetenvBool("InterContainerCommunication", *flInterContainerComm) if err := job.Run(); err != nil { log.Fatal(err) } + // Serve api + if err := eng.Job("serveapi", flHosts...).Run(); err != nil { + log.Fatal(err) + } } else { if len(flHosts) > 1 { log.Fatal("Please specify only one -H") diff --git a/server.go b/server.go index 314df0256b..d8b70b88ae 100644 --- a/server.go +++ b/server.go @@ -33,30 +33,20 @@ func (srv *Server) Close() error { } func init() { - engine.Register("serveapi", JobServeApi) + engine.Register("initapi", jobInitApi) } -func JobServeApi(job *engine.Job) string { +// jobInitApi runs the remote api server `srv` as a daemon, +// Only one api server can run at the same time - this is enforced by a pidfile. +// The signals SIGINT, SIGKILL and SIGTERM are intercepted for cleanup. +func jobInitApi(job *engine.Job) string { srv, err := NewServer(ConfigFromJob(job)) if err != nil { return err.Error() } - defer srv.Close() - if err := srv.Daemon(); err != nil { - return err.Error() - } - return "0" -} - -// Daemon runs the remote api server `srv` as a daemon, -// Only one api server can run at the same time - this is enforced by a pidfile. -// The signals SIGINT, SIGKILL and SIGTERM are intercepted for cleanup. -func (srv *Server) Daemon() error { if err := utils.CreatePidFile(srv.runtime.config.Pidfile); err != nil { log.Fatal(err) } - defer utils.RemovePidFile(srv.runtime.config.Pidfile) - c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM)) go func() { @@ -66,8 +56,17 @@ func (srv *Server) Daemon() error { srv.Close() os.Exit(0) }() + err = engine.Register("serveapi", func(job *engine.Job) string { + return srv.ListenAndServe(job.Args...).Error() + }) + if err != nil { + return err.Error() + } + return "0" +} - protoAddrs := srv.runtime.config.ProtoAddresses + +func (srv *Server) ListenAndServe(protoAddrs ...string) error { chErrors := make(chan error, len(protoAddrs)) for _, protoAddr := range protoAddrs { protoAddrParts := strings.SplitN(protoAddr, "://", 2) From 958b4a8757e83c3fada757b10dd1be4ab7bff5ee Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sat, 26 Oct 2013 19:24:01 -0700 Subject: [PATCH 02/13] Engine: 'start' starts the specified container --- api.go | 24 +++++-------- api_test.go | 6 ++-- engine/engine.go | 10 +++--- engine/hack.go | 23 ++++++++++++ engine/job.go | 60 ++++++++++++++++++++++++++++++- engine/{init_test.go => utils.go} | 4 +-- server.go | 54 ++++++++++++++++++---------- server_test.go | 52 ++++++++++++++++++--------- utils_test.go | 13 +++++++ 9 files changed, 186 insertions(+), 60 deletions(-) create mode 100644 engine/hack.go rename engine/{init_test.go => utils.go} (90%) diff --git a/api.go b/api.go index 0c06b2e6d1..1a3d92de32 100644 --- a/api.go +++ b/api.go @@ -639,26 +639,20 @@ func deleteImages(srv *Server, version float64, w http.ResponseWriter, r *http.R } func postContainersStart(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - var hostConfig *HostConfig - // allow a nil body for backwards compatibility - if r.Body != nil { - if matchesContentType(r.Header.Get("Content-Type"), "application/json") { - hostConfig = &HostConfig{} - if err := json.NewDecoder(r.Body).Decode(hostConfig); err != nil { - return err - } - } - } - if vars == nil { return fmt.Errorf("Missing parameter") } name := vars["name"] - // Register any links from the host config before starting the container - if err := srv.RegisterLinks(name, hostConfig); err != nil { - return err + job := srv.Eng.Job("start", name) + // allow a nil body for backwards compatibility + if r.Body != nil { + if matchesContentType(r.Header.Get("Content-Type"), "application/json") { + if err := job.DecodeEnv(r.Body); err != nil { + return err + } + } } - if err := srv.ContainerStart(name, hostConfig); err != nil { + if err := job.Run(); err != nil { return err } w.WriteHeader(http.StatusNoContent) diff --git a/api_test.go b/api_test.go index fcca8ce83f..50a86e3f23 100644 --- a/api_test.go +++ b/api_test.go @@ -781,11 +781,11 @@ func TestPostContainersRestart(t *testing.T) { } func TestPostContainersStart(t *testing.T) { - runtime := mkRuntime(t) + eng := NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) - srv := &Server{runtime: runtime} - container, _, err := runtime.Create( &Config{ Image: GetTestImage(runtime).ID, diff --git a/engine/engine.go b/engine/engine.go index 8d67242ca2..a0d5a3c4ad 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -13,10 +13,11 @@ type Handler func(*Job) string var globalHandlers map[string]Handler +func init() { + globalHandlers = make(map[string]Handler) +} + func Register(name string, handler Handler) error { - if globalHandlers == nil { - globalHandlers = make(map[string]Handler) - } globalHandlers[name] = handler return nil } @@ -27,6 +28,7 @@ func Register(name string, handler Handler) error { type Engine struct { root string handlers map[string]Handler + hack Hack // data for temporary hackery (see hack.go) } // New initializes a new engine managing the directory specified at `root`. @@ -66,7 +68,7 @@ func New(root string) (*Engine, error) { // This function mimics `Command` from the standard os/exec package. func (eng *Engine) Job(name string, args ...string) *Job { job := &Job{ - eng: eng, + Eng: eng, Name: name, Args: args, Stdin: os.Stdin, diff --git a/engine/hack.go b/engine/hack.go new file mode 100644 index 0000000000..7f6e79c0e9 --- /dev/null +++ b/engine/hack.go @@ -0,0 +1,23 @@ +package engine + + +type Hack map[string]interface{} + + +func (eng *Engine) Hack_GetGlobalVar(key string) interface{} { + if eng.hack == nil { + return nil + } + val, exists := eng.hack[key] + if !exists { + return nil + } + return val +} + +func (eng *Engine) Hack_SetGlobalVar(key string, val interface{}) { + if eng.hack == nil { + eng.hack = make(Hack) + } + eng.hack[key] = val +} diff --git a/engine/job.go b/engine/job.go index 0bde2a0beb..5c02fe15d5 100644 --- a/engine/job.go +++ b/engine/job.go @@ -1,6 +1,7 @@ package engine import ( + "bytes" "io" "strings" "fmt" @@ -22,7 +23,7 @@ import ( // This allows for richer error reporting. // type Job struct { - eng *Engine + Eng *Engine Name string Args []string env []string @@ -111,3 +112,60 @@ func (job *Job) SetenvList(key string, value []string) error { func (job *Job) Setenv(key, value string) { job.env = append(job.env, key + "=" + value) } + +// DecodeEnv decodes `src` as a json dictionary, and adds +// each decoded key-value pair to the environment. +// +// If `text` cannot be decoded as a json dictionary, an error +// is returned. +func (job *Job) DecodeEnv(src io.Reader) error { + m := make(map[string]interface{}) + if err := json.NewDecoder(src).Decode(&m); err != nil { + return err + } + for k, v := range m { + if sval, ok := v.(string); ok { + job.Setenv(k, sval) + } else if val, err := json.Marshal(v); err == nil { + job.Setenv(k, string(val)) + } else { + job.Setenv(k, fmt.Sprintf("%v", v)) + } + } + return nil +} + +func (job *Job) EncodeEnv(dst io.Writer) error { + return json.NewEncoder(dst).Encode(job.Environ()) +} + +func (job *Job) ExportEnv(dst interface{}) error { + var buf bytes.Buffer + if err := job.EncodeEnv(&buf); err != nil { + return err + } + if err := json.NewDecoder(&buf).Decode(dst); err != nil { + return err + } + return nil +} + +func (job *Job) ImportEnv(src interface{}) error { + var buf bytes.Buffer + if err := json.NewEncoder(&buf).Encode(src); err != nil { + return err + } + if err := job.DecodeEnv(&buf); err != nil { + return err + } + return nil +} + +func (job *Job) Environ() map[string]string { + m := make(map[string]string) + for _, kv := range job.env { + parts := strings.SplitN(kv, "=", 2) + m[parts[0]] = parts[1] + } + return m +} diff --git a/engine/init_test.go b/engine/utils.go similarity index 90% rename from engine/init_test.go rename to engine/utils.go index 5c03ded87d..58e5f9aae2 100644 --- a/engine/init_test.go +++ b/engine/utils.go @@ -15,7 +15,7 @@ func init() { Register("dummy", func(job *Job) string { return ""; }) } -func mkEngine(t *testing.T) *Engine { +func NewTestEngine(t *testing.T) *Engine { // Use the caller function name as a prefix. // This helps trace temp directories back to their test. pc, _, _, _ := runtime.Caller(1) @@ -38,5 +38,5 @@ func mkEngine(t *testing.T) *Engine { } func mkJob(t *testing.T, name string, args ...string) *Job { - return mkEngine(t).Job(name, args...) + return NewTestEngine(t).Job(name, args...) } diff --git a/server.go b/server.go index d8b70b88ae..efc34d14e1 100644 --- a/server.go +++ b/server.go @@ -40,7 +40,7 @@ func init() { // Only one api server can run at the same time - this is enforced by a pidfile. // The signals SIGINT, SIGKILL and SIGTERM are intercepted for cleanup. func jobInitApi(job *engine.Job) string { - srv, err := NewServer(ConfigFromJob(job)) + srv, err := NewServer(job.Eng, ConfigFromJob(job)) if err != nil { return err.Error() } @@ -56,17 +56,19 @@ func jobInitApi(job *engine.Job) string { srv.Close() os.Exit(0) }() - err = engine.Register("serveapi", func(job *engine.Job) string { - return srv.ListenAndServe(job.Args...).Error() - }) - if err != nil { + job.Eng.Hack_SetGlobalVar("httpapi.server", srv) + if err := engine.Register("start", srv.ContainerStart); err != nil { + return err.Error() + } + if err := engine.Register("serveapi", srv.ListenAndServe); err != nil { return err.Error() } return "0" } -func (srv *Server) ListenAndServe(protoAddrs ...string) error { +func (srv *Server) ListenAndServe(job *engine.Job) string { + protoAddrs := job.Args chErrors := make(chan error, len(protoAddrs)) for _, protoAddr := range protoAddrs { protoAddrParts := strings.SplitN(protoAddr, "://", 2) @@ -80,7 +82,7 @@ func (srv *Server) ListenAndServe(protoAddrs ...string) error { log.Println("/!\\ DON'T BIND ON ANOTHER IP ADDRESS THAN 127.0.0.1 IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\") } default: - return fmt.Errorf("Invalid protocol format.") + return "Invalid protocol format." } go func() { chErrors <- ListenAndServe(protoAddrParts[0], protoAddrParts[1], srv, true) @@ -89,10 +91,10 @@ func (srv *Server) ListenAndServe(protoAddrs ...string) error { for i := 0; i < len(protoAddrs); i += 1 { err := <-chErrors if err != nil { - return err + return err.Error() } } - return nil + return "0" } func (srv *Server) DockerVersion() APIVersion { @@ -1282,8 +1284,7 @@ func (srv *Server) RegisterLinks(name string, hostConfig *HostConfig) error { return fmt.Errorf("No such container: %s", name) } - // Register links - if hostConfig != nil && hostConfig.Links != nil { + if hostConfig.Links != nil { for _, l := range hostConfig.Links { parts, err := parseLink(l) if err != nil { @@ -1296,7 +1297,6 @@ func (srv *Server) RegisterLinks(name string, hostConfig *HostConfig) error { if child == nil { return fmt.Errorf("Could not get container for %s", parts["name"]) } - if err := runtime.RegisterLink(container, child, parts["alias"]); err != nil { return err } @@ -1312,22 +1312,36 @@ func (srv *Server) RegisterLinks(name string, hostConfig *HostConfig) error { return nil } -func (srv *Server) ContainerStart(name string, hostConfig *HostConfig) error { +func (srv *Server) ContainerStart(job *engine.Job) string { + if len(job.Args) < 1 { + return fmt.Sprintf("Usage: %s container_id", job.Name) + } + name := job.Args[0] runtime := srv.runtime container := runtime.Get(name) if container == nil { - return fmt.Errorf("No such container: %s", name) + return fmt.Sprintf("No such container: %s", name) } - if hostConfig != nil { - container.hostConfig = hostConfig + // If no environment was set, then no hostconfig was passed. + if len(job.Environ()) > 0 { + var hostConfig HostConfig + if err := job.ExportEnv(&hostConfig); err != nil { + return err.Error() + } + // Register any links from the host config before starting the container + // FIXME: we could just pass the container here, no need to lookup by name again. + if err := srv.RegisterLinks(name, &hostConfig); err != nil { + return err.Error() + } + container.hostConfig = &hostConfig container.ToDisk() } if err := container.Start(); err != nil { - return fmt.Errorf("Cannot start container %s: %s", name, err) + return fmt.Sprintf("Cannot start container %s: %s", name, err) } srv.LogEvent("start", container.ShortID(), runtime.repositories.ImageName(container.Image)) - return nil + return "0" } func (srv *Server) ContainerStop(name string, t int) error { @@ -1478,12 +1492,13 @@ func (srv *Server) ContainerCopy(name string, resource string, out io.Writer) er } -func NewServer(config *DaemonConfig) (*Server, error) { +func NewServer(eng *engine.Engine, config *DaemonConfig) (*Server, error) { runtime, err := NewRuntime(config) if err != nil { return nil, err } srv := &Server{ + Eng: eng, runtime: runtime, pullingPool: make(map[string]struct{}), pushingPool: make(map[string]struct{}), @@ -1527,4 +1542,5 @@ type Server struct { events []utils.JSONMessage listeners map[string]chan utils.JSONMessage reqFactory *utils.HTTPRequestFactory + Eng *engine.Engine } diff --git a/server_test.go b/server_test.go index 7f9cbaadfd..20894f8fe1 100644 --- a/server_test.go +++ b/server_test.go @@ -1,6 +1,7 @@ package docker import ( + "github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/utils" "strings" "testing" @@ -109,10 +110,11 @@ func TestCreateRm(t *testing.T) { } func TestCreateRmVolumes(t *testing.T) { - runtime := mkRuntime(t) - defer nuke(runtime) + eng := engine.NewTestEngine(t) - srv := &Server{runtime: runtime} + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime + defer nuke(runtime) config, hostConfig, _, err := ParseRun([]string{"-v", "/srv", GetTestImage(runtime).ID, "echo test"}, nil) if err != nil { @@ -128,8 +130,11 @@ func TestCreateRmVolumes(t *testing.T) { t.Errorf("Expected 1 container, %v found", len(runtime.List())) } - err = srv.ContainerStart(id, hostConfig) - if err != nil { + job := eng.Job("start", id) + if err := job.ImportEnv(hostConfig); err != nil { + t.Fatal(err) + } + if err := job.Run(); err != nil { t.Fatal(err) } @@ -169,11 +174,11 @@ func TestCommit(t *testing.T) { } func TestCreateStartRestartStopStartKillRm(t *testing.T) { - runtime := mkRuntime(t) + eng := engine.NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) - srv := &Server{runtime: runtime} - config, hostConfig, _, err := ParseRun([]string{GetTestImage(runtime).ID, "/bin/cat"}, nil) if err != nil { t.Fatal(err) @@ -188,7 +193,11 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) { t.Errorf("Expected 1 container, %v found", len(runtime.List())) } - if err := srv.ContainerStart(id, hostConfig); err != nil { + job := eng.Job("start", id) + if err := job.ImportEnv(hostConfig); err != nil { + t.Fatal(err) + } + if err := job.Run(); err != nil { t.Fatal(err) } @@ -200,7 +209,11 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) { t.Fatal(err) } - if err := srv.ContainerStart(id, hostConfig); err != nil { + job = eng.Job("start", id) + if err := job.ImportEnv(hostConfig); err != nil { + t.Fatal(err) + } + if err := job.Run(); err != nil { t.Fatal(err) } @@ -384,9 +397,10 @@ func TestLogEvent(t *testing.T) { } func TestRmi(t *testing.T) { - runtime := mkRuntime(t) + eng := engine.NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) - srv := &Server{runtime: runtime} initialImages, err := srv.Images(false, "") if err != nil { @@ -404,8 +418,11 @@ func TestRmi(t *testing.T) { } //To remove - err = srv.ContainerStart(containerID, hostConfig) - if err != nil { + job := eng.Job("start", containerID) + if err := job.ImportEnv(hostConfig); err != nil { + t.Fatal(err) + } + if err := job.Run(); err != nil { t.Fatal(err) } @@ -425,8 +442,11 @@ func TestRmi(t *testing.T) { } //To remove - err = srv.ContainerStart(containerID, hostConfig) - if err != nil { + job = eng.Job("start", containerID) + if err := job.ImportEnv(hostConfig); err != nil { + t.Fatal(err) + } + if err := job.Run(); err != nil { t.Fatal(err) } diff --git a/utils_test.go b/utils_test.go index 14cbd7c6be..282954285c 100644 --- a/utils_test.go +++ b/utils_test.go @@ -2,6 +2,7 @@ package docker import ( "fmt" + "github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/utils" "io" "io/ioutil" @@ -40,6 +41,18 @@ func mkRuntime(f Fataler) *Runtime { return runtime } +func mkServerFromEngine(eng *engine.Engine, t Fataler) *Server { + iSrv := eng.Hack_GetGlobalVar("httpapi.server") + if iSrv == nil { + t.Fatal("Legacy server field not set in engine") + } + srv, ok := iSrv.(*Server) + if !ok { + t.Fatal("Legacy server field in engine does not cast to *Server") + } + return srv +} + // A common interface to access the Fatal method of // both testing.B and testing.T. type Fataler interface { From 7b17d555992c65c6b10da21c8aa48062a1aba0d9 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 27 Oct 2013 06:51:43 +0000 Subject: [PATCH 03/13] httpapi: don't create a pidfile if it isn't set in the configuration --- server.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server.go b/server.go index efc34d14e1..de79dd966d 100644 --- a/server.go +++ b/server.go @@ -44,8 +44,11 @@ func jobInitApi(job *engine.Job) string { if err != nil { return err.Error() } - if err := utils.CreatePidFile(srv.runtime.config.Pidfile); err != nil { - log.Fatal(err) + if srv.runtime.config.Pidfile != "" { + job.Logf("Creating pidfile") + if err := utils.CreatePidFile(srv.runtime.config.Pidfile); err != nil { + log.Fatal(err) + } } c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM)) From 847411a1ee6e5ee5d051fc4729425215dc0c8561 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 27 Oct 2013 06:54:51 +0000 Subject: [PATCH 04/13] Engine: fix a bug which caused handlers to be shared between multiple engine instances --- engine/engine.go | 22 +++++++++++++++++++++- server.go | 4 ++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/engine/engine.go b/engine/engine.go index a0d5a3c4ad..956847adee 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -18,6 +18,10 @@ func init() { } func Register(name string, handler Handler) error { + _, exists := globalHandlers[name] + if exists { + return fmt.Errorf("Can't overwrite global handler for command %s", name) + } globalHandlers[name] = handler return nil } @@ -31,6 +35,17 @@ type Engine struct { hack Hack // data for temporary hackery (see hack.go) } +func (eng *Engine) Register(name string, handler Handler) error { + eng.Logf("Register(%s) (handlers=%v)", name, eng.handlers) + _, exists := eng.handlers[name] + if exists { + return fmt.Errorf("Can't overwrite handler for command %s", name) + } + eng.handlers[name] = handler + return nil +} + + // New initializes a new engine managing the directory specified at `root`. // `root` is used to store containers and any other state private to the engine. // Changing the contents of the root without executing a job will cause unspecified @@ -59,7 +74,12 @@ func New(root string) (*Engine, error) { } eng := &Engine{ root: root, - handlers: globalHandlers, + handlers: make(map[string]Handler), + id: utils.RandomString(), + } + // Copy existing global handlers + for k, v := range globalHandlers { + eng.handlers[k] = v } return eng, nil } diff --git a/server.go b/server.go index de79dd966d..72ad39cdce 100644 --- a/server.go +++ b/server.go @@ -60,10 +60,10 @@ func jobInitApi(job *engine.Job) string { os.Exit(0) }() job.Eng.Hack_SetGlobalVar("httpapi.server", srv) - if err := engine.Register("start", srv.ContainerStart); err != nil { + if err := job.Eng.Register("start", srv.ContainerStart); err != nil { return err.Error() } - if err := engine.Register("serveapi", srv.ListenAndServe); err != nil { + if err := job.Eng.Register("serveapi", srv.ListenAndServe); err != nil { return err.Error() } return "0" From ca6f0aa107117d2125a63eb5e78d74095bf08a4c Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 27 Oct 2013 06:57:29 +0000 Subject: [PATCH 05/13] Engine: don't export private testing utilities --- engine/utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engine/utils.go b/engine/utils.go index 58e5f9aae2..14ee4742ff 100644 --- a/engine/utils.go +++ b/engine/utils.go @@ -15,7 +15,7 @@ func init() { Register("dummy", func(job *Job) string { return ""; }) } -func NewTestEngine(t *testing.T) *Engine { +func newTestEngine(t *testing.T) *Engine { // Use the caller function name as a prefix. // This helps trace temp directories back to their test. pc, _, _, _ := runtime.Caller(1) @@ -38,5 +38,5 @@ func NewTestEngine(t *testing.T) *Engine { } func mkJob(t *testing.T, name string, args ...string) *Job { - return NewTestEngine(t).Job(name, args...) + return newTestEngine(t).Job(name, args...) } From 4e7cb37dcc18975010df630d8c9580a3a65e0e69 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 27 Oct 2013 07:01:15 +0000 Subject: [PATCH 06/13] Engine: improved logging and identification of jobs --- engine/engine.go | 15 +++++++++++++++ engine/job.go | 28 +++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/engine/engine.go b/engine/engine.go index 956847adee..565c6ed4fa 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -5,6 +5,7 @@ import ( "os" "log" "runtime" + "strings" "github.com/dotcloud/docker/utils" ) @@ -33,6 +34,11 @@ type Engine struct { root string handlers map[string]Handler hack Hack // data for temporary hackery (see hack.go) + id string +} + +func (eng *Engine) Root() string { + return eng.root } func (eng *Engine) Register(name string, handler Handler) error { @@ -84,6 +90,10 @@ func New(root string) (*Engine, error) { return eng, nil } +func (eng *Engine) String() string { + return fmt.Sprintf("%s|%s", eng.Root(), eng.id[:8]) +} + // Job creates a new job which can later be executed. // This function mimics `Command` from the standard os/exec package. func (eng *Engine) Job(name string, args ...string) *Job { @@ -102,3 +112,8 @@ func (eng *Engine) Job(name string, args ...string) *Job { return job } + +func (eng *Engine) Logf(format string, args ...interface{}) (n int, err error) { + prefixedFormat := fmt.Sprintf("[%s] %s\n", eng, strings.TrimRight(format, "\n")) + return fmt.Printf(prefixedFormat, args...) +} diff --git a/engine/job.go b/engine/job.go index 5c02fe15d5..7a261409e0 100644 --- a/engine/job.go +++ b/engine/job.go @@ -6,7 +6,6 @@ import ( "strings" "fmt" "encoding/json" - "github.com/dotcloud/docker/utils" ) // A job is the fundamental unit of work in the docker engine. @@ -38,9 +37,10 @@ type Job struct { // If the job returns a failure status, an error is returned // which includes the status. func (job *Job) Run() error { - randId := utils.RandomString()[:4] - fmt.Printf("Job #%s: %s\n", randId, job) - defer fmt.Printf("Job #%s: %s = '%s'", randId, job, job.status) + job.Logf("{") + defer func() { + job.Logf("}") + }() if job.handler == nil { job.status = "command not found" } else { @@ -54,7 +54,20 @@ func (job *Job) Run() error { // String returns a human-readable description of `job` func (job *Job) String() string { - return strings.Join(append([]string{job.Name}, job.Args...), " ") + s := fmt.Sprintf("%s.%s(%s)", job.Eng, job.Name, strings.Join(job.Args, ", ")) + // FIXME: if a job returns the empty string, it will be printed + // as not having returned. + // (this only affects String which is a convenience function). + if job.status != "" { + var okerr string + if job.status == "0" { + okerr = "OK" + } else { + okerr = "ERR" + } + s = fmt.Sprintf("%s = %s (%s)", s, okerr, job.status) + } + return s } func (job *Job) Getenv(key string) (value string) { @@ -169,3 +182,8 @@ func (job *Job) Environ() map[string]string { } return m } + +func (job *Job) Logf(format string, args ...interface{}) (n int, err error) { + prefixedFormat := fmt.Sprintf("[%s] %s\n", job, strings.TrimRight(format, "\n")) + return fmt.Fprintf(job.Stdout, prefixedFormat, args...) +} From 02ddaad5d985186eed94dea4105a57fa21ba24db Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 27 Oct 2013 07:06:43 +0000 Subject: [PATCH 07/13] Engine: optional environment variable 'Logging' in 'serveapi' --- docker/docker.go | 4 +++- server.go | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docker/docker.go b/docker/docker.go index 2fc864adf4..e58bd40013 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -86,7 +86,9 @@ func main() { log.Fatal(err) } // Serve api - if err := eng.Job("serveapi", flHosts...).Run(); err != nil { + job := eng.Job("serveapi", flHosts...) + job.Setenv("Logging", true) + if err := job.Run(); err != nil { log.Fatal(err) } } else { diff --git a/server.go b/server.go index 72ad39cdce..c041491ead 100644 --- a/server.go +++ b/server.go @@ -88,7 +88,8 @@ func (srv *Server) ListenAndServe(job *engine.Job) string { return "Invalid protocol format." } go func() { - chErrors <- ListenAndServe(protoAddrParts[0], protoAddrParts[1], srv, true) + // FIXME: merge Server.ListenAndServe with ListenAndServe + chErrors <- ListenAndServe(protoAddrParts[0], protoAddrParts[1], srv, job.GetenvBool("Logging")) }() } for i := 0; i < len(protoAddrs); i += 1 { From 5a85456d481a5f88fc0efc02c41b3bff987c0ed1 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 27 Oct 2013 07:13:45 +0000 Subject: [PATCH 08/13] Hack: simplify the creation of test directories --- runtime_test.go | 45 ++++++++++++----------- server_test.go | 7 ++-- utils/utils.go | 6 ++++ utils_test.go | 96 ++++++++++++++++++++++++++----------------------- 4 files changed, 85 insertions(+), 69 deletions(-) diff --git a/runtime_test.go b/runtime_test.go index 4e46b7b6d0..4aac2f3442 100644 --- a/runtime_test.go +++ b/runtime_test.go @@ -3,6 +3,7 @@ package docker import ( "bytes" "fmt" + "github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/sysinit" "github.com/dotcloud/docker/utils" "io" @@ -17,6 +18,7 @@ import ( "syscall" "testing" "time" + "net/url" ) const ( @@ -118,22 +120,19 @@ func init() { } func setupBaseImage() { - config := &DaemonConfig{ - Root: unitTestStoreBase, - AutoRestart: false, - BridgeIface: unitTestNetworkBridge, - } - runtime, err := NewRuntimeFromDirectory(config) + eng, err := engine.New(unitTestStoreBase) if err != nil { + log.Fatalf("Can't initialize engine at %s: %s", unitTestStoreBase, err) + } + job := eng.Job("initapi") + job.Setenv("Root", unitTestStoreBase) + job.SetenvBool("Autorestart", false) + job.Setenv("BridgeIface", unitTestNetworkBridge) + if err := job.Run(); err != nil { log.Fatalf("Unable to create a runtime for tests:", err) } - - // Create the "Server" - srv := &Server{ - runtime: runtime, - pullingPool: make(map[string]struct{}), - pushingPool: make(map[string]struct{}), - } + srv := mkServerFromEngine(eng, log.New(os.Stderr, "", 0)) + runtime := srv.runtime // If the unit test is not found, try to download it. if img, err := runtime.repositories.LookupImage(unitTestImageName); err != nil || img.ID != unitTestImageID { @@ -149,18 +148,22 @@ func spawnGlobalDaemon() { utils.Debugf("Global runtime already exists. Skipping.") return } - globalRuntime = mkRuntime(log.New(os.Stderr, "", 0)) - srv := &Server{ - runtime: globalRuntime, - pullingPool: make(map[string]struct{}), - pushingPool: make(map[string]struct{}), - } + t := log.New(os.Stderr, "", 0) + eng := NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + globalRuntime = srv.runtime // Spawn a Daemon go func() { utils.Debugf("Spawning global daemon for integration tests") - if err := ListenAndServe(testDaemonProto, testDaemonAddr, srv, os.Getenv("DEBUG") != ""); err != nil { - log.Fatalf("Unable to spawn the test daemon:", err) + listenURL := &url.URL{ + Scheme: testDaemonProto, + Host: testDaemonAddr, + } + job := eng.Job("serveapi", listenURL.String()) + job.SetenvBool("Logging", os.Getenv("DEBUG") != "") + if err := job.Run(); err != nil { + log.Fatalf("Unable to spawn the test daemon: %s", err) } }() // Give some time to ListenAndServer to actually start diff --git a/server_test.go b/server_test.go index 20894f8fe1..a4bfb0155f 100644 --- a/server_test.go +++ b/server_test.go @@ -1,7 +1,6 @@ package docker import ( - "github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/utils" "strings" "testing" @@ -110,7 +109,7 @@ func TestCreateRm(t *testing.T) { } func TestCreateRmVolumes(t *testing.T) { - eng := engine.NewTestEngine(t) + eng := NewTestEngine(t) srv := mkServerFromEngine(eng, t) runtime := srv.runtime @@ -174,7 +173,7 @@ func TestCommit(t *testing.T) { } func TestCreateStartRestartStopStartKillRm(t *testing.T) { - eng := engine.NewTestEngine(t) + eng := NewTestEngine(t) srv := mkServerFromEngine(eng, t) runtime := srv.runtime defer nuke(runtime) @@ -397,7 +396,7 @@ func TestLogEvent(t *testing.T) { } func TestRmi(t *testing.T) { - eng := engine.NewTestEngine(t) + eng := NewTestEngine(t) srv := mkServerFromEngine(eng, t) runtime := srv.runtime defer nuke(runtime) diff --git a/utils/utils.go b/utils/utils.go index cadd095031..5fd5e4e5b9 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -27,6 +27,12 @@ var ( INITSHA1 string // sha1sum of separate static dockerinit, if Docker itself was compiled dynamically via ./hack/make.sh dynbinary ) +// A common interface to access the Fatal method of +// both testing.B and testing.T. +type Fataler interface { + Fatal(args ...interface{}) +} + // ListOpts type type ListOpts []string diff --git a/utils_test.go b/utils_test.go index 282954285c..95e12b6393 100644 --- a/utils_test.go +++ b/utils_test.go @@ -21,27 +21,24 @@ var globalTestID string // Create a temporary runtime suitable for unit testing. // Call t.Fatal() at the first error. -func mkRuntime(f Fataler) *Runtime { - // Use the caller function name as a prefix. - // This helps trace temp directories back to their test. - pc, _, _, _ := runtime.Caller(1) - callerLongName := runtime.FuncForPC(pc).Name() - parts := strings.Split(callerLongName, ".") - callerShortName := parts[len(parts)-1] - if globalTestID == "" { - globalTestID = GenerateID()[:4] - } - prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, callerShortName) - utils.Debugf("prefix = '%s'", prefix) - - runtime, err := newTestRuntime(prefix) +func mkRuntime(f utils.Fataler) *Runtime { + root, err := newTestDirectory(unitTestStoreBase) if err != nil { f.Fatal(err) } - return runtime + config := &DaemonConfig{ + Root: root, + AutoRestart: false, + } + r, err := NewRuntimeFromDirectory(config) + if err != nil { + f.Fatal(err) + } + r.UpdateCapabilities(true) + return r } -func mkServerFromEngine(eng *engine.Engine, t Fataler) *Server { +func mkServerFromEngine(eng *engine.Engine, t utils.Fataler) *Server { iSrv := eng.Hack_GetGlobalVar("httpapi.server") if iSrv == nil { t.Fatal("Legacy server field not set in engine") @@ -53,42 +50,53 @@ func mkServerFromEngine(eng *engine.Engine, t Fataler) *Server { return srv } -// A common interface to access the Fatal method of -// both testing.B and testing.T. -type Fataler interface { - Fatal(args ...interface{}) + +func NewTestEngine(t utils.Fataler) *engine.Engine { + root, err := newTestDirectory(unitTestStoreBase) + if err != nil { + t.Fatal(err) + } + eng, err := engine.New(root) + if err != nil { + t.Fatal(err) + } + // Load default plugins + // (This is manually copied and modified from main() until we have a more generic plugin system) + job := eng.Job("initapi") + job.Setenv("Root", root) + job.SetenvBool("AutoRestart", false) + if err := job.Run(); err != nil { + t.Fatal(err) + } + return eng } -func newTestRuntime(prefix string) (runtime *Runtime, err error) { +func newTestDirectory(templateDir string) (dir string, err error) { + if globalTestID == "" { + globalTestID = GenerateID()[:4] + } + prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, getCallerName(2)) if prefix == "" { prefix = "docker-test-" } - utils.Debugf("prefix = %s", prefix) - utils.Debugf("newTestRuntime start") - root, err := ioutil.TempDir("", prefix) - defer func() { - utils.Debugf("newTestRuntime: %s", root) - }() - if err != nil { - return nil, err + dir, err = ioutil.TempDir("", prefix) + if err = os.Remove(dir); err != nil { + return } - if err := os.Remove(root); err != nil { - return nil, err - } - if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil { - return nil, err + if err = utils.CopyDirectory(templateDir, dir); err != nil { + return } + return +} - config := &DaemonConfig{ - Root: root, - AutoRestart: false, - } - runtime, err = NewRuntimeFromDirectory(config) - if err != nil { - return nil, err - } - runtime.UpdateCapabilities(true) - return runtime, nil +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 } // Write `content` to the file at path `dst`, creating it if necessary, From 434f06d03dc2825cb4f348a88ddc6d1aa17ea19c Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 27 Oct 2013 07:15:22 +0000 Subject: [PATCH 09/13] Engine: fix a bug when encoding a job environment to json --- api.go | 3 +++ engine/job.go | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/api.go b/api.go index 1a3d92de32..18251eba2f 100644 --- a/api.go +++ b/api.go @@ -644,6 +644,9 @@ func postContainersStart(srv *Server, version float64, w http.ResponseWriter, r } name := vars["name"] job := srv.Eng.Job("start", name) + if err := job.ImportEnv(HostConfig{}); err != nil { + return fmt.Errorf("Couldn't initialize host configuration") + } // allow a nil body for backwards compatibility if r.Body != nil { if matchesContentType(r.Header.Get("Content-Type"), "application/json") { diff --git a/engine/job.go b/engine/job.go index 7a261409e0..b6296eb91d 100644 --- a/engine/job.go +++ b/engine/job.go @@ -149,10 +149,22 @@ func (job *Job) DecodeEnv(src io.Reader) error { } func (job *Job) EncodeEnv(dst io.Writer) error { - return json.NewEncoder(dst).Encode(job.Environ()) + m := make(map[string]interface{}) + for k, v := range job.Environ() { + var val interface{} + if err := json.Unmarshal([]byte(v), &val); err == nil { + m[k] = val + } else { + m[k] = v + } + } + if err := json.NewEncoder(dst).Encode(&m); err != nil { + return err + } + return nil } -func (job *Job) ExportEnv(dst interface{}) error { +func (job *Job) ExportEnv(dst interface{}) (err error) { var buf bytes.Buffer if err := job.EncodeEnv(&buf); err != nil { return err From d3f074494a9594bc268bf4c639a7aea0934ec7c0 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 27 Oct 2013 07:16:32 +0000 Subject: [PATCH 10/13] Better error reporting in engine logs and unit tests --- runtime_test.go | 2 +- server.go | 7 ++++++- utils_test.go | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/runtime_test.go b/runtime_test.go index 4aac2f3442..946a8ebf61 100644 --- a/runtime_test.go +++ b/runtime_test.go @@ -183,7 +183,7 @@ func GetTestImage(runtime *Runtime) *Image { return image } } - log.Fatalf("Test image %v not found", unitTestImageID) + log.Fatalf("Test image %v not found in %s: %s", unitTestImageID, runtime.graph.Root, imgs) return nil } diff --git a/server.go b/server.go index c041491ead..0a1270bede 100644 --- a/server.go +++ b/server.go @@ -40,6 +40,7 @@ func init() { // Only one api server can run at the same time - this is enforced by a pidfile. // The signals SIGINT, SIGKILL and SIGTERM are intercepted for cleanup. func jobInitApi(job *engine.Job) string { + job.Logf("Creating server") srv, err := NewServer(job.Eng, ConfigFromJob(job)) if err != nil { return err.Error() @@ -50,6 +51,7 @@ func jobInitApi(job *engine.Job) string { log.Fatal(err) } } + job.Logf("Setting up signal traps") c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM)) go func() { @@ -1288,7 +1290,7 @@ func (srv *Server) RegisterLinks(name string, hostConfig *HostConfig) error { return fmt.Errorf("No such container: %s", name) } - if hostConfig.Links != nil { + if hostConfig != nil && hostConfig.Links != nil { for _, l := range hostConfig.Links { parts, err := parseLink(l) if err != nil { @@ -1317,11 +1319,14 @@ func (srv *Server) RegisterLinks(name string, hostConfig *HostConfig) error { } func (srv *Server) ContainerStart(job *engine.Job) string { + job.Logf("srv engine = %s", srv.Eng.Root()) + job.Logf("job engine = %s", job.Eng.Root()) if len(job.Args) < 1 { return fmt.Sprintf("Usage: %s container_id", job.Name) } name := job.Args[0] runtime := srv.runtime + job.Logf("loading containers from %s", runtime.repository) container := runtime.Get(name) if container == nil { return fmt.Sprintf("No such container: %s", name) diff --git a/utils_test.go b/utils_test.go index 95e12b6393..529af127e5 100644 --- a/utils_test.go +++ b/utils_test.go @@ -41,11 +41,11 @@ func mkRuntime(f utils.Fataler) *Runtime { func mkServerFromEngine(eng *engine.Engine, t utils.Fataler) *Server { iSrv := eng.Hack_GetGlobalVar("httpapi.server") if iSrv == nil { - t.Fatal("Legacy server field not set in engine") + panic("Legacy server field not set in engine") } srv, ok := iSrv.(*Server) if !ok { - t.Fatal("Legacy server field in engine does not cast to *Server") + panic("Legacy server field in engine does not cast to *Server") } return srv } From 5c42b2b5122c1db08d229c258da26869b4d4d9cc Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Tue, 5 Nov 2013 19:57:40 +0000 Subject: [PATCH 11/13] Fix main() --- docker/docker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/docker.go b/docker/docker.go index e58bd40013..213ec73fd9 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -86,8 +86,8 @@ func main() { log.Fatal(err) } // Serve api - job := eng.Job("serveapi", flHosts...) - job.Setenv("Logging", true) + job = eng.Job("serveapi", flHosts...) + job.SetenvBool("Logging", true) if err := job.Run(); err != nil { log.Fatal(err) } From e5f8ab6160401fb541121da5b5cbc3af4fce28b7 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 27 Oct 2013 19:20:00 -0700 Subject: [PATCH 12/13] Engine: 'create' creates a container and prints its ID on stdout --- api.go | 31 ++++----- api_test.go | 28 ++++---- engine/engine.go | 2 +- engine/job.go | 165 +++++++++++++++++++++++++++++++++++++++++++---- runtime_test.go | 58 ++++++----------- server.go | 35 ++++++---- server_test.go | 64 +++++++----------- utils_test.go | 16 +++++ 8 files changed, 260 insertions(+), 139 deletions(-) diff --git a/api.go b/api.go index 18251eba2f..1fc74d764a 100644 --- a/api.go +++ b/api.go @@ -526,43 +526,36 @@ func postContainersCreate(srv *Server, version float64, w http.ResponseWriter, r if err := parseForm(r); err != nil { return nil } - config := &Config{} out := &APIRun{} - name := r.Form.Get("name") - - if err := json.NewDecoder(r.Body).Decode(config); err != nil { + job := srv.Eng.Job("create", r.Form.Get("name")) + if err := job.DecodeEnv(r.Body); err != nil { return err } - resolvConf, err := utils.GetResolvConf() if err != nil { return err } - - if !config.NetworkDisabled && len(config.Dns) == 0 && len(srv.runtime.config.Dns) == 0 && utils.CheckLocalDns(resolvConf) { + if !job.GetenvBool("NetworkDisabled") && len(job.Getenv("Dns")) == 0 && len(srv.runtime.config.Dns) == 0 && utils.CheckLocalDns(resolvConf) { out.Warnings = append(out.Warnings, fmt.Sprintf("Docker detected local DNS server on resolv.conf. Using default external servers: %v", defaultDns)) - config.Dns = defaultDns + job.SetenvList("Dns", defaultDns) } - - id, warnings, err := srv.ContainerCreate(config, name) - if err != nil { + // Read container ID from the first line of stdout + job.StdoutParseString(&out.ID) + // Read warnings from stderr + job.StderrParseLines(&out.Warnings, 0) + if err := job.Run(); err != nil { return err } - out.ID = id - for _, warning := range warnings { - out.Warnings = append(out.Warnings, warning) - } - - if config.Memory > 0 && !srv.runtime.capabilities.MemoryLimit { + if job.GetenvInt("Memory") > 0 && !srv.runtime.capabilities.MemoryLimit { log.Println("WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.") out.Warnings = append(out.Warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.") } - if config.Memory > 0 && !srv.runtime.capabilities.SwapLimit { + if job.GetenvInt("Memory") > 0 && !srv.runtime.capabilities.SwapLimit { log.Println("WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.") out.Warnings = append(out.Warnings, "Your kernel does not support memory swap capabilities. Limitation discarded.") } - if !config.NetworkDisabled && srv.runtime.capabilities.IPv4ForwardingDisabled { + if !job.GetenvBool("NetworkDisabled") && srv.runtime.capabilities.IPv4ForwardingDisabled { log.Println("Warning: IPv4 forwarding is disabled.") out.Warnings = append(out.Warnings, "IPv4 forwarding is disabled.") } diff --git a/api_test.go b/api_test.go index 50a86e3f23..7b0dfaa077 100644 --- a/api_test.go +++ b/api_test.go @@ -634,11 +634,11 @@ func TestPostCommit(t *testing.T) { } func TestPostContainersCreate(t *testing.T) { - runtime := mkRuntime(t) + eng := NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) - srv := &Server{runtime: runtime} - configJSON, err := json.Marshal(&Config{ Image: GetTestImage(runtime).ID, Memory: 33554432, @@ -786,22 +786,18 @@ func TestPostContainersStart(t *testing.T) { runtime := srv.runtime defer nuke(runtime) - container, _, err := runtime.Create( + id := createTestContainer( + eng, &Config{ Image: GetTestImage(runtime).ID, Cmd: []string{"/bin/cat"}, OpenStdin: true, }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer runtime.Destroy(container) + t) hostConfigJSON, err := json.Marshal(&HostConfig{}) - req, err := http.NewRequest("POST", "/containers/"+container.ID+"/start", bytes.NewReader(hostConfigJSON)) + req, err := http.NewRequest("POST", "/containers/"+id+"/start", bytes.NewReader(hostConfigJSON)) if err != nil { t.Fatal(err) } @@ -809,22 +805,26 @@ func TestPostContainersStart(t *testing.T) { req.Header.Set("Content-Type", "application/json") r := httptest.NewRecorder() - if err := postContainersStart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil { + if err := postContainersStart(srv, APIVERSION, r, req, map[string]string{"name": id}); err != nil { t.Fatal(err) } if r.Code != http.StatusNoContent { t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code) } + container := runtime.Get(id) + if container == nil { + t.Fatalf("Container %s was not created", id) + } // Give some time to the process to start + // FIXME: use Wait once it's available as a job container.WaitTimeout(500 * time.Millisecond) - if !container.State.Running { t.Errorf("Container should be running") } r = httptest.NewRecorder() - if err = postContainersStart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err == nil { + if err = postContainersStart(srv, APIVERSION, r, req, map[string]string{"name": id}); err == nil { t.Fatalf("A running container should be able to be started") } diff --git a/engine/engine.go b/engine/engine.go index 565c6ed4fa..428ef706c9 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -115,5 +115,5 @@ func (eng *Engine) Job(name string, args ...string) *Job { func (eng *Engine) Logf(format string, args ...interface{}) (n int, err error) { prefixedFormat := fmt.Sprintf("[%s] %s\n", eng, strings.TrimRight(format, "\n")) - return fmt.Printf(prefixedFormat, args...) + return fmt.Fprintf(os.Stderr, prefixedFormat, args...) } diff --git a/engine/job.go b/engine/job.go index b6296eb91d..ece8d8f8d6 100644 --- a/engine/job.go +++ b/engine/job.go @@ -1,11 +1,16 @@ package engine import ( + "bufio" "bytes" "io" + "io/ioutil" + "strconv" "strings" "fmt" + "sync" "encoding/json" + "os" ) // A job is the fundamental unit of work in the docker engine. @@ -26,20 +31,38 @@ type Job struct { Name string Args []string env []string - Stdin io.ReadCloser - Stdout io.WriteCloser - Stderr io.WriteCloser + Stdin io.Reader + Stdout io.Writer + Stderr io.Writer handler func(*Job) string status string + onExit []func() } // Run executes the job and blocks until the job completes. // If the job returns a failure status, an error is returned // which includes the status. func (job *Job) Run() error { - job.Logf("{") defer func() { - job.Logf("}") + var wg sync.WaitGroup + for _, f := range job.onExit { + wg.Add(1) + go func(f func()) { + f() + wg.Done() + }(f) + } + wg.Wait() + }() + if job.Stdout != nil && job.Stdout != os.Stdout { + job.Stdout = io.MultiWriter(job.Stdout, os.Stdout) + } + if job.Stderr != nil && job.Stderr != os.Stderr { + job.Stderr = io.MultiWriter(job.Stderr, os.Stderr) + } + job.Eng.Logf("+job %s", job.CallString()) + defer func() { + job.Eng.Logf("-job %s%s", job.CallString(), job.StatusString()) }() if job.handler == nil { job.status = "command not found" @@ -52,9 +75,66 @@ func (job *Job) Run() error { return nil } -// String returns a human-readable description of `job` -func (job *Job) String() string { - s := fmt.Sprintf("%s.%s(%s)", job.Eng, job.Name, strings.Join(job.Args, ", ")) +func (job *Job) StdoutParseLines(dst *[]string, limit int) { + job.parseLines(job.StdoutPipe(), dst, limit) +} + +func (job *Job) StderrParseLines(dst *[]string, limit int) { + job.parseLines(job.StderrPipe(), dst, limit) +} + +func (job *Job) parseLines(src io.Reader, dst *[]string, limit int) { + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + scanner := bufio.NewScanner(src) + for scanner.Scan() { + // If the limit is reached, flush the rest of the source and return + if limit > 0 && len(*dst) >= limit { + io.Copy(ioutil.Discard, src) + return + } + line := scanner.Text() + // Append the line (with delimitor removed) + *dst = append(*dst, line) + } + }() + job.onExit = append(job.onExit, wg.Wait) +} + +func (job *Job) StdoutParseString(dst *string) { + lines := make([]string, 0, 1) + job.StdoutParseLines(&lines, 1) + job.onExit = append(job.onExit, func() { if len(lines) >= 1 { *dst = lines[0] }}) +} + +func (job *Job) StderrParseString(dst *string) { + lines := make([]string, 0, 1) + job.StderrParseLines(&lines, 1) + job.onExit = append(job.onExit, func() { *dst = lines[0]; }) +} + +func (job *Job) StdoutPipe() io.ReadCloser { + r, w := io.Pipe() + job.Stdout = w + job.onExit = append(job.onExit, func(){ w.Close() }) + return r +} + +func (job *Job) StderrPipe() io.ReadCloser { + r, w := io.Pipe() + job.Stderr = w + job.onExit = append(job.onExit, func(){ w.Close() }) + return r +} + + +func (job *Job) CallString() string { + return fmt.Sprintf("%s(%s)", job.Name, strings.Join(job.Args, ", ")) +} + +func (job *Job) StatusString() string { // FIXME: if a job returns the empty string, it will be printed // as not having returned. // (this only affects String which is a convenience function). @@ -65,9 +145,14 @@ func (job *Job) String() string { } else { okerr = "ERR" } - s = fmt.Sprintf("%s = %s (%s)", s, okerr, job.status) + return fmt.Sprintf(" = %s (%s)", okerr, job.status) } - return s + return "" +} + +// String returns a human-readable description of `job` +func (job *Job) String() string { + return fmt.Sprintf("%s.%s%s", job.Eng, job.CallString(), job.StatusString()) } func (job *Job) Getenv(key string) (value string) { @@ -104,6 +189,19 @@ func (job *Job) SetenvBool(key string, value bool) { } } +func (job *Job) GetenvInt(key string) int64 { + s := strings.Trim(job.Getenv(key), " \t") + val, err := strconv.ParseInt(s, 10, 64) + if err != nil { + return -1 + } + return val +} + +func (job *Job) SetenvInt(key string, value int64) { + job.Setenv(key, fmt.Sprintf("%d", value)) +} + func (job *Job) GetenvList(key string) []string { sval := job.Getenv(key) l := make([]string, 0, 1) @@ -137,13 +235,21 @@ func (job *Job) DecodeEnv(src io.Reader) error { return err } for k, v := range m { - if sval, ok := v.(string); ok { + // FIXME: we fix-convert float values to int, because + // encoding/json decodes integers to float64, but cannot encode them back. + // (See http://golang.org/src/pkg/encoding/json/decode.go#L46) + if fval, ok := v.(float64); ok { + job.Logf("Converted to float: %v->%v", v, fval) + job.SetenvInt(k, int64(fval)) + } else if sval, ok := v.(string); ok { + job.Logf("Converted to string: %v->%v", v, sval) job.Setenv(k, sval) } else if val, err := json.Marshal(v); err == nil { job.Setenv(k, string(val)) } else { job.Setenv(k, fmt.Sprintf("%v", v)) } + job.Logf("Decoded %s=%#v to %s=%#v", k, v, k, job.Getenv(k)) } return nil } @@ -153,10 +259,17 @@ func (job *Job) EncodeEnv(dst io.Writer) error { for k, v := range job.Environ() { var val interface{} if err := json.Unmarshal([]byte(v), &val); err == nil { + // FIXME: we fix-convert float values to int, because + // encoding/json decodes integers to float64, but cannot encode them back. + // (See http://golang.org/src/pkg/encoding/json/decode.go#L46) + if fval, isFloat := val.(float64); isFloat { + val = int(fval) + } m[k] = val } else { m[k] = v } + job.Logf("Encoded %s=%#v to %s=%#v", k, v, k, m[k]) } if err := json.NewEncoder(dst).Encode(&m); err != nil { return err @@ -165,21 +278,38 @@ func (job *Job) EncodeEnv(dst io.Writer) error { } func (job *Job) ExportEnv(dst interface{}) (err error) { + fmt.Fprintf(os.Stderr, "ExportEnv()\n") + defer func() { + if err != nil { + err = fmt.Errorf("ExportEnv %s", err) + } + }() var buf bytes.Buffer + job.Logf("ExportEnv: step 1: encode/marshal the env to an intermediary json representation") + fmt.Fprintf(os.Stderr, "Printed ExportEnv step 1\n") if err := job.EncodeEnv(&buf); err != nil { return err } + job.Logf("ExportEnv: step 1 complete: json=|%s|", buf) + job.Logf("ExportEnv: step 2: decode/unmarshal the intermediary json into the destination object") if err := json.NewDecoder(&buf).Decode(dst); err != nil { return err } + job.Logf("ExportEnv: step 2 complete") return nil } -func (job *Job) ImportEnv(src interface{}) error { +func (job *Job) ImportEnv(src interface{}) (err error) { + defer func() { + if err != nil { + err = fmt.Errorf("ImportEnv: %s", err) + } + }() var buf bytes.Buffer if err := json.NewEncoder(&buf).Encode(src); err != nil { return err } + job.Logf("ImportEnv: json=|%s|", buf) if err := job.DecodeEnv(&buf); err != nil { return err } @@ -197,5 +327,14 @@ func (job *Job) Environ() map[string]string { func (job *Job) Logf(format string, args ...interface{}) (n int, err error) { prefixedFormat := fmt.Sprintf("[%s] %s\n", job, strings.TrimRight(format, "\n")) - return fmt.Fprintf(job.Stdout, prefixedFormat, args...) + return fmt.Fprintf(job.Stderr, prefixedFormat, args...) +} + +func (job *Job) Printf(format string, args ...interface{}) (n int, err error) { + return fmt.Fprintf(job.Stdout, format, args...) +} + +func (job *Job) Errorf(format string, args ...interface{}) (n int, err error) { + return fmt.Fprintf(job.Stderr, format, args...) + } diff --git a/runtime_test.go b/runtime_test.go index 946a8ebf61..ce69465961 100644 --- a/runtime_test.go +++ b/runtime_test.go @@ -645,20 +645,17 @@ func TestReloadContainerLinks(t *testing.T) { } func TestDefaultContainerName(t *testing.T) { - runtime := mkRuntime(t) + eng := NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) - srv := &Server{runtime: runtime} config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil) if err != nil { t.Fatal(err) } - shortId, _, err := srv.ContainerCreate(config, "some_name") - if err != nil { - t.Fatal(err) - } - container := runtime.Get(shortId) + container := runtime.Get(createNamedTestContainer(eng, config, t, "some_name")) containerID := container.ID if container.Name != "/some_name" { @@ -682,20 +679,17 @@ func TestDefaultContainerName(t *testing.T) { } func TestRandomContainerName(t *testing.T) { - runtime := mkRuntime(t) + eng := NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) - srv := &Server{runtime: runtime} config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil) if err != nil { t.Fatal(err) } - shortId, _, err := srv.ContainerCreate(config, "") - if err != nil { - t.Fatal(err) - } - container := runtime.Get(shortId) + container := runtime.Get(createTestContainer(eng, config, t)) containerID := container.ID if container.Name == "" { @@ -719,20 +713,17 @@ func TestRandomContainerName(t *testing.T) { } func TestLinkChildContainer(t *testing.T) { - runtime := mkRuntime(t) + eng := NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) - srv := &Server{runtime: runtime} config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil) if err != nil { t.Fatal(err) } - shortId, _, err := srv.ContainerCreate(config, "/webapp") - if err != nil { - t.Fatal(err) - } - container := runtime.Get(shortId) + container := runtime.Get(createNamedTestContainer(eng, config, t, "/webapp")) webapp, err := runtime.GetByName("/webapp") if err != nil { @@ -748,12 +739,7 @@ func TestLinkChildContainer(t *testing.T) { t.Fatal(err) } - shortId, _, err = srv.ContainerCreate(config, "") - if err != nil { - t.Fatal(err) - } - - childContainer := runtime.Get(shortId) + childContainer := runtime.Get(createTestContainer(eng, config, t)) if err := runtime.RegisterLink(webapp, childContainer, "db"); err != nil { t.Fatal(err) @@ -770,20 +756,17 @@ func TestLinkChildContainer(t *testing.T) { } func TestGetAllChildren(t *testing.T) { - runtime := mkRuntime(t) + eng := NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) - srv := &Server{runtime: runtime} config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil) if err != nil { t.Fatal(err) } - shortId, _, err := srv.ContainerCreate(config, "/webapp") - if err != nil { - t.Fatal(err) - } - container := runtime.Get(shortId) + container := runtime.Get(createNamedTestContainer(eng, config, t, "/webapp")) webapp, err := runtime.GetByName("/webapp") if err != nil { @@ -799,12 +782,7 @@ func TestGetAllChildren(t *testing.T) { t.Fatal(err) } - shortId, _, err = srv.ContainerCreate(config, "") - if err != nil { - t.Fatal(err) - } - - childContainer := runtime.Get(shortId) + childContainer := runtime.Get(createTestContainer(eng, config, t)) if err := runtime.RegisterLink(webapp, childContainer, "db"); err != nil { t.Fatal(err) diff --git a/server.go b/server.go index 0a1270bede..6196df8981 100644 --- a/server.go +++ b/server.go @@ -62,6 +62,9 @@ func jobInitApi(job *engine.Job) string { os.Exit(0) }() job.Eng.Hack_SetGlobalVar("httpapi.server", srv) + if err := job.Eng.Register("create", srv.ContainerCreate); err != nil { + return err.Error() + } if err := job.Eng.Register("start", srv.ContainerStart); err != nil { return err.Error() } @@ -1009,33 +1012,43 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write return nil } -func (srv *Server) ContainerCreate(config *Config, name string) (string, []string, error) { - if config.Memory != 0 && config.Memory < 524288 { - return "", nil, fmt.Errorf("Memory limit must be given in bytes (minimum 524288 bytes)") +func (srv *Server) ContainerCreate(job *engine.Job) string { + var name string + if len(job.Args) == 1 { + name = job.Args[0] + } else if len(job.Args) > 1 { + return fmt.Sprintf("Usage: %s ", job.Name) + } + var config Config + if err := job.ExportEnv(&config); err != nil { + return err.Error() + } + if config.Memory != 0 && config.Memory < 524288 { + return "Memory limit must be given in bytes (minimum 524288 bytes)" } - if config.Memory > 0 && !srv.runtime.capabilities.MemoryLimit { config.Memory = 0 } - if config.Memory > 0 && !srv.runtime.capabilities.SwapLimit { config.MemorySwap = -1 } - container, buildWarnings, err := srv.runtime.Create(config, name) + container, buildWarnings, err := srv.runtime.Create(&config, name) if err != nil { if srv.runtime.graph.IsNotExist(err) { - _, tag := utils.ParseRepositoryTag(config.Image) if tag == "" { tag = DEFAULTTAG } - - return "", nil, fmt.Errorf("No such image: %s (tag: %s)", config.Image, tag) + return fmt.Sprintf("No such image: %s (tag: %s)", config.Image, tag) } - return "", nil, err + return err.Error() } srv.LogEvent("create", container.ShortID(), srv.runtime.repositories.ImageName(container.Image)) - return container.ShortID(), buildWarnings, nil + job.Printf("%s\n", container.ShortID()) + for _, warning := range buildWarnings { + job.Errorf("%s\n", warning) + } + return "0" } func (srv *Server) ContainerRestart(name string, t int) error { diff --git a/server_test.go b/server_test.go index a4bfb0155f..111043eca2 100644 --- a/server_test.go +++ b/server_test.go @@ -79,20 +79,17 @@ func TestContainerTagImageDelete(t *testing.T) { } func TestCreateRm(t *testing.T) { - runtime := mkRuntime(t) + eng := NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) - srv := &Server{runtime: runtime} - config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil) if err != nil { t.Fatal(err) } - id, _, err := srv.ContainerCreate(config, "") - if err != nil { - t.Fatal(err) - } + id := createTestContainer(eng, config, t) if len(runtime.List()) != 1 { t.Errorf("Expected 1 container, %v found", len(runtime.List())) @@ -120,10 +117,7 @@ func TestCreateRmVolumes(t *testing.T) { t.Fatal(err) } - id, _, err := srv.ContainerCreate(config, "") - if err != nil { - t.Fatal(err) - } + id := createTestContainer(eng, config, t) if len(runtime.List()) != 1 { t.Errorf("Expected 1 container, %v found", len(runtime.List())) @@ -152,20 +146,17 @@ func TestCreateRmVolumes(t *testing.T) { } func TestCommit(t *testing.T) { - runtime := mkRuntime(t) + eng := NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) - srv := &Server{runtime: runtime} - config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "/bin/cat"}, nil) if err != nil { t.Fatal(err) } - id, _, err := srv.ContainerCreate(config, "") - if err != nil { - t.Fatal(err) - } + id := createTestContainer(eng, config, t) if _, err := srv.ContainerCommit(id, "testrepo", "testtag", "", "", config); err != nil { t.Fatal(err) @@ -183,10 +174,7 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) { t.Fatal(err) } - id, _, err := srv.ContainerCreate(config, "") - if err != nil { - t.Fatal(err) - } + id := createTestContainer(eng, config, t) if len(runtime.List()) != 1 { t.Errorf("Expected 1 container, %v found", len(runtime.List())) @@ -232,22 +220,22 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) { } func TestRunWithTooLowMemoryLimit(t *testing.T) { - runtime := mkRuntime(t) + eng := NewTestEngine(t) + srv := mkServerFromEngine(eng, t) + runtime := srv.runtime defer nuke(runtime) // Try to create a container with a memory limit of 1 byte less than the minimum allowed limit. - if _, _, err := (*Server).ContainerCreate(&Server{runtime: runtime}, - &Config{ - Image: GetTestImage(runtime).ID, - Memory: 524287, - CpuShares: 1000, - Cmd: []string{"/bin/cat"}, - }, - "", - ); err == nil { + job := eng.Job("create") + job.Setenv("Image", GetTestImage(runtime).ID) + job.Setenv("Memory", "524287") + job.Setenv("CpuShares", "1000") + job.SetenvList("Cmd", []string{"/bin/cat"}) + var id string + job.StdoutParseString(&id) + if err := job.Run(); err == nil { t.Errorf("Memory limit is smaller than the allowed limit. Container creation should've failed!") } - } func TestContainerTop(t *testing.T) { @@ -411,10 +399,7 @@ func TestRmi(t *testing.T) { t.Fatal(err) } - containerID, _, err := srv.ContainerCreate(config, "") - if err != nil { - t.Fatal(err) - } + containerID := createTestContainer(eng, config, t) //To remove job := eng.Job("start", containerID) @@ -435,10 +420,7 @@ func TestRmi(t *testing.T) { t.Fatal(err) } - containerID, _, err = srv.ContainerCreate(config, "") - if err != nil { - t.Fatal(err) - } + containerID = createTestContainer(eng, config, t) //To remove job = eng.Job("start", containerID) diff --git a/utils_test.go b/utils_test.go index 529af127e5..2e8c0ceb1b 100644 --- a/utils_test.go +++ b/utils_test.go @@ -38,6 +38,22 @@ func mkRuntime(f utils.Fataler) *Runtime { return r } +func createNamedTestContainer(eng *engine.Engine, config *Config, f utils.Fataler, name string) (shortId string) { + job := eng.Job("create", name) + if err := job.ImportEnv(config); err != nil { + f.Fatal(err) + } + job.StdoutParseString(&shortId) + if err := job.Run(); err != nil { + f.Fatal(err) + } + return +} + +func createTestContainer(eng *engine.Engine, config *Config, f utils.Fataler) (shortId string) { + return createNamedTestContainer(eng, config, f, "") +} + func mkServerFromEngine(eng *engine.Engine, t utils.Fataler) *Server { iSrv := eng.Hack_GetGlobalVar("httpapi.server") if iSrv == nil { From 8d6df3a7e2080d4fad9743beb159f12caa0ff6f7 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Tue, 5 Nov 2013 22:22:37 +0000 Subject: [PATCH 13/13] Remove debug messages --- engine/job.go | 13 ++----------- server.go | 3 --- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/engine/job.go b/engine/job.go index ece8d8f8d6..419ac73466 100644 --- a/engine/job.go +++ b/engine/job.go @@ -239,17 +239,14 @@ func (job *Job) DecodeEnv(src io.Reader) error { // encoding/json decodes integers to float64, but cannot encode them back. // (See http://golang.org/src/pkg/encoding/json/decode.go#L46) if fval, ok := v.(float64); ok { - job.Logf("Converted to float: %v->%v", v, fval) job.SetenvInt(k, int64(fval)) } else if sval, ok := v.(string); ok { - job.Logf("Converted to string: %v->%v", v, sval) job.Setenv(k, sval) } else if val, err := json.Marshal(v); err == nil { job.Setenv(k, string(val)) } else { job.Setenv(k, fmt.Sprintf("%v", v)) } - job.Logf("Decoded %s=%#v to %s=%#v", k, v, k, job.Getenv(k)) } return nil } @@ -269,7 +266,6 @@ func (job *Job) EncodeEnv(dst io.Writer) error { } else { m[k] = v } - job.Logf("Encoded %s=%#v to %s=%#v", k, v, k, m[k]) } if err := json.NewEncoder(dst).Encode(&m); err != nil { return err @@ -278,24 +274,20 @@ func (job *Job) EncodeEnv(dst io.Writer) error { } func (job *Job) ExportEnv(dst interface{}) (err error) { - fmt.Fprintf(os.Stderr, "ExportEnv()\n") defer func() { if err != nil { err = fmt.Errorf("ExportEnv %s", err) } }() var buf bytes.Buffer - job.Logf("ExportEnv: step 1: encode/marshal the env to an intermediary json representation") - fmt.Fprintf(os.Stderr, "Printed ExportEnv step 1\n") + // step 1: encode/marshal the env to an intermediary json representation if err := job.EncodeEnv(&buf); err != nil { return err } - job.Logf("ExportEnv: step 1 complete: json=|%s|", buf) - job.Logf("ExportEnv: step 2: decode/unmarshal the intermediary json into the destination object") + // step 2: decode/unmarshal the intermediary json into the destination object if err := json.NewDecoder(&buf).Decode(dst); err != nil { return err } - job.Logf("ExportEnv: step 2 complete") return nil } @@ -309,7 +301,6 @@ func (job *Job) ImportEnv(src interface{}) (err error) { if err := json.NewEncoder(&buf).Encode(src); err != nil { return err } - job.Logf("ImportEnv: json=|%s|", buf) if err := job.DecodeEnv(&buf); err != nil { return err } diff --git a/server.go b/server.go index 6196df8981..abcb001bb0 100644 --- a/server.go +++ b/server.go @@ -1332,14 +1332,11 @@ func (srv *Server) RegisterLinks(name string, hostConfig *HostConfig) error { } func (srv *Server) ContainerStart(job *engine.Job) string { - job.Logf("srv engine = %s", srv.Eng.Root()) - job.Logf("job engine = %s", job.Eng.Root()) if len(job.Args) < 1 { return fmt.Sprintf("Usage: %s container_id", job.Name) } name := job.Args[0] runtime := srv.runtime - job.Logf("loading containers from %s", runtime.repository) container := runtime.Get(name) if container == nil { return fmt.Sprintf("No such container: %s", name)