driver.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. type Driver interface {
  35. Run(c *Command, pipes *Pipes, startCallback StartCallback) (int, error) // Run executes the process and blocks until the process exits and returns the exit code
  36. // Exec executes the process in an existing container, blocks until the process exits and returns the exit code
  37. Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, startCallback StartCallback) (int, error)
  38. Kill(c *Command, sig int) error
  39. Pause(c *Command) error
  40. Unpause(c *Command) error
  41. Name() string // Driver name
  42. Info(id string) Info // "temporary" hack (until we move state from core to plugins)
  43. GetPidsForContainer(id string) ([]int, error) // Returns a list of pids for the given container.
  44. Terminate(c *Command) error // kill it with fire
  45. Clean(id string) error // clean all traces of container exec
  46. }
  47. // Network settings of the container
  48. type Network struct {
  49. Interface *NetworkInterface `json:"interface"` // if interface is nil then networking is disabled
  50. Mtu int `json:"mtu"`
  51. ContainerID string `json:"container_id"` // id of the container to join network.
  52. HostNetworking bool `json:"host_networking"`
  53. }
  54. type NetworkInterface struct {
  55. Gateway string `json:"gateway"`
  56. IPAddress string `json:"ip"`
  57. IPPrefixLen int `json:"ip_prefix_len"`
  58. MacAddress string `json:"mac_address"`
  59. Bridge string `json:"bridge"`
  60. }
  61. type Resources struct {
  62. Memory int64 `json:"memory"`
  63. MemorySwap int64 `json:"memory_swap"`
  64. CpuShares int64 `json:"cpu_shares"`
  65. Cpuset string `json:"cpuset"`
  66. }
  67. type Mount struct {
  68. Source string `json:"source"`
  69. Destination string `json:"destination"`
  70. Writable bool `json:"writable"`
  71. Private bool `json:"private"`
  72. Slave bool `json:"slave"`
  73. }
  74. // Describes a process that will be run inside a container.
  75. type ProcessConfig struct {
  76. exec.Cmd `json:"-"`
  77. Privileged bool `json:"privileged"`
  78. User string `json:"user"`
  79. Tty bool `json:"tty"`
  80. Entrypoint string `json:"entrypoint"`
  81. Arguments []string `json:"arguments"`
  82. Terminal Terminal `json:"-"` // standard or tty terminal
  83. Console string `json:"-"` // dev/console path
  84. }
  85. // Process wrapps an os/exec.Cmd to add more metadata
  86. type Command struct {
  87. ID string `json:"id"`
  88. Rootfs string `json:"rootfs"` // root fs of the container
  89. InitPath string `json:"initpath"` // dockerinit
  90. WorkingDir string `json:"working_dir"`
  91. ConfigPath string `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver
  92. Network *Network `json:"network"`
  93. Resources *Resources `json:"resources"`
  94. Mounts []Mount `json:"mounts"`
  95. AllowedDevices []*devices.Device `json:"allowed_devices"`
  96. AutoCreatedDevices []*devices.Device `json:"autocreated_devices"`
  97. CapAdd []string `json:"cap_add"`
  98. CapDrop []string `json:"cap_drop"`
  99. ContainerPid int `json:"container_pid"` // the pid for the process inside a container
  100. ProcessConfig ProcessConfig `json:"process_config"` // Describes the init process of the container.
  101. ProcessLabel string `json:"process_label"`
  102. MountLabel string `json:"mount_label"`
  103. LxcConfig []string `json:"lxc_config"`
  104. AppArmorProfile string `json:"apparmor_profile"`
  105. }