push.go 4.6 KB

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