push.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. return progress.Run(ctx, func(ctx context.Context) error {
  33. return s.push(ctx, project, options)
  34. })
  35. }
  36. func (s *composeService) push(ctx context.Context, project *types.Project, options api.PushOptions) error {
  37. eg, ctx := errgroup.WithContext(ctx)
  38. info, err := s.apiClient.Info(ctx)
  39. if err != nil {
  40. return err
  41. }
  42. if info.IndexServerAddress == "" {
  43. info.IndexServerAddress = registry.IndexServer
  44. }
  45. w := progress.ContextWriter(ctx)
  46. for _, service := range project.Services {
  47. if service.Build == nil || service.Image == "" {
  48. w.Event(progress.Event{
  49. ID: service.Name,
  50. Status: progress.Done,
  51. Text: "Skipped",
  52. })
  53. continue
  54. }
  55. service := service
  56. eg.Go(func() error {
  57. err := s.pushServiceImage(ctx, service, info, s.configFile, w)
  58. if err != nil {
  59. if !options.IgnoreFailures {
  60. return err
  61. }
  62. w.TailMsgf("Pushing %s: %s", service.Name, err.Error())
  63. }
  64. return nil
  65. })
  66. }
  67. return eg.Wait()
  68. }
  69. func (s *composeService) pushServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer) error {
  70. ref, err := reference.ParseNormalizedNamed(service.Image)
  71. if err != nil {
  72. return err
  73. }
  74. repoInfo, err := registry.ParseRepositoryInfo(ref)
  75. if err != nil {
  76. return err
  77. }
  78. key := repoInfo.Index.Name
  79. if repoInfo.Index.Official {
  80. key = info.IndexServerAddress
  81. }
  82. authConfig, err := configFile.GetAuthConfig(key)
  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, service.Image, moby.ImagePushOptions{
  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 err == io.EOF {
  101. break
  102. }
  103. return err
  104. }
  105. if jm.Error != nil {
  106. return errors.New(jm.Error.Message)
  107. }
  108. toPushProgressEvent(service.Name, jm, w)
  109. }
  110. return nil
  111. }
  112. func toPushProgressEvent(prefix string, jm jsonmessage.JSONMessage, w progress.Writer) {
  113. if jm.ID == "" {
  114. // skipped
  115. return
  116. }
  117. var (
  118. text string
  119. status = progress.Working
  120. )
  121. if jm.Status == "Pull complete" || jm.Status == "Already exists" {
  122. status = progress.Done
  123. }
  124. if jm.Error != nil {
  125. status = progress.Error
  126. text = jm.Error.Message
  127. }
  128. if jm.Progress != nil {
  129. text = jm.Progress.String()
  130. }
  131. w.Event(progress.Event{
  132. ID: fmt.Sprintf("Pushing %s: %s", prefix, jm.ID),
  133. Text: jm.Status,
  134. Status: status,
  135. StatusText: text,
  136. })
  137. }