pull.go 4.1 KB

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