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 23:37:16 +00:00
|
|
|
"github.com/docker/containerd/execution"
|
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 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-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-12-02 23:37:16 +00:00
|
|
|
func (r *OCIRuntime) Create(id string, o execution.CreateOpts) (*execution.Container, error) {
|
|
|
|
// /run/runc/redis/1/pid
|
|
|
|
pidFile := filepath.Join(r.root, id, "1", "pid")
|
|
|
|
cmd := command(r.root, "create",
|
|
|
|
"--pid-file", pidFile,
|
|
|
|
"--bundle", o.Bundle,
|
|
|
|
id,
|
|
|
|
)
|
|
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = o.Stdin, o.Stdout, o.Stderr
|
2016-09-28 22:09:22 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-02 23:37:16 +00:00
|
|
|
// TODO: kill on error
|
2016-09-28 22:09:22 +00:00
|
|
|
data, err := ioutil.ReadFile(pidFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-02 23:37:16 +00:00
|
|
|
pid, err := strconv.Atoi(string(data))
|
2016-09-28 22:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-02 23:37:16 +00:00
|
|
|
container := execution.NewContainer(r)
|
|
|
|
container.ID = id
|
|
|
|
container.Root = filepath.Join(r.root, id)
|
|
|
|
container.Bundle = o.Bundle
|
|
|
|
process, err := container.CreateProcess(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
process.Pid = pid
|
|
|
|
process.Stdin = o.Stdin
|
|
|
|
process.Stdout = o.Stdout
|
|
|
|
process.Stderr = o.Stderr
|
|
|
|
return container, nil
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 23:37:16 +00:00
|
|
|
func (r *OCIRuntime) Load(id string) (containerd.ProcessDelegate, error) {
|
|
|
|
data, err := r.Command("state", id).Output()
|
|
|
|
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-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 23:37:16 +00:00
|
|
|
func (r *OCIRuntime) Delete(id string) error {
|
|
|
|
return command(r.root, "delete", 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-02 23:37:16 +00:00
|
|
|
func command(root, args ...string) *exec.Cmd {
|
|
|
|
return exec.Command("runc", append(
|
|
|
|
[]string{"--root", root},
|
|
|
|
args...)...)
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|