push.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "fmt"
  19. "io"
  20. "github.com/compose-spec/compose-go/types"
  21. "github.com/distribution/distribution/v3/reference"
  22. "github.com/docker/buildx/driver"
  23. moby "github.com/docker/docker/api/types"
  24. "github.com/docker/docker/pkg/jsonmessage"
  25. "github.com/docker/docker/registry"
  26. "github.com/pkg/errors"
  27. "golang.org/x/sync/errgroup"
  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. })
  38. }
  39. func (s *composeService) push(ctx context.Context, project *types.Project, options api.PushOptions) error {
  40. eg, ctx := errgroup.WithContext(ctx)
  41. info, err := s.apiClient().Info(ctx)
  42. if err != nil {
  43. return err
  44. }
  45. if info.IndexServerAddress == "" {
  46. info.IndexServerAddress = registry.IndexServer
  47. }
  48. w := progress.ContextWriter(ctx)
  49. for _, service := range project.Services {
  50. if service.Build == nil || service.Image == "" {
  51. w.Event(progress.Event{
  52. ID: service.Name,
  53. Status: progress.Done,
  54. Text: "Skipped",
  55. })
  56. continue
  57. }
  58. service := service
  59. eg.Go(func() error {
  60. err := s.pushServiceImage(ctx, service, info, s.configFile(), w, options.Quiet)
  61. if err != nil {
  62. if !options.IgnoreFailures {
  63. return err
  64. }
  65. w.TailMsgf("Pushing %s: %s", service.Name, err.Error())
  66. }
  67. return nil
  68. })
  69. }
  70. return eg.Wait()
  71. }
  72. func (s *composeService) pushServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer, quietPush bool) error {
  73. ref, err := reference.ParseNormalizedNamed(service.Image)
  74. if err != nil {
  75. return err
  76. }
  77. repoInfo, err := registry.ParseRepositoryInfo(ref)
  78. if err != nil {
  79. return err
  80. }
  81. key := repoInfo.Index.Name
  82. if repoInfo.Index.Official {
  83. key = info.IndexServerAddress
  84. }
  85. authConfig, err := configFile.GetAuthConfig(key)
  86. if err != nil {
  87. return err
  88. }
  89. buf, err := json.Marshal(authConfig)
  90. if err != nil {
  91. return err
  92. }
  93. stream, err := s.apiClient().ImagePush(ctx, service.Image, moby.ImagePushOptions{
  94. RegistryAuth: base64.URLEncoding.EncodeToString(buf),
  95. })
  96. if err != nil {
  97. return err
  98. }
  99. dec := json.NewDecoder(stream)
  100. for {
  101. var jm jsonmessage.JSONMessage
  102. if err := dec.Decode(&jm); err != nil {
  103. if err == io.EOF {
  104. break
  105. }
  106. return err
  107. }
  108. if jm.Error != nil {
  109. return errors.New(jm.Error.Message)
  110. }
  111. if !quietPush {
  112. toPushProgressEvent(service.Name, jm, w)
  113. }
  114. }
  115. return nil
  116. }
  117. func toPushProgressEvent(prefix string, jm jsonmessage.JSONMessage, w progress.Writer) {
  118. if jm.ID == "" {
  119. // skipped
  120. return
  121. }
  122. var (
  123. text string
  124. status = progress.Working
  125. )
  126. if jm.Status == "Pull complete" || jm.Status == "Already exists" {
  127. status = progress.Done
  128. }
  129. if jm.Error != nil {
  130. status = progress.Error
  131. text = jm.Error.Message
  132. }
  133. if jm.Progress != nil {
  134. text = jm.Progress.String()
  135. }
  136. w.Event(progress.Event{
  137. ID: fmt.Sprintf("Pushing %s: %s", prefix, jm.ID),
  138. Text: jm.Status,
  139. Status: status,
  140. StatusText: text,
  141. })
  142. }