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"
|
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-05 23:38:32 +00:00
|
|
|
func (r *OCIRuntime) Create(id string, o execution.CreateOpts) (container *execution.Container, err error) {
|
2016-12-07 18:44:05 +00:00
|
|
|
io, err := getRuncIO(o.Stdin, o.Stdout, o.Stderr)
|
|
|
|
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-07 18:44:05 +00:00
|
|
|
closeRuncIO(io)
|
2016-12-05 23:38:32 +00:00
|
|
|
}
|
|
|
|
}()
|
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-06 00:17:46 +00:00
|
|
|
initDir, err := container.StateDir().NewProcess()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pidFile := filepath.Join(initDir, "pid")
|
2016-12-05 22:15:03 +00:00
|
|
|
err = r.runc.Create(id, o.Bundle, &runc.CreateOpts{
|
2016-12-06 00:17:46 +00:00
|
|
|
PidFile: pidFile,
|
2016-12-07 18:44:05 +00:00
|
|
|
IO: io,
|
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 {
|
|
|
|
r.runc.Kill(id, int(syscall.SIGKILL))
|
|
|
|
r.runc.Delete(id)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
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-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-07 18:44:05 +00:00
|
|
|
r.ios[id] = io
|
|
|
|
|
2016-12-02 23:37:16 +00:00
|
|
|
return container, nil
|
2016-09-28 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 00:17:46 +00:00
|
|
|
func (r *OCIRuntime) Start(c *execution.Container) error {
|
|
|
|
return r.runc.Start(c.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OCIRuntime) Status(c *execution.Container) (execution.Status, error) {
|
|
|
|
state, err := r.runc.State(c.ID())
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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-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 {
|
2016-12-07 18:44:05 +00:00
|
|
|
id := c.ID()
|
|
|
|
if err := r.runc.Delete(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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OCIRuntime) Pause(c *execution.Container) error {
|
2016-12-06 00:17:46 +00:00
|
|
|
return r.runc.Pause(c.ID())
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OCIRuntime) Resume(c *execution.Container) error {
|
2016-12-06 00:17:46 +00:00
|
|
|
return r.runc.Resume(c.ID())
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 00:17:46 +00:00
|
|
|
func (r *OCIRuntime) StartProcess(c *execution.Container, o execution.CreateProcessOpts) (p execution.Process, err error) {
|
2016-12-07 18:44:05 +00:00
|
|
|
io, err := getRuncIO(o.Stdin, o.Stdout, o.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
closeRuncIO(io)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-12-06 00:17:46 +00:00
|
|
|
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 {
|
2016-12-06 00:17:46 +00:00
|
|
|
c.StateDir().DeleteProcess(filepath.Base(processStateDir))
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-12-06 00:17:46 +00:00
|
|
|
pidFile := filepath.Join(processStateDir, "pid")
|
|
|
|
if err := r.runc.ExecProcess(c.ID(), o.Spec, &runc.ExecOpts{
|
|
|
|
PidFile: pidFile,
|
2016-12-05 22:15:03 +00:00
|
|
|
Detach: true,
|
2016-12-07 18:44:05 +00:00
|
|
|
IO: io,
|
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-06 00:17:46 +00:00
|
|
|
process, err := newProcess(filepath.Base(processStateDir), 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-07 18:44:05 +00:00
|
|
|
r.ios[fmt.Sprintf("%s-%s", c.ID(), process.ID())] = io
|
|
|
|
|
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")
|
|
|
|
}
|
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-05 22:15:03 +00:00
|
|
|
func (r *OCIRuntime) DeleteProcess(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
|
|
|
}
|