pull.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. "fmt"
  20. "io"
  21. "strings"
  22. "github.com/compose-spec/compose-go/types"
  23. "github.com/distribution/distribution/v3/reference"
  24. "github.com/docker/buildx/driver"
  25. moby "github.com/docker/docker/api/types"
  26. "github.com/docker/docker/pkg/jsonmessage"
  27. "github.com/docker/docker/registry"
  28. "golang.org/x/sync/errgroup"
  29. "github.com/docker/compose/v2/pkg/api"
  30. "github.com/docker/compose/v2/pkg/progress"
  31. )
  32. func (s *composeService) Pull(ctx context.Context, project *types.Project, opts api.PullOptions) error {
  33. if opts.Quiet {
  34. return s.pull(ctx, project, opts)
  35. }
  36. return progress.Run(ctx, func(ctx context.Context) error {
  37. return s.pull(ctx, project, opts)
  38. })
  39. }
  40. func (s *composeService) pull(ctx context.Context, project *types.Project, opts api.PullOptions) error {
  41. info, err := s.apiClient.Info(ctx)
  42. if err != nil {
  43. return err
  44. }
  45. if info.IndexServerAddress == "" {
  46. info.IndexServerAddress = registry.IndexServer
  47. }
  48. w := progress.ContextWriter(ctx)
  49. eg, ctx := errgroup.WithContext(ctx)
  50. var mustBuild []string
  51. images, err := s.getLocalImagesDigests(ctx, project)
  52. if err != nil {
  53. return err
  54. }
  55. imagesBeingPulled := map[string]string{}
  56. for _, service := range project.Services {
  57. service := service
  58. if service.Image == "" {
  59. w.Event(progress.Event{
  60. ID: service.Name,
  61. Status: progress.Done,
  62. Text: "Skipped - No image to be pulled",
  63. })
  64. continue
  65. }
  66. if _, ok := images[service.Image]; ok {
  67. w.Event(progress.Event{
  68. ID: service.Name,
  69. Status: progress.Done,
  70. Text: "Skipped - Image is already present locally",
  71. })
  72. continue
  73. }
  74. if s, ok := imagesBeingPulled[service.Image]; ok {
  75. w.Event(progress.Event{
  76. ID: service.Name,
  77. Status: progress.Done,
  78. Text: fmt.Sprintf("Skipped - Image is already being pulled by %v", s),
  79. })
  80. continue
  81. }
  82. imagesBeingPulled[service.Image] = service.Name
  83. eg.Go(func() error {
  84. err := s.pullServiceImage(ctx, service, info, s.configFile, w, false)
  85. if err != nil {
  86. if !opts.IgnoreFailures {
  87. if service.Build != nil {
  88. mustBuild = append(mustBuild, service.Name)
  89. }
  90. return err
  91. }
  92. w.TailMsgf("Pulling %s: %s", service.Name, err.Error())
  93. }
  94. return nil
  95. })
  96. }
  97. err = eg.Wait()
  98. if !opts.IgnoreFailures && len(mustBuild) > 0 {
  99. w.TailMsgf("WARNING: Some service image(s) must be built from source by running:\n docker compose build %s", strings.Join(mustBuild, " "))
  100. }
  101. return err
  102. }
  103. func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer, quietPull bool) error {
  104. w.Event(progress.Event{
  105. ID: service.Name,
  106. Status: progress.Working,
  107. Text: "Pulling",
  108. })
  109. ref, err := reference.ParseNormalizedNamed(service.Image)
  110. if err != nil {
  111. return err
  112. }
  113. repoInfo, err := registry.ParseRepositoryInfo(ref)
  114. if err != nil {
  115. return err
  116. }
  117. key := repoInfo.Index.Name
  118. if repoInfo.Index.Official {
  119. key = info.IndexServerAddress
  120. }
  121. authConfig, err := configFile.GetAuthConfig(key)
  122. if err != nil {
  123. return err
  124. }
  125. buf, err := json.Marshal(authConfig)
  126. if err != nil {
  127. return err
  128. }
  129. stream, err := s.apiClient.ImagePull(ctx, service.Image, moby.ImagePullOptions{
  130. RegistryAuth: base64.URLEncoding.EncodeToString(buf),
  131. Platform: service.Platform,
  132. })
  133. if err != nil {
  134. w.Event(progress.Event{
  135. ID: service.Name,
  136. Status: progress.Error,
  137. Text: "Error",
  138. })
  139. return WrapCategorisedComposeError(err, PullFailure)
  140. }
  141. dec := json.NewDecoder(stream)
  142. for {
  143. var jm jsonmessage.JSONMessage
  144. if err := dec.Decode(&jm); err != nil {
  145. if err == io.EOF {
  146. break
  147. }
  148. return WrapCategorisedComposeError(err, PullFailure)
  149. }
  150. if jm.Error != nil {
  151. return WrapCategorisedComposeError(errors.New(jm.Error.Message), PullFailure)
  152. }
  153. if !quietPull {
  154. toPullProgressEvent(service.Name, jm, w)
  155. }
  156. }
  157. w.Event(progress.Event{
  158. ID: service.Name,
  159. Status: progress.Done,
  160. Text: "Pulled",
  161. })
  162. return nil
  163. }
  164. func (s *composeService) pullRequiredImages(ctx context.Context, project *types.Project, images map[string]string, quietPull bool) error {
  165. info, err := s.apiClient.Info(ctx)
  166. if err != nil {
  167. return err
  168. }
  169. if info.IndexServerAddress == "" {
  170. info.IndexServerAddress = registry.IndexServer
  171. }
  172. var needPull []types.ServiceConfig
  173. for _, service := range project.Services {
  174. if service.Image == "" {
  175. continue
  176. }
  177. switch service.PullPolicy {
  178. case "", types.PullPolicyMissing, types.PullPolicyIfNotPresent:
  179. if _, ok := images[service.Image]; ok {
  180. continue
  181. }
  182. case types.PullPolicyNever, types.PullPolicyBuild:
  183. continue
  184. case types.PullPolicyAlways:
  185. // force pull
  186. }
  187. needPull = append(needPull, service)
  188. }
  189. if len(needPull) == 0 {
  190. return nil
  191. }
  192. return progress.Run(ctx, func(ctx context.Context) error {
  193. w := progress.ContextWriter(ctx)
  194. eg, ctx := errgroup.WithContext(ctx)
  195. for _, service := range needPull {
  196. service := service
  197. eg.Go(func() error {
  198. err := s.pullServiceImage(ctx, service, info, s.configFile, w, quietPull)
  199. if err != nil && service.Build != nil {
  200. // image can be built, so we can ignore pull failure
  201. return nil
  202. }
  203. return err
  204. })
  205. }
  206. return eg.Wait()
  207. })
  208. }
  209. func toPullProgressEvent(parent string, jm jsonmessage.JSONMessage, w progress.Writer) {
  210. if jm.ID == "" || jm.Progress == nil {
  211. return
  212. }
  213. var (
  214. text string
  215. status = progress.Working
  216. )
  217. text = jm.Progress.String()
  218. if jm.Status == "Pull complete" ||
  219. jm.Status == "Already exists" ||
  220. strings.Contains(jm.Status, "Image is up to date") ||
  221. strings.Contains(jm.Status, "Downloaded newer image") {
  222. status = progress.Done
  223. }
  224. if jm.Error != nil {
  225. status = progress.Error
  226. text = jm.Error.Message
  227. }
  228. w.Event(progress.Event{
  229. ID: jm.ID,
  230. ParentID: parent,
  231. Text: jm.Status,
  232. Status: status,
  233. StatusText: text,
  234. })
  235. }