2015-11-05 23:29:53 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2015-11-10 21:44:35 +00:00
|
|
|
"strconv"
|
|
|
|
"syscall"
|
2015-11-05 23:29:53 +00:00
|
|
|
|
2015-11-10 19:38:26 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-11-12 21:40:23 +00:00
|
|
|
"github.com/docker/containerd"
|
2015-12-01 19:56:08 +00:00
|
|
|
"github.com/docker/containerd/runtime"
|
2015-11-05 23:29:53 +00:00
|
|
|
"github.com/gorilla/mux"
|
2015-11-10 22:57:10 +00:00
|
|
|
"github.com/opencontainers/specs"
|
2015-11-05 23:29:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewServer(supervisor *containerd.Supervisor) http.Handler {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
s := &server{
|
|
|
|
supervisor: supervisor,
|
|
|
|
r: r,
|
|
|
|
}
|
2015-12-04 00:07:53 +00:00
|
|
|
// process handlers
|
2015-11-10 21:44:35 +00:00
|
|
|
r.HandleFunc("/containers/{id:.*}/process/{pid:.*}", s.signalPid).Methods("POST")
|
2015-11-10 22:57:10 +00:00
|
|
|
r.HandleFunc("/containers/{id:.*}/process", s.addProcess).Methods("PUT")
|
2015-12-04 00:07:53 +00:00
|
|
|
|
|
|
|
// checkpoint and restore handlers
|
|
|
|
// TODO: PUT handler for adding a checkpoint to containerd??
|
|
|
|
r.HandleFunc("/containers/{id:.*}/checkpoint/{name:.*}", s.createCheckpoint).Methods("POST")
|
2015-12-04 22:00:07 +00:00
|
|
|
r.HandleFunc("/containers/{id:.*}/checkpoint/{name:.*}", s.deleteCheckpoint).Methods("DELETE")
|
2015-12-04 00:07:53 +00:00
|
|
|
r.HandleFunc("/containers/{id:.*}/checkpoint", s.listCheckpoints).Methods("GET")
|
|
|
|
|
|
|
|
// container handlers
|
2015-11-06 00:40:57 +00:00
|
|
|
r.HandleFunc("/containers/{id:.*}", s.createContainer).Methods("POST")
|
2015-11-12 21:40:23 +00:00
|
|
|
r.HandleFunc("/containers/{id:.*}", s.updateContainer).Methods("PATCH")
|
2015-12-04 00:07:53 +00:00
|
|
|
|
2015-12-02 22:41:49 +00:00
|
|
|
// internal method for replaying the journal
|
2015-12-04 22:44:08 +00:00
|
|
|
// r.HandleFunc("/event", s.event).Methods("POST")
|
2015-11-30 23:46:36 +00:00
|
|
|
r.HandleFunc("/events", s.events).Methods("GET")
|
2015-12-04 00:07:53 +00:00
|
|
|
|
|
|
|
// containerd handlers
|
2015-12-03 19:49:56 +00:00
|
|
|
r.HandleFunc("/state", s.state).Methods("GET")
|
2015-11-05 23:29:53 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
r *mux.Router
|
|
|
|
supervisor *containerd.Supervisor
|
|
|
|
}
|
|
|
|
|
2015-11-05 23:49:13 +00:00
|
|
|
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
s.r.ServeHTTP(w, r)
|
2015-11-05 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 21:40:23 +00:00
|
|
|
func (s *server) updateContainer(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := mux.Vars(r)["id"]
|
|
|
|
var state ContainerState
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&state); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e := containerd.NewEvent(containerd.UpdateContainerEventType)
|
|
|
|
e.ID = id
|
2015-12-04 22:10:50 +00:00
|
|
|
if state.Signal != 0 {
|
|
|
|
e.Signal = syscall.Signal(state.Signal)
|
|
|
|
}
|
2015-12-01 19:56:08 +00:00
|
|
|
e.State = &runtime.State{
|
|
|
|
Status: runtime.Status(string(state.Status)),
|
2015-11-12 21:40:23 +00:00
|
|
|
}
|
|
|
|
s.supervisor.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 23:46:36 +00:00
|
|
|
func (s *server) events(w http.ResponseWriter, r *http.Request) {
|
2015-12-01 23:49:24 +00:00
|
|
|
events := s.supervisor.Events()
|
2015-11-30 23:46:36 +00:00
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
for evt := range events {
|
|
|
|
var v interface{}
|
|
|
|
switch evt.Type {
|
|
|
|
case containerd.ExitEventType:
|
|
|
|
v = createExitEvent(evt)
|
|
|
|
}
|
|
|
|
if err := enc.Encode(v); err != nil {
|
|
|
|
// TODO: handled closed conn
|
|
|
|
logrus.WithField("error", err).Error("encode event")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createExitEvent(e *containerd.Event) *Event {
|
|
|
|
return &Event{
|
|
|
|
Type: "exit",
|
|
|
|
ID: e.ID,
|
|
|
|
Status: e.Status,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 22:57:10 +00:00
|
|
|
func (s *server) event(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var e containerd.Event
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&e); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e.Err = make(chan error, 1)
|
|
|
|
s.supervisor.SendEvent(&e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e.Containers != nil && len(e.Containers) > 0 {
|
2015-12-03 19:49:56 +00:00
|
|
|
if err := s.writeState(w, &e); err != nil {
|
2015-11-10 22:57:10 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) addProcess(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := mux.Vars(r)["id"]
|
|
|
|
var process specs.Process
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&process); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e := containerd.NewEvent(containerd.AddProcessEventType)
|
|
|
|
e.ID = id
|
|
|
|
e.Process = &process
|
|
|
|
s.supervisor.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := Process{
|
|
|
|
Pid: e.Pid,
|
|
|
|
Terminal: process.Terminal,
|
|
|
|
Args: process.Args,
|
|
|
|
Env: process.Env,
|
|
|
|
Cwd: process.Cwd,
|
|
|
|
}
|
|
|
|
p.User.UID = process.User.UID
|
|
|
|
p.User.GID = process.User.GID
|
|
|
|
p.User.AdditionalGids = process.User.AdditionalGids
|
|
|
|
if err := json.NewEncoder(w).Encode(p); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:44:35 +00:00
|
|
|
func (s *server) signalPid(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
vars = mux.Vars(r)
|
|
|
|
id = vars["id"]
|
|
|
|
spid = vars["pid"]
|
|
|
|
)
|
|
|
|
pid, err := strconv.Atoi(spid)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var signal Signal
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&signal); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2015-11-10 22:24:34 +00:00
|
|
|
|
|
|
|
e := containerd.NewEvent(containerd.SignalEventType)
|
|
|
|
e.ID = id
|
|
|
|
e.Pid = pid
|
|
|
|
e.Signal = syscall.Signal(signal.Signal)
|
2015-11-10 21:44:35 +00:00
|
|
|
s.supervisor.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
status := http.StatusInternalServerError
|
|
|
|
if err == containerd.ErrContainerNotFound {
|
|
|
|
status = http.StatusNotFound
|
|
|
|
}
|
|
|
|
http.Error(w, err.Error(), status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 19:49:56 +00:00
|
|
|
func (s *server) state(w http.ResponseWriter, r *http.Request) {
|
2015-11-10 22:24:34 +00:00
|
|
|
e := containerd.NewEvent(containerd.GetContainerEventType)
|
2015-11-10 19:38:26 +00:00
|
|
|
s.supervisor.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2015-12-03 19:49:56 +00:00
|
|
|
if err := s.writeState(w, e); err != nil {
|
2015-11-10 22:57:10 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 19:49:56 +00:00
|
|
|
func (s *server) writeState(w http.ResponseWriter, e *containerd.Event) error {
|
|
|
|
m := s.supervisor.Machine()
|
|
|
|
state := State{
|
|
|
|
Containers: []Container{},
|
|
|
|
Machine: Machine{
|
2015-12-07 23:19:56 +00:00
|
|
|
ID: m.ID,
|
2015-12-03 19:49:56 +00:00
|
|
|
Cpus: m.Cpus,
|
|
|
|
Memory: m.Memory,
|
|
|
|
},
|
|
|
|
}
|
2015-11-10 19:38:26 +00:00
|
|
|
for _, c := range e.Containers {
|
|
|
|
processes, err := c.Processes()
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"error": err,
|
|
|
|
"container": c.ID(),
|
|
|
|
}).Error("get processes for container")
|
|
|
|
}
|
2015-11-10 22:57:10 +00:00
|
|
|
var pids []Process
|
2015-11-10 21:44:35 +00:00
|
|
|
for _, p := range processes {
|
2015-11-13 22:52:25 +00:00
|
|
|
if proc := createProcess(p); proc != nil {
|
|
|
|
pids = append(pids, *proc)
|
|
|
|
}
|
2015-11-10 21:44:35 +00:00
|
|
|
}
|
2015-11-10 19:38:26 +00:00
|
|
|
state.Containers = append(state.Containers, Container{
|
|
|
|
ID: c.ID(),
|
|
|
|
BundlePath: c.Path(),
|
2015-11-10 21:44:35 +00:00
|
|
|
Processes: pids,
|
2015-11-12 21:40:23 +00:00
|
|
|
State: &ContainerState{
|
|
|
|
Status: Status(c.State().Status),
|
|
|
|
},
|
2015-11-10 19:38:26 +00:00
|
|
|
})
|
|
|
|
}
|
2015-11-10 22:57:10 +00:00
|
|
|
return json.NewEncoder(w).Encode(&state)
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:56:08 +00:00
|
|
|
func createProcess(in runtime.Process) *Process {
|
2015-11-10 22:57:10 +00:00
|
|
|
pid, err := in.Pid()
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("error", err).Error("get process pid")
|
2015-11-13 22:52:25 +00:00
|
|
|
return nil
|
2015-11-10 22:57:10 +00:00
|
|
|
}
|
|
|
|
process := in.Spec()
|
2015-11-13 22:52:25 +00:00
|
|
|
p := &Process{
|
2015-11-10 22:57:10 +00:00
|
|
|
Pid: pid,
|
|
|
|
Terminal: process.Terminal,
|
|
|
|
Args: process.Args,
|
|
|
|
Env: process.Env,
|
|
|
|
Cwd: process.Cwd,
|
2015-11-10 19:38:26 +00:00
|
|
|
}
|
2015-11-10 22:57:10 +00:00
|
|
|
p.User.UID = process.User.UID
|
|
|
|
p.User.GID = process.User.GID
|
|
|
|
p.User.AdditionalGids = process.User.AdditionalGids
|
|
|
|
return p
|
2015-11-05 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) createContainer(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := mux.Vars(r)["id"]
|
|
|
|
var c Container
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&c); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2015-11-10 21:44:35 +00:00
|
|
|
if c.BundlePath == "" {
|
|
|
|
http.Error(w, "empty bundle path", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2015-11-10 22:24:34 +00:00
|
|
|
e := containerd.NewEvent(containerd.StartContainerEventType)
|
|
|
|
e.ID = id
|
|
|
|
e.BundlePath = c.BundlePath
|
2015-12-04 22:44:08 +00:00
|
|
|
if c.Checkpoint != "" {
|
2015-12-04 00:07:53 +00:00
|
|
|
e.Checkpoint = &runtime.Checkpoint{
|
2015-12-04 22:44:08 +00:00
|
|
|
Name: c.Checkpoint,
|
2015-12-04 00:07:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-01 19:56:08 +00:00
|
|
|
e.Stdio = &runtime.Stdio{
|
2015-11-13 21:22:42 +00:00
|
|
|
Stderr: c.Stderr,
|
|
|
|
Stdout: c.Stdout,
|
|
|
|
}
|
2015-11-05 23:29:53 +00:00
|
|
|
s.supervisor.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
code := http.StatusInternalServerError
|
|
|
|
if err == containerd.ErrBundleNotFound {
|
|
|
|
code = http.StatusNotFound
|
|
|
|
}
|
|
|
|
http.Error(w, err.Error(), code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
}
|
2015-12-04 00:07:53 +00:00
|
|
|
|
|
|
|
func (s *server) listCheckpoints(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := mux.Vars(r)["id"]
|
|
|
|
e := containerd.NewEvent(containerd.GetContainerEventType)
|
|
|
|
s.supervisor.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var container runtime.Container
|
|
|
|
for _, c := range e.Containers {
|
|
|
|
if c.ID() == id {
|
|
|
|
container = c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if container == nil {
|
|
|
|
http.Error(w, "container not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
checkpoints, err := container.Checkpoints()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out := []Checkpoint{}
|
|
|
|
for _, c := range checkpoints {
|
|
|
|
out = append(out, Checkpoint{
|
|
|
|
Name: c.Name,
|
|
|
|
Tcp: c.Tcp,
|
|
|
|
Shell: c.Shell,
|
|
|
|
UnixSockets: c.UnixSockets,
|
2015-12-04 23:12:57 +00:00
|
|
|
Timestamp: c.Timestamp,
|
2015-12-04 00:07:53 +00:00
|
|
|
})
|
|
|
|
}
|
2015-12-04 22:00:07 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(out); err != nil {
|
|
|
|
logrus.WithField("error", err).Error("encode checkpoints")
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 00:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) createCheckpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
vars = mux.Vars(r)
|
|
|
|
id = vars["id"]
|
|
|
|
name = vars["name"]
|
|
|
|
)
|
|
|
|
var cp Checkpoint
|
2015-12-04 21:31:17 +00:00
|
|
|
// most options to the checkpoint action can be left out so don't
|
|
|
|
// decode unless the client passed anything in the body.
|
|
|
|
if r.ContentLength > 0 {
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&cp); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 00:07:53 +00:00
|
|
|
}
|
|
|
|
e := containerd.NewEvent(containerd.CreateCheckpointEventType)
|
|
|
|
e.ID = id
|
|
|
|
e.Checkpoint = &runtime.Checkpoint{
|
|
|
|
Name: name,
|
2015-12-04 21:35:03 +00:00
|
|
|
Exit: cp.Exit,
|
2015-12-04 00:07:53 +00:00
|
|
|
Tcp: cp.Tcp,
|
|
|
|
UnixSockets: cp.UnixSockets,
|
|
|
|
Shell: cp.Shell,
|
|
|
|
}
|
|
|
|
s.supervisor.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) deleteCheckpoint(w http.ResponseWriter, r *http.Request) {
|
2015-12-04 22:00:07 +00:00
|
|
|
var (
|
|
|
|
vars = mux.Vars(r)
|
|
|
|
id = vars["id"]
|
|
|
|
name = vars["name"]
|
|
|
|
)
|
|
|
|
if name == "" {
|
|
|
|
http.Error(w, "checkpoint name cannot be empty", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var cp Checkpoint
|
|
|
|
if r.ContentLength > 0 {
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&cp); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e := containerd.NewEvent(containerd.DeleteCheckpointEventType)
|
|
|
|
e.ID = id
|
|
|
|
e.Checkpoint = &runtime.Checkpoint{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
s.supervisor.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 00:07:53 +00:00
|
|
|
}
|