2016-07-29 22:35:10 +00:00
|
|
|
package oci
|
|
|
|
|
|
|
|
import (
|
2016-08-29 18:48:58 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-09-16 23:32:19 +00:00
|
|
|
"io/ioutil"
|
2016-08-01 17:39:42 +00:00
|
|
|
"os"
|
2016-08-29 18:48:58 +00:00
|
|
|
"os/exec"
|
2016-07-29 22:35:10 +00:00
|
|
|
"path/filepath"
|
2016-09-16 23:32:19 +00:00
|
|
|
"strconv"
|
2016-07-29 22:35:10 +00:00
|
|
|
"strings"
|
2016-09-13 20:58:25 +00:00
|
|
|
"sync"
|
2016-09-15 23:40:44 +00:00
|
|
|
"syscall"
|
2016-08-31 23:32:17 +00:00
|
|
|
"time"
|
2016-07-29 22:35:10 +00:00
|
|
|
|
2016-09-15 23:40:44 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-26 23:55:12 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/utils"
|
2016-08-29 18:48:58 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2016-09-27 18:35:40 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2016-07-29 22:35:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// New creates a new Runtime with options provided
|
2016-09-28 19:49:46 +00:00
|
|
|
func New(runtimePath string, containerDir string, conmonPath string) (*Runtime, error) {
|
2016-07-29 22:35:10 +00:00
|
|
|
r := &Runtime{
|
2016-08-01 22:08:21 +00:00
|
|
|
name: filepath.Base(runtimePath),
|
|
|
|
path: runtimePath,
|
|
|
|
containerDir: containerDir,
|
2016-09-28 19:49:46 +00:00
|
|
|
conmonPath: conmonPath,
|
2016-07-29 22:35:10 +00:00
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:22:34 +00:00
|
|
|
// Runtime stores the information about a oci runtime
|
2016-07-29 22:35:10 +00:00
|
|
|
type Runtime struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
containerDir string
|
2016-09-28 19:49:46 +00:00
|
|
|
conmonPath string
|
2016-07-29 22:35:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 23:40:44 +00:00
|
|
|
// syncInfo is used to return data from monitor process to daemon
|
|
|
|
type syncInfo struct {
|
2016-09-19 07:21:14 +00:00
|
|
|
Pid int `json:"pid"`
|
2016-09-15 23:40:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 22:35:10 +00:00
|
|
|
// Name returns the name of the OCI Runtime
|
|
|
|
func (r *Runtime) Name() string {
|
|
|
|
return r.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path returns the full path the OCI Runtime executable
|
|
|
|
func (r *Runtime) Path() string {
|
|
|
|
return r.path
|
|
|
|
}
|
|
|
|
|
2016-08-01 22:08:21 +00:00
|
|
|
// ContainerDir returns the path to the base directory for storing container configurations
|
|
|
|
func (r *Runtime) ContainerDir() string {
|
|
|
|
return r.containerDir
|
|
|
|
}
|
|
|
|
|
2016-07-29 22:35:10 +00:00
|
|
|
// Version returns the version of the OCI Runtime
|
|
|
|
func (r *Runtime) Version() (string, error) {
|
|
|
|
runtimeVersion, err := getOCIVersion(r.path, "-v")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return runtimeVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOCIVersion(name string, args ...string) (string, error) {
|
|
|
|
out, err := utils.ExecCmd(name, args...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
firstLine := out[:strings.Index(out, "\n")]
|
|
|
|
v := firstLine[strings.LastIndex(firstLine, " ")+1:]
|
|
|
|
return v, nil
|
|
|
|
}
|
2016-08-01 17:39:42 +00:00
|
|
|
|
|
|
|
// CreateContainer creates a container.
|
|
|
|
func (r *Runtime) CreateContainer(c *Container) error {
|
2016-09-15 23:40:44 +00:00
|
|
|
parentPipe, childPipe, err := newPipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating socket pair: %v", err)
|
|
|
|
}
|
|
|
|
defer parentPipe.Close()
|
|
|
|
|
|
|
|
args := []string{"-c", c.name}
|
2016-09-17 09:37:45 +00:00
|
|
|
args = append(args, "-r", r.path)
|
2016-09-15 23:40:44 +00:00
|
|
|
if c.terminal {
|
|
|
|
args = append(args, "-t")
|
|
|
|
}
|
|
|
|
|
2016-09-28 19:49:46 +00:00
|
|
|
cmd := exec.Command(r.conmonPath, args...)
|
2016-09-15 23:40:44 +00:00
|
|
|
cmd.Dir = c.bundlePath
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Setpgid: true,
|
|
|
|
}
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.ExtraFiles = append(cmd.ExtraFiles, childPipe)
|
|
|
|
// 0, 1 and 2 are stdin, stdout and stderr
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("_OCI_SYNCPIPE=%d", 3))
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
childPipe.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need childPipe on the parent side
|
|
|
|
childPipe.Close()
|
|
|
|
|
|
|
|
// Wait to get container pid from conmon
|
|
|
|
// TODO(mrunalp): Add a timeout here
|
|
|
|
var si *syncInfo
|
|
|
|
if err := json.NewDecoder(parentPipe).Decode(&si); err != nil {
|
|
|
|
return fmt.Errorf("reading pid from init pipe: %v", err)
|
|
|
|
}
|
|
|
|
logrus.Infof("Received container pid: %v", si.Pid)
|
|
|
|
return nil
|
2016-08-15 22:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartContainer starts a container.
|
|
|
|
func (r *Runtime) StartContainer(c *Container) error {
|
2016-09-12 22:43:11 +00:00
|
|
|
if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "start", c.name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.state.Started = time.Now()
|
|
|
|
return nil
|
2016-08-15 22:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StopContainer stops a container.
|
|
|
|
func (r *Runtime) StopContainer(c *Container) error {
|
2016-09-27 18:35:40 +00:00
|
|
|
if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", c.name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
if i == 1000 {
|
|
|
|
err := unix.Kill(c.state.Pid, syscall.SIGKILL)
|
|
|
|
if err != nil && err != syscall.ESRCH {
|
|
|
|
return fmt.Errorf("failed to kill process: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if the process is still around
|
|
|
|
err := unix.Kill(c.state.Pid, 0)
|
|
|
|
if err == syscall.ESRCH {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 17:16:05 +00:00
|
|
|
// DeleteContainer deletes a container.
|
|
|
|
func (r *Runtime) DeleteContainer(c *Container) error {
|
|
|
|
return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "delete", c.name)
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
// UpdateStatus refreshes the status of the container.
|
2016-08-29 18:48:58 +00:00
|
|
|
func (r *Runtime) UpdateStatus(c *Container) error {
|
2016-09-13 20:58:25 +00:00
|
|
|
c.stateLock.Lock()
|
|
|
|
defer c.stateLock.Unlock()
|
2016-08-29 18:48:58 +00:00
|
|
|
out, err := exec.Command(r.path, "state", c.name).Output()
|
|
|
|
if err != nil {
|
2016-09-20 08:27:11 +00:00
|
|
|
return fmt.Errorf("error getting container state for %s: %s: %v", c.name, err, out)
|
2016-08-29 18:48:58 +00:00
|
|
|
}
|
|
|
|
stateReader := bytes.NewReader(out)
|
|
|
|
if err := json.NewDecoder(stateReader).Decode(&c.state); err != nil {
|
|
|
|
return fmt.Errorf("failed to decode container status for %s: %s", c.name, err)
|
|
|
|
}
|
2016-09-16 23:32:19 +00:00
|
|
|
|
|
|
|
if c.state.Status == "stopped" {
|
|
|
|
exitFilePath := filepath.Join(c.bundlePath, "exit")
|
|
|
|
fi, err := os.Stat(exitFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to find container exit file: %v", err)
|
|
|
|
}
|
|
|
|
st := fi.Sys().(*syscall.Stat_t)
|
2016-09-19 07:21:14 +00:00
|
|
|
c.state.Finished = time.Unix(st.Ctim.Sec, st.Ctim.Nsec)
|
2016-09-16 23:32:19 +00:00
|
|
|
|
|
|
|
statusCodeStr, err := ioutil.ReadFile(exitFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read exit file: %v", err)
|
|
|
|
}
|
|
|
|
statusCode, err := strconv.Atoi(string(statusCodeStr))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("status code conversion failed: %v", err)
|
|
|
|
}
|
2016-09-19 19:24:53 +00:00
|
|
|
c.state.ExitCode = int32(utils.StatusToExitCode(statusCode))
|
2016-09-16 23:32:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 18:48:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerStatus returns the state of a container.
|
2016-08-31 23:32:17 +00:00
|
|
|
func (r *Runtime) ContainerStatus(c *Container) *ContainerState {
|
2016-09-13 20:58:25 +00:00
|
|
|
c.stateLock.Lock()
|
|
|
|
defer c.stateLock.Unlock()
|
2016-08-29 18:48:58 +00:00
|
|
|
return c.state
|
|
|
|
}
|
|
|
|
|
2016-08-01 17:39:42 +00:00
|
|
|
// Container respresents a runtime container.
|
|
|
|
type Container struct {
|
|
|
|
name string
|
|
|
|
bundlePath string
|
|
|
|
logPath string
|
|
|
|
labels map[string]string
|
|
|
|
sandbox string
|
2016-09-12 23:52:29 +00:00
|
|
|
terminal bool
|
2016-08-31 23:32:17 +00:00
|
|
|
state *ContainerState
|
2016-09-13 20:58:25 +00:00
|
|
|
stateLock sync.Mutex
|
2016-08-29 18:48:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
// ContainerState represents the status of a container.
|
2016-08-31 23:32:17 +00:00
|
|
|
type ContainerState struct {
|
|
|
|
specs.State
|
2016-09-16 23:32:19 +00:00
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Started time.Time `json:"started"`
|
|
|
|
Finished time.Time `json:"finished"`
|
|
|
|
ExitCode int32 `json:"exitCode"`
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 22:22:34 +00:00
|
|
|
// NewContainer creates a container object.
|
2016-09-12 23:52:29 +00:00
|
|
|
func NewContainer(name string, bundlePath string, logPath string, labels map[string]string, sandbox string, terminal bool) (*Container, error) {
|
2016-08-01 17:39:42 +00:00
|
|
|
c := &Container{
|
|
|
|
name: name,
|
|
|
|
bundlePath: bundlePath,
|
|
|
|
logPath: logPath,
|
|
|
|
labels: labels,
|
|
|
|
sandbox: sandbox,
|
2016-09-12 23:52:29 +00:00
|
|
|
terminal: terminal,
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the container.
|
|
|
|
func (c *Container) Name() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// BundlePath returns the bundlePath of the container.
|
|
|
|
func (c *Container) BundlePath() string {
|
|
|
|
return c.bundlePath
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogPath returns the log path of the container.
|
|
|
|
func (c *Container) LogPath() string {
|
|
|
|
return c.logPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Labels returns the labels of the container.
|
|
|
|
func (c *Container) Labels() map[string]string {
|
|
|
|
return c.labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sandbox returns the sandbox name of the container.
|
|
|
|
func (c *Container) Sandbox() string {
|
|
|
|
return c.sandbox
|
|
|
|
}
|
2016-08-31 23:38:14 +00:00
|
|
|
|
|
|
|
// NetNsPath returns the path to the network namespace of the container.
|
|
|
|
func (c *Container) NetNsPath() (string, error) {
|
|
|
|
if c.state == nil {
|
|
|
|
return "", fmt.Errorf("container state is not populated")
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("/proc/%d/ns/net", c.state.Pid), nil
|
|
|
|
}
|
2016-09-15 23:40:44 +00:00
|
|
|
|
|
|
|
// newPipe creates a unix socket pair for communication
|
|
|
|
func newPipe() (parent *os.File, child *os.File, err error) {
|
|
|
|
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return os.NewFile(uintptr(fds[1]), "parent"), os.NewFile(uintptr(fds[0]), "child"), nil
|
|
|
|
}
|