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-09 17:17:34 +00:00
|
|
|
"io"
|
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-10-05 22:07:20 +00:00
|
|
|
var ErrRootEmpty = errors.New("oci: runtime root cannot be an empty string")
|
|
|
|
|
2016-12-02 23:37:16 +00:00
|
|
|
func New(root string) *OCIRuntime {
|
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-07 18:44:05 +00:00
|
|
|
ios: make(map[string]runc.IO),
|
2016-12-02 19:33:58 +00:00
|
|
|
}
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 17:39:26 +00:00
|
|
|
type OCIRuntime struct {
|
|
|
|
// root holds runtime state information for the containers
|
2016-09-28 22:09:22 +00:00
|
|
|
root string
|
2016-12-05 22:15:03 +00:00
|
|
|
runc *runc.Runc
|
2016-12-07 18:44:05 +00:00
|
|
|
|
|
|
|
// We need to keep track of the created IO for
|
|
|
|
ios map[string]runc.IO
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeRuncIO(io runc.IO) {
|
|
|
|
if io.Stdin != nil {
|
|
|
|
io.Stdin.(*os.File).Close()
|
|
|
|
}
|
|
|
|
if io.Stdout != nil {
|
|
|
|
io.Stdout.(*os.File).Close()
|
|
|
|
}
|
|
|
|
if io.Stderr != nil {
|
|
|
|
io.Stderr.(*os.File).Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRuncIO(stdin, stdout, stderr string) (io runc.IO, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
closeRuncIO(io)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if io.Stdin, err = os.OpenFile(stdin, os.O_RDONLY, 0); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if io.Stdout, err = os.OpenFile(stdout, os.O_WRONLY, 0); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if io.Stderr, err = os.OpenFile(stderr, os.O_WRONLY, 0); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 17:17:34 +00:00
|
|
|
func setupConsole(rio runc.IO) (*os.File, string, error) {
|
|
|
|
master, console, err := newConsole(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
go io.Copy(master, rio.Stdin)
|
|
|
|
go func() {
|
|
|
|
io.Copy(rio.Stdout, master)
|
|
|
|
master.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return master, console, nil
|
|
|
|
}
|
|
|
|
|
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 17:17:34 +00:00
|
|
|
rio, err := getRuncIO(o.Stdin, o.Stdout, o.Stderr)
|
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 17:17:34 +00:00
|
|
|
closeRuncIO(rio)
|
2016-12-05 23:38:32 +00:00
|
|
|
}
|
|
|
|
}()
|
2016-12-09 17:17:34 +00:00
|
|
|
consolePath := ""
|
|
|
|
if o.Console {
|
|
|
|
master, console, err := setupConsole(rio)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
consolePath = console
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
master.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
|
|
|
|
if container, err = execution.NewContainer(r.root, id, o.Bundle, "created"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func(c *execution.Container) {
|
|
|
|
if err != nil {
|
|
|
|
c.StateDir().Delete()
|
|
|
|
}
|
|
|
|
}(container)
|
|
|
|
|
2016-12-09 18:47:29 +00:00
|
|
|
initProcID, initStateDir, err := container.StateDir().NewProcess()
|
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 17:17:34 +00:00
|
|
|
Console: consolePath,
|
|
|
|
IO: 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-09 18:47:29 +00:00
|
|
|
process, err := newProcess(initProcID, 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 17:17:34 +00:00
|
|
|
r.ios[id] = rio
|
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
|
|
|
|
}
|
|
|
|
process, err := newProcess(filepath.Base(d), pid)
|
|
|
|
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 {
|
|
|
|
container, err := r.load(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
containers = append(containers, container)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07 18:44:05 +00:00
|
|
|
closeRuncIO(r.ios[id])
|
|
|
|
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 17:17:34 +00:00
|
|
|
rio, err := getRuncIO(o.Stdin, o.Stdout, o.Stderr)
|
2016-12-07 18:44:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-12-09 17:17:34 +00:00
|
|
|
closeRuncIO(rio)
|
2016-12-07 18:44:05 +00:00
|
|
|
}
|
|
|
|
}()
|
2016-12-09 17:17:34 +00:00
|
|
|
consolePath := ""
|
|
|
|
if o.Console {
|
|
|
|
master, console, err := setupConsole(rio)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
consolePath = console
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
master.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2016-12-09 18:47:29 +00:00
|
|
|
procID, procStateDir, err := c.StateDir().NewProcess()
|
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-09 18:47:29 +00:00
|
|
|
c.StateDir().DeleteProcess(procID)
|
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,
|
|
|
|
Console: consolePath,
|
|
|
|
Cwd: o.Spec.Cwd,
|
|
|
|
IO: 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-09 18:47:29 +00:00
|
|
|
process, err := newProcess(procID, 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 17:17:34 +00:00
|
|
|
r.ios[fmt.Sprintf("%s-%s", c.ID(), process.ID())] = rio
|
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)
|
|
|
|
closeRuncIO(r.ios[ioID])
|
|
|
|
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
|
|
|
}
|