From fdb3de7b11e4b85ec86f76aad51290730c037668 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 17 Nov 2013 00:26:04 +0000 Subject: [PATCH 1/5] Engine: new command 'kill' sends a signal to a running container --- api.go | 16 ++++------------ server.go | 29 +++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/api.go b/api.go index 0aa9695702..61890d86d4 100644 --- a/api.go +++ b/api.go @@ -150,19 +150,11 @@ func postContainersKill(srv *Server, version float64, w http.ResponseWriter, r * if err := parseForm(r); err != nil { return err } - name := vars["name"] - - signal := 0 - if r != nil { - if s := r.Form.Get("signal"); s != "" { - s, err := strconv.Atoi(s) - if err != nil { - return err - } - signal = s - } + job := srv.Eng.Job("kill", vars["name"]) + if sig := r.Form.Get("signal"); sig != "" { + job.Args = append(job.Args, sig) } - if err := srv.ContainerKill(name, signal); err != nil { + if err := job.Run(); err != nil { return err } w.WriteHeader(http.StatusNoContent) diff --git a/server.go b/server.go index 49b25f7098..297ac33549 100644 --- a/server.go +++ b/server.go @@ -23,6 +23,7 @@ import ( "path/filepath" "runtime" "strings" + "strconv" "sync" "syscall" "time" @@ -78,6 +79,9 @@ func jobInitApi(job *engine.Job) engine.Status { job.Error(err) return engine.StatusErr } + if err := job.Eng.Register("kill", srv.ContainerKill); err != nil { + return err.Error() + } if err := job.Eng.Register("serveapi", srv.ListenAndServe); err != nil { job.Error(err) return engine.StatusErr @@ -169,25 +173,38 @@ func (srv *Server) versionInfos() []utils.VersionInfo { // If no signal is given (sig 0), then Kill with SIGKILL and wait // for the container to exit. // If a signal is given, then just send it to the container and return. -func (srv *Server) ContainerKill(name string, sig int) error { +func (srv *Server) ContainerKill(job *engine.Job) string { + if n := len(job.Args); n < 1 || n > 2 { + return fmt.Sprintf("Usage: %s CONTAINER [SIGNAL]", job.Name) + } + name := job.Args[0] + var sig uint64 + if len(job.Args) == 2 && job.Args[1] != "" { + var err error + // The largest legal signal is 31, so let's parse on 5 bits + sig, err = strconv.ParseUint(job.Args[1], 10, 5) + if err != nil { + return fmt.Sprintf("Invalid signal: %s", job.Args[1]) + } + } if container := srv.runtime.Get(name); container != nil { // If no signal is passed, perform regular Kill (SIGKILL + wait()) if sig == 0 { if err := container.Kill(); err != nil { - return fmt.Errorf("Cannot kill container %s: %s", name, err) + return fmt.Sprintf("Cannot kill container %s: %s", name, err) } srv.LogEvent("kill", container.ID, srv.runtime.repositories.ImageName(container.Image)) } else { // Otherwise, just send the requested signal - if err := container.kill(sig); err != nil { - return fmt.Errorf("Cannot kill container %s: %s", name, err) + if err := container.kill(int(sig)); err != nil { + return fmt.Sprintf("Cannot kill container %s: %s", name, err) } // FIXME: Add event for signals } } else { - return fmt.Errorf("No such container: %s", name) + return fmt.Sprintf("No such container: %s", name) } - return nil + return "0" } func (srv *Server) ContainerExport(name string, out io.Writer) error { From 2546a2c645c59d094bc2c09dc9eb0d8eb5c36786 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 17 Nov 2013 00:26:42 +0000 Subject: [PATCH 2/5] Hack: use new 'kill' command in integration tests --- integration/server_test.go | 2 +- integration/utils_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/integration/server_test.go b/integration/server_test.go index 3e85effe8f..9ad8accf26 100644 --- a/integration/server_test.go +++ b/integration/server_test.go @@ -199,7 +199,7 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) { t.Fatal(err) } - if err := srv.ContainerKill(id, 0); err != nil { + if err := eng.Job("kill", id).Run(); err != nil { t.Fatal(err) } diff --git a/integration/utils_test.go b/integration/utils_test.go index 62ec5a51fd..8ee1a0af08 100644 --- a/integration/utils_test.go +++ b/integration/utils_test.go @@ -105,7 +105,7 @@ func containerWaitTimeout(eng *engine.Engine, id string, t utils.Fataler) error } func containerKill(eng *engine.Engine, id string, t utils.Fataler) { - if err := getContainer(eng, id, t).Kill(); err != nil { + if err := eng.Job("kill", id).Run(); err != nil { t.Fatal(err) } } From 9b1930c5a07fe3f696ec54f5d429b8020cc9086f Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Mon, 18 Nov 2013 20:17:51 +0000 Subject: [PATCH 3/5] gofmt --- server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.go b/server.go index 297ac33549..2117c7bfbb 100644 --- a/server.go +++ b/server.go @@ -22,8 +22,8 @@ import ( "path" "path/filepath" "runtime" - "strings" "strconv" + "strings" "sync" "syscall" "time" From 427bdb60e73ad059e813babd981168ea2464ea1f Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Fri, 22 Nov 2013 03:41:17 +0000 Subject: [PATCH 4/5] Engine: port 'kill' to the new integer status. --- engine/streams.go | 2 +- server.go | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/engine/streams.go b/engine/streams.go index ff7049074a..697407f1f4 100644 --- a/engine/streams.go +++ b/engine/streams.go @@ -89,7 +89,7 @@ func (o *Output) Write(p []byte) (n int, err error) { firstErr = err } } - return len(p), firstErr + return len(p), err } // Close unregisters all destinations and waits for all background diff --git a/server.go b/server.go index 2117c7bfbb..eba1e11b7f 100644 --- a/server.go +++ b/server.go @@ -80,7 +80,8 @@ func jobInitApi(job *engine.Job) engine.Status { return engine.StatusErr } if err := job.Eng.Register("kill", srv.ContainerKill); err != nil { - return err.Error() + job.Error(err) + return engine.StatusErr } if err := job.Eng.Register("serveapi", srv.ListenAndServe); err != nil { job.Error(err) @@ -173,9 +174,10 @@ func (srv *Server) versionInfos() []utils.VersionInfo { // If no signal is given (sig 0), then Kill with SIGKILL and wait // for the container to exit. // If a signal is given, then just send it to the container and return. -func (srv *Server) ContainerKill(job *engine.Job) string { +func (srv *Server) ContainerKill(job *engine.Job) engine.Status { if n := len(job.Args); n < 1 || n > 2 { - return fmt.Sprintf("Usage: %s CONTAINER [SIGNAL]", job.Name) + job.Errorf("Usage: %s CONTAINER [SIGNAL]", job.Name) + return engine.StatusErr } name := job.Args[0] var sig uint64 @@ -184,27 +186,31 @@ func (srv *Server) ContainerKill(job *engine.Job) string { // The largest legal signal is 31, so let's parse on 5 bits sig, err = strconv.ParseUint(job.Args[1], 10, 5) if err != nil { - return fmt.Sprintf("Invalid signal: %s", job.Args[1]) + job.Errorf("Invalid signal: %s", job.Args[1]) + return engine.StatusErr } } if container := srv.runtime.Get(name); container != nil { // If no signal is passed, perform regular Kill (SIGKILL + wait()) if sig == 0 { if err := container.Kill(); err != nil { - return fmt.Sprintf("Cannot kill container %s: %s", name, err) + job.Errorf("Cannot kill container %s: %s", name, err) + return engine.StatusErr } srv.LogEvent("kill", container.ID, srv.runtime.repositories.ImageName(container.Image)) } else { // Otherwise, just send the requested signal if err := container.kill(int(sig)); err != nil { - return fmt.Sprintf("Cannot kill container %s: %s", name, err) + job.Errorf("Cannot kill container %s: %s", name, err) + return engine.StatusErr } // FIXME: Add event for signals } } else { - return fmt.Sprintf("No such container: %s", name) + job.Errorf("No such container: %s", name) + return engine.StatusErr } - return "0" + return engine.StatusOK } func (srv *Server) ContainerExport(name string, out io.Writer) error { From b98d51dddb081e4bf4f54cbc9c48ef123c2254d0 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 10 Dec 2013 15:37:03 -0800 Subject: [PATCH 5/5] revert 'firstErr' --- engine/streams.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/streams.go b/engine/streams.go index 697407f1f4..ff7049074a 100644 --- a/engine/streams.go +++ b/engine/streams.go @@ -89,7 +89,7 @@ func (o *Output) Write(p []byte) (n int, err error) { firstErr = err } } - return len(p), err + return len(p), firstErr } // Close unregisters all destinations and waits for all background