2016-09-30 17:39:26 +00:00
|
|
|
package oci
|
2016-09-28 22:09:22 +00:00
|
|
|
|
|
|
|
import (
|
2016-10-05 22:07:20 +00:00
|
|
|
"errors"
|
2016-09-28 22:09:22 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
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-05 22:15:03 +00:00
|
|
|
Runc: &runc.Runc{
|
|
|
|
Root: filepath.Join(root, "runc"),
|
|
|
|
},
|
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-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 23:38:32 +00:00
|
|
|
func (r *OCIRuntime) Create(id string, o execution.CreateOpts) (container *execution.Container, err error) {
|
|
|
|
if container, err = execution.NewContainer(r.root, id, o.Bundle); 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 {
|
|
|
|
container.StateDir().Delete()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
var (
|
|
|
|
initDir = container.StateDir().NewProcess()
|
|
|
|
pidFile = filepath.Join(initDir, "pid")
|
|
|
|
)
|
2016-12-05 22:15:03 +00:00
|
|
|
err = r.runc.Create(id, o.Bundle, &runc.CreateOpts{
|
|
|
|
Pidfile: pidfile,
|
|
|
|
Stdin: o.Stdin,
|
|
|
|
Stdout: o.Stdout,
|
|
|
|
Stderr: o.Stderr,
|
|
|
|
})
|
2016-09-28 22:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 22:15:03 +00:00
|
|
|
pid, err := runc.ReadPifFile(pidfile)
|
2016-12-02 23:37:16 +00:00
|
|
|
if err != nil {
|
2016-12-05 23:38:32 +00:00
|
|
|
// TODO: kill the container if we are going to return
|
2016-12-02 23:37:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 23:38:32 +00:00
|
|
|
process, err := newProcess(filepath.Base(initDir), 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-02 23:37:16 +00:00
|
|
|
return container, nil
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
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-05 22:15:03 +00:00
|
|
|
|
2016-12-05 23:38:32 +00:00
|
|
|
dirs, err := ioutil.ReadDir(filepath.Join(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 {
|
|
|
|
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-05 22:15:03 +00:00
|
|
|
func (r *OCIRuntime) List() ([]*execution.Container, error) {
|
|
|
|
runcCs, err := r.runc.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
containers := make([]*execution.Container)
|
|
|
|
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-05 22:15:03 +00:00
|
|
|
func (r *OCIRuntime) Load(id string) (*execution.Container, error) {
|
|
|
|
runcC, err := r.runc.State(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OCIRuntime) Delete(c *execution.Container) error {
|
|
|
|
if err := r.runc.Delete(c.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.StateDir.Delete()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OCIRuntime) Pause(c *execution.Container) error {
|
|
|
|
return r.runc.Pause(c.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OCIRuntime) Resume(c *execution.Container) error {
|
|
|
|
return r.runc.Resume(c.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OCIRuntime) StartProcess(c *execution.Container, o CreateProcessOpts) (execution.Process, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
processStateDir, 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 {
|
|
|
|
c.StateDir.DeleteProcess(filepath.Base(processStateDir))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
pidFile := filepath.Join(processStateDir, id)
|
|
|
|
err := r.runc.ExecProcess(c.ID, o.spec, &runc.ExecOpts{
|
|
|
|
PidFile: pidfile,
|
|
|
|
Detach: true,
|
|
|
|
Stdin: o.stdin,
|
|
|
|
Stdout: o.stdout,
|
|
|
|
Stderr: o.stderr,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-09-28 22:27:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 22:15:03 +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
|
|
|
|
|
|
|
process, err := newProcess(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-05 23:38:32 +00:00
|
|
|
container.AddProcess(process, false)
|
2016-12-05 22:15:03 +00:00
|
|
|
|
|
|
|
return process, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OCIRuntime) SignalProcess(c *execution.Container, id string, sig os.Signal) error {
|
|
|
|
process := c.GetProcess(id)
|
|
|
|
if process == nil {
|
|
|
|
return fmt.Errorf("Make a Process Not Found error")
|
|
|
|
}
|
|
|
|
return syscall.Kill(int(process.Pid()), os.Signal)
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (r *OCIRuntime) GetProcess(c *execution.Container, id string) process {
|
|
|
|
return c.GetProcess(id)
|
2016-09-30 20:51:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func (r *OCIRuntime) DeleteProcess(c *execution.Container, id string) error {
|
|
|
|
c.StateDir.DeleteProcess(id)
|
|
|
|
return nil
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|