driver.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package execdriver
  2. import (
  3. "errors"
  4. "io"
  5. "os/exec"
  6. "time"
  7. "github.com/opencontainers/runc/libcontainer"
  8. )
  9. // Context is a generic key value pair that allows
  10. // arbitrary data to be sent
  11. type Context map[string]string
  12. // Define error messages
  13. var (
  14. ErrNotRunning = errors.New("Container is not running")
  15. ErrWaitTimeoutReached = errors.New("Wait timeout reached")
  16. ErrDriverAlreadyRegistered = errors.New("A driver already registered this docker init function")
  17. ErrDriverNotFound = errors.New("The requested docker init has not been found")
  18. )
  19. // DriverCallback defines a callback function which is used in "Run" and "Exec".
  20. // This allows work to be done in the parent process when the child is passing
  21. // through PreStart, Start and PostStop events.
  22. // Callbacks are provided a processConfig pointer and the pid of the child.
  23. // The channel will be used to notify the OOM events.
  24. type DriverCallback func(processConfig *ProcessConfig, pid int, chOOM <-chan struct{}) error
  25. // Hooks is a struct containing function pointers to callbacks
  26. // used by any execdriver implementation exploiting hooks capabilities
  27. type Hooks struct {
  28. // PreStart is called before container's CMD/ENTRYPOINT is executed
  29. PreStart []DriverCallback
  30. // Start is called after the container's process is full started
  31. Start DriverCallback
  32. // PostStop is called after the container process exits
  33. PostStop []DriverCallback
  34. }
  35. // Info is driver specific information based on
  36. // processes registered with the driver
  37. type Info interface {
  38. IsRunning() bool
  39. }
  40. // Terminal represents a pseudo TTY, it is for when
  41. // using a container interactively.
  42. type Terminal interface {
  43. io.Closer
  44. Resize(height, width int) error
  45. }
  46. // Driver is an interface for drivers to implement
  47. // including all basic functions a driver should have
  48. type Driver interface {
  49. // Run executes the process, blocks until the process exits and returns
  50. // the exit code. It's the last stage on Docker side for running a container.
  51. Run(c *Command, pipes *Pipes, hooks Hooks) (ExitStatus, error)
  52. // Exec executes the process in an existing container, blocks until the
  53. // process exits and returns the exit code.
  54. Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, hooks Hooks) (int, error)
  55. // Kill sends signals to process in container.
  56. Kill(c *Command, sig int) error
  57. // Pause pauses a container.
  58. Pause(c *Command) error
  59. // Unpause unpauses a container.
  60. Unpause(c *Command) error
  61. // Name returns the name of the driver.
  62. Name() string
  63. // Info returns the configuration stored in the driver struct,
  64. // "temporary" hack (until we move state from core to plugins).
  65. Info(id string) Info
  66. // GetPidsForContainer returns a list of pid for the processes running in a container.
  67. GetPidsForContainer(id string) ([]int, error)
  68. // Terminate kills a container by sending signal SIGKILL.
  69. Terminate(c *Command) error
  70. // Clean removes all traces of container exec.
  71. Clean(id string) error
  72. // Stats returns resource stats for a running container
  73. Stats(id string) (*ResourceStats, error)
  74. // Update updates resource configs for a container
  75. Update(c *Command) error
  76. // SupportsHooks refers to the driver capability to exploit pre/post hook functionality
  77. SupportsHooks() bool
  78. }
  79. // CommonResources contains the resource configs for a driver that are
  80. // common across platforms.
  81. type CommonResources struct {
  82. Memory int64 `json:"memory"`
  83. MemoryReservation int64 `json:"memory_reservation"`
  84. CPUShares int64 `json:"cpu_shares"`
  85. BlkioWeight uint16 `json:"blkio_weight"`
  86. }
  87. // ResourceStats contains information about resource usage by a container.
  88. type ResourceStats struct {
  89. *libcontainer.Stats
  90. Read time.Time `json:"read"`
  91. MemoryLimit int64 `json:"memory_limit"`
  92. SystemUsage uint64 `json:"system_usage"`
  93. }
  94. // CommonProcessConfig is the common platform agnostic part of the ProcessConfig
  95. // structure that describes a process that will be run inside a container.
  96. type CommonProcessConfig struct {
  97. exec.Cmd `json:"-"`
  98. Tty bool `json:"tty"`
  99. Entrypoint string `json:"entrypoint"`
  100. Arguments []string `json:"arguments"`
  101. Terminal Terminal `json:"-"` // standard or tty terminal
  102. }
  103. // CommonCommand is the common platform agnostic part of the Command structure
  104. // which wraps an os/exec.Cmd to add more metadata
  105. type CommonCommand struct {
  106. ContainerPid int `json:"container_pid"` // the pid for the process inside a container
  107. ID string `json:"id"`
  108. InitPath string `json:"initpath"` // dockerinit
  109. MountLabel string `json:"mount_label"` // TODO Windows. More involved, but can be factored out
  110. Mounts []Mount `json:"mounts"`
  111. Network *Network `json:"network"`
  112. ProcessConfig ProcessConfig `json:"process_config"` // Describes the init process of the container.
  113. ProcessLabel string `json:"process_label"` // TODO Windows. More involved, but can be factored out
  114. Resources *Resources `json:"resources"`
  115. Rootfs string `json:"rootfs"` // root fs of the container
  116. WorkingDir string `json:"working_dir"`
  117. TmpDir string `json:"tmpdir"` // Directory used to store docker tmpdirs.
  118. }