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