2015-11-18 17:18:44 -05:00
|
|
|
package distribution
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/distribution/metadata"
|
2015-11-13 19:59:01 -05:00
|
|
|
"github.com/docker/docker/distribution/xfer"
|
2015-11-18 17:18:44 -05:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
2015-11-13 19:59:01 -05:00
|
|
|
"github.com/docker/docker/pkg/progress"
|
2015-12-04 16:55:15 -05:00
|
|
|
"github.com/docker/docker/reference"
|
2015-11-18 17:18:44 -05:00
|
|
|
"github.com/docker/docker/registry"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-11-18 17:18:44 -05:00
|
|
|
"github.com/docker/libtrust"
|
2015-11-13 19:59:01 -05:00
|
|
|
"golang.org/x/net/context"
|
2015-11-18 17:18:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImagePushConfig stores push configuration.
|
|
|
|
type ImagePushConfig struct {
|
|
|
|
// MetaHeaders store HTTP headers with metadata about the image
|
|
|
|
MetaHeaders map[string][]string
|
|
|
|
// AuthConfig holds authentication credentials for authenticating with
|
|
|
|
// the registry.
|
2015-12-11 23:11:42 -05:00
|
|
|
AuthConfig *types.AuthConfig
|
2015-11-13 19:59:01 -05:00
|
|
|
// ProgressOutput is the interface for showing the status of the push
|
2015-11-18 17:18:44 -05:00
|
|
|
// operation.
|
2015-11-13 19:59:01 -05:00
|
|
|
ProgressOutput progress.Output
|
2015-11-18 17:18:44 -05:00
|
|
|
// RegistryService is the registry service to use for TLS configuration
|
|
|
|
// and endpoint lookup.
|
2016-05-21 10:00:28 -04:00
|
|
|
RegistryService registry.Service
|
2015-12-21 17:55:23 -05:00
|
|
|
// ImageEventLogger notifies events for a given image
|
|
|
|
ImageEventLogger func(id, name, action string)
|
2015-11-18 17:18:44 -05:00
|
|
|
// MetadataStore is the storage backend for distribution-specific
|
|
|
|
// metadata.
|
|
|
|
MetadataStore metadata.Store
|
2015-12-13 11:00:39 -05:00
|
|
|
// LayerStore manages layers.
|
2015-11-18 17:18:44 -05:00
|
|
|
LayerStore layer.Store
|
|
|
|
// ImageStore manages images.
|
|
|
|
ImageStore image.Store
|
2015-12-04 16:55:15 -05:00
|
|
|
// ReferenceStore manages tags.
|
|
|
|
ReferenceStore reference.Store
|
2015-11-18 17:18:44 -05:00
|
|
|
// TrustKey is the private key for legacy signatures. This is typically
|
|
|
|
// an ephemeral key, since these signatures are no longer verified.
|
|
|
|
TrustKey libtrust.PrivateKey
|
2015-11-13 19:59:01 -05:00
|
|
|
// UploadManager dispatches uploads.
|
|
|
|
UploadManager *xfer.LayerUploadManager
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pusher is an interface that abstracts pushing for different API versions.
|
|
|
|
type Pusher interface {
|
|
|
|
// Push tries to push the image configured at the creation of Pusher.
|
|
|
|
// Push returns an error if any, as well as a boolean that determines whether to retry Push on the next configured endpoint.
|
|
|
|
//
|
|
|
|
// TODO(tiborvass): have Push() take a reference to repository + tag, so that the pusher itself is repository-agnostic.
|
2015-12-04 16:42:33 -05:00
|
|
|
Push(ctx context.Context) error
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const compressionBufSize = 32768
|
|
|
|
|
|
|
|
// NewPusher creates a new Pusher interface that will push to either a v1 or v2
|
|
|
|
// registry. The endpoint argument contains a Version field that determines
|
|
|
|
// whether a v1 or v2 pusher will be created. The other parameters are passed
|
|
|
|
// through to the underlying pusher implementation for use during the actual
|
|
|
|
// push operation.
|
2015-11-13 19:59:01 -05:00
|
|
|
func NewPusher(ref reference.Named, endpoint registry.APIEndpoint, repoInfo *registry.RepositoryInfo, imagePushConfig *ImagePushConfig) (Pusher, error) {
|
2015-11-18 17:18:44 -05:00
|
|
|
switch endpoint.Version {
|
|
|
|
case registry.APIVersion2:
|
|
|
|
return &v2Pusher{
|
2016-01-13 22:34:27 -05:00
|
|
|
v2MetadataService: metadata.NewV2MetadataService(imagePushConfig.MetadataStore),
|
|
|
|
ref: ref,
|
|
|
|
endpoint: endpoint,
|
|
|
|
repoInfo: repoInfo,
|
|
|
|
config: imagePushConfig,
|
2015-11-18 17:18:44 -05:00
|
|
|
}, nil
|
|
|
|
case registry.APIVersion1:
|
|
|
|
return &v1Pusher{
|
|
|
|
v1IDService: metadata.NewV1IDService(imagePushConfig.MetadataStore),
|
|
|
|
ref: ref,
|
|
|
|
endpoint: endpoint,
|
|
|
|
repoInfo: repoInfo,
|
|
|
|
config: imagePushConfig,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown version %d for registry %s", endpoint.Version, endpoint.URL)
|
|
|
|
}
|
|
|
|
|
2016-06-06 18:57:51 -04:00
|
|
|
// Push initiates a push operation on ref.
|
2015-11-18 17:18:44 -05:00
|
|
|
// ref is the specific variant of the image to be pushed.
|
|
|
|
// If no tag is provided, all tags will be pushed.
|
2015-11-13 19:59:01 -05:00
|
|
|
func Push(ctx context.Context, ref reference.Named, imagePushConfig *ImagePushConfig) error {
|
2015-11-18 17:18:44 -05:00
|
|
|
// FIXME: Allow to interrupt current push when new push of same image is done.
|
|
|
|
|
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
|
|
|
repoInfo, err := imagePushConfig.RegistryService.ResolveRepository(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
endpoints, err := imagePushConfig.RegistryService.LookupPushEndpoints(repoInfo.Hostname())
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-11 14:00:13 -05:00
|
|
|
progress.Messagef(imagePushConfig.ProgressOutput, "", "The push refers to a repository [%s]", repoInfo.FullName())
|
2015-11-18 17:18:44 -05:00
|
|
|
|
2015-12-11 14:00:13 -05:00
|
|
|
associations := imagePushConfig.ReferenceStore.ReferencesByName(repoInfo)
|
2015-11-18 17:18:44 -05:00
|
|
|
if len(associations) == 0 {
|
2016-04-03 16:25:07 -04:00
|
|
|
return fmt.Errorf("An image does not exist locally with the tag: %s", repoInfo.Name())
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
2015-12-04 16:42:33 -05:00
|
|
|
var (
|
|
|
|
lastErr error
|
|
|
|
|
|
|
|
// confirmedV2 is set to true if a push attempt managed to
|
|
|
|
// confirm that it was talking to a v2 registry. This will
|
|
|
|
// prevent fallback to the v1 protocol.
|
|
|
|
confirmedV2 bool
|
2016-02-11 18:45:29 -05:00
|
|
|
|
|
|
|
// confirmedTLSRegistries is a map indicating which registries
|
|
|
|
// are known to be using TLS. There should never be a plaintext
|
|
|
|
// retry for any of these.
|
|
|
|
confirmedTLSRegistries = make(map[string]struct{})
|
2015-12-04 16:42:33 -05:00
|
|
|
)
|
|
|
|
|
2015-11-18 17:18:44 -05:00
|
|
|
for _, endpoint := range endpoints {
|
2015-12-04 16:42:33 -05:00
|
|
|
if confirmedV2 && endpoint.Version == registry.APIVersion1 {
|
|
|
|
logrus.Debugf("Skipping v1 endpoint %s because v2 registry was detected", endpoint.URL)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-02-17 19:53:25 -05:00
|
|
|
if endpoint.URL.Scheme != "https" {
|
|
|
|
if _, confirmedTLS := confirmedTLSRegistries[endpoint.URL.Host]; confirmedTLS {
|
2016-02-11 18:45:29 -05:00
|
|
|
logrus.Debugf("Skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 14:00:13 -05:00
|
|
|
logrus.Debugf("Trying to push %s to %s %s", repoInfo.FullName(), endpoint.URL, endpoint.Version)
|
2015-11-18 17:18:44 -05:00
|
|
|
|
2015-11-13 19:59:01 -05:00
|
|
|
pusher, err := NewPusher(ref, endpoint, repoInfo, imagePushConfig)
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
lastErr = err
|
|
|
|
continue
|
|
|
|
}
|
2015-12-04 16:42:33 -05:00
|
|
|
if err := pusher.Push(ctx); err != nil {
|
2015-11-13 19:59:01 -05:00
|
|
|
// Was this push cancelled? If so, don't try to fall
|
|
|
|
// back.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
default:
|
2015-12-04 16:42:33 -05:00
|
|
|
if fallbackErr, ok := err.(fallbackError); ok {
|
|
|
|
confirmedV2 = confirmedV2 || fallbackErr.confirmedV2
|
2016-02-17 19:53:25 -05:00
|
|
|
if fallbackErr.transportOK && endpoint.URL.Scheme == "https" {
|
|
|
|
confirmedTLSRegistries[endpoint.URL.Host] = struct{}{}
|
2016-02-11 18:45:29 -05:00
|
|
|
}
|
2015-12-04 16:42:33 -05:00
|
|
|
err = fallbackErr.err
|
|
|
|
lastErr = err
|
2016-02-11 17:08:49 -05:00
|
|
|
logrus.Errorf("Attempting next endpoint for push after error: %v", err)
|
2015-12-04 16:42:33 -05:00
|
|
|
continue
|
|
|
|
}
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
2016-02-11 17:08:49 -05:00
|
|
|
logrus.Errorf("Not continuing with push after error: %v", err)
|
2015-11-18 17:18:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-21 17:55:23 -05:00
|
|
|
imagePushConfig.ImageEventLogger(ref.String(), repoInfo.Name(), "push")
|
2015-11-18 17:18:44 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastErr == nil {
|
2015-12-11 14:00:13 -05:00
|
|
|
lastErr = fmt.Errorf("no endpoints found for %s", repoInfo.FullName())
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
return lastErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// compress returns an io.ReadCloser which will supply a compressed version of
|
|
|
|
// the provided Reader. The caller must close the ReadCloser after reading the
|
|
|
|
// compressed data.
|
|
|
|
//
|
|
|
|
// Note that this function returns a reader instead of taking a writer as an
|
|
|
|
// argument so that it can be used with httpBlobWriter's ReadFrom method.
|
|
|
|
// Using httpBlobWriter's Write method would send a PATCH request for every
|
|
|
|
// Write call.
|
2016-01-29 17:34:50 -05:00
|
|
|
//
|
|
|
|
// The second return value is a channel that gets closed when the goroutine
|
|
|
|
// is finished. This allows the caller to make sure the goroutine finishes
|
|
|
|
// before it releases any resources connected with the reader that was
|
|
|
|
// passed in.
|
|
|
|
func compress(in io.Reader) (io.ReadCloser, chan struct{}) {
|
|
|
|
compressionDone := make(chan struct{})
|
|
|
|
|
2015-11-18 17:18:44 -05:00
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
// Use a bufio.Writer to avoid excessive chunking in HTTP request.
|
|
|
|
bufWriter := bufio.NewWriterSize(pipeWriter, compressionBufSize)
|
|
|
|
compressor := gzip.NewWriter(bufWriter)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(compressor, in)
|
|
|
|
if err == nil {
|
|
|
|
err = compressor.Close()
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = bufWriter.Flush()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
} else {
|
|
|
|
pipeWriter.Close()
|
|
|
|
}
|
2016-01-29 17:34:50 -05:00
|
|
|
close(compressionDone)
|
2015-11-18 17:18:44 -05:00
|
|
|
}()
|
|
|
|
|
2016-01-29 17:34:50 -05:00
|
|
|
return pipeReader, compressionDone
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|