pull.go 5.6 KB

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