controller.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package container
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. executorpkg "github.com/docker/docker/daemon/cluster/executor"
  7. "github.com/docker/engine-api/types"
  8. "github.com/docker/swarmkit/agent/exec"
  9. "github.com/docker/swarmkit/api"
  10. "github.com/docker/swarmkit/log"
  11. "golang.org/x/net/context"
  12. )
  13. // controller implements agent.Controller against docker's API.
  14. //
  15. // Most operations against docker's API are done through the container name,
  16. // which is unique to the task.
  17. type controller struct {
  18. backend executorpkg.Backend
  19. task *api.Task
  20. adapter *containerAdapter
  21. closed chan struct{}
  22. err error
  23. }
  24. var _ exec.Controller = &controller{}
  25. // NewController returns a dockerexec runner for the provided task.
  26. func newController(b executorpkg.Backend, task *api.Task) (*controller, error) {
  27. adapter, err := newContainerAdapter(b, task)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &controller{
  32. backend: b,
  33. task: task,
  34. adapter: adapter,
  35. closed: make(chan struct{}),
  36. }, nil
  37. }
  38. func (r *controller) Task() (*api.Task, error) {
  39. return r.task, nil
  40. }
  41. // ContainerStatus returns the container-specific status for the task.
  42. func (r *controller) ContainerStatus(ctx context.Context) (*api.ContainerStatus, error) {
  43. ctnr, err := r.adapter.inspect(ctx)
  44. if err != nil {
  45. if isUnknownContainer(err) {
  46. return nil, nil
  47. }
  48. return nil, err
  49. }
  50. return parseContainerStatus(ctnr)
  51. }
  52. // Update tasks a recent task update and applies it to the container.
  53. func (r *controller) Update(ctx context.Context, t *api.Task) error {
  54. log.G(ctx).Warnf("task updates not yet supported")
  55. // TODO(stevvooe): While assignment of tasks is idempotent, we do allow
  56. // updates of metadata, such as labelling, as well as any other properties
  57. // that make sense.
  58. return nil
  59. }
  60. // Prepare creates a container and ensures the image is pulled.
  61. //
  62. // If the container has already be created, exec.ErrTaskPrepared is returned.
  63. func (r *controller) Prepare(ctx context.Context) error {
  64. if err := r.checkClosed(); err != nil {
  65. return err
  66. }
  67. // Make sure all the networks that the task needs are created.
  68. if err := r.adapter.createNetworks(ctx); err != nil {
  69. return err
  70. }
  71. // Make sure all the volumes that the task needs are created.
  72. if err := r.adapter.createVolumes(ctx, r.backend); err != nil {
  73. return err
  74. }
  75. for {
  76. if err := r.checkClosed(); err != nil {
  77. return err
  78. }
  79. if err := r.adapter.create(ctx, r.backend); err != nil {
  80. if isContainerCreateNameConflict(err) {
  81. if _, err := r.adapter.inspect(ctx); err != nil {
  82. return err
  83. }
  84. // container is already created. success!
  85. return exec.ErrTaskPrepared
  86. }
  87. if !strings.Contains(err.Error(), "No such image") { // todo: better error detection
  88. return err
  89. }
  90. if err := r.adapter.pullImage(ctx); err != nil {
  91. return err
  92. }
  93. continue // retry to create the container
  94. }
  95. break
  96. }
  97. return nil
  98. }
  99. // Start the container. An error will be returned if the container is already started.
  100. func (r *controller) Start(ctx context.Context) error {
  101. if err := r.checkClosed(); err != nil {
  102. return err
  103. }
  104. ctnr, err := r.adapter.inspect(ctx)
  105. if err != nil {
  106. return err
  107. }
  108. // Detect whether the container has *ever* been started. If so, we don't
  109. // issue the start.
  110. //
  111. // TODO(stevvooe): This is very racy. While reading inspect, another could
  112. // start the process and we could end up starting it twice.
  113. if ctnr.State.Status != "created" {
  114. return exec.ErrTaskStarted
  115. }
  116. if err := r.adapter.start(ctx); err != nil {
  117. return err
  118. }
  119. return nil
  120. }
  121. // Wait on the container to exit.
  122. func (r *controller) Wait(pctx context.Context) error {
  123. if err := r.checkClosed(); err != nil {
  124. return err
  125. }
  126. ctx, cancel := context.WithCancel(pctx)
  127. defer cancel()
  128. c, err := r.adapter.wait(ctx)
  129. if err != nil {
  130. return err
  131. }
  132. <-c
  133. if ctx.Err() != nil {
  134. return ctx.Err()
  135. }
  136. ctnr, err := r.adapter.inspect(ctx)
  137. if err != nil {
  138. // TODO(stevvooe): Need to handle missing container here. It is likely
  139. // that a Wait call with a not found error should result in no waiting
  140. // and no error at all.
  141. return err
  142. }
  143. if ctnr.State.ExitCode != 0 {
  144. var cause error
  145. if ctnr.State.Error != "" {
  146. cause = errors.New(ctnr.State.Error)
  147. }
  148. cstatus, _ := parseContainerStatus(ctnr)
  149. return &exitError{
  150. code: ctnr.State.ExitCode,
  151. cause: cause,
  152. containerStatus: cstatus,
  153. }
  154. }
  155. return nil
  156. }
  157. // Shutdown the container cleanly.
  158. func (r *controller) Shutdown(ctx context.Context) error {
  159. if err := r.checkClosed(); err != nil {
  160. return err
  161. }
  162. if err := r.adapter.shutdown(ctx); err != nil {
  163. if isUnknownContainer(err) || isStoppedContainer(err) {
  164. return nil
  165. }
  166. return err
  167. }
  168. return nil
  169. }
  170. // Terminate the container, with force.
  171. func (r *controller) Terminate(ctx context.Context) error {
  172. if err := r.checkClosed(); err != nil {
  173. return err
  174. }
  175. if err := r.adapter.terminate(ctx); err != nil {
  176. if isUnknownContainer(err) {
  177. return nil
  178. }
  179. return err
  180. }
  181. return nil
  182. }
  183. // Remove the container and its resources.
  184. func (r *controller) Remove(ctx context.Context) error {
  185. if err := r.checkClosed(); err != nil {
  186. return err
  187. }
  188. // It may be necessary to shut down the task before removing it.
  189. if err := r.Shutdown(ctx); err != nil {
  190. if isUnknownContainer(err) {
  191. return nil
  192. }
  193. // This may fail if the task was already shut down.
  194. log.G(ctx).WithError(err).Debug("shutdown failed on removal")
  195. }
  196. // Try removing networks referenced in this task in case this
  197. // task is the last one referencing it
  198. if err := r.adapter.removeNetworks(ctx); err != nil {
  199. if isUnknownContainer(err) {
  200. return nil
  201. }
  202. return err
  203. }
  204. if err := r.adapter.remove(ctx); err != nil {
  205. if isUnknownContainer(err) {
  206. return nil
  207. }
  208. return err
  209. }
  210. return nil
  211. }
  212. // Close the runner and clean up any ephemeral resources.
  213. func (r *controller) Close() error {
  214. select {
  215. case <-r.closed:
  216. return r.err
  217. default:
  218. r.err = exec.ErrControllerClosed
  219. close(r.closed)
  220. }
  221. return nil
  222. }
  223. func (r *controller) checkClosed() error {
  224. select {
  225. case <-r.closed:
  226. return r.err
  227. default:
  228. return nil
  229. }
  230. }
  231. func parseContainerStatus(ctnr types.ContainerJSON) (*api.ContainerStatus, error) {
  232. status := &api.ContainerStatus{
  233. ContainerID: ctnr.ID,
  234. PID: int32(ctnr.State.Pid),
  235. ExitCode: int32(ctnr.State.ExitCode),
  236. }
  237. return status, nil
  238. }
  239. type exitError struct {
  240. code int
  241. cause error
  242. containerStatus *api.ContainerStatus
  243. }
  244. func (e *exitError) Error() string {
  245. if e.cause != nil {
  246. return fmt.Sprintf("task: non-zero exit (%v): %v", e.code, e.cause)
  247. }
  248. return fmt.Sprintf("task: non-zero exit (%v)", e.code)
  249. }
  250. func (e *exitError) ExitCode() int {
  251. return int(e.containerStatus.ExitCode)
  252. }
  253. func (e *exitError) Cause() error {
  254. return e.cause
  255. }