pull.go 3.6 KB

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