driver.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package execdriver
  2. import (
  3. "errors"
  4. "io"
  5. "os"
  6. "os/exec"
  7. )
  8. // Context is a generic key value pair that allows
  9. // arbatrary data to be sent
  10. type Context map[string]string
  11. var (
  12. ErrNotRunning = errors.New("Process could not be started")
  13. ErrWaitTimeoutReached = errors.New("Wait timeout reached")
  14. ErrDriverAlreadyRegistered = errors.New("A driver already registered this docker init function")
  15. ErrDriverNotFound = errors.New("The requested docker init has not been found")
  16. )
  17. var dockerInitFcts map[string]InitFunc
  18. type (
  19. StartCallback func(*Command)
  20. InitFunc func(i *InitArgs) error
  21. )
  22. func RegisterInitFunc(name string, fct InitFunc) error {
  23. if dockerInitFcts == nil {
  24. dockerInitFcts = make(map[string]InitFunc)
  25. }
  26. if _, ok := dockerInitFcts[name]; ok {
  27. return ErrDriverAlreadyRegistered
  28. }
  29. dockerInitFcts[name] = fct
  30. return nil
  31. }
  32. func GetInitFunc(name string) (InitFunc, error) {
  33. fct, ok := dockerInitFcts[name]
  34. if !ok {
  35. return nil, ErrDriverNotFound
  36. }
  37. return fct, nil
  38. }
  39. // Args provided to the init function for a driver
  40. type InitArgs struct {
  41. User string
  42. Gateway string
  43. Ip string
  44. WorkDir string
  45. Privileged bool
  46. Env []string
  47. Args []string
  48. Mtu int
  49. Driver string
  50. Console string
  51. Pipe int
  52. Root string
  53. }
  54. // Driver specific information based on
  55. // processes registered with the driver
  56. type Info interface {
  57. IsRunning() bool
  58. }
  59. // Terminal in an interface for drivers to implement
  60. // if they want to support Close and Resize calls from
  61. // the core
  62. type Terminal interface {
  63. io.Closer
  64. Resize(height, width int) error
  65. }
  66. type TtyTerminal interface {
  67. Master() *os.File
  68. }
  69. type Driver interface {
  70. Run(c *Command, pipes *Pipes, startCallback StartCallback) (int, error) // Run executes the process and blocks until the process exits and returns the exit code
  71. Kill(c *Command, sig int) error
  72. Name() string // Driver name
  73. Info(id string) Info // "temporary" hack (until we move state from core to plugins)
  74. GetPidsForContainer(id string) ([]int, error) // Returns a list of pids for the given container.
  75. Terminate(c *Command) error // kill it with fire
  76. }
  77. // Network settings of the container
  78. type Network struct {
  79. Interface *NetworkInterface `json:"interface"` // if interface is nil then networking is disabled
  80. Mtu int `json:"mtu"`
  81. ContainerID string `json:"container_id"` // id of the container to join network.
  82. HostNetworking bool `json:"host_networking"`
  83. }
  84. type NetworkInterface struct {
  85. Gateway string `json:"gateway"`
  86. IPAddress string `json:"ip"`
  87. Bridge string `json:"bridge"`
  88. IPPrefixLen int `json:"ip_prefix_len"`
  89. }
  90. type Resources struct {
  91. Memory int64 `json:"memory"`
  92. MemorySwap int64 `json:"memory_swap"`
  93. CpuShares int64 `json:"cpu_shares"`
  94. Cpuset string `json:"cpuset"`
  95. }
  96. type Mount struct {
  97. Source string `json:"source"`
  98. Destination string `json:"destination"`
  99. Writable bool `json:"writable"`
  100. Private bool `json:"private"`
  101. }
  102. // Process wrapps an os/exec.Cmd to add more metadata
  103. type Command struct {
  104. exec.Cmd `json:"-"`
  105. ID string `json:"id"`
  106. Privileged bool `json:"privileged"`
  107. User string `json:"user"`
  108. Rootfs string `json:"rootfs"` // root fs of the container
  109. InitPath string `json:"initpath"` // dockerinit
  110. Entrypoint string `json:"entrypoint"`
  111. Arguments []string `json:"arguments"`
  112. WorkingDir string `json:"working_dir"`
  113. ConfigPath string `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver
  114. Tty bool `json:"tty"`
  115. Network *Network `json:"network"`
  116. Config map[string][]string `json:"config"` // generic values that specific drivers can consume
  117. Resources *Resources `json:"resources"`
  118. Mounts []Mount `json:"mounts"`
  119. Terminal Terminal `json:"-"` // standard or tty terminal
  120. Console string `json:"-"` // dev/console path
  121. ContainerPid int `json:"container_pid"` // the pid for the process inside a container
  122. }
  123. // Return the pid of the process
  124. // If the process is nil -1 will be returned
  125. func (c *Command) Pid() int {
  126. return c.ContainerPid
  127. }