2016-09-30 17:39:26 +00:00
|
|
|
package oci
|
2016-09-28 22:09:22 +00:00
|
|
|
|
|
|
|
import (
|
2016-12-09 18:00:30 +00:00
|
|
|
"context"
|
2016-10-05 22:07:20 +00:00
|
|
|
"errors"
|
2016-09-28 22:09:22 +00:00
|
|
|
"fmt"
|
2016-12-05 22:15:03 +00:00
|
|
|
"os"
|
2016-09-28 22:09:22 +00:00
|
|
|
"path/filepath"
|
2016-12-05 22:15:03 +00:00
|
|
|
"syscall"
|
2016-09-28 22:09:22 +00:00
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
"github.com/crosbymichael/go-runc"
|
2016-12-02 23:37:16 +00:00
|
|
|
"github.com/docker/containerd/execution"
|
2016-09-28 22:09:22 +00:00
|
|
|
)
|
|
|
|
|
2016-12-14 17:16:23 +00:00
|
|
|
const (
|
|
|
|
initProcessID = "init"
|
|
|
|
)
|
|
|
|
|
2016-12-12 22:26:51 +00:00
|
|
|
var (
|
|
|
|
ErrRootEmpty = errors.New("oci: runtime root cannot be an empty string")
|
|
|
|
)
|
2016-10-05 22:07:20 +00:00
|
|
|
|
2016-12-11 19:07:32 +00:00
|
|
|
func New(root string) (*OCIRuntime, error) {
|
|
|
|
err := SetSubreaper(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-30 17:39:26 +00:00
|
|
|
return &OCIRuntime{
|
2016-12-02 23:37:16 +00:00
|
|
|
root: root,
|
2016-12-06 00:17:46 +00:00
|
|
|
runc: &runc.Runc{
|
2016-12-05 22:15:03 +00:00
|
|
|
Root: filepath.Join(root, "runc"),
|
|
|
|
},
|
2016-12-09 19:20:03 +00:00
|
|
|
ios: make(map[string]OIO),
|
2016-12-11 19:07:32 +00:00
|
|
|
}, nil
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 17:39:26 +00:00
|
|
|
type OCIRuntime struct {
|
2016-09-28 22:09:22 +00:00
|
|
|
root string
|
2016-12-05 22:15:03 +00:00
|
|
|
runc *runc.Runc
|
2016-12-11 19:07:32 +00:00
|
|
|
ios map[string]OIO // ios tracks created process io for cleanup purpose on delete
|
2016-12-09 17:17:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) Create(ctx context.Context, id string, o execution.CreateOpts) (container *execution.Container, err error) {
|
2016-12-09 19:20:03 +00:00
|
|
|
oio, err := newOIO(o.Stdin, o.Stdout, o.Stderr, o.Console)
|
2016-12-07 18:44:05 +00:00
|
|
|
if err != nil {
|
2016-09-28 22:09:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 23:38:32 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-12-09 19:20:03 +00:00
|
|
|
oio.cleanup()
|
2016-12-05 23:38:32 +00:00
|
|
|
}
|
|
|
|
}()
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2016-12-11 19:07:32 +00:00
|
|
|
if container, err = execution.NewContainer(r.root, id, o.Bundle); err != nil {
|
2016-12-07 18:44:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func(c *execution.Container) {
|
|
|
|
if err != nil {
|
|
|
|
c.StateDir().Delete()
|
|
|
|
}
|
|
|
|
}(container)
|
|
|
|
|
2016-12-14 17:16:23 +00:00
|
|
|
initStateDir, err := container.StateDir().NewProcess(initProcessID)
|
2016-12-06 00:17:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-09 18:47:29 +00:00
|
|
|
pidFile := filepath.Join(initStateDir, "pid")
|
2016-12-09 18:00:30 +00:00
|
|
|
err = r.runc.Create(ctx, id, o.Bundle, &runc.CreateOpts{
|
2016-12-06 00:17:46 +00:00
|
|
|
PidFile: pidFile,
|
2016-12-09 19:20:03 +00:00
|
|
|
Console: oio.console,
|
|
|
|
IO: oio.rio,
|
2016-12-05 22:15:03 +00:00
|
|
|
})
|
2016-09-28 22:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-12-09 18:00:30 +00:00
|
|
|
r.runc.Kill(ctx, id, int(syscall.SIGKILL))
|
|
|
|
r.runc.Delete(ctx, id)
|
2016-12-07 18:44:05 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-12-06 00:17:46 +00:00
|
|
|
pid, err := runc.ReadPidFile(pidFile)
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-14 17:16:23 +00:00
|
|
|
process, err := newProcess(container, initProcessID, pid)
|
2016-12-05 22:15:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-05 23:38:32 +00:00
|
|
|
container.AddProcess(process, true)
|
2016-12-05 22:15:03 +00:00
|
|
|
|
2016-12-09 19:20:03 +00:00
|
|
|
r.ios[id] = oio
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2016-12-02 23:37:16 +00:00
|
|
|
return container, nil
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) Start(ctx context.Context, c *execution.Container) error {
|
|
|
|
return r.runc.Start(ctx, c.ID())
|
2016-12-06 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) Status(ctx context.Context, c *execution.Container) (execution.Status, error) {
|
|
|
|
state, err := r.runc.State(ctx, c.ID())
|
2016-12-06 00:17:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return execution.Status(state.Status), nil
|
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (r *OCIRuntime) load(runcC *runc.Container) (*execution.Container, error) {
|
2016-12-05 23:38:32 +00:00
|
|
|
container := execution.LoadContainer(
|
|
|
|
execution.StateDir(filepath.Join(r.root, runcC.ID)),
|
|
|
|
runcC.ID,
|
|
|
|
runcC.Bundle,
|
2016-12-07 18:44:05 +00:00
|
|
|
runcC.Status,
|
2016-12-06 00:17:46 +00:00
|
|
|
int64(runcC.Pid),
|
2016-12-05 23:38:32 +00:00
|
|
|
)
|
2016-12-05 22:15:03 +00:00
|
|
|
|
2016-12-06 00:17:46 +00:00
|
|
|
dirs, err := container.StateDir().Processes()
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 23:38:32 +00:00
|
|
|
for _, d := range dirs {
|
|
|
|
pid, err := runc.ReadPidFile(filepath.Join(d, "pid"))
|
|
|
|
if err != nil {
|
2016-12-09 17:17:34 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Process died in between
|
|
|
|
continue
|
|
|
|
}
|
2016-12-05 23:38:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-14 17:16:23 +00:00
|
|
|
process, err := newProcess(container, filepath.Base(d), pid)
|
2016-12-05 23:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
container.AddProcess(process, pid == runcC.Pid)
|
2016-12-02 23:37:16 +00:00
|
|
|
}
|
2016-12-05 22:15:03 +00:00
|
|
|
|
|
|
|
return container, nil
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) List(ctx context.Context) ([]*execution.Container, error) {
|
|
|
|
runcCs, err := r.runc.List(ctx)
|
2016-12-05 22:15:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-06 00:17:46 +00:00
|
|
|
var containers []*execution.Container
|
2016-12-05 22:15:03 +00:00
|
|
|
for _, c := range runcCs {
|
2016-12-11 19:07:32 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
container, err := r.load(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
containers = append(containers, container)
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return containers, nil
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) Load(ctx context.Context, id string) (*execution.Container, error) {
|
|
|
|
runcC, err := r.runc.State(ctx, id)
|
2016-09-28 22:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 22:15:03 +00:00
|
|
|
|
|
|
|
return r.load(runcC)
|
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) Delete(ctx context.Context, c *execution.Container) error {
|
2016-12-07 18:44:05 +00:00
|
|
|
id := c.ID()
|
2016-12-09 18:00:30 +00:00
|
|
|
if err := r.runc.Delete(ctx, id); err != nil {
|
2016-12-05 22:15:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-12-06 00:17:46 +00:00
|
|
|
c.StateDir().Delete()
|
2016-12-09 19:20:03 +00:00
|
|
|
r.ios[id].cleanup()
|
2016-12-07 18:44:05 +00:00
|
|
|
delete(r.ios, id)
|
2016-12-05 22:15:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) Pause(ctx context.Context, c *execution.Container) error {
|
|
|
|
return r.runc.Pause(ctx, c.ID())
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) Resume(ctx context.Context, c *execution.Container) error {
|
|
|
|
return r.runc.Resume(ctx, c.ID())
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) StartProcess(ctx context.Context, c *execution.Container, o execution.StartProcessOpts) (p execution.Process, err error) {
|
2016-12-09 19:20:03 +00:00
|
|
|
oio, err := newOIO(o.Stdin, o.Stdout, o.Stderr, o.Console)
|
2016-12-07 18:44:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-12-09 19:20:03 +00:00
|
|
|
oio.cleanup()
|
2016-12-07 18:44:05 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-12-14 17:16:23 +00:00
|
|
|
procStateDir, err := c.StateDir().NewProcess(o.ID)
|
2016-09-28 22:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 22:15:03 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-12-14 17:16:23 +00:00
|
|
|
c.StateDir().DeleteProcess(o.ID)
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-12-09 18:47:29 +00:00
|
|
|
pidFile := filepath.Join(procStateDir, "pid")
|
2016-12-09 18:00:30 +00:00
|
|
|
if err := r.runc.Exec(ctx, c.ID(), o.Spec, &runc.ExecOpts{
|
2016-12-06 00:17:46 +00:00
|
|
|
PidFile: pidFile,
|
2016-12-09 17:17:34 +00:00
|
|
|
Detach: false,
|
2016-12-09 19:20:03 +00:00
|
|
|
Console: oio.console,
|
2016-12-09 17:17:34 +00:00
|
|
|
Cwd: o.Spec.Cwd,
|
2016-12-09 19:20:03 +00:00
|
|
|
IO: oio.rio,
|
2016-12-06 00:17:46 +00:00
|
|
|
}); err != nil {
|
2016-09-28 22:27:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-06 00:17:46 +00:00
|
|
|
pid, err := runc.ReadPidFile(pidFile)
|
2016-09-28 22:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 22:15:03 +00:00
|
|
|
|
2016-12-14 17:16:23 +00:00
|
|
|
process, err := newProcess(c, o.ID, pid)
|
2016-09-28 22:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 22:15:03 +00:00
|
|
|
|
2016-12-06 00:17:46 +00:00
|
|
|
c.AddProcess(process, false)
|
2016-12-05 22:15:03 +00:00
|
|
|
|
2016-12-09 19:20:03 +00:00
|
|
|
r.ios[fmt.Sprintf("%s-%s", c.ID(), process.ID())] = oio
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
return process, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) SignalProcess(ctx context.Context, c *execution.Container, id string, sig os.Signal) error {
|
2016-12-05 22:15:03 +00:00
|
|
|
process := c.GetProcess(id)
|
|
|
|
if process == nil {
|
|
|
|
return fmt.Errorf("Make a Process Not Found error")
|
|
|
|
}
|
2016-12-06 00:17:46 +00:00
|
|
|
return syscall.Kill(int(process.Pid()), sig.(syscall.Signal))
|
2016-09-30 20:51:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:00:30 +00:00
|
|
|
func (r *OCIRuntime) DeleteProcess(ctx context.Context, c *execution.Container, id string) error {
|
2016-12-07 18:44:05 +00:00
|
|
|
ioID := fmt.Sprintf("%s-%s", c.ID(), id)
|
2016-12-09 19:20:03 +00:00
|
|
|
r.ios[ioID].cleanup()
|
2016-12-07 18:44:05 +00:00
|
|
|
delete(r.ios, ioID)
|
2016-12-06 00:17:46 +00:00
|
|
|
return c.StateDir().DeleteProcess(id)
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|