push.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "fmt"
  19. "io"
  20. "github.com/compose-spec/compose-go/types"
  21. "github.com/distribution/distribution/v3/reference"
  22. "github.com/docker/buildx/driver"
  23. moby "github.com/docker/docker/api/types"
  24. "github.com/docker/docker/pkg/jsonmessage"
  25. "github.com/docker/docker/registry"
  26. "github.com/pkg/errors"
  27. "golang.org/x/sync/errgroup"
  28. "github.com/docker/compose/v2/pkg/api"
  29. "github.com/docker/compose/v2/pkg/progress"
  30. )
  31. func (s *composeService) Push(ctx context.Context, project *types.Project, options api.PushOptions) error {
  32. if options.Quiet {
  33. return s.push(ctx, project, options)
  34. }
  35. return progress.RunWithTitle(ctx, func(ctx context.Context) error {
  36. return s.push(ctx, project, options)
  37. }, s.stdinfo(), "Pushing")
  38. }
  39. func (s *composeService) push(ctx context.Context, project *types.Project, options api.PushOptions) error {
  40. eg, ctx := errgroup.WithContext(ctx)
  41. eg.SetLimit(s.maxConcurrency)
  42. info, err := s.apiClient().Info(ctx)
  43. if err != nil {
  44. return err
  45. }
  46. if info.IndexServerAddress == "" {
  47. info.IndexServerAddress = registry.IndexServer
  48. }
  49. w := progress.ContextWriter(ctx)
  50. for _, service := range project.Services {
  51. if service.Build == nil || service.Image == "" {
  52. w.Event(progress.Event{
  53. ID: service.Name,
  54. Status: progress.Done,
  55. Text: "Skipped",
  56. })
  57. continue
  58. }
  59. service := service
  60. eg.Go(func() error {
  61. err := s.pushServiceImage(ctx, service, info, s.configFile(), w, options.Quiet)
  62. if err != nil {
  63. if !options.IgnoreFailures {
  64. return err
  65. }
  66. w.TailMsgf("Pushing %s: %s", service.Name, err.Error())
  67. }
  68. return nil
  69. })
  70. }
  71. return eg.Wait()
  72. }
  73. func (s *composeService) pushServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer, quietPush bool) error {
  74. ref, err := reference.ParseNormalizedNamed(service.Image)
  75. if err != nil {
  76. return err
  77. }
  78. repoInfo, err := registry.ParseRepositoryInfo(ref)
  79. if err != nil {
  80. return err
  81. }
  82. key := repoInfo.Index.Name
  83. if repoInfo.Index.Official {
  84. key = info.IndexServerAddress
  85. }
  86. authConfig, err := configFile.GetAuthConfig(key)
  87. if err != nil {
  88. return err
  89. }
  90. buf, err := json.Marshal(authConfig)
  91. if err != nil {
  92. return err
  93. }
  94. stream, err := s.apiClient().ImagePush(ctx, service.Image, moby.ImagePushOptions{
  95. RegistryAuth: base64.URLEncoding.EncodeToString(buf),
  96. })
  97. if err != nil {
  98. return err
  99. }
  100. dec := json.NewDecoder(stream)
  101. for {
  102. var jm jsonmessage.JSONMessage
  103. if err := dec.Decode(&jm); err != nil {
  104. if err == io.EOF {
  105. break
  106. }
  107. return err
  108. }
  109. if jm.Error != nil {
  110. return errors.New(jm.Error.Message)
  111. }
  112. if !quietPush {
  113. toPushProgressEvent(service.Name, jm, w)
  114. }
  115. }
  116. return nil
  117. }
  118. func toPushProgressEvent(prefix string, jm jsonmessage.JSONMessage, w progress.Writer) {
  119. if jm.ID == "" {
  120. // skipped
  121. return
  122. }
  123. var (
  124. text string
  125. status = progress.Working
  126. total int64
  127. current int64
  128. percent int
  129. )
  130. if jm.Status == "Pushed" || jm.Status == "Already exists" {
  131. status = progress.Done
  132. percent = 100
  133. }
  134. if jm.Error != nil {
  135. status = progress.Error
  136. text = jm.Error.Message
  137. }
  138. if jm.Progress != nil {
  139. text = jm.Progress.String()
  140. if jm.Progress.Total != 0 {
  141. current = jm.Progress.Current
  142. total = jm.Progress.Total
  143. if jm.Progress.Total > 0 {
  144. percent = int(jm.Progress.Current * 100 / jm.Progress.Total)
  145. }
  146. }
  147. }
  148. w.Event(progress.Event{
  149. ID: fmt.Sprintf("Pushing %s: %s", prefix, jm.ID),
  150. Text: jm.Status,
  151. Status: status,
  152. Current: current,
  153. Total: total,
  154. Percent: percent,
  155. StatusText: text,
  156. })
  157. }