pull.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. "fmt"
  20. "io"
  21. "strings"
  22. "github.com/compose-spec/compose-go/types"
  23. "github.com/distribution/distribution/v3/reference"
  24. "github.com/docker/buildx/driver"
  25. cliconfig "github.com/docker/cli/cli/config"
  26. moby "github.com/docker/docker/api/types"
  27. "github.com/docker/docker/pkg/jsonmessage"
  28. "github.com/docker/docker/registry"
  29. "golang.org/x/sync/errgroup"
  30. "github.com/docker/compose-cli/api/compose"
  31. "github.com/docker/compose-cli/api/config"
  32. "github.com/docker/compose-cli/api/progress"
  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. // If IgnoreFailures we still want to show the error message
  65. w.Event(progress.Event{
  66. ID: fmt.Sprintf("Pulling %s:", service.Name),
  67. Text: fmt.Sprintf("%v", err),
  68. Status: progress.Error,
  69. StatusText: fmt.Sprintf("%s", err),
  70. })
  71. }
  72. return nil
  73. })
  74. }
  75. return eg.Wait()
  76. }
  77. func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer) error {
  78. w.Event(progress.Event{
  79. ID: service.Name,
  80. Status: progress.Working,
  81. Text: "Pulling",
  82. })
  83. ref, err := reference.ParseNormalizedNamed(service.Image)
  84. if err != nil {
  85. return err
  86. }
  87. repoInfo, err := registry.ParseRepositoryInfo(ref)
  88. if err != nil {
  89. return err
  90. }
  91. key := repoInfo.Index.Name
  92. if repoInfo.Index.Official {
  93. key = info.IndexServerAddress
  94. }
  95. authConfig, err := configFile.GetAuthConfig(key)
  96. if err != nil {
  97. return err
  98. }
  99. buf, err := json.Marshal(authConfig)
  100. if err != nil {
  101. return err
  102. }
  103. stream, err := s.apiClient.ImagePull(ctx, service.Image, moby.ImagePullOptions{
  104. RegistryAuth: base64.URLEncoding.EncodeToString(buf),
  105. Platform: service.Platform,
  106. })
  107. if err != nil {
  108. w.Event(progress.Event{
  109. ID: service.Name,
  110. Status: progress.Error,
  111. Text: "Error",
  112. })
  113. return err
  114. }
  115. dec := json.NewDecoder(stream)
  116. for {
  117. var jm jsonmessage.JSONMessage
  118. if err := dec.Decode(&jm); err != nil {
  119. if err == io.EOF {
  120. break
  121. }
  122. return err
  123. }
  124. if jm.Error != nil {
  125. return errors.New(jm.Error.Message)
  126. }
  127. toPullProgressEvent(service.Name, jm, w)
  128. }
  129. w.Event(progress.Event{
  130. ID: service.Name,
  131. Status: progress.Done,
  132. Text: "Pulled",
  133. })
  134. return nil
  135. }
  136. func toPullProgressEvent(parent string, jm jsonmessage.JSONMessage, w progress.Writer) {
  137. if jm.ID == "" || jm.Progress == nil {
  138. return
  139. }
  140. var (
  141. text string
  142. status = progress.Working
  143. )
  144. text = jm.Progress.String()
  145. if jm.Status == "Pull complete" ||
  146. jm.Status == "Already exists" ||
  147. strings.Contains(jm.Status, "Image is up to date") ||
  148. strings.Contains(jm.Status, "Downloaded newer image") {
  149. status = progress.Done
  150. }
  151. if jm.Error != nil {
  152. status = progress.Error
  153. text = jm.Error.Message
  154. }
  155. w.Event(progress.Event{
  156. ID: jm.ID,
  157. ParentID: parent,
  158. Text: jm.Status,
  159. Status: status,
  160. StatusText: text,
  161. })
  162. }