github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/daemon/execdriver/driver.go (about)

     1  package execdriver
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"os/exec"
     7  	"time"
     8  
     9  	// TODO Windows: Factor out ulimit
    10  	"github.com/docker/docker/pkg/ulimit"
    11  	"github.com/opencontainers/runc/libcontainer"
    12  	"github.com/opencontainers/runc/libcontainer/configs"
    13  )
    14  
    15  // Context is a generic key value pair that allows
    16  // arbatrary data to be sent
    17  type Context map[string]string
    18  
    19  // Define error messages
    20  var (
    21  	ErrNotRunning              = errors.New("Container is not running")
    22  	ErrWaitTimeoutReached      = errors.New("Wait timeout reached")
    23  	ErrDriverAlreadyRegistered = errors.New("A driver already registered this docker init function")
    24  	ErrDriverNotFound          = errors.New("The requested docker init has not been found")
    25  )
    26  
    27  // StartCallback defines a callback function.
    28  // It's used by 'Run' and 'Exec', does some work in parent process
    29  // after child process is started.
    30  type StartCallback func(*ProcessConfig, int)
    31  
    32  // Info is driver specific information based on
    33  // processes registered with the driver
    34  type Info interface {
    35  	IsRunning() bool
    36  }
    37  
    38  // Terminal represents a pseudo TTY, it is for when
    39  // using a container interactively.
    40  type Terminal interface {
    41  	io.Closer
    42  	Resize(height, width int) error
    43  }
    44  
    45  // ExitStatus provides exit reasons for a container.
    46  type ExitStatus struct {
    47  	// The exit code with which the container exited.
    48  	ExitCode int
    49  
    50  	// Whether the container encountered an OOM.
    51  	OOMKilled bool
    52  }
    53  
    54  // Driver is an interface for drivers to implement
    55  // including all basic functions a driver should have
    56  type Driver interface {
    57  	// Run executes the process, blocks until the process exits and returns
    58  	// the exit code. It's the last stage on Docker side for running a container.
    59  	Run(c *Command, pipes *Pipes, startCallback StartCallback) (ExitStatus, error)
    60  
    61  	// Exec executes the process in an existing container, blocks until the
    62  	// process exits and returns the exit code.
    63  	Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, startCallback StartCallback) (int, error)
    64  
    65  	// Kill sends signals to process in container.
    66  	Kill(c *Command, sig int) error
    67  
    68  	// Pause pauses a container.
    69  	Pause(c *Command) error
    70  
    71  	// Unpause unpauses a container.
    72  	Unpause(c *Command) error
    73  
    74  	// Name returns the name of the driver.
    75  	Name() string
    76  
    77  	// Info returns the configuration stored in the driver struct,
    78  	// "temporary" hack (until we move state from core to plugins).
    79  	Info(id string) Info
    80  
    81  	// GetPidsForContainer returns a list of pid for the processes running in a container.
    82  	GetPidsForContainer(id string) ([]int, error)
    83  
    84  	// Terminate kills a container by sending signal SIGKILL.
    85  	Terminate(c *Command) error
    86  
    87  	// Clean removes all traces of container exec.
    88  	Clean(id string) error
    89  
    90  	// Stats returns resource stats for a running container
    91  	Stats(id string) (*ResourceStats, error)
    92  }
    93  
    94  // Ipc settings of the container
    95  // It is for IPC namespace setting. Usually different containers
    96  // have their own IPC namespace, however this specifies to use
    97  // an existing IPC namespace.
    98  // You can join the host's or a container's IPC namespace.
    99  type Ipc struct {
   100  	ContainerID string `json:"container_id"` // id of the container to join ipc.
   101  	HostIpc     bool   `json:"host_ipc"`
   102  }
   103  
   104  // Pid settings of the container
   105  // It is for PID namespace setting. Usually different containers
   106  // have their own PID namespace, however this specifies to use
   107  // an existing PID namespace.
   108  // Joining the host's PID namespace is currently the only supported
   109  // option.
   110  type Pid struct {
   111  	HostPid bool `json:"host_pid"`
   112  }
   113  
   114  // UTS settings of the container
   115  // It is for UTS namespace setting. Usually different containers
   116  // have their own UTS namespace, however this specifies to use
   117  // an existing UTS namespace.
   118  // Joining the host's UTS namespace is currently the only supported
   119  // option.
   120  type UTS struct {
   121  	HostUTS bool `json:"host_uts"`
   122  }
   123  
   124  // Resources contains all resource configs for a driver.
   125  // Currently these are all for cgroup configs.
   126  // TODO Windows: Factor out ulimit.Rlimit
   127  type Resources struct {
   128  	Memory           int64            `json:"memory"`
   129  	MemorySwap       int64            `json:"memory_swap"`
   130  	KernelMemory     int64            `json:"kernel_memory"`
   131  	CPUShares        int64            `json:"cpu_shares"`
   132  	CpusetCpus       string           `json:"cpuset_cpus"`
   133  	CpusetMems       string           `json:"cpuset_mems"`
   134  	CPUPeriod        int64            `json:"cpu_period"`
   135  	CPUQuota         int64            `json:"cpu_quota"`
   136  	BlkioWeight      int64            `json:"blkio_weight"`
   137  	Rlimits          []*ulimit.Rlimit `json:"rlimits"`
   138  	OomKillDisable   bool             `json:"oom_kill_disable"`
   139  	MemorySwappiness int64            `json:"memory_swappiness"`
   140  }
   141  
   142  // ResourceStats contains information about resource usage by a container.
   143  type ResourceStats struct {
   144  	*libcontainer.Stats
   145  	Read        time.Time `json:"read"`
   146  	MemoryLimit int64     `json:"memory_limit"`
   147  	SystemUsage uint64    `json:"system_usage"`
   148  }
   149  
   150  // Mount contains information for a mount operation.
   151  type Mount struct {
   152  	Source      string `json:"source"`
   153  	Destination string `json:"destination"`
   154  	Writable    bool   `json:"writable"`
   155  	Private     bool   `json:"private"`
   156  	Slave       bool   `json:"slave"`
   157  }
   158  
   159  // ProcessConfig describes a process that will be run inside a container.
   160  type ProcessConfig struct {
   161  	exec.Cmd `json:"-"`
   162  
   163  	Privileged  bool     `json:"privileged"`
   164  	User        string   `json:"user"`
   165  	Tty         bool     `json:"tty"`
   166  	Entrypoint  string   `json:"entrypoint"`
   167  	Arguments   []string `json:"arguments"`
   168  	Terminal    Terminal `json:"-"` // standard or tty terminal
   169  	Console     string   `json:"-"` // dev/console path
   170  	ConsoleSize [2]int   `json:"-"` // h,w of initial console size
   171  }
   172  
   173  // Command wrapps an os/exec.Cmd to add more metadata
   174  //
   175  // TODO Windows: Factor out unused fields such as LxcConfig, AppArmorProfile,
   176  // and CgroupParent.
   177  type Command struct {
   178  	ID                 string            `json:"id"`
   179  	Rootfs             string            `json:"rootfs"` // root fs of the container
   180  	ReadonlyRootfs     bool              `json:"readonly_rootfs"`
   181  	InitPath           string            `json:"initpath"` // dockerinit
   182  	WorkingDir         string            `json:"working_dir"`
   183  	ConfigPath         string            `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver
   184  	Network            *Network          `json:"network"`
   185  	Ipc                *Ipc              `json:"ipc"`
   186  	Pid                *Pid              `json:"pid"`
   187  	UTS                *UTS              `json:"uts"`
   188  	Resources          *Resources        `json:"resources"`
   189  	Mounts             []Mount           `json:"mounts"`
   190  	AllowedDevices     []*configs.Device `json:"allowed_devices"`
   191  	AutoCreatedDevices []*configs.Device `json:"autocreated_devices"`
   192  	CapAdd             []string          `json:"cap_add"`
   193  	CapDrop            []string          `json:"cap_drop"`
   194  	GroupAdd           []string          `json:"group_add"`
   195  	ContainerPid       int               `json:"container_pid"`  // the pid for the process inside a container
   196  	ProcessConfig      ProcessConfig     `json:"process_config"` // Describes the init process of the container.
   197  	ProcessLabel       string            `json:"process_label"`
   198  	MountLabel         string            `json:"mount_label"`
   199  	LxcConfig          []string          `json:"lxc_config"`
   200  	AppArmorProfile    string            `json:"apparmor_profile"`
   201  	CgroupParent       string            `json:"cgroup_parent"` // The parent cgroup for this command.
   202  	FirstStart         bool              `json:"first_start"`
   203  	LayerPaths         []string          `json:"layer_paths"` // Windows needs to know the layer paths and folder for a command
   204  	LayerFolder        string            `json:"layer_folder"`
   205  }