driver.go 8.3 KB

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