push.go 4.4 KB

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