2016-07-29 22:35:10 +00:00
|
|
|
package oci
|
|
|
|
|
|
|
|
import (
|
2016-08-29 18:48:58 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
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"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mrunalp/ocid/utils"
|
2016-08-29 18:48:58 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2016-07-29 22:35:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// New creates a new Runtime with options provided
|
2016-08-01 23:05:37 +00:00
|
|
|
func New(runtimePath string, containerDir 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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-15 22:22:34 +00:00
|
|
|
return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "--systemd-cgroup", "create", "--bundle", c.bundlePath, c.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartContainer starts a container.
|
|
|
|
func (r *Runtime) StartContainer(c *Container) error {
|
|
|
|
return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "start", c.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopContainer stops a container.
|
|
|
|
func (r *Runtime) StopContainer(c *Container) error {
|
|
|
|
// TODO: Check if it is still running after some time and send SIGKILL
|
|
|
|
return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", c.name)
|
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-08-29 18:48:58 +00:00
|
|
|
// updateStatus refreshes the status of the container.
|
|
|
|
func (r *Runtime) UpdateStatus(c *Container) error {
|
|
|
|
out, err := exec.Command(r.path, "state", c.name).Output()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting container state for %s: %s", c.name, err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerStatus returns the state of a container.
|
|
|
|
func (r *Runtime) ContainerStatus(c *Container) *specs.State {
|
|
|
|
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-08-29 18:48:58 +00:00
|
|
|
state *specs.State
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerStatus represents the status of a container.
|
|
|
|
type ContainerStatus struct {
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 22:22:34 +00:00
|
|
|
// NewContainer creates a container object.
|
2016-08-01 17:39:42 +00:00
|
|
|
func NewContainer(name string, bundlePath string, logPath string, labels map[string]string, sandbox string) (*Container, error) {
|
|
|
|
c := &Container{
|
|
|
|
name: name,
|
|
|
|
bundlePath: bundlePath,
|
|
|
|
logPath: logPath,
|
|
|
|
labels: labels,
|
|
|
|
sandbox: sandbox,
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|