2014-01-09 02:47:57 +00:00
|
|
|
package execdriver
|
|
|
|
|
|
|
|
import (
|
2014-01-14 00:18:46 +00:00
|
|
|
"errors"
|
2014-02-21 21:27:15 +00:00
|
|
|
"io"
|
2014-01-09 23:04:45 +00:00
|
|
|
"os/exec"
|
2015-01-07 22:43:04 +00:00
|
|
|
"time"
|
2015-02-11 19:21:38 +00:00
|
|
|
|
2015-07-16 23:00:55 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2014-01-09 02:47:57 +00:00
|
|
|
)
|
|
|
|
|
2014-03-18 20:49:16 +00:00
|
|
|
// Context is a generic key value pair that allows
|
2015-11-06 01:58:06 +00:00
|
|
|
// arbitrary data to be sent
|
2014-03-18 20:49:16 +00:00
|
|
|
type Context map[string]string
|
|
|
|
|
2015-07-28 00:43:22 +00:00
|
|
|
// Define error messages
|
2014-01-14 00:18:46 +00:00
|
|
|
var (
|
2015-01-08 02:02:08 +00:00
|
|
|
ErrNotRunning = errors.New("Container is not running")
|
2014-01-14 02:36:59 +00:00
|
|
|
ErrWaitTimeoutReached = errors.New("Wait timeout reached")
|
|
|
|
ErrDriverAlreadyRegistered = errors.New("A driver already registered this docker init function")
|
|
|
|
ErrDriverNotFound = errors.New("The requested docker init has not been found")
|
2014-01-14 00:18:46 +00:00
|
|
|
)
|
|
|
|
|
2015-09-11 19:05:57 +00:00
|
|
|
// DriverCallback defines a callback function which is used in "Run" and "Exec".
|
|
|
|
// This allows work to be done in the parent process when the child is passing
|
|
|
|
// through PreStart, Start and PostStop events.
|
2015-09-11 10:01:47 +00:00
|
|
|
// Callbacks are provided a processConfig pointer and the pid of the child.
|
|
|
|
// The channel will be used to notify the OOM events.
|
2015-09-29 17:51:40 +00:00
|
|
|
type DriverCallback func(processConfig *ProcessConfig, pid int, chOOM <-chan struct{}) error
|
2015-09-11 19:05:57 +00:00
|
|
|
|
|
|
|
// Hooks is a struct containing function pointers to callbacks
|
|
|
|
// used by any execdriver implementation exploiting hooks capabilities
|
|
|
|
type Hooks struct {
|
|
|
|
// PreStart is called before container's CMD/ENTRYPOINT is executed
|
|
|
|
PreStart []DriverCallback
|
|
|
|
// Start is called after the container's process is full started
|
|
|
|
Start DriverCallback
|
|
|
|
// PostStop is called after the container process exits
|
|
|
|
PostStop []DriverCallback
|
|
|
|
}
|
2014-01-13 23:02:12 +00:00
|
|
|
|
2015-07-28 00:43:22 +00:00
|
|
|
// Info is driver specific information based on
|
2014-01-15 21:57:07 +00:00
|
|
|
// processes registered with the driver
|
2014-01-15 19:46:25 +00:00
|
|
|
type Info interface {
|
|
|
|
IsRunning() bool
|
|
|
|
}
|
|
|
|
|
2015-07-28 00:43:22 +00:00
|
|
|
// Terminal represents a pseudo TTY, it is for when
|
|
|
|
// using a container interactively.
|
2014-02-21 21:27:15 +00:00
|
|
|
type Terminal interface {
|
|
|
|
io.Closer
|
|
|
|
Resize(height, width int) error
|
|
|
|
}
|
|
|
|
|
2015-07-28 00:43:22 +00:00
|
|
|
// Driver is an interface for drivers to implement
|
|
|
|
// including all basic functions a driver should have
|
2014-01-09 23:04:45 +00:00
|
|
|
type Driver interface {
|
2015-07-28 00:43:22 +00:00
|
|
|
// Run executes the process, blocks until the process exits and returns
|
|
|
|
// the exit code. It's the last stage on Docker side for running a container.
|
2015-09-29 17:51:40 +00:00
|
|
|
Run(c *Command, pipes *Pipes, hooks Hooks) (ExitStatus, error)
|
2015-07-28 00:43:22 +00:00
|
|
|
|
|
|
|
// Exec executes the process in an existing container, blocks until the
|
|
|
|
// process exits and returns the exit code.
|
2015-09-29 17:51:40 +00:00
|
|
|
Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, hooks Hooks) (int, error)
|
2015-07-28 00:43:22 +00:00
|
|
|
|
|
|
|
// Kill sends signals to process in container.
|
2014-01-21 00:05:07 +00:00
|
|
|
Kill(c *Command, sig int) error
|
2015-07-28 00:43:22 +00:00
|
|
|
|
|
|
|
// Pause pauses a container.
|
2014-05-21 21:06:18 +00:00
|
|
|
Pause(c *Command) error
|
2015-07-28 00:43:22 +00:00
|
|
|
|
|
|
|
// Unpause unpauses a container.
|
2014-05-21 21:06:18 +00:00
|
|
|
Unpause(c *Command) error
|
2015-07-28 00:43:22 +00:00
|
|
|
|
|
|
|
// Name returns the name of the driver.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// Info returns the configuration stored in the driver struct,
|
|
|
|
// "temporary" hack (until we move state from core to plugins).
|
|
|
|
Info(id string) Info
|
|
|
|
|
|
|
|
// GetPidsForContainer returns a list of pid for the processes running in a container.
|
|
|
|
GetPidsForContainer(id string) ([]int, error)
|
|
|
|
|
|
|
|
// Terminate kills a container by sending signal SIGKILL.
|
|
|
|
Terminate(c *Command) error
|
|
|
|
|
|
|
|
// Clean removes all traces of container exec.
|
|
|
|
Clean(id string) error
|
|
|
|
|
|
|
|
// Stats returns resource stats for a running container
|
|
|
|
Stats(id string) (*ResourceStats, error)
|
2015-09-11 19:05:57 +00:00
|
|
|
|
|
|
|
// SupportsHooks refers to the driver capability to exploit pre/post hook functionality
|
|
|
|
SupportsHooks() bool
|
2014-01-09 23:04:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 17:11:10 +00:00
|
|
|
// CommonResources contains the resource configs for a driver that are
|
|
|
|
// common across platforms.
|
|
|
|
type CommonResources struct {
|
|
|
|
Memory int64 `json:"memory"`
|
|
|
|
MemoryReservation int64 `json:"memory_reservation"`
|
|
|
|
CPUShares int64 `json:"cpu_shares"`
|
|
|
|
BlkioWeight uint16 `json:"blkio_weight"`
|
2014-01-20 21:23:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 00:43:22 +00:00
|
|
|
// ResourceStats contains information about resource usage by a container.
|
2015-01-07 22:43:04 +00:00
|
|
|
type ResourceStats struct {
|
2015-03-05 17:55:14 +00:00
|
|
|
*libcontainer.Stats
|
2015-01-08 00:22:42 +00:00
|
|
|
Read time.Time `json:"read"`
|
|
|
|
MemoryLimit int64 `json:"memory_limit"`
|
|
|
|
SystemUsage uint64 `json:"system_usage"`
|
2015-01-07 22:43:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 15:53:15 +00:00
|
|
|
// CommonProcessConfig is the common platform agnostic part of the ProcessConfig
|
|
|
|
// structure that describes a process that will be run inside a container.
|
|
|
|
type CommonProcessConfig struct {
|
2014-01-21 00:05:07 +00:00
|
|
|
exec.Cmd `json:"-"`
|
2014-01-10 19:44:35 +00:00
|
|
|
|
2015-11-01 15:53:15 +00:00
|
|
|
Tty bool `json:"tty"`
|
|
|
|
Entrypoint string `json:"entrypoint"`
|
|
|
|
Arguments []string `json:"arguments"`
|
|
|
|
Terminal Terminal `json:"-"` // standard or tty terminal
|
2014-08-26 22:05:37 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 21:27:39 +00:00
|
|
|
// CommonCommand is the common platform agnostic part of the Command structure
|
|
|
|
// which wraps an os/exec.Cmd to add more metadata
|
|
|
|
type CommonCommand struct {
|
|
|
|
ContainerPid int `json:"container_pid"` // the pid for the process inside a container
|
|
|
|
ID string `json:"id"`
|
|
|
|
InitPath string `json:"initpath"` // dockerinit
|
|
|
|
MountLabel string `json:"mount_label"` // TODO Windows. More involved, but can be factored out
|
|
|
|
Mounts []Mount `json:"mounts"`
|
|
|
|
Network *Network `json:"network"`
|
|
|
|
ProcessConfig ProcessConfig `json:"process_config"` // Describes the init process of the container.
|
|
|
|
ProcessLabel string `json:"process_label"` // TODO Windows. More involved, but can be factored out
|
|
|
|
Resources *Resources `json:"resources"`
|
|
|
|
Rootfs string `json:"rootfs"` // root fs of the container
|
|
|
|
WorkingDir string `json:"working_dir"`
|
2014-01-09 23:04:45 +00:00
|
|
|
}
|