push.go 4.5 KB

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