push.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/docker/buildx/driver"
  21. "github.com/docker/compose-cli/config"
  22. "github.com/docker/compose-cli/progress"
  23. "github.com/compose-spec/compose-go/types"
  24. cliconfig "github.com/docker/cli/cli/config"
  25. "github.com/docker/distribution/reference"
  26. moby "github.com/docker/docker/api/types"
  27. "github.com/docker/docker/pkg/jsonmessage"
  28. "github.com/docker/docker/registry"
  29. "github.com/pkg/errors"
  30. "golang.org/x/sync/errgroup"
  31. )
  32. func (s *composeService) Push(ctx context.Context, project *types.Project) error {
  33. configFile, err := cliconfig.Load(config.Dir(ctx))
  34. if err != nil {
  35. return err
  36. }
  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. return s.pullServiceImage(ctx, service, info, configFile, w)
  58. })
  59. }
  60. return eg.Wait()
  61. }
  62. func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer) error {
  63. ref, err := reference.ParseNormalizedNamed(service.Image)
  64. if err != nil {
  65. return err
  66. }
  67. repoInfo, err := registry.ParseRepositoryInfo(ref)
  68. if err != nil {
  69. return err
  70. }
  71. key := repoInfo.Index.Name
  72. if repoInfo.Index.Official {
  73. key = info.IndexServerAddress
  74. }
  75. authConfig, err := configFile.GetAuthConfig(key)
  76. if err != nil {
  77. return err
  78. }
  79. buf, err := json.Marshal(authConfig)
  80. if err != nil {
  81. return err
  82. }
  83. stream, err := s.apiClient.ImagePush(ctx, service.Image, moby.ImagePushOptions{
  84. RegistryAuth: base64.URLEncoding.EncodeToString(buf),
  85. })
  86. if err != nil {
  87. return err
  88. }
  89. dec := json.NewDecoder(stream)
  90. for {
  91. var jm jsonmessage.JSONMessage
  92. if err := dec.Decode(&jm); err != nil {
  93. if err == io.EOF {
  94. break
  95. }
  96. return err
  97. }
  98. if jm.Error != nil {
  99. return errors.New(jm.Error.Message)
  100. }
  101. toPushProgressEvent("Pushing "+service.Name, jm, w)
  102. }
  103. return nil
  104. }
  105. func toPushProgressEvent(prefix string, jm jsonmessage.JSONMessage, w progress.Writer) {
  106. if jm.ID == "" {
  107. // skipped
  108. return
  109. }
  110. var (
  111. text string
  112. status = progress.Working
  113. )
  114. if jm.Status == "Pull complete" || jm.Status == "Already exists" {
  115. status = progress.Done
  116. }
  117. if jm.Error != nil {
  118. status = progress.Error
  119. text = jm.Error.Message
  120. }
  121. if jm.Progress != nil {
  122. text = jm.Progress.String()
  123. }
  124. w.Event(progress.Event{
  125. ID: fmt.Sprintf("Pushing %s: %s", prefix, jm.ID),
  126. Text: jm.Status,
  127. Status: status,
  128. StatusText: text,
  129. })
  130. }