push.go 3.1 KB

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