driver.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(*Command)
  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. Kill(c *Command, sig int) error
  37. Pause(c *Command) error
  38. Unpause(c *Command) error
  39. Name() string // Driver name
  40. Info(id string) Info // "temporary" hack (until we move state from core to plugins)
  41. GetPidsForContainer(id string) ([]int, error) // Returns a list of pids for the given container.
  42. Terminate(c *Command) error // kill it with fire
  43. }
  44. // Network settings of the container
  45. type Network struct {
  46. Interface *NetworkInterface `json:"interface"` // if interface is nil then networking is disabled
  47. Mtu int `json:"mtu"`
  48. ContainerID string `json:"container_id"` // id of the container to join network.
  49. HostNetworking bool `json:"host_networking"`
  50. }
  51. type NetworkInterface struct {
  52. Gateway string `json:"gateway"`
  53. IPAddress string `json:"ip"`
  54. Bridge string `json:"bridge"`
  55. IPPrefixLen int `json:"ip_prefix_len"`
  56. }
  57. type Resources struct {
  58. Memory int64 `json:"memory"`
  59. MemorySwap int64 `json:"memory_swap"`
  60. CpuShares int64 `json:"cpu_shares"`
  61. Cpuset string `json:"cpuset"`
  62. }
  63. type Mount struct {
  64. Source string `json:"source"`
  65. Destination string `json:"destination"`
  66. Writable bool `json:"writable"`
  67. Private bool `json:"private"`
  68. }
  69. // Process wrapps an os/exec.Cmd to add more metadata
  70. type Command struct {
  71. exec.Cmd `json:"-"`
  72. ID string `json:"id"`
  73. Privileged bool `json:"privileged"`
  74. User string `json:"user"`
  75. Rootfs string `json:"rootfs"` // root fs of the container
  76. InitPath string `json:"initpath"` // dockerinit
  77. Entrypoint string `json:"entrypoint"`
  78. Arguments []string `json:"arguments"`
  79. WorkingDir string `json:"working_dir"`
  80. ConfigPath string `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver
  81. Tty bool `json:"tty"`
  82. Network *Network `json:"network"`
  83. Config map[string][]string `json:"config"` // generic values that specific drivers can consume
  84. Resources *Resources `json:"resources"`
  85. Mounts []Mount `json:"mounts"`
  86. AllowedDevices []*devices.Device `json:"allowed_devices"`
  87. AutoCreatedDevices []*devices.Device `json:"autocreated_devices"`
  88. CapAdd []string `json:"cap_add"`
  89. CapDrop []string `json:"cap_drop"`
  90. Terminal Terminal `json:"-"` // standard or tty terminal
  91. Console string `json:"-"` // dev/console path
  92. ContainerPid int `json:"container_pid"` // the pid for the process inside a container
  93. }
  94. // Return the pid of the process
  95. // If the process is nil -1 will be returned
  96. func (c *Command) Pid() int {
  97. return c.ContainerPid
  98. }