2014-01-08 21:47:57 -05:00
|
|
|
package execdriver
|
|
|
|
|
|
|
|
import (
|
2014-01-13 19:18:46 -05:00
|
|
|
"errors"
|
2014-02-21 16:27:15 -05:00
|
|
|
"io"
|
2014-01-09 18:04:45 -05:00
|
|
|
"os/exec"
|
2015-01-07 17:43:04 -05:00
|
|
|
"time"
|
2015-02-11 14:21:38 -05:00
|
|
|
|
2015-07-16 19:00:55 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2014-01-08 21:47:57 -05:00
|
|
|
)
|
|
|
|
|
2014-03-18 16:49:16 -04:00
|
|
|
// Context is a generic key value pair that allows
|
|
|
|
// arbatrary data to be sent
|
|
|
|
type Context map[string]string
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// Define error messages
|
2014-01-13 19:18:46 -05:00
|
|
|
var (
|
2015-01-07 21:02:08 -05:00
|
|
|
ErrNotRunning = errors.New("Container is not running")
|
2014-01-13 21:36:59 -05: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-13 19:18:46 -05:00
|
|
|
)
|
|
|
|
|
2015-09-11 15:05:57 -04: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 06:01:47 -04: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 13:51:40 -04:00
|
|
|
type DriverCallback func(processConfig *ProcessConfig, pid int, chOOM <-chan struct{}) error
|
2015-09-11 15:05:57 -04: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 18:02:12 -05:00
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// Info is driver specific information based on
|
2014-01-15 16:57:07 -05:00
|
|
|
// processes registered with the driver
|
2014-01-15 14:46:25 -05:00
|
|
|
type Info interface {
|
|
|
|
IsRunning() bool
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// Terminal represents a pseudo TTY, it is for when
|
|
|
|
// using a container interactively.
|
2014-02-21 16:27:15 -05:00
|
|
|
type Terminal interface {
|
|
|
|
io.Closer
|
|
|
|
Resize(height, width int) error
|
|
|
|
}
|
|
|
|
|
2014-10-08 13:03:57 -04:00
|
|
|
// ExitStatus provides exit reasons for a container.
|
|
|
|
type ExitStatus struct {
|
|
|
|
// The exit code with which the container exited.
|
|
|
|
ExitCode int
|
|
|
|
|
|
|
|
// Whether the container encountered an OOM.
|
|
|
|
OOMKilled bool
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// Driver is an interface for drivers to implement
|
|
|
|
// including all basic functions a driver should have
|
2014-01-09 18:04:45 -05:00
|
|
|
type Driver interface {
|
2015-07-27 20:43:22 -04: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 13:51:40 -04:00
|
|
|
Run(c *Command, pipes *Pipes, hooks Hooks) (ExitStatus, error)
|
2015-07-27 20:43:22 -04:00
|
|
|
|
|
|
|
// Exec executes the process in an existing container, blocks until the
|
|
|
|
// process exits and returns the exit code.
|
2015-09-29 13:51:40 -04:00
|
|
|
Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, hooks Hooks) (int, error)
|
2015-07-27 20:43:22 -04:00
|
|
|
|
|
|
|
// Kill sends signals to process in container.
|
2014-01-20 19:05:07 -05:00
|
|
|
Kill(c *Command, sig int) error
|
2015-07-27 20:43:22 -04:00
|
|
|
|
|
|
|
// Pause pauses a container.
|
2014-05-21 17:06:18 -04:00
|
|
|
Pause(c *Command) error
|
2015-07-27 20:43:22 -04:00
|
|
|
|
|
|
|
// Unpause unpauses a container.
|
2014-05-21 17:06:18 -04:00
|
|
|
Unpause(c *Command) error
|
2015-07-27 20:43:22 -04: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 15:05:57 -04:00
|
|
|
|
|
|
|
// SupportsHooks refers to the driver capability to exploit pre/post hook functionality
|
|
|
|
SupportsHooks() bool
|
2014-01-09 18:04:45 -05:00
|
|
|
}
|
|
|
|
|
2015-10-05 13:11:10 -04: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 16:23:02 -05:00
|
|
|
}
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// ResourceStats contains information about resource usage by a container.
|
2015-01-07 17:43:04 -05:00
|
|
|
type ResourceStats struct {
|
2015-03-05 12:55:14 -05:00
|
|
|
*libcontainer.Stats
|
2015-01-07 19:22:42 -05:00
|
|
|
Read time.Time `json:"read"`
|
|
|
|
MemoryLimit int64 `json:"memory_limit"`
|
|
|
|
SystemUsage uint64 `json:"system_usage"`
|
2015-01-07 17:43:04 -05:00
|
|
|
}
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
// User contains the uid and gid representing a Unix user
|
2015-09-09 22:23:06 -04:00
|
|
|
// TODO Windows: Factor out User
|
2015-10-08 11:51:41 -04:00
|
|
|
type User struct {
|
|
|
|
UID int `json:"root_uid"`
|
|
|
|
GID int `json:"root_gid"`
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// ProcessConfig describes a process that will be run inside a container.
|
2014-08-26 18:05:37 -04:00
|
|
|
type ProcessConfig struct {
|
2014-01-20 19:05:07 -05:00
|
|
|
exec.Cmd `json:"-"`
|
2014-01-10 14:44:35 -05:00
|
|
|
|
2015-05-27 16:15:14 -04:00
|
|
|
Privileged bool `json:"privileged"`
|
|
|
|
User string `json:"user"`
|
|
|
|
Tty bool `json:"tty"`
|
|
|
|
Entrypoint string `json:"entrypoint"`
|
|
|
|
Arguments []string `json:"arguments"`
|
2015-10-05 17:27:39 -04:00
|
|
|
Terminal Terminal `json:"-"` // standard or tty terminal (Unix)
|
|
|
|
Console string `json:"-"` // dev/console path (Unix)
|
|
|
|
ConsoleSize [2]int `json:"-"` // h,w of initial console size (Windows)
|
2014-08-26 18:05:37 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 17:27:39 -04: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 18:04:45 -05:00
|
|
|
}
|