pull.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. cliconfig "github.com/docker/cli/cli/config"
  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-cli/api/compose"
  30. "github.com/docker/compose-cli/api/config"
  31. "github.com/docker/compose-cli/api/progress"
  32. "github.com/docker/compose-cli/cli/metrics"
  33. )
  34. func (s *composeService) Pull(ctx context.Context, project *types.Project, opts compose.PullOptions) error {
  35. configFile, err := cliconfig.Load(config.Dir())
  36. if err != nil {
  37. return err
  38. }
  39. info, err := s.apiClient.Info(ctx)
  40. if err != nil {
  41. return err
  42. }
  43. if info.IndexServerAddress == "" {
  44. info.IndexServerAddress = registry.IndexServer
  45. }
  46. w := progress.ContextWriter(ctx)
  47. eg, ctx := errgroup.WithContext(ctx)
  48. for _, srv := range project.Services {
  49. service := srv
  50. if service.Image == "" {
  51. w.Event(progress.Event{
  52. ID: service.Name,
  53. Status: progress.Done,
  54. Text: "Skipped",
  55. })
  56. continue
  57. }
  58. eg.Go(func() error {
  59. err := s.pullServiceImage(ctx, service, info, configFile, w)
  60. if err != nil {
  61. if !opts.IgnoreFailures {
  62. return err
  63. }
  64. w.TailMsgf("Pulling %s: %s", service.Name, err.Error())
  65. }
  66. return nil
  67. })
  68. }
  69. return eg.Wait()
  70. }
  71. func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer) error {
  72. w.Event(progress.Event{
  73. ID: service.Name,
  74. Status: progress.Working,
  75. Text: "Pulling",
  76. })
  77. ref, err := reference.ParseNormalizedNamed(service.Image)
  78. if err != nil {
  79. return err
  80. }
  81. repoInfo, err := registry.ParseRepositoryInfo(ref)
  82. if err != nil {
  83. return err
  84. }
  85. key := repoInfo.Index.Name
  86. if repoInfo.Index.Official {
  87. key = info.IndexServerAddress
  88. }
  89. authConfig, err := configFile.GetAuthConfig(key)
  90. if err != nil {
  91. return err
  92. }
  93. buf, err := json.Marshal(authConfig)
  94. if err != nil {
  95. return err
  96. }
  97. stream, err := s.apiClient.ImagePull(ctx, service.Image, moby.ImagePullOptions{
  98. RegistryAuth: base64.URLEncoding.EncodeToString(buf),
  99. Platform: service.Platform,
  100. })
  101. if err != nil {
  102. w.Event(progress.Event{
  103. ID: service.Name,
  104. Status: progress.Error,
  105. Text: "Error",
  106. })
  107. return metrics.WrapCategorisedComposeError(err, metrics.PullFailure)
  108. }
  109. dec := json.NewDecoder(stream)
  110. for {
  111. var jm jsonmessage.JSONMessage
  112. if err := dec.Decode(&jm); err != nil {
  113. if err == io.EOF {
  114. break
  115. }
  116. return metrics.WrapCategorisedComposeError(err, metrics.PullFailure)
  117. }
  118. if jm.Error != nil {
  119. return metrics.WrapCategorisedComposeError(errors.New(jm.Error.Message), metrics.PullFailure)
  120. }
  121. toPullProgressEvent(service.Name, jm, w)
  122. }
  123. w.Event(progress.Event{
  124. ID: service.Name,
  125. Status: progress.Done,
  126. Text: "Pulled",
  127. })
  128. return nil
  129. }
  130. func toPullProgressEvent(parent string, jm jsonmessage.JSONMessage, w progress.Writer) {
  131. if jm.ID == "" || jm.Progress == nil {
  132. return
  133. }
  134. var (
  135. text string
  136. status = progress.Working
  137. )
  138. text = jm.Progress.String()
  139. if jm.Status == "Pull complete" ||
  140. jm.Status == "Already exists" ||
  141. strings.Contains(jm.Status, "Image is up to date") ||
  142. strings.Contains(jm.Status, "Downloaded newer image") {
  143. status = progress.Done
  144. }
  145. if jm.Error != nil {
  146. status = progress.Error
  147. text = jm.Error.Message
  148. }
  149. w.Event(progress.Event{
  150. ID: jm.ID,
  151. ParentID: parent,
  152. Text: jm.Status,
  153. Status: status,
  154. StatusText: text,
  155. })
  156. }