push.go 4.3 KB

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