driver.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package execdriver
  2. import (
  3. "errors"
  4. "os/exec"
  5. "syscall"
  6. )
  7. var (
  8. ErrNotRunning = errors.New("Process could not be started")
  9. ErrWaitTimeoutReached = errors.New("Wait timeout reached")
  10. ErrDriverAlreadyRegistered = errors.New("A driver already registered this docker init function")
  11. ErrDriverNotFound = errors.New("The requested docker init has not been found")
  12. )
  13. var dockerInitFcts map[string]InitFunc
  14. type (
  15. StartCallback func(*Process)
  16. InitFunc func(i *InitArgs) error
  17. )
  18. func RegisterInitFunc(name string, fct InitFunc) error {
  19. if dockerInitFcts == nil {
  20. dockerInitFcts = make(map[string]InitFunc)
  21. }
  22. if _, ok := dockerInitFcts[name]; ok {
  23. return ErrDriverAlreadyRegistered
  24. }
  25. dockerInitFcts[name] = fct
  26. return nil
  27. }
  28. func GetInitFunc(name string) (InitFunc, error) {
  29. fct, ok := dockerInitFcts[name]
  30. if !ok {
  31. return nil, ErrDriverNotFound
  32. }
  33. return fct, nil
  34. }
  35. // Args provided to the init function for a driver
  36. type InitArgs struct {
  37. User string
  38. Gateway string
  39. Ip string
  40. WorkDir string
  41. Privileged bool
  42. Env []string
  43. Args []string
  44. Mtu int
  45. Driver string
  46. }
  47. // Driver specific information based on
  48. // processes registered with the driver
  49. type Info interface {
  50. IsRunning() bool
  51. }
  52. type Driver interface {
  53. Run(c *Process, startCallback StartCallback) (int, error) // Run executes the process and blocks until the process exits and returns the exit code
  54. Kill(c *Process, sig int) error
  55. Wait(id string) error // Wait on an out of process...process - lxc ghosts TODO: Rename to reattach, reconnect
  56. Name() string // Driver name
  57. Info(id string) Info // "temporary" hack (until we move state from core to plugins)
  58. }
  59. // Network settings of the container
  60. type Network struct {
  61. Gateway string `json:"gateway"`
  62. IPAddress string `json:"ip"`
  63. Bridge string `json:"bridge"`
  64. IPPrefixLen int `json:"ip_prefix_len"`
  65. Mtu int `json:"mtu"`
  66. }
  67. type Resources struct {
  68. Memory int64 `json:"memory"`
  69. MemorySwap int64 `json:"memory_swap"`
  70. CpuShares int64 `json:"cpu_shares"`
  71. }
  72. // Process wrapps an os/exec.Cmd to add more metadata
  73. // TODO: Rename to Command
  74. type Process struct {
  75. exec.Cmd
  76. ID string `json:"id"`
  77. Privileged bool `json:"privileged"`
  78. User string `json:"user"`
  79. Rootfs string `json:"rootfs"` // root fs of the container
  80. InitPath string `json:"initpath"` // dockerinit
  81. Entrypoint string `json:"entrypoint"`
  82. Arguments []string `json:"arguments"`
  83. WorkingDir string `json:"working_dir"`
  84. ConfigPath string `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver
  85. Tty bool `json:"tty"`
  86. Network *Network `json:"network"` // if network is nil then networking is disabled
  87. Config []string `json:"config"` // generic values that specific drivers can consume
  88. Resources *Resources `json:"resources"`
  89. }
  90. // Return the pid of the process
  91. // If the process is nil -1 will be returned
  92. func (c *Process) Pid() int {
  93. if c.Process == nil {
  94. return -1
  95. }
  96. return c.Process.Pid
  97. }
  98. // Return the exit code of the process
  99. // if the process has not exited -1 will be returned
  100. func (c *Process) GetExitCode() int {
  101. if c.ProcessState == nil {
  102. return -1
  103. }
  104. return c.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
  105. }