pull.go 3.5 KB

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