2016-12-02 19:33:58 +00:00
|
|
|
package execution
|
|
|
|
|
|
|
|
import (
|
2016-12-05 22:15:03 +00:00
|
|
|
"fmt"
|
2016-12-06 00:17:46 +00:00
|
|
|
"syscall"
|
2016-12-02 19:33:58 +00:00
|
|
|
|
|
|
|
api "github.com/docker/containerd/api/execution"
|
2016-12-05 22:15:03 +00:00
|
|
|
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"golang.org/x/net/context"
|
2016-12-02 19:33:58 +00:00
|
|
|
)
|
|
|
|
|
2016-12-09 17:17:34 +00:00
|
|
|
var (
|
|
|
|
emptyResponse = &google_protobuf.Empty{}
|
|
|
|
ErrProcessNotFound = fmt.Errorf("Process not found")
|
|
|
|
)
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2016-12-06 00:17:46 +00:00
|
|
|
func New(executor Executor) (*Service, error) {
|
2016-12-02 19:33:58 +00:00
|
|
|
return &Service{
|
|
|
|
executor: executor,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
2016-12-06 00:17:46 +00:00
|
|
|
executor Executor
|
|
|
|
supervisor *Supervisor
|
2016-12-02 19:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Create(ctx context.Context, r *api.CreateContainerRequest) (*api.CreateContainerResponse, error) {
|
|
|
|
// TODO: write io and bundle path to dir
|
2016-12-07 18:44:05 +00:00
|
|
|
var err error
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Create(ctx, r.ID, CreateOpts{
|
2016-12-09 17:17:34 +00:00
|
|
|
Bundle: r.BundlePath,
|
|
|
|
Console: r.Console,
|
|
|
|
Stdin: r.Stdin,
|
|
|
|
Stdout: r.Stdout,
|
|
|
|
Stderr: r.Stderr,
|
2016-12-05 22:15:03 +00:00
|
|
|
})
|
2016-12-02 19:33:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
s.supervisor.Add(container)
|
2016-12-02 19:33:58 +00:00
|
|
|
|
|
|
|
return &api.CreateContainerResponse{
|
|
|
|
Container: toGRPCContainer(container),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (s *Service) Delete(ctx context.Context, r *api.DeleteContainerRequest) (*google_protobuf.Empty, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.ID)
|
2016-12-05 22:15:03 +00:00
|
|
|
if err != nil {
|
2016-12-07 18:44:05 +00:00
|
|
|
return emptyResponse, err
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
if err = s.executor.Delete(ctx, container); err != nil {
|
2016-12-07 18:44:05 +00:00
|
|
|
return emptyResponse, err
|
2016-12-02 19:33:58 +00:00
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
return emptyResponse, nil
|
2016-12-02 19:33:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (s *Service) List(ctx context.Context, r *api.ListContainersRequest) (*api.ListContainersResponse, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
containers, err := s.executor.List(ctx)
|
2016-12-02 19:33:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 22:15:03 +00:00
|
|
|
resp := &api.ListContainersResponse{}
|
2016-12-02 19:33:58 +00:00
|
|
|
for _, c := range containers {
|
2016-12-05 22:15:03 +00:00
|
|
|
resp.Containers = append(resp.Containers, toGRPCContainer(c))
|
2016-12-02 19:33:58 +00:00
|
|
|
}
|
2016-12-05 22:15:03 +00:00
|
|
|
return resp, nil
|
2016-12-02 19:33:58 +00:00
|
|
|
}
|
2016-12-02 23:37:16 +00:00
|
|
|
func (s *Service) Get(ctx context.Context, r *api.GetContainerRequest) (*api.GetContainerResponse, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.ID)
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &api.GetContainerResponse{
|
|
|
|
Container: toGRPCContainer(container),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (s *Service) Update(ctx context.Context, r *api.UpdateContainerRequest) (*google_protobuf.Empty, error) {
|
2016-12-07 18:44:05 +00:00
|
|
|
return emptyResponse, nil
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (s *Service) Pause(ctx context.Context, r *api.PauseContainerRequest) (*google_protobuf.Empty, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.ID)
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-09 18:00:30 +00:00
|
|
|
return emptyResponse, s.executor.Pause(ctx, container)
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (s *Service) Resume(ctx context.Context, r *api.ResumeContainerRequest) (*google_protobuf.Empty, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.ID)
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-09 18:00:30 +00:00
|
|
|
return emptyResponse, s.executor.Resume(ctx, container)
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 23:38:32 +00:00
|
|
|
func (s *Service) Start(ctx context.Context, r *api.StartContainerRequest) (*google_protobuf.Empty, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.ID)
|
2016-12-05 23:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-09 18:00:30 +00:00
|
|
|
return emptyResponse, s.executor.Start(ctx, container)
|
2016-12-05 23:38:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (s *Service) StartProcess(ctx context.Context, r *api.StartProcessRequest) (*api.StartProcessResponse, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.ContainerId)
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
// TODO: generate spec
|
2016-12-09 17:17:34 +00:00
|
|
|
spec := specs.Process{
|
|
|
|
Terminal: r.Process.Terminal,
|
|
|
|
ConsoleSize: specs.Box{
|
|
|
|
80,
|
|
|
|
80,
|
|
|
|
},
|
|
|
|
Args: r.Process.Args,
|
|
|
|
Env: r.Process.Env,
|
|
|
|
Cwd: r.Process.Cwd,
|
|
|
|
NoNewPrivileges: true,
|
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
process, err := s.executor.StartProcess(ctx, container, StartProcessOpts{
|
2016-12-09 17:17:34 +00:00
|
|
|
Spec: spec,
|
|
|
|
Console: r.Console,
|
|
|
|
Stdin: r.Stdin,
|
|
|
|
Stdout: r.Stdout,
|
|
|
|
Stderr: r.Stderr,
|
2016-12-05 22:15:03 +00:00
|
|
|
})
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.supervisor.Add(process)
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
return &api.StartProcessResponse{
|
|
|
|
Process: toGRPCProcess(process),
|
2016-12-02 23:37:16 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// containerd managed execs + system pids forked in container
|
|
|
|
func (s *Service) GetProcess(ctx context.Context, r *api.GetProcessRequest) (*api.GetProcessResponse, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.Container.ID)
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-06 00:17:46 +00:00
|
|
|
process := container.GetProcess(r.ProcessId)
|
2016-12-05 22:15:03 +00:00
|
|
|
if process == nil {
|
2016-12-09 17:17:34 +00:00
|
|
|
return nil, ErrProcessNotFound
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
|
|
|
return &api.GetProcessResponse{
|
2016-12-05 22:15:03 +00:00
|
|
|
Process: toGRPCProcess(process),
|
2016-12-02 23:37:16 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (s *Service) SignalProcess(ctx context.Context, r *api.SignalProcessRequest) (*google_protobuf.Empty, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.Container.ID)
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
2016-12-07 18:44:05 +00:00
|
|
|
return emptyResponse, err
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
2016-12-06 00:17:46 +00:00
|
|
|
process := container.GetProcess(r.Process.ID)
|
|
|
|
if process == nil {
|
|
|
|
return nil, fmt.Errorf("Make me a constant! Process not foumd!")
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
return emptyResponse, process.Signal(syscall.Signal(r.Signal))
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (s *Service) DeleteProcess(ctx context.Context, r *api.DeleteProcessRequest) (*google_protobuf.Empty, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.Container.ID)
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
2016-12-07 18:44:05 +00:00
|
|
|
return emptyResponse, err
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
2016-12-09 18:00:30 +00:00
|
|
|
if err := s.executor.DeleteProcess(ctx, container, r.Process.ID); err != nil {
|
2016-12-07 18:44:05 +00:00
|
|
|
return emptyResponse, err
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
return emptyResponse, nil
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) ListProcesses(ctx context.Context, r *api.ListProcessesRequest) (*api.ListProcessesResponse, error) {
|
2016-12-09 18:00:30 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.Container.ID)
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 22:15:03 +00:00
|
|
|
processes := container.Processes()
|
2016-12-02 23:37:16 +00:00
|
|
|
return &api.ListProcessesResponse{
|
2016-12-05 22:15:03 +00:00
|
|
|
Processes: toGRPCProcesses(processes),
|
2016-12-02 23:37:16 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2016-12-02 19:33:58 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
_ = (api.ExecutionServiceServer)(&Service{})
|
|
|
|
)
|
2016-12-06 00:17:46 +00:00
|
|
|
|
|
|
|
func toGRPCContainer(container *Container) *api.Container {
|
2016-12-07 18:44:05 +00:00
|
|
|
c := &api.Container{
|
2016-12-06 00:17:46 +00:00
|
|
|
ID: container.ID(),
|
|
|
|
BundlePath: container.Bundle(),
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
status := container.Status()
|
|
|
|
switch status {
|
|
|
|
case "created":
|
|
|
|
c.Status = api.Status_CREATED
|
|
|
|
case "running":
|
|
|
|
c.Status = api.Status_RUNNING
|
|
|
|
case "stopped":
|
|
|
|
c.Status = api.Status_STOPPED
|
|
|
|
case "paused":
|
|
|
|
c.Status = api.Status_PAUSED
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
2016-12-06 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toGRPCProcesses(processes []Process) []*api.Process {
|
|
|
|
var out []*api.Process
|
|
|
|
for _, p := range processes {
|
|
|
|
out = append(out, toGRPCProcess(p))
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGRPCProcess(process Process) *api.Process {
|
|
|
|
return &api.Process{
|
|
|
|
ID: process.ID(),
|
|
|
|
Pid: process.Pid(),
|
|
|
|
}
|
|
|
|
}
|