2015-11-18 17:18:44 -05:00
|
|
|
package distribution
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-11-13 19:59:01 -05:00
|
|
|
"os"
|
2015-11-18 17:18:44 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-12-31 08:57:58 -05:00
|
|
|
"github.com/docker/docker/api"
|
2015-11-18 17:18:44 -05:00
|
|
|
"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"
|
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-13 19:59:01 -05:00
|
|
|
"golang.org/x/net/context"
|
2015-11-18 17:18:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImagePullConfig stores pull configuration.
|
|
|
|
type ImagePullConfig struct {
|
|
|
|
// MetaHeaders stores HTTP headers with metadata about the image
|
|
|
|
// (DockerHeaders with prefix X-Meta- in the request).
|
|
|
|
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 pull
|
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.
|
|
|
|
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
|
|
|
|
// ImageStore manages images.
|
|
|
|
ImageStore image.Store
|
2015-12-04 16:55:15 -05:00
|
|
|
// ReferenceStore manages tags.
|
|
|
|
ReferenceStore reference.Store
|
2015-11-13 19:59:01 -05:00
|
|
|
// DownloadManager manages concurrent pulls.
|
|
|
|
DownloadManager *xfer.LayerDownloadManager
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Puller is an interface that abstracts pulling for different API versions.
|
|
|
|
type Puller interface {
|
|
|
|
// Pull tries to pull the image referenced by `tag`
|
|
|
|
// Pull returns an error if any, as well as a boolean that determines whether to retry Pull on the next configured endpoint.
|
|
|
|
//
|
2015-12-04 16:42:33 -05:00
|
|
|
Pull(ctx context.Context, ref reference.Named) error
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// newPuller returns a Puller interface that will pull from either a v1 or v2
|
|
|
|
// registry. The endpoint argument contains a Version field that determines
|
|
|
|
// whether a v1 or v2 puller will be created. The other parameters are passed
|
|
|
|
// through to the underlying puller implementation for use during the actual
|
|
|
|
// pull operation.
|
2015-11-13 19:59:01 -05:00
|
|
|
func newPuller(endpoint registry.APIEndpoint, repoInfo *registry.RepositoryInfo, imagePullConfig *ImagePullConfig) (Puller, error) {
|
2015-11-18 17:18:44 -05:00
|
|
|
switch endpoint.Version {
|
|
|
|
case registry.APIVersion2:
|
|
|
|
return &v2Puller{
|
|
|
|
blobSumService: metadata.NewBlobSumService(imagePullConfig.MetadataStore),
|
|
|
|
endpoint: endpoint,
|
|
|
|
config: imagePullConfig,
|
|
|
|
repoInfo: repoInfo,
|
|
|
|
}, nil
|
|
|
|
case registry.APIVersion1:
|
|
|
|
return &v1Puller{
|
|
|
|
v1IDService: metadata.NewV1IDService(imagePullConfig.MetadataStore),
|
|
|
|
endpoint: endpoint,
|
|
|
|
config: imagePullConfig,
|
|
|
|
repoInfo: repoInfo,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown version %d for registry %s", endpoint.Version, endpoint.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull initiates a pull operation. image is the repository name to pull, and
|
|
|
|
// tag may be either empty, or indicate a specific tag to pull.
|
2015-11-13 19:59:01 -05:00
|
|
|
func Pull(ctx context.Context, ref reference.Named, imagePullConfig *ImagePullConfig) error {
|
2015-11-18 17:18:44 -05:00
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
|
|
|
repoInfo, err := imagePullConfig.RegistryService.ResolveRepository(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// makes sure name is not empty or `scratch`
|
2015-12-11 14:00:13 -05:00
|
|
|
if err := validateRepoName(repoInfo.Name()); err != nil {
|
2015-11-18 17:18:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-11 14:00:13 -05:00
|
|
|
endpoints, err := imagePullConfig.RegistryService.LookupPullEndpoints(repoInfo)
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// use a slice to append the error strings and return a joined string to caller
|
|
|
|
errors []string
|
|
|
|
|
|
|
|
// discardNoSupportErrors is used to track whether an endpoint encountered an error of type registry.ErrNoSupport
|
|
|
|
// By default it is false, which means that if a ErrNoSupport error is encountered, it will be saved in errors.
|
|
|
|
// As soon as another kind of error is encountered, discardNoSupportErrors is set to true, avoiding the saving of
|
|
|
|
// any subsequent ErrNoSupport errors in errors.
|
|
|
|
// It's needed for pull-by-digest on v1 endpoints: if there are only v1 endpoints configured, the error should be
|
|
|
|
// returned and displayed, but if there was a v2 endpoint which supports pull-by-digest, then the last relevant
|
|
|
|
// error is the ones from v2 endpoints not v1.
|
|
|
|
discardNoSupportErrors bool
|
2015-12-04 16:42:33 -05:00
|
|
|
|
|
|
|
// confirmedV2 is set to true if a pull attempt managed to
|
|
|
|
// confirm that it was talking to a v2 registry. This will
|
|
|
|
// prevent fallback to the v1 protocol.
|
|
|
|
confirmedV2 bool
|
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
|
|
|
|
}
|
2015-12-11 14:00:13 -05:00
|
|
|
logrus.Debugf("Trying to pull %s from %s %s", repoInfo.Name(), endpoint.URL, endpoint.Version)
|
2015-11-18 17:18:44 -05:00
|
|
|
|
2015-11-13 19:59:01 -05:00
|
|
|
puller, err := newPuller(endpoint, repoInfo, imagePullConfig)
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2015-12-04 16:42:33 -05:00
|
|
|
if err := puller.Pull(ctx, ref); err != nil {
|
2015-11-13 19:59:01 -05:00
|
|
|
// Was this pull cancelled? If so, don't try to fall
|
|
|
|
// back.
|
2015-12-04 16:42:33 -05:00
|
|
|
fallback := false
|
2015-11-13 19:59:01 -05:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
default:
|
2015-12-04 16:42:33 -05:00
|
|
|
if fallbackErr, ok := err.(fallbackError); ok {
|
|
|
|
fallback = true
|
|
|
|
confirmedV2 = confirmedV2 || fallbackErr.confirmedV2
|
|
|
|
err = fallbackErr.err
|
|
|
|
}
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
2015-11-18 17:18:44 -05:00
|
|
|
if fallback {
|
|
|
|
if _, ok := err.(registry.ErrNoSupport); !ok {
|
|
|
|
// Because we found an error that's not ErrNoSupport, discard all subsequent ErrNoSupport errors.
|
|
|
|
discardNoSupportErrors = true
|
|
|
|
// append subsequent errors
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
} else if !discardNoSupportErrors {
|
|
|
|
// Save the ErrNoSupport error, because it's either the first error or all encountered errors
|
|
|
|
// were also ErrNoSupport errors.
|
|
|
|
// append subsequent errors
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
logrus.Debugf("Not continuing with error: %v", fmt.Errorf(strings.Join(errors, "\n")))
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return fmt.Errorf(strings.Join(errors, "\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 17:55:23 -05:00
|
|
|
imagePullConfig.ImageEventLogger(ref.String(), repoInfo.Name(), "pull")
|
2015-11-18 17:18:44 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) == 0 {
|
|
|
|
return fmt.Errorf("no endpoints found for %s", ref.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return fmt.Errorf(strings.Join(errors, "\n"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeStatus writes a status message to out. If layersDownloaded is true, the
|
|
|
|
// status message indicates that a newer image was downloaded. Otherwise, it
|
|
|
|
// indicates that the image is up to date. requestedTag is the tag the message
|
|
|
|
// will refer to.
|
2015-11-13 19:59:01 -05:00
|
|
|
func writeStatus(requestedTag string, out progress.Output, layersDownloaded bool) {
|
2015-11-18 17:18:44 -05:00
|
|
|
if layersDownloaded {
|
2015-11-13 19:59:01 -05:00
|
|
|
progress.Message(out, "", "Status: Downloaded newer image for "+requestedTag)
|
2015-11-18 17:18:44 -05:00
|
|
|
} else {
|
2015-11-13 19:59:01 -05:00
|
|
|
progress.Message(out, "", "Status: Image is up to date for "+requestedTag)
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateRepoName validates the name of a repository.
|
|
|
|
func validateRepoName(name string) error {
|
|
|
|
if name == "" {
|
|
|
|
return fmt.Errorf("Repository name can't be empty")
|
|
|
|
}
|
2015-12-31 08:57:58 -05:00
|
|
|
if name == api.NoBaseImageSpecifier {
|
|
|
|
return fmt.Errorf("'%s' is a reserved name", api.NoBaseImageSpecifier)
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
// tmpFileClose creates a closer function for a temporary file that closes the file
|
|
|
|
// and also deletes it.
|
|
|
|
func tmpFileCloser(tmpFile *os.File) func() error {
|
|
|
|
return func() error {
|
|
|
|
tmpFile.Close()
|
|
|
|
if err := os.RemoveAll(tmpFile.Name()); err != nil {
|
|
|
|
logrus.Errorf("Failed to remove temp file: %s", tmpFile.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|