pull.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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)
  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) 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. toPullProgressEvent(service.Name, jm, w)
  124. }
  125. w.Event(progress.Event{
  126. ID: service.Name,
  127. Status: progress.Done,
  128. Text: "Pulled",
  129. })
  130. return nil
  131. }
  132. func toPullProgressEvent(parent string, jm jsonmessage.JSONMessage, w progress.Writer) {
  133. if jm.ID == "" || jm.Progress == nil {
  134. return
  135. }
  136. var (
  137. text string
  138. status = progress.Working
  139. )
  140. text = jm.Progress.String()
  141. if jm.Status == "Pull complete" ||
  142. jm.Status == "Already exists" ||
  143. strings.Contains(jm.Status, "Image is up to date") ||
  144. strings.Contains(jm.Status, "Downloaded newer image") {
  145. status = progress.Done
  146. }
  147. if jm.Error != nil {
  148. status = progress.Error
  149. text = jm.Error.Message
  150. }
  151. w.Event(progress.Event{
  152. ID: jm.ID,
  153. ParentID: parent,
  154. Text: jm.Status,
  155. Status: status,
  156. StatusText: text,
  157. })
  158. }