push.go 4.3 KB

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