driver.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package execdriver
  2. import (
  3. "errors"
  4. "io"
  5. "os"
  6. "os/exec"
  7. "github.com/docker/libcontainer/devices"
  8. )
  9. // Context is a generic key value pair that allows
  10. // arbatrary data to be sent
  11. type Context map[string]string
  12. var (
  13. ErrNotRunning = errors.New("Process could not be started")
  14. ErrWaitTimeoutReached = errors.New("Wait timeout reached")
  15. ErrDriverAlreadyRegistered = errors.New("A driver already registered this docker init function")
  16. ErrDriverNotFound = errors.New("The requested docker init has not been found")
  17. )
  18. type StartCallback func(*ProcessConfig, int)
  19. // Driver specific information based on
  20. // processes registered with the driver
  21. type Info interface {
  22. IsRunning() bool
  23. }
  24. // Terminal in an interface for drivers to implement
  25. // if they want to support Close and Resize calls from
  26. // the core
  27. type Terminal interface {
  28. io.Closer
  29. Resize(height, width int) error
  30. }
  31. type TtyTerminal interface {
  32. Master() *os.File
  33. }
  34. // ExitStatus provides exit reasons for a container.
  35. type ExitStatus struct {
  36. // The exit code with which the container exited.
  37. ExitCode int
  38. // Whether the container encountered an OOM.
  39. OOMKilled bool
  40. }
  41. type Driver interface {
  42. Run(c *Command, pipes *Pipes, startCallback StartCallback) (ExitStatus, error) // Run executes the process and blocks until the process exits and returns the exit code
  43. // Exec executes the process in an existing container, blocks until the process exits and returns the exit code
  44. Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, startCallback StartCallback) (int, error)
  45. Kill(c *Command, sig int) error
  46. Pause(c *Command) error
  47. Unpause(c *Command) error
  48. Name() string // Driver name
  49. Info(id string) Info // "temporary" hack (until we move state from core to plugins)
  50. GetPidsForContainer(id string) ([]int, error) // Returns a list of pids for the given container.
  51. Terminate(c *Command) error // kill it with fire
  52. Clean(id string) error // clean all traces of container exec
  53. }
  54. // Network settings of the container
  55. type Network struct {
  56. Interface *NetworkInterface `json:"interface"` // if interface is nil then networking is disabled
  57. Mtu int `json:"mtu"`
  58. ContainerID string `json:"container_id"` // id of the container to join network.
  59. HostNetworking bool `json:"host_networking"`
  60. }
  61. // IPC settings of the container
  62. type Ipc struct {
  63. ContainerID string `json:"container_id"` // id of the container to join ipc.
  64. HostIpc bool `json:"host_ipc"`
  65. }
  66. // PID settings of the container
  67. type Pid struct {
  68. HostPid bool `json:"host_pid"`
  69. }
  70. type NetworkInterface struct {
  71. Gateway string `json:"gateway"`
  72. IPAddress string `json:"ip"`
  73. IPPrefixLen int `json:"ip_prefix_len"`
  74. MacAddress string `json:"mac"`
  75. Bridge string `json:"bridge"`
  76. GlobalIPv6Address string `json:"global_ipv6"`
  77. LinkLocalIPv6Address string `json:"link_local_ipv6"`
  78. GlobalIPv6PrefixLen int `json:"global_ipv6_prefix_len"`
  79. IPv6Gateway string `json:"ipv6_gateway"`
  80. }
  81. type Resources struct {
  82. Memory int64 `json:"memory"`
  83. MemorySwap int64 `json:"memory_swap"`
  84. CpuShares int64 `json:"cpu_shares"`
  85. Cpuset string `json:"cpuset"`
  86. }
  87. type Mount struct {
  88. Source string `json:"source"`
  89. Destination string `json:"destination"`
  90. Writable bool `json:"writable"`
  91. Private bool `json:"private"`
  92. Slave bool `json:"slave"`
  93. }
  94. // Describes a process that will be run inside a container.
  95. type ProcessConfig struct {
  96. exec.Cmd `json:"-"`
  97. Privileged bool `json:"privileged"`
  98. User string `json:"user"`
  99. Tty bool `json:"tty"`
  100. Entrypoint string `json:"entrypoint"`
  101. Arguments []string `json:"arguments"`
  102. Terminal Terminal `json:"-"` // standard or tty terminal
  103. Console string `json:"-"` // dev/console path
  104. }
  105. // Process wrapps an os/exec.Cmd to add more metadata
  106. type Command struct {
  107. ID string `json:"id"`
  108. Rootfs string `json:"rootfs"` // root fs of the container
  109. ReadonlyRootfs bool `json:"readonly_rootfs"`
  110. InitPath string `json:"initpath"` // dockerinit
  111. WorkingDir string `json:"working_dir"`
  112. ConfigPath string `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver
  113. Network *Network `json:"network"`
  114. Ipc *Ipc `json:"ipc"`
  115. Pid *Pid `json:"pid"`
  116. Resources *Resources `json:"resources"`
  117. Mounts []Mount `json:"mounts"`
  118. AllowedDevices []*devices.Device `json:"allowed_devices"`
  119. AutoCreatedDevices []*devices.Device `json:"autocreated_devices"`
  120. CapAdd []string `json:"cap_add"`
  121. CapDrop []string `json:"cap_drop"`
  122. ContainerPid int `json:"container_pid"` // the pid for the process inside a container
  123. ProcessConfig ProcessConfig `json:"process_config"` // Describes the init process of the container.
  124. ProcessLabel string `json:"process_label"`
  125. MountLabel string `json:"mount_label"`
  126. LxcConfig []string `json:"lxc_config"`
  127. AppArmorProfile string `json:"apparmor_profile"`
  128. }