2020-12-08 11:53:36 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2023-09-26 00:57:12 +02:00
|
|
|
"errors"
|
2020-12-08 11:53:36 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-11-17 16:09:12 +01:00
|
|
|
"strings"
|
2020-12-08 11:53:36 +01:00
|
|
|
|
2023-11-08 10:19:24 +01:00
|
|
|
"github.com/compose-spec/compose-go/v2/types"
|
2023-08-31 09:13:58 +02:00
|
|
|
"github.com/distribution/reference"
|
2021-02-18 18:24:52 +01:00
|
|
|
"github.com/docker/buildx/driver"
|
2024-03-22 11:42:35 +01:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2023-12-05 10:59:26 +01:00
|
|
|
"github.com/docker/docker/api/types/system"
|
2020-12-08 11:53:36 +01:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2021-02-18 18:24:52 +01:00
|
|
|
|
2021-08-31 18:53:24 +02:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
|
|
|
"github.com/docker/compose/v2/pkg/progress"
|
2020-12-08 11:53:36 +01:00
|
|
|
)
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (s *composeService) Push(ctx context.Context, project *types.Project, options api.PushOptions) error {
|
2022-02-13 22:40:54 +05:30
|
|
|
if options.Quiet {
|
|
|
|
return s.push(ctx, project, options)
|
|
|
|
}
|
2023-05-02 20:15:35 +02:00
|
|
|
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
|
2021-06-03 13:57:56 +02:00
|
|
|
return s.push(ctx, project, options)
|
2023-05-11 18:45:00 +02:00
|
|
|
}, s.stdinfo(), "Pushing")
|
2021-06-03 13:57:56 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (s *composeService) push(ctx context.Context, project *types.Project, options api.PushOptions) error {
|
2020-12-08 11:53:36 +01:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
2022-11-30 12:08:26 +01:00
|
|
|
eg.SetLimit(s.maxConcurrency)
|
2020-12-08 11:53:36 +01:00
|
|
|
|
2022-02-23 11:28:56 +01:00
|
|
|
info, err := s.apiClient().Info(ctx)
|
2020-12-08 11:53:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IndexServerAddress == "" {
|
|
|
|
info.IndexServerAddress = registry.IndexServer
|
|
|
|
}
|
|
|
|
|
2021-01-04 16:08:27 +01:00
|
|
|
w := progress.ContextWriter(ctx)
|
2020-12-08 11:53:36 +01:00
|
|
|
for _, service := range project.Services {
|
2021-01-04 16:08:27 +01:00
|
|
|
if service.Build == nil || service.Image == "" {
|
2025-05-12 10:29:37 +02:00
|
|
|
if options.ImageMandatory && service.Image == "" && service.Provider == nil {
|
2024-10-25 16:01:57 -04:00
|
|
|
return fmt.Errorf("%q attribute is mandatory to push an image for service %q", "service.image", service.Name)
|
2024-10-18 09:44:14 +02:00
|
|
|
}
|
2021-01-04 16:08:27 +01:00
|
|
|
w.Event(progress.Event{
|
|
|
|
ID: service.Name,
|
|
|
|
Status: progress.Done,
|
|
|
|
Text: "Skipped",
|
|
|
|
})
|
2020-12-08 11:53:36 +01:00
|
|
|
continue
|
|
|
|
}
|
2023-11-17 16:09:12 +01:00
|
|
|
tags := []string{service.Image}
|
|
|
|
if service.Build != nil {
|
|
|
|
tags = append(tags, service.Build.Tags...)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
eg.Go(func() error {
|
|
|
|
err := s.pushServiceImage(ctx, tag, info, s.configFile(), w, options.Quiet)
|
|
|
|
if err != nil {
|
|
|
|
if !options.IgnoreFailures {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.TailMsgf("Pushing %s: %s", service.Name, err.Error())
|
2021-03-05 13:20:13 +01:00
|
|
|
}
|
2023-11-17 16:09:12 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2021-01-04 16:08:27 +01:00
|
|
|
}
|
|
|
|
return eg.Wait()
|
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
|
2023-12-05 10:59:26 +01:00
|
|
|
func (s *composeService) pushServiceImage(ctx context.Context, tag string, info system.Info, configFile driver.Auth, w progress.Writer, quietPush bool) error {
|
2023-11-17 16:09:12 +01:00
|
|
|
ref, err := reference.ParseNormalizedNamed(tag)
|
2021-01-04 16:08:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
|
2021-01-04 16:08:27 +01:00
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
|
2021-01-04 16:08:27 +01:00
|
|
|
key := repoInfo.Index.Name
|
|
|
|
if repoInfo.Index.Official {
|
|
|
|
key = info.IndexServerAddress
|
|
|
|
}
|
|
|
|
authConfig, err := configFile.GetAuthConfig(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
|
2021-01-04 16:08:27 +01:00
|
|
|
buf, err := json.Marshal(authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
|
2024-03-22 11:42:35 +01:00
|
|
|
stream, err := s.apiClient().ImagePush(ctx, tag, image.PushOptions{
|
2021-01-04 16:08:27 +01:00
|
|
|
RegistryAuth: base64.URLEncoding.EncodeToString(buf),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(stream)
|
|
|
|
for {
|
|
|
|
var jm jsonmessage.JSONMessage
|
|
|
|
if err := dec.Decode(&jm); err != nil {
|
2023-09-26 00:57:12 +02:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-01-04 16:08:27 +01:00
|
|
|
break
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
2021-01-04 16:08:27 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if jm.Error != nil {
|
|
|
|
return errors.New(jm.Error.Message)
|
|
|
|
}
|
2022-02-13 22:40:54 +05:30
|
|
|
|
|
|
|
if !quietPush {
|
2023-11-17 16:09:12 +01:00
|
|
|
toPushProgressEvent(tag, jm, w)
|
2022-02-13 22:40:54 +05:30
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
2023-11-17 16:09:12 +01:00
|
|
|
|
2021-01-04 16:08:27 +01:00
|
|
|
return nil
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func toPushProgressEvent(prefix string, jm jsonmessage.JSONMessage, w progress.Writer) {
|
|
|
|
if jm.ID == "" {
|
|
|
|
// skipped
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var (
|
2023-02-14 16:13:38 +01:00
|
|
|
text string
|
|
|
|
status = progress.Working
|
|
|
|
total int64
|
|
|
|
current int64
|
|
|
|
percent int
|
2020-12-08 11:53:36 +01:00
|
|
|
)
|
2023-11-17 16:09:12 +01:00
|
|
|
if isDone(jm) {
|
2020-12-08 11:53:36 +01:00
|
|
|
status = progress.Done
|
2023-02-14 16:13:38 +01:00
|
|
|
percent = 100
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
|
|
|
if jm.Error != nil {
|
|
|
|
status = progress.Error
|
|
|
|
text = jm.Error.Message
|
|
|
|
}
|
|
|
|
if jm.Progress != nil {
|
|
|
|
text = jm.Progress.String()
|
2023-02-14 16:13:38 +01:00
|
|
|
if jm.Progress.Total != 0 {
|
|
|
|
current = jm.Progress.Current
|
|
|
|
total = jm.Progress.Total
|
|
|
|
if jm.Progress.Total > 0 {
|
|
|
|
percent = int(jm.Progress.Current * 100 / jm.Progress.Total)
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
2023-02-14 16:13:38 +01:00
|
|
|
|
2020-12-08 11:53:36 +01:00
|
|
|
w.Event(progress.Event{
|
|
|
|
ID: fmt.Sprintf("Pushing %s: %s", prefix, jm.ID),
|
|
|
|
Text: jm.Status,
|
|
|
|
Status: status,
|
2023-02-14 16:13:38 +01:00
|
|
|
Current: current,
|
|
|
|
Total: total,
|
|
|
|
Percent: percent,
|
2020-12-08 11:53:36 +01:00
|
|
|
StatusText: text,
|
|
|
|
})
|
|
|
|
}
|
2023-11-17 16:09:12 +01:00
|
|
|
|
|
|
|
func isDone(msg jsonmessage.JSONMessage) bool {
|
|
|
|
// TODO there should be a better way to detect push is done than such a status message check
|
|
|
|
switch strings.ToLower(msg.Status) {
|
|
|
|
case "pushed", "layer already exists":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|