push.go 4.5 KB

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