push.go 4.7 KB

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