pull.go 5.5 KB

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