push.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(ctx))
  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.Event(progress.Event{
  64. ID: fmt.Sprintf("Pushing %s:", service.Name),
  65. Text: fmt.Sprintf("%v", err),
  66. Status: progress.Error,
  67. StatusText: fmt.Sprintf("%s", err),
  68. })
  69. }
  70. return nil
  71. })
  72. }
  73. return eg.Wait()
  74. }
  75. func (s *composeService) pushServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer) error {
  76. ref, err := reference.ParseNormalizedNamed(service.Image)
  77. if err != nil {
  78. return err
  79. }
  80. repoInfo, err := registry.ParseRepositoryInfo(ref)
  81. if err != nil {
  82. return err
  83. }
  84. key := repoInfo.Index.Name
  85. if repoInfo.Index.Official {
  86. key = info.IndexServerAddress
  87. }
  88. authConfig, err := configFile.GetAuthConfig(key)
  89. if err != nil {
  90. return err
  91. }
  92. buf, err := json.Marshal(authConfig)
  93. if err != nil {
  94. return err
  95. }
  96. stream, err := s.apiClient.ImagePush(ctx, service.Image, moby.ImagePushOptions{
  97. RegistryAuth: base64.URLEncoding.EncodeToString(buf),
  98. })
  99. if err != nil {
  100. return err
  101. }
  102. dec := json.NewDecoder(stream)
  103. for {
  104. var jm jsonmessage.JSONMessage
  105. if err := dec.Decode(&jm); err != nil {
  106. if err == io.EOF {
  107. break
  108. }
  109. return err
  110. }
  111. if jm.Error != nil {
  112. return errors.New(jm.Error.Message)
  113. }
  114. toPushProgressEvent(service.Name, jm, w)
  115. }
  116. return nil
  117. }
  118. func toPushProgressEvent(prefix string, jm jsonmessage.JSONMessage, w progress.Writer) {
  119. if jm.ID == "" {
  120. // skipped
  121. return
  122. }
  123. var (
  124. text string
  125. status = progress.Working
  126. )
  127. if jm.Status == "Pull complete" || jm.Status == "Already exists" {
  128. status = progress.Done
  129. }
  130. if jm.Error != nil {
  131. status = progress.Error
  132. text = jm.Error.Message
  133. }
  134. if jm.Progress != nil {
  135. text = jm.Progress.String()
  136. }
  137. w.Event(progress.Event{
  138. ID: fmt.Sprintf("Pushing %s: %s", prefix, jm.ID),
  139. Text: jm.Status,
  140. Status: status,
  141. StatusText: text,
  142. })
  143. }