1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #12248 from duglin/RemoveJobKill

Remove Job from `docker kill`
This commit is contained in:
Michael Crosby 2015-04-10 10:52:18 -07:00
commit 267bd51adb
5 changed files with 29 additions and 41 deletions

View file

@ -28,6 +28,7 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/parsers/filters"
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/docker/pkg/streamformatter"
"github.com/docker/docker/pkg/version"
@ -193,16 +194,34 @@ func postContainersKill(eng *engine.Engine, version version.Version, w http.Resp
if vars == nil {
return fmt.Errorf("Missing parameter")
}
if err := parseForm(r); err != nil {
err := parseForm(r)
if err != nil {
return err
}
job := eng.Job("kill", vars["name"])
if sig := r.Form.Get("signal"); sig != "" {
job.Args = append(job.Args, sig)
var sig uint64
name := vars["name"]
// If we have a signal, look at it. Otherwise, do nothing
if sigStr := vars["signal"]; sigStr != "" {
// Check if we passed the signal as a number:
// The largest legal signal is 31, so let's parse on 5 bits
sig, err = strconv.ParseUint(sigStr, 10, 5)
if err != nil {
// The signal is not a number, treat it as a string (either like
// "KILL" or like "SIGKILL")
sig = uint64(signal.SignalMap[strings.TrimPrefix(sigStr, "SIG")])
}
if sig == 0 {
return fmt.Errorf("Invalid signal: %s", sigStr)
}
}
if err := job.Run(); err != nil {
if err = getDaemon(eng).ContainerKill(name, sig); err != nil {
return err
}
w.WriteHeader(http.StatusNoContent)
return nil
}

View file

@ -123,7 +123,6 @@ func (daemon *Daemon) Install(eng *engine.Engine) error {
"create": daemon.ContainerCreate,
"export": daemon.ContainerExport,
"info": daemon.CmdInfo,
"kill": daemon.ContainerKill,
"logs": daemon.ContainerLogs,
"restart": daemon.ContainerRestart,
"start": daemon.ContainerStart,

View file

@ -2,43 +2,14 @@ package daemon
import (
"fmt"
"strconv"
"strings"
"syscall"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/signal"
)
// ContainerKill send signal to the container
// 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 (daemon *Daemon) ContainerKill(job *engine.Job) error {
if n := len(job.Args); n < 1 || n > 2 {
return fmt.Errorf("Usage: %s CONTAINER [SIGNAL]", job.Name)
}
var (
name = job.Args[0]
sig uint64
err error
)
// If we have a signal, look at it. Otherwise, do nothing
if len(job.Args) == 2 && job.Args[1] != "" {
// Check if we passed the signal as a number:
// 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 {
// The signal is not a number, treat it as a string (either like "KILL" or like "SIGKILL")
sig = uint64(signal.SignalMap[strings.TrimPrefix(job.Args[1], "SIG")])
}
if sig == 0 {
return fmt.Errorf("Invalid signal: %s", job.Args[1])
}
}
func (daemon *Daemon) ContainerKill(name string, sig uint64) error {
container, err := daemon.Get(name)
if err != nil {
return err
@ -49,13 +20,12 @@ func (daemon *Daemon) ContainerKill(job *engine.Job) error {
if err := container.Kill(); err != nil {
return fmt.Errorf("Cannot kill container %s: %s", name, err)
}
container.LogEvent("kill")
} else {
// Otherwise, just send the requested signal
if err := container.KillSig(int(sig)); err != nil {
return fmt.Errorf("Cannot kill container %s: %s", name, err)
}
// FIXME: Add event for signals
}
container.LogEvent("kill")
return nil
}

View file

@ -132,8 +132,8 @@ func TestRestartKillWait(t *testing.T) {
if err := job.Run(); err != nil {
t.Fatal(err)
}
job = eng.Job("kill", id)
if err := job.Run(); err != nil {
if err := runtime.ContainerKill(id, 0); err != nil {
t.Fatal(err)
}

View file

@ -105,7 +105,7 @@ func containerWaitTimeout(eng *engine.Engine, id string, t Fataler) error {
}
func containerKill(eng *engine.Engine, id string, t Fataler) {
if err := eng.Job("kill", id).Run(); err != nil {
if err := getDaemon(eng).ContainerKill(id, 0); err != nil {
t.Fatal(err)
}
}