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"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2016-09-30 20:51:10 +00:00
|
|
|
"time"
|
2016-09-28 22:09:22 +00:00
|
|
|
|
|
|
|
"github.com/docker/containerkit"
|
|
|
|
)
|
|
|
|
|
2016-10-05 22:07:20 +00:00
|
|
|
var ErrRootEmpty = errors.New("oci: runtime root cannot be an empty string")
|
|
|
|
|
2016-09-30 20:51:10 +00:00
|
|
|
type Opts struct {
|
2016-10-03 22:20:45 +00:00
|
|
|
Name string
|
|
|
|
Root string
|
|
|
|
Args []string
|
2016-09-30 20:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(opts Opts) (*OCIRuntime, error) {
|
2016-10-05 22:07:20 +00:00
|
|
|
if opts.Root == "" {
|
|
|
|
return nil, ErrRootEmpty
|
|
|
|
}
|
2016-09-30 20:51:10 +00:00
|
|
|
if err := os.MkdirAll(opts.Root, 0711); err != nil {
|
2016-09-28 22:09:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
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-09-28 22:09:22 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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-09-30 17:39:26 +00:00
|
|
|
func (r *OCIRuntime) Create(c *containerkit.Container) (containerkit.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-09-30 17:39:26 +00:00
|
|
|
func (r *OCIRuntime) Start(c *containerkit.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-09-30 17:39:26 +00:00
|
|
|
func (r *OCIRuntime) Delete(c *containerkit.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-09-30 17:39:26 +00:00
|
|
|
func (r *OCIRuntime) Exec(c *containerkit.Container, p *containerkit.Process) (containerkit.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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OCIRuntime) Load(id string) (containerkit.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
|
|
|
}
|