2017-01-24 23:55:32 +00:00
|
|
|
package shim
|
2017-01-19 22:16:50 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-24 00:18:10 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
runc "github.com/crosbymichael/go-runc"
|
2017-01-24 23:55:32 +00:00
|
|
|
apishim "github.com/docker/containerd/api/shim"
|
2017-01-19 22:16:50 +00:00
|
|
|
"github.com/docker/containerd/utils"
|
|
|
|
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2017-01-19 23:21:25 +00:00
|
|
|
var emptyResponse = &google_protobuf.Empty{}
|
|
|
|
|
2017-01-25 21:27:48 +00:00
|
|
|
// NewService returns a new shim service that can be used via GRPC
|
2017-01-24 23:55:32 +00:00
|
|
|
func NewService() *Service {
|
|
|
|
return &Service{
|
|
|
|
processes: make(map[int]process),
|
2017-01-25 21:27:48 +00:00
|
|
|
events: make(chan *apishim.Event, 2048),
|
2017-01-24 23:55:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
2017-01-25 21:27:48 +00:00
|
|
|
initProcess *initProcess
|
|
|
|
id string
|
|
|
|
mu sync.Mutex
|
|
|
|
processes map[int]process
|
|
|
|
events chan *apishim.Event
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) Create(ctx context.Context, r *apishim.CreateRequest) (*apishim.CreateResponse, error) {
|
2017-01-24 00:18:10 +00:00
|
|
|
process, err := newInitProcess(ctx, r)
|
2017-01-19 22:16:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Lock()
|
2017-01-25 21:27:48 +00:00
|
|
|
s.initProcess = process
|
2017-01-24 00:18:10 +00:00
|
|
|
pid := process.Pid()
|
2017-01-25 21:27:48 +00:00
|
|
|
s.processes[pid] = process
|
|
|
|
s.id = r.ID
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Unlock()
|
2017-01-24 23:55:32 +00:00
|
|
|
return &apishim.CreateResponse{
|
2017-01-24 00:18:10 +00:00
|
|
|
Pid: uint32(pid),
|
2017-01-19 22:16:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) Start(ctx context.Context, r *apishim.StartRequest) (*google_protobuf.Empty, error) {
|
2017-01-25 21:27:48 +00:00
|
|
|
if err := s.initProcess.Start(ctx); err != nil {
|
2017-01-19 23:21:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return emptyResponse, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) Delete(ctx context.Context, r *apishim.DeleteRequest) (*apishim.DeleteResponse, error) {
|
2017-01-25 21:27:48 +00:00
|
|
|
if err := s.initProcess.Delete(ctx); err != nil {
|
2017-01-24 00:18:10 +00:00
|
|
|
return nil, err
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Lock()
|
2017-01-25 21:27:48 +00:00
|
|
|
delete(s.processes, s.initProcess.pid)
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Unlock()
|
2017-01-24 23:55:32 +00:00
|
|
|
return &apishim.DeleteResponse{
|
2017-01-25 21:27:48 +00:00
|
|
|
ExitStatus: uint32(s.initProcess.Status()),
|
2017-01-24 00:18:10 +00:00
|
|
|
}, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) Exec(ctx context.Context, r *apishim.ExecRequest) (*apishim.ExecResponse, error) {
|
2017-01-24 22:45:18 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2017-01-25 21:27:48 +00:00
|
|
|
process, err := newExecProcess(ctx, r, s.initProcess)
|
2017-01-24 22:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pid := process.Pid()
|
|
|
|
s.processes[pid] = process
|
2017-01-24 23:55:32 +00:00
|
|
|
return &apishim.ExecResponse{
|
2017-01-24 22:45:18 +00:00
|
|
|
Pid: uint32(pid),
|
|
|
|
}, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) Pty(ctx context.Context, r *apishim.PtyRequest) (*google_protobuf.Empty, error) {
|
2017-01-24 00:18:10 +00:00
|
|
|
ws := runc.WinSize{
|
2017-01-19 22:16:50 +00:00
|
|
|
Width: uint16(r.Width),
|
|
|
|
Height: uint16(r.Height),
|
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
p, ok := s.processes[int(r.Pid)]
|
|
|
|
s.mu.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("process does not exist %d", r.Pid)
|
|
|
|
}
|
|
|
|
if err := p.Resize(ws); err != nil {
|
2017-01-19 22:16:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-19 23:21:25 +00:00
|
|
|
return emptyResponse, nil
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:27:48 +00:00
|
|
|
func (s *Service) Events(r *apishim.EventsRequest, stream apishim.Shim_EventsServer) error {
|
|
|
|
for e := range s.events {
|
|
|
|
if err := stream.Send(e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func (s *Service) ProcessExit(e utils.Exit) error {
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
if p, ok := s.processes[e.Pid]; ok {
|
|
|
|
p.Exited(e.Status)
|
2017-01-25 21:27:48 +00:00
|
|
|
s.events <- &apishim.Event{
|
|
|
|
Type: apishim.EventType_EXIT,
|
|
|
|
ID: s.id,
|
|
|
|
Pid: uint32(p.Pid()),
|
|
|
|
ExitStatus: uint32(e.Status),
|
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
}
|
2017-01-24 00:18:10 +00:00
|
|
|
s.mu.Unlock()
|
2017-01-19 22:16:50 +00:00
|
|
|
return nil
|
|
|
|
}
|