2015-12-09 22:18:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-02-01 19:02:41 +00:00
|
|
|
"fmt"
|
2015-12-09 22:18:31 +00:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/containerd/api/grpc/types"
|
|
|
|
"github.com/docker/containerd/runtime"
|
2015-12-18 00:07:04 +00:00
|
|
|
"github.com/docker/containerd/supervisor"
|
2015-12-09 22:18:31 +00:00
|
|
|
"github.com/opencontainers/specs"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type apiServer struct {
|
2015-12-18 00:07:04 +00:00
|
|
|
sv *supervisor.Supervisor
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns grpc server instance
|
2015-12-18 00:07:04 +00:00
|
|
|
func NewServer(sv *supervisor.Supervisor) types.APIServer {
|
2015-12-09 22:18:31 +00:00
|
|
|
return &apiServer{
|
|
|
|
sv: sv,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) CreateContainer(ctx context.Context, c *types.CreateContainerRequest) (*types.CreateContainerResponse, error) {
|
|
|
|
if c.BundlePath == "" {
|
|
|
|
return nil, errors.New("empty bundle path")
|
|
|
|
}
|
2015-12-18 00:07:04 +00:00
|
|
|
e := supervisor.NewEvent(supervisor.StartContainerEventType)
|
2015-12-09 22:18:31 +00:00
|
|
|
e.ID = c.Id
|
|
|
|
e.BundlePath = c.BundlePath
|
2016-02-03 21:56:15 +00:00
|
|
|
e.Stdin = c.Stdin
|
|
|
|
e.Stdout = c.Stdout
|
|
|
|
e.Stderr = c.Stderr
|
2016-02-11 21:44:25 +00:00
|
|
|
e.Labels = c.Labels
|
2015-12-18 00:07:04 +00:00
|
|
|
e.StartResponse = make(chan supervisor.StartResponse, 1)
|
2015-12-09 22:18:31 +00:00
|
|
|
if c.Checkpoint != "" {
|
|
|
|
e.Checkpoint = &runtime.Checkpoint{
|
|
|
|
Name: c.Checkpoint,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.sv.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-11 19:56:11 +00:00
|
|
|
r := <-e.StartResponse
|
|
|
|
apiC, err := createAPIContainer(r.Container)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.CreateContainerResponse{
|
|
|
|
Container: apiC,
|
|
|
|
}, nil
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) Signal(ctx context.Context, r *types.SignalRequest) (*types.SignalResponse, error) {
|
2015-12-18 00:07:04 +00:00
|
|
|
e := supervisor.NewEvent(supervisor.SignalEventType)
|
2015-12-09 22:18:31 +00:00
|
|
|
e.ID = r.Id
|
2016-01-06 21:32:46 +00:00
|
|
|
e.Pid = r.Pid
|
2015-12-09 22:18:31 +00:00
|
|
|
e.Signal = syscall.Signal(int(r.Signal))
|
|
|
|
s.sv.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.SignalResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) AddProcess(ctx context.Context, r *types.AddProcessRequest) (*types.AddProcessResponse, error) {
|
|
|
|
process := &specs.Process{
|
|
|
|
Terminal: r.Terminal,
|
|
|
|
Args: r.Args,
|
|
|
|
Env: r.Env,
|
|
|
|
Cwd: r.Cwd,
|
|
|
|
User: specs.User{
|
|
|
|
UID: r.User.Uid,
|
|
|
|
GID: r.User.Gid,
|
|
|
|
AdditionalGids: r.User.AdditionalGids,
|
|
|
|
},
|
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
if r.Id == "" {
|
|
|
|
return nil, fmt.Errorf("container id cannot be empty")
|
|
|
|
}
|
|
|
|
if r.Pid == "" {
|
|
|
|
return nil, fmt.Errorf("process id cannot be empty")
|
|
|
|
}
|
2015-12-18 00:07:04 +00:00
|
|
|
e := supervisor.NewEvent(supervisor.AddProcessEventType)
|
2015-12-09 22:18:31 +00:00
|
|
|
e.ID = r.Id
|
2016-02-01 19:02:41 +00:00
|
|
|
e.Pid = r.Pid
|
2016-01-06 21:32:46 +00:00
|
|
|
e.ProcessSpec = process
|
2016-02-03 21:56:15 +00:00
|
|
|
e.Stdin = r.Stdin
|
|
|
|
e.Stdout = r.Stdout
|
|
|
|
e.Stderr = r.Stderr
|
2016-02-01 19:02:41 +00:00
|
|
|
e.StartResponse = make(chan supervisor.StartResponse, 1)
|
2015-12-09 22:18:31 +00:00
|
|
|
s.sv.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
<-e.StartResponse
|
|
|
|
return &types.AddProcessResponse{}, nil
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) CreateCheckpoint(ctx context.Context, r *types.CreateCheckpointRequest) (*types.CreateCheckpointResponse, error) {
|
2015-12-18 00:07:04 +00:00
|
|
|
e := supervisor.NewEvent(supervisor.CreateCheckpointEventType)
|
2015-12-09 22:18:31 +00:00
|
|
|
e.ID = r.Id
|
|
|
|
e.Checkpoint = &runtime.Checkpoint{
|
|
|
|
Name: r.Checkpoint.Name,
|
|
|
|
Exit: r.Checkpoint.Exit,
|
|
|
|
Tcp: r.Checkpoint.Tcp,
|
|
|
|
UnixSockets: r.Checkpoint.UnixSockets,
|
|
|
|
Shell: r.Checkpoint.Shell,
|
|
|
|
}
|
|
|
|
s.sv.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.CreateCheckpointResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) DeleteCheckpoint(ctx context.Context, r *types.DeleteCheckpointRequest) (*types.DeleteCheckpointResponse, error) {
|
|
|
|
if r.Name == "" {
|
|
|
|
return nil, errors.New("checkpoint name cannot be empty")
|
|
|
|
}
|
2015-12-18 00:07:04 +00:00
|
|
|
e := supervisor.NewEvent(supervisor.DeleteCheckpointEventType)
|
2015-12-09 22:18:31 +00:00
|
|
|
e.ID = r.Id
|
|
|
|
e.Checkpoint = &runtime.Checkpoint{
|
|
|
|
Name: r.Name,
|
|
|
|
}
|
|
|
|
s.sv.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.DeleteCheckpointResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) ListCheckpoint(ctx context.Context, r *types.ListCheckpointRequest) (*types.ListCheckpointResponse, error) {
|
2015-12-18 00:07:04 +00:00
|
|
|
e := supervisor.NewEvent(supervisor.GetContainerEventType)
|
2015-12-09 22:18:31 +00:00
|
|
|
s.sv.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var container runtime.Container
|
|
|
|
for _, c := range e.Containers {
|
|
|
|
if c.ID() == r.Id {
|
|
|
|
container = c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if container == nil {
|
|
|
|
return nil, grpc.Errorf(codes.NotFound, "no such containers")
|
|
|
|
}
|
|
|
|
var out []*types.Checkpoint
|
2016-02-01 23:07:02 +00:00
|
|
|
checkpoints, err := container.Checkpoints()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, c := range checkpoints {
|
|
|
|
out = append(out, &types.Checkpoint{
|
|
|
|
Name: c.Name,
|
|
|
|
Tcp: c.Tcp,
|
|
|
|
Shell: c.Shell,
|
|
|
|
UnixSockets: c.UnixSockets,
|
|
|
|
// TODO: figure out timestamp
|
|
|
|
//Timestamp: c.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
2015-12-09 22:18:31 +00:00
|
|
|
return &types.ListCheckpointResponse{Checkpoints: out}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) State(ctx context.Context, r *types.StateRequest) (*types.StateResponse, error) {
|
2015-12-18 00:07:04 +00:00
|
|
|
e := supervisor.NewEvent(supervisor.GetContainerEventType)
|
2016-02-11 20:20:29 +00:00
|
|
|
e.ID = r.Id
|
2015-12-09 22:18:31 +00:00
|
|
|
s.sv.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m := s.sv.Machine()
|
|
|
|
state := &types.StateResponse{
|
|
|
|
Machine: &types.Machine{
|
|
|
|
Cpus: uint32(m.Cpus),
|
|
|
|
Memory: uint64(m.Cpus),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range e.Containers {
|
2016-02-11 19:56:11 +00:00
|
|
|
apiC, err := createAPIContainer(c)
|
2015-12-09 22:18:31 +00:00
|
|
|
if err != nil {
|
2016-02-11 19:56:11 +00:00
|
|
|
return nil, err
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
2016-02-11 19:56:11 +00:00
|
|
|
state.Containers = append(state.Containers, apiC)
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
2016-02-11 19:56:11 +00:00
|
|
|
func createAPIContainer(c runtime.Container) (*types.Container, error) {
|
|
|
|
processes, err := c.Processes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, grpc.Errorf(codes.Internal, "get processes for container")
|
|
|
|
}
|
|
|
|
var procs []*types.Process
|
|
|
|
for _, p := range processes {
|
|
|
|
oldProc := p.Spec()
|
|
|
|
stdio := p.Stdio()
|
|
|
|
procs = append(procs, &types.Process{
|
|
|
|
Pid: p.ID(),
|
|
|
|
SystemPid: uint32(p.SystemPid()),
|
|
|
|
Terminal: oldProc.Terminal,
|
|
|
|
Args: oldProc.Args,
|
|
|
|
Env: oldProc.Env,
|
|
|
|
Cwd: oldProc.Cwd,
|
|
|
|
Stdin: stdio.Stdin,
|
|
|
|
Stdout: stdio.Stdout,
|
|
|
|
Stderr: stdio.Stderr,
|
|
|
|
User: &types.User{
|
|
|
|
Uid: oldProc.User.UID,
|
|
|
|
Gid: oldProc.User.GID,
|
|
|
|
AdditionalGids: oldProc.User.AdditionalGids,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return &types.Container{
|
|
|
|
Id: c.ID(),
|
|
|
|
BundlePath: c.Path(),
|
|
|
|
Processes: procs,
|
2016-02-11 21:44:25 +00:00
|
|
|
Labels: c.Labels(),
|
2016-02-11 19:56:11 +00:00
|
|
|
Status: string(c.State()),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:18:31 +00:00
|
|
|
func (s *apiServer) UpdateContainer(ctx context.Context, r *types.UpdateContainerRequest) (*types.UpdateContainerResponse, error) {
|
2015-12-18 00:07:04 +00:00
|
|
|
e := supervisor.NewEvent(supervisor.UpdateContainerEventType)
|
2015-12-09 22:18:31 +00:00
|
|
|
e.ID = r.Id
|
2016-01-27 22:19:10 +00:00
|
|
|
e.State = runtime.State(r.Status)
|
2015-12-09 22:18:31 +00:00
|
|
|
s.sv.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.UpdateContainerResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:21:25 +00:00
|
|
|
func (s *apiServer) UpdateProcess(ctx context.Context, r *types.UpdateProcessRequest) (*types.UpdateProcessResponse, error) {
|
|
|
|
e := supervisor.NewEvent(supervisor.UpdateProcessEventType)
|
|
|
|
e.ID = r.Id
|
|
|
|
e.Pid = r.Pid
|
|
|
|
e.Height = int(r.Height)
|
|
|
|
e.Width = int(r.Width)
|
|
|
|
e.CloseStdin = r.CloseStdin
|
|
|
|
s.sv.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.UpdateProcessResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:18:31 +00:00
|
|
|
func (s *apiServer) Events(r *types.EventsRequest, stream types.API_EventsServer) error {
|
|
|
|
events := s.sv.Events()
|
2015-12-10 20:30:04 +00:00
|
|
|
defer s.sv.Unsubscribe(events)
|
2015-12-09 22:18:31 +00:00
|
|
|
for evt := range events {
|
2015-12-16 00:22:53 +00:00
|
|
|
var ev *types.Event
|
2015-12-09 22:18:31 +00:00
|
|
|
switch evt.Type {
|
2015-12-18 00:07:04 +00:00
|
|
|
case supervisor.ExitEventType, supervisor.ExecExitEventType:
|
2015-12-16 00:22:53 +00:00
|
|
|
ev = &types.Event{
|
2015-12-09 22:18:31 +00:00
|
|
|
Type: "exit",
|
|
|
|
Id: evt.ID,
|
2016-01-06 21:32:46 +00:00
|
|
|
Pid: evt.Pid,
|
2015-12-09 22:18:31 +00:00
|
|
|
Status: uint32(evt.Status),
|
|
|
|
}
|
2015-12-18 00:07:04 +00:00
|
|
|
case supervisor.OOMEventType:
|
2015-12-16 00:22:53 +00:00
|
|
|
ev = &types.Event{
|
|
|
|
Type: "oom",
|
|
|
|
Id: evt.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ev != nil {
|
2015-12-09 22:18:31 +00:00
|
|
|
if err := stream.Send(ev); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-12-16 00:22:53 +00:00
|
|
|
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-14 22:43:00 +00:00
|
|
|
|
|
|
|
func (s *apiServer) GetStats(r *types.StatsRequest, stream types.API_GetStatsServer) error {
|
2015-12-18 00:07:04 +00:00
|
|
|
e := supervisor.NewEvent(supervisor.StatsEventType)
|
2015-12-14 22:43:00 +00:00
|
|
|
e.ID = r.Id
|
|
|
|
s.sv.SendEvent(e)
|
|
|
|
if err := <-e.Err; err != nil {
|
2015-12-18 00:07:04 +00:00
|
|
|
if err == supervisor.ErrContainerNotFound {
|
2015-12-14 22:43:00 +00:00
|
|
|
return grpc.Errorf(codes.NotFound, err.Error())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2015-12-18 00:07:04 +00:00
|
|
|
ue := supervisor.NewEvent(supervisor.UnsubscribeStatsEventType)
|
2015-12-14 22:43:00 +00:00
|
|
|
ue.ID = e.ID
|
|
|
|
ue.Stats = e.Stats
|
|
|
|
s.sv.SendEvent(ue)
|
|
|
|
if err := <-ue.Err; err != nil {
|
|
|
|
logrus.Errorf("Error unsubscribing %s: %v", r.Id, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case st := <-e.Stats:
|
|
|
|
pbSt, ok := st.(*types.Stats)
|
|
|
|
if !ok {
|
|
|
|
panic("invalid stats type from collector")
|
|
|
|
}
|
|
|
|
if err := stream.Send(pbSt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-stream.Context().Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|