pull.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/pkg/api"
  29. "github.com/docker/compose-cli/pkg/progress"
  30. )
  31. func (s *composeService) Pull(ctx context.Context, project *types.Project, opts api.PullOptions) error {
  32. if opts.Quiet {
  33. return s.pull(ctx, project, opts)
  34. }
  35. return progress.Run(ctx, func(ctx context.Context) error {
  36. return s.pull(ctx, project, opts)
  37. })
  38. }
  39. func (s *composeService) pull(ctx context.Context, project *types.Project, opts api.PullOptions) error {
  40. info, err := s.apiClient.Info(ctx)
  41. if err != nil {
  42. return err
  43. }
  44. if info.IndexServerAddress == "" {
  45. info.IndexServerAddress = registry.IndexServer
  46. }
  47. w := progress.ContextWriter(ctx)
  48. eg, ctx := errgroup.WithContext(ctx)
  49. for _, srv := range project.Services {
  50. service := srv
  51. if service.Image == "" {
  52. w.Event(progress.Event{
  53. ID: service.Name,
  54. Status: progress.Done,
  55. Text: "Skipped",
  56. })
  57. continue
  58. }
  59. eg.Go(func() error {
  60. err := s.pullServiceImage(ctx, service, info, s.configFile, w, false)
  61. if err != nil {
  62. if !opts.IgnoreFailures {
  63. return err
  64. }
  65. w.TailMsgf("Pulling %s: %s", service.Name, err.Error())
  66. }
  67. return nil
  68. })
  69. }
  70. return eg.Wait()
  71. }
  72. func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer, quietPull bool) error {
  73. w.Event(progress.Event{
  74. ID: service.Name,
  75. Status: progress.Working,
  76. Text: "Pulling",
  77. })
  78. ref, err := reference.ParseNormalizedNamed(service.Image)
  79. if err != nil {
  80. return err
  81. }
  82. repoInfo, err := registry.ParseRepositoryInfo(ref)
  83. if err != nil {
  84. return err
  85. }
  86. key := repoInfo.Index.Name
  87. if repoInfo.Index.Official {
  88. key = info.IndexServerAddress
  89. }
  90. authConfig, err := configFile.GetAuthConfig(key)
  91. if err != nil {
  92. return err
  93. }
  94. buf, err := json.Marshal(authConfig)
  95. if err != nil {
  96. return err
  97. }
  98. stream, err := s.apiClient.ImagePull(ctx, service.Image, moby.ImagePullOptions{
  99. RegistryAuth: base64.URLEncoding.EncodeToString(buf),
  100. Platform: service.Platform,
  101. })
  102. if err != nil {
  103. w.Event(progress.Event{
  104. ID: service.Name,
  105. Status: progress.Error,
  106. Text: "Error",
  107. })
  108. return WrapCategorisedComposeError(err, PullFailure)
  109. }
  110. dec := json.NewDecoder(stream)
  111. for {
  112. var jm jsonmessage.JSONMessage
  113. if err := dec.Decode(&jm); err != nil {
  114. if err == io.EOF {
  115. break
  116. }
  117. return WrapCategorisedComposeError(err, PullFailure)
  118. }
  119. if jm.Error != nil {
  120. return WrapCategorisedComposeError(errors.New(jm.Error.Message), PullFailure)
  121. }
  122. if !quietPull {
  123. toPullProgressEvent(service.Name, jm, w)
  124. }
  125. }
  126. w.Event(progress.Event{
  127. ID: service.Name,
  128. Status: progress.Done,
  129. Text: "Pulled",
  130. })
  131. return nil
  132. }
  133. func (s *composeService) pullRequiredImages(ctx context.Context, project *types.Project, images map[string]string, quietPull bool) error {
  134. info, err := s.apiClient.Info(ctx)
  135. if err != nil {
  136. return err
  137. }
  138. if info.IndexServerAddress == "" {
  139. info.IndexServerAddress = registry.IndexServer
  140. }
  141. var needPull []types.ServiceConfig
  142. for _, service := range project.Services {
  143. if service.Image == "" {
  144. continue
  145. }
  146. switch service.PullPolicy {
  147. case "", types.PullPolicyMissing, types.PullPolicyIfNotPresent:
  148. if _, ok := images[service.Image]; ok {
  149. continue
  150. }
  151. case types.PullPolicyNever, types.PullPolicyBuild:
  152. continue
  153. case types.PullPolicyAlways:
  154. // force pull
  155. }
  156. needPull = append(needPull, service)
  157. }
  158. if len(needPull) == 0 {
  159. return nil
  160. }
  161. return progress.Run(ctx, func(ctx context.Context) error {
  162. w := progress.ContextWriter(ctx)
  163. eg, ctx := errgroup.WithContext(ctx)
  164. for _, service := range needPull {
  165. service := service
  166. eg.Go(func() error {
  167. err := s.pullServiceImage(ctx, service, info, s.configFile, w, quietPull)
  168. if err != nil && service.Build != nil {
  169. // image can be built, so we can ignore pull failure
  170. return nil
  171. }
  172. return err
  173. })
  174. }
  175. return eg.Wait()
  176. })
  177. }
  178. func toPullProgressEvent(parent string, jm jsonmessage.JSONMessage, w progress.Writer) {
  179. if jm.ID == "" || jm.Progress == nil {
  180. return
  181. }
  182. var (
  183. text string
  184. status = progress.Working
  185. )
  186. text = jm.Progress.String()
  187. if jm.Status == "Pull complete" ||
  188. jm.Status == "Already exists" ||
  189. strings.Contains(jm.Status, "Image is up to date") ||
  190. strings.Contains(jm.Status, "Downloaded newer image") {
  191. status = progress.Done
  192. }
  193. if jm.Error != nil {
  194. status = progress.Error
  195. text = jm.Error.Message
  196. }
  197. w.Event(progress.Event{
  198. ID: jm.ID,
  199. ParentID: parent,
  200. Text: jm.Status,
  201. Status: status,
  202. StatusText: text,
  203. })
  204. }