pull.go 5.8 KB

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