2016-09-30 17:39:26 +00:00
|
|
|
package oci
|
2016-09-28 22:09:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-10-05 22:07:20 +00:00
|
|
|
"errors"
|
2016-09-28 22:09:22 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2016-09-30 20:51:10 +00:00
|
|
|
"time"
|
2016-09-28 22:09:22 +00:00
|
|
|
|
2016-12-01 18:47:47 +00:00
|
|
|
"github.com/docker/containerd"
|
2016-12-02 19:33:58 +00:00
|
|
|
"github.com/docker/containerd/executors"
|
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 19:33:58 +00:00
|
|
|
func init() {
|
|
|
|
executors.Register("oci", New)
|
|
|
|
executors.Register("runc", New)
|
2016-09-30 20:51:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 19:33:58 +00:00
|
|
|
func New() *OCIRuntime {
|
2016-09-30 17:39:26 +00:00
|
|
|
return &OCIRuntime{
|
2016-09-30 20:51:10 +00:00
|
|
|
root: opts.Root,
|
|
|
|
name: opts.Name,
|
|
|
|
args: opts.Args,
|
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
|
|
|
|
// launched by the runtime
|
2016-09-28 22:09:22 +00:00
|
|
|
root string
|
2016-09-30 17:39:26 +00:00
|
|
|
// name is the name of the runtime, i.e. runc
|
|
|
|
name string
|
2016-09-30 20:51:10 +00:00
|
|
|
// args specifies additional arguments to the OCI runtime
|
|
|
|
args []string
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 22:20:45 +00:00
|
|
|
func (r *OCIRuntime) Name() string {
|
|
|
|
return r.name
|
|
|
|
}
|
|
|
|
|
2016-10-05 22:07:20 +00:00
|
|
|
func (r *OCIRuntime) Args() []string {
|
|
|
|
return r.args
|
|
|
|
}
|
|
|
|
|
2016-10-06 22:18:26 +00:00
|
|
|
func (r *OCIRuntime) Root() string {
|
|
|
|
return r.root
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:47:47 +00:00
|
|
|
func (r *OCIRuntime) Create(c *containerd.Container) (containerd.ProcessDelegate, error) {
|
2016-09-28 22:09:22 +00:00
|
|
|
pidFile := fmt.Sprintf("%s/%s.pid", filepath.Join(r.root, c.ID()), "init")
|
2016-10-03 22:20:45 +00:00
|
|
|
cmd := r.Command("create", "--pid-file", pidFile, "--bundle", c.Path(), c.ID())
|
2016-09-28 22:09:22 +00:00
|
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = c.Stdin, c.Stdout, c.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadFile(pidFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newProcess(i)
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:47:47 +00:00
|
|
|
func (r *OCIRuntime) Start(c *containerd.Container) error {
|
2016-10-03 22:20:45 +00:00
|
|
|
return r.Command("start", c.ID()).Run()
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 18:47:47 +00:00
|
|
|
func (r *OCIRuntime) Delete(c *containerd.Container) error {
|
2016-10-03 22:20:45 +00:00
|
|
|
return r.Command("delete", c.ID()).Run()
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 18:47:47 +00:00
|
|
|
func (r *OCIRuntime) Exec(c *containerd.Container, p *containerd.Process) (containerd.ProcessDelegate, error) {
|
2016-09-28 22:09:22 +00:00
|
|
|
f, err := ioutil.TempFile(filepath.Join(r.root, c.ID()), "process")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path := f.Name()
|
|
|
|
pidFile := fmt.Sprintf("%s/%s.pid", filepath.Join(r.root, c.ID()), filepath.Base(path))
|
|
|
|
err = json.NewEncoder(f).Encode(p.Spec())
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-03 22:20:45 +00:00
|
|
|
cmd := r.Command("exec", "--detach", "--process", path, "--pid-file", pidFile, c.ID())
|
2016-09-28 22:09:22 +00:00
|
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = p.Stdin, p.Stdout, p.Stderr
|
2016-09-28 22:27:24 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-28 22:09:22 +00:00
|
|
|
data, err := ioutil.ReadFile(pidFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newProcess(i)
|
|
|
|
}
|
|
|
|
|
2016-09-30 20:51:10 +00:00
|
|
|
type state struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Pid int `json:"pid"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Bundle string `json:"bundle"`
|
|
|
|
Rootfs string `json:"rootfs"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Annotations map[string]string `json:"annotations"`
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:47:47 +00:00
|
|
|
func (r *OCIRuntime) Load(id string) (containerd.ProcessDelegate, error) {
|
2016-10-03 22:20:45 +00:00
|
|
|
data, err := r.Command("state", id).Output()
|
2016-09-30 20:51:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var s state
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newProcess(s.Pid)
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:20:45 +00:00
|
|
|
func (r *OCIRuntime) Command(args ...string) *exec.Cmd {
|
2016-09-30 20:51:10 +00:00
|
|
|
baseArgs := append([]string{
|
2016-09-28 22:09:22 +00:00
|
|
|
"--root", r.root,
|
2016-09-30 20:51:10 +00:00
|
|
|
}, r.args...)
|
|
|
|
return exec.Command(r.name, append(baseArgs, args...)...)
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|