зеркало из https://github.com/microsoft/docker.git
Engine: new command 'kill' sends a signal to a running container
This commit is contained in:
Родитель
7fd64e0196
Коммит
fdb3de7b11
16
api.go
16
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)
|
||||
|
|
29
server.go
29
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 {
|
||||
|
|
Загрузка…
Ссылка в новой задаче