controller.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package container
  2. import (
  3. "fmt"
  4. "os"
  5. executorpkg "github.com/docker/docker/daemon/cluster/executor"
  6. "github.com/docker/engine-api/types"
  7. "github.com/docker/swarmkit/agent/exec"
  8. "github.com/docker/swarmkit/api"
  9. "github.com/docker/swarmkit/log"
  10. "github.com/pkg/errors"
  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. // TODO(stevvooe): While assignment of tasks is idempotent, we do allow
  55. // updates of metadata, such as labelling, as well as any other properties
  56. // that make sense.
  57. return nil
  58. }
  59. // Prepare creates a container and ensures the image is pulled.
  60. //
  61. // If the container has already be created, exec.ErrTaskPrepared is returned.
  62. func (r *controller) Prepare(ctx context.Context) error {
  63. if err := r.checkClosed(); err != nil {
  64. return err
  65. }
  66. // Make sure all the networks that the task needs are created.
  67. if err := r.adapter.createNetworks(ctx); err != nil {
  68. return err
  69. }
  70. // Make sure all the volumes that the task needs are created.
  71. if err := r.adapter.createVolumes(ctx, r.backend); err != nil {
  72. return err
  73. }
  74. if os.Getenv("DOCKER_SERVICE_PREFER_OFFLINE_IMAGE") != "1" {
  75. if err := r.adapter.pullImage(ctx); err != nil {
  76. // NOTE(stevvooe): We always try to pull the image to make sure we have
  77. // the most up to date version. This will return an error, but we only
  78. // log it. If the image truly doesn't exist, the create below will
  79. // error out.
  80. //
  81. // This gives us some nice behavior where we use up to date versions of
  82. // mutable tags, but will still run if the old image is available but a
  83. // registry is down.
  84. //
  85. // If you don't want this behavior, lock down your image to an
  86. // immutable tag or digest.
  87. log.G(ctx).WithError(err).Error("pulling image failed")
  88. }
  89. }
  90. if err := r.adapter.create(ctx, r.backend); err != nil {
  91. if isContainerCreateNameConflict(err) {
  92. if _, err := r.adapter.inspect(ctx); err != nil {
  93. return err
  94. }
  95. // container is already created. success!
  96. return exec.ErrTaskPrepared
  97. }
  98. return err
  99. }
  100. return nil
  101. }
  102. // Start the container. An error will be returned if the container is already started.
  103. func (r *controller) Start(ctx context.Context) error {
  104. if err := r.checkClosed(); err != nil {
  105. return err
  106. }
  107. ctnr, err := r.adapter.inspect(ctx)
  108. if err != nil {
  109. return err
  110. }
  111. // Detect whether the container has *ever* been started. If so, we don't
  112. // issue the start.
  113. //
  114. // TODO(stevvooe): This is very racy. While reading inspect, another could
  115. // start the process and we could end up starting it twice.
  116. if ctnr.State.Status != "created" {
  117. return exec.ErrTaskStarted
  118. }
  119. if err := r.adapter.start(ctx); err != nil {
  120. return errors.Wrap(err, "starting container failed")
  121. }
  122. return nil
  123. }
  124. // Wait on the container to exit.
  125. func (r *controller) Wait(pctx context.Context) error {
  126. if err := r.checkClosed(); err != nil {
  127. return err
  128. }
  129. ctx, cancel := context.WithCancel(pctx)
  130. defer cancel()
  131. err := r.adapter.wait(ctx)
  132. if ctx.Err() != nil {
  133. return ctx.Err()
  134. }
  135. if err != nil {
  136. ee := &exitError{}
  137. if err.Error() != "" {
  138. ee.cause = err
  139. }
  140. if ec, ok := err.(exec.ExitCoder); ok {
  141. ee.code = ec.ExitCode()
  142. }
  143. return ee
  144. }
  145. return nil
  146. }
  147. // Shutdown the container cleanly.
  148. func (r *controller) Shutdown(ctx context.Context) error {
  149. if err := r.checkClosed(); err != nil {
  150. return err
  151. }
  152. if err := r.adapter.shutdown(ctx); err != nil {
  153. if isUnknownContainer(err) || isStoppedContainer(err) {
  154. return nil
  155. }
  156. return err
  157. }
  158. return nil
  159. }
  160. // Terminate the container, with force.
  161. func (r *controller) Terminate(ctx context.Context) error {
  162. if err := r.checkClosed(); err != nil {
  163. return err
  164. }
  165. if err := r.adapter.terminate(ctx); err != nil {
  166. if isUnknownContainer(err) {
  167. return nil
  168. }
  169. return err
  170. }
  171. return nil
  172. }
  173. // Remove the container and its resources.
  174. func (r *controller) Remove(ctx context.Context) error {
  175. if err := r.checkClosed(); err != nil {
  176. return err
  177. }
  178. // It may be necessary to shut down the task before removing it.
  179. if err := r.Shutdown(ctx); err != nil {
  180. if isUnknownContainer(err) {
  181. return nil
  182. }
  183. // This may fail if the task was already shut down.
  184. log.G(ctx).WithError(err).Debug("shutdown failed on removal")
  185. }
  186. // Try removing networks referenced in this task in case this
  187. // task is the last one referencing it
  188. if err := r.adapter.removeNetworks(ctx); err != nil {
  189. if isUnknownContainer(err) {
  190. return nil
  191. }
  192. return err
  193. }
  194. if err := r.adapter.remove(ctx); err != nil {
  195. if isUnknownContainer(err) {
  196. return nil
  197. }
  198. return err
  199. }
  200. return nil
  201. }
  202. // Close the runner and clean up any ephemeral resources.
  203. func (r *controller) Close() error {
  204. select {
  205. case <-r.closed:
  206. return r.err
  207. default:
  208. r.err = exec.ErrControllerClosed
  209. close(r.closed)
  210. }
  211. return nil
  212. }
  213. func (r *controller) checkClosed() error {
  214. select {
  215. case <-r.closed:
  216. return r.err
  217. default:
  218. return nil
  219. }
  220. }
  221. func parseContainerStatus(ctnr types.ContainerJSON) (*api.ContainerStatus, error) {
  222. status := &api.ContainerStatus{
  223. ContainerID: ctnr.ID,
  224. PID: int32(ctnr.State.Pid),
  225. ExitCode: int32(ctnr.State.ExitCode),
  226. }
  227. return status, nil
  228. }
  229. type exitError struct {
  230. code int
  231. cause error
  232. }
  233. func (e *exitError) Error() string {
  234. if e.cause != nil {
  235. return fmt.Sprintf("task: non-zero exit (%v): %v", e.code, e.cause)
  236. }
  237. return fmt.Sprintf("task: non-zero exit (%v)", e.code)
  238. }
  239. func (e *exitError) ExitCode() int {
  240. return int(e.code)
  241. }
  242. func (e *exitError) Cause() error {
  243. return e.cause
  244. }