2016-12-02 19:33:58 +00:00
|
|
|
package execution
|
|
|
|
|
|
|
|
import (
|
2016-12-05 22:15:03 +00:00
|
|
|
"fmt"
|
2016-12-15 18:40:24 +00:00
|
|
|
"os"
|
2016-12-06 00:17:46 +00:00
|
|
|
"syscall"
|
2016-12-12 22:26:51 +00:00
|
|
|
"time"
|
2016-12-02 19:33:58 +00:00
|
|
|
|
|
|
|
api "github.com/docker/containerd/api/execution"
|
2016-12-12 22:26:51 +00:00
|
|
|
"github.com/docker/containerd/events"
|
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 (
|
2016-12-15 03:10:35 +00:00
|
|
|
emptyResponse = &google_protobuf.Empty{}
|
2016-12-09 17:17:34 +00:00
|
|
|
)
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2016-12-15 18:40:24 +00:00
|
|
|
func New(ctx context.Context, executor Executor) (*Service, error) {
|
|
|
|
svc := &Service{
|
2016-12-02 19:33:58 +00:00
|
|
|
executor: executor,
|
2016-12-15 18:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List existing container, some of them may have died away if
|
|
|
|
// we've been restarted
|
|
|
|
containers, err := executor.List(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range containers {
|
|
|
|
status := c.Status()
|
2017-01-12 18:31:42 +00:00
|
|
|
// generate exit event for all processes, (generate event for init last)
|
|
|
|
processes := c.Processes()
|
|
|
|
processes = append(processes[1:], processes[0])
|
|
|
|
for _, p := range c.processes {
|
|
|
|
if status == Stopped || status == Deleted {
|
2016-12-15 18:40:24 +00:00
|
|
|
if p.Status() != Stopped {
|
|
|
|
p.Signal(os.Kill)
|
|
|
|
}
|
|
|
|
sc, err := p.Wait()
|
|
|
|
if err != nil {
|
|
|
|
sc = UnknownStatusCode
|
|
|
|
}
|
|
|
|
topic := GetContainerProcessEventTopic(c.ID(), p.ID())
|
|
|
|
svc.publishEvent(ctx, topic, &ContainerExitEvent{
|
|
|
|
ContainerEvent: ContainerEvent{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
ID: c.ID(),
|
|
|
|
Action: "exit",
|
|
|
|
},
|
|
|
|
PID: p.ID(),
|
|
|
|
StatusCode: sc,
|
|
|
|
})
|
2017-01-12 18:31:42 +00:00
|
|
|
} else {
|
|
|
|
svc.monitorProcess(ctx, c, p)
|
2016-12-15 18:40:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc, nil
|
2016-12-02 19:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
2016-12-15 03:10:35 +00:00
|
|
|
executor Executor
|
2016-12-02 19:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Create(ctx context.Context, r *api.CreateContainerRequest) (*api.CreateContainerResponse, error) {
|
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-11 19:07:32 +00:00
|
|
|
procs := container.Processes()
|
|
|
|
initProcess := procs[0]
|
|
|
|
|
2016-12-12 22:26:51 +00:00
|
|
|
s.monitorProcess(ctx, container, initProcess)
|
2016-12-02 19:33:58 +00:00
|
|
|
|
|
|
|
return &api.CreateContainerResponse{
|
2016-12-11 19:07:32 +00:00
|
|
|
Container: toGRPCContainer(container),
|
|
|
|
InitProcess: toGRPCProcess(initProcess),
|
2016-12-02 19:33:58 +00:00
|
|
|
}, 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 19:32:45 +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-09 17:17:34 +00:00
|
|
|
spec := specs.Process{
|
|
|
|
Terminal: r.Process.Terminal,
|
|
|
|
ConsoleSize: specs.Box{
|
2016-12-20 22:08:42 +00:00
|
|
|
Height: 80,
|
|
|
|
Width: 80,
|
2016-12-09 17:17:34 +00:00
|
|
|
},
|
|
|
|
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-14 17:16:23 +00:00
|
|
|
ID: r.Process.ID,
|
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
|
|
|
|
}
|
2016-12-11 19:07:32 +00:00
|
|
|
|
2016-12-12 22:26:51 +00:00
|
|
|
s.monitorProcess(ctx, container, process)
|
2016-12-02 23:37:16 +00:00
|
|
|
|
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 19:32:45 +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-09 19:32:45 +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 19:32:45 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.ContainerID)
|
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 19:32:45 +00:00
|
|
|
process := container.GetProcess(r.ProcessID)
|
2016-12-06 00:17:46 +00:00
|
|
|
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 19:32:45 +00:00
|
|
|
container, err := s.executor.Load(ctx, r.ContainerID)
|
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 19:32:45 +00:00
|
|
|
if err := s.executor.DeleteProcess(ctx, container, r.ProcessID); 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 19:32:45 +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-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
|
|
|
|
2016-12-12 22:26:51 +00:00
|
|
|
func (s *Service) publishEvent(ctx context.Context, topic string, v interface{}) {
|
|
|
|
ctx = events.WithTopic(ctx, topic)
|
|
|
|
events.GetPoster(ctx).Post(ctx, v)
|
2016-12-11 19:07:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 22:26:51 +00:00
|
|
|
func (s *Service) monitorProcess(ctx context.Context, container *Container, process Process) {
|
2016-12-11 19:07:32 +00:00
|
|
|
go func() {
|
|
|
|
status, err := process.Wait()
|
|
|
|
if err == nil {
|
2016-12-12 22:26:51 +00:00
|
|
|
topic := GetContainerProcessEventTopic(container.ID(), process.ID())
|
|
|
|
s.publishEvent(ctx, topic, &ContainerExitEvent{
|
2016-12-11 19:07:32 +00:00
|
|
|
ContainerEvent: ContainerEvent{
|
2016-12-12 22:26:51 +00:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
ID: container.ID(),
|
|
|
|
Action: "exit",
|
2016-12-11 19:07:32 +00:00
|
|
|
},
|
|
|
|
PID: process.ID(),
|
|
|
|
StatusCode: status,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:26:51 +00:00
|
|
|
func GetContainerEventTopic(id string) string {
|
|
|
|
return fmt.Sprintf(containerEventsTopicFormat, id)
|
2016-12-11 19:07:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 22:26:51 +00:00
|
|
|
func GetContainerProcessEventTopic(containerID, processID string) string {
|
|
|
|
return fmt.Sprintf(containerProcessEventsTopicFormat, containerID, processID)
|
2016-12-11 19:07:32 +00:00
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|