driver.go 3.3 KB

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