pull.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "encoding/base64"
  17. "encoding/json"
  18. "errors"
  19. "io"
  20. "strings"
  21. "github.com/compose-spec/compose-go/types"
  22. "github.com/distribution/distribution/v3/reference"
  23. "github.com/docker/buildx/driver"
  24. moby "github.com/docker/docker/api/types"
  25. "github.com/docker/docker/pkg/jsonmessage"
  26. "github.com/docker/docker/registry"
  27. "golang.org/x/sync/errgroup"
  28. "github.com/docker/compose/v2/pkg/api"
  29. "github.com/docker/compose/v2/pkg/progress"
  30. )
  31. func (s *composeService) Pull(ctx context.Context, project *types.Project, options api.PullOptions) error {
  32. if options.Quiet {
  33. return s.pull(ctx, project, options)
  34. }
  35. return progress.Run(ctx, func(ctx context.Context) error {
  36. return s.pull(ctx, project, options)
  37. })
  38. }
  39. func (s *composeService) pull(ctx context.Context, project *types.Project, opts api.PullOptions) error {
  40. info, err := s.apiClient().Info(ctx)
  41. if err != nil {
  42. return err
  43. }
  44. if info.IndexServerAddress == "" {
  45. info.IndexServerAddress = registry.IndexServer
  46. }
  47. images, err := s.getLocalImagesDigests(ctx, project)
  48. if err != nil {
  49. return err
  50. }
  51. w := progress.ContextWriter(ctx)
  52. eg, ctx := errgroup.WithContext(ctx)
  53. var mustBuild []string
  54. for _, service := range project.Services {
  55. service := service
  56. if service.Image == "" {
  57. w.Event(progress.Event{
  58. ID: service.Name,
  59. Status: progress.Done,
  60. Text: "Skipped",
  61. })
  62. continue
  63. }
  64. switch service.PullPolicy {
  65. case types.PullPolicyNever, types.PullPolicyBuild:
  66. w.Event(progress.Event{
  67. ID: service.Name,
  68. Status: progress.Done,
  69. Text: "Skipped",
  70. })
  71. continue
  72. case types.PullPolicyMissing, types.PullPolicyIfNotPresent:
  73. if _, ok := images[service.Image]; ok {
  74. w.Event(progress.Event{
  75. ID: service.Name,
  76. Status: progress.Done,
  77. Text: "Exists",
  78. })
  79. continue
  80. }
  81. }
  82. eg.Go(func() error {
  83. err := s.pullServiceImage(ctx, service, info, s.configFile(), w, false)
  84. if err != nil {
  85. if !opts.IgnoreFailures {
  86. if service.Build != nil {
  87. mustBuild = append(mustBuild, service.Name)
  88. }
  89. return err
  90. }
  91. w.TailMsgf("Pulling %s: %s", service.Name, err.Error())
  92. }
  93. return nil
  94. })
  95. }
  96. err = eg.Wait()
  97. if !opts.IgnoreFailures && len(mustBuild) > 0 {
  98. w.TailMsgf("WARNING: Some service image(s) must be built from source by running:\n docker compose build %s", strings.Join(mustBuild, " "))
  99. }
  100. return err
  101. }
  102. func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer, quietPull bool) error {
  103. w.Event(progress.Event{
  104. ID: service.Name,
  105. Status: progress.Working,
  106. Text: "Pulling",
  107. })
  108. ref, err := reference.ParseNormalizedNamed(service.Image)
  109. if err != nil {
  110. return err
  111. }
  112. repoInfo, err := registry.ParseRepositoryInfo(ref)
  113. if err != nil {
  114. return err
  115. }
  116. key := repoInfo.Index.Name
  117. if repoInfo.Index.Official {
  118. key = info.IndexServerAddress
  119. }
  120. authConfig, err := configFile.GetAuthConfig(key)
  121. if err != nil {
  122. return err
  123. }
  124. buf, err := json.Marshal(authConfig)
  125. if err != nil {
  126. return err
  127. }
  128. stream, err := s.apiClient().ImagePull(ctx, service.Image, moby.ImagePullOptions{
  129. RegistryAuth: base64.URLEncoding.EncodeToString(buf),
  130. Platform: service.Platform,
  131. })
  132. if err != nil {
  133. w.Event(progress.Event{
  134. ID: service.Name,
  135. Status: progress.Error,
  136. Text: "Error",
  137. })
  138. return WrapCategorisedComposeError(err, PullFailure)
  139. }
  140. dec := json.NewDecoder(stream)
  141. for {
  142. var jm jsonmessage.JSONMessage
  143. if err := dec.Decode(&jm); err != nil {
  144. if err == io.EOF {
  145. break
  146. }
  147. return WrapCategorisedComposeError(err, PullFailure)
  148. }
  149. if jm.Error != nil {
  150. return WrapCategorisedComposeError(errors.New(jm.Error.Message), PullFailure)
  151. }
  152. if !quietPull {
  153. toPullProgressEvent(service.Name, jm, w)
  154. }
  155. }
  156. w.Event(progress.Event{
  157. ID: service.Name,
  158. Status: progress.Done,
  159. Text: "Pulled",
  160. })
  161. return nil
  162. }
  163. func (s *composeService) pullRequiredImages(ctx context.Context, project *types.Project, images map[string]string, quietPull bool) error {
  164. info, err := s.apiClient().Info(ctx)
  165. if err != nil {
  166. return err
  167. }
  168. if info.IndexServerAddress == "" {
  169. info.IndexServerAddress = registry.IndexServer
  170. }
  171. var needPull []types.ServiceConfig
  172. for _, service := range project.Services {
  173. if service.Image == "" {
  174. continue
  175. }
  176. switch service.PullPolicy {
  177. case "", types.PullPolicyMissing, types.PullPolicyIfNotPresent:
  178. if _, ok := images[service.Image]; ok {
  179. continue
  180. }
  181. case types.PullPolicyNever, types.PullPolicyBuild:
  182. continue
  183. case types.PullPolicyAlways:
  184. // force pull
  185. }
  186. needPull = append(needPull, service)
  187. }
  188. if len(needPull) == 0 {
  189. return nil
  190. }
  191. return progress.Run(ctx, func(ctx context.Context) error {
  192. w := progress.ContextWriter(ctx)
  193. eg, ctx := errgroup.WithContext(ctx)
  194. for _, service := range needPull {
  195. service := service
  196. eg.Go(func() error {
  197. err := s.pullServiceImage(ctx, service, info, s.configFile(), w, quietPull)
  198. if err != nil && service.Build != nil {
  199. // image can be built, so we can ignore pull failure
  200. return nil
  201. }
  202. return err
  203. })
  204. }
  205. return eg.Wait()
  206. })
  207. }
  208. func toPullProgressEvent(parent string, jm jsonmessage.JSONMessage, w progress.Writer) {
  209. if jm.ID == "" || jm.Progress == nil {
  210. return
  211. }
  212. var (
  213. text string
  214. status = progress.Working
  215. )
  216. text = jm.Progress.String()
  217. if jm.Status == "Pull complete" ||
  218. jm.Status == "Already exists" ||
  219. strings.Contains(jm.Status, "Image is up to date") ||
  220. strings.Contains(jm.Status, "Downloaded newer image") {
  221. status = progress.Done
  222. }
  223. if jm.Error != nil {
  224. status = progress.Error
  225. text = jm.Error.Message
  226. }
  227. w.Event(progress.Event{
  228. ID: jm.ID,
  229. ParentID: parent,
  230. Text: jm.Status,
  231. Status: status,
  232. StatusText: text,
  233. })
  234. }