2014-08-08 02:58:58 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-04-22 08:06:58 -04:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2015-03-17 22:18:41 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2014-08-08 02:58:58 -04:00
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
// ImagePullConfig stores pull configuration.
|
2015-04-15 07:43:15 -04:00
|
|
|
type ImagePullConfig struct {
|
2015-07-21 12:21:45 -04:00
|
|
|
// MetaHeaders store meta data about the image (DockerHeaders with prefix X-Meta- in the request).
|
2015-04-15 07:43:15 -04:00
|
|
|
MetaHeaders map[string][]string
|
2015-07-21 12:21:45 -04:00
|
|
|
// AuthConfig holds authentication information for authorizing with the registry.
|
|
|
|
AuthConfig *cliconfig.AuthConfig
|
|
|
|
// OutStream is the output writer for showing the status of the pull operation.
|
|
|
|
OutStream io.Writer
|
2015-04-15 07:43:15 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
// Puller is an interface to define Pull behavior.
|
2015-02-12 13:23:22 -05:00
|
|
|
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.
|
|
|
|
//
|
|
|
|
// TODO(tiborvass): have Pull() take a reference to repository + tag, so that the puller itself is repository-agnostic.
|
|
|
|
Pull(tag string) (fallback bool, err error)
|
|
|
|
}
|
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
// NewPuller returns a new instance of an implementation conforming to Puller interface.
|
2015-02-12 13:23:22 -05:00
|
|
|
func NewPuller(s *TagStore, endpoint registry.APIEndpoint, repoInfo *registry.RepositoryInfo, imagePullConfig *ImagePullConfig, sf *streamformatter.StreamFormatter) (Puller, error) {
|
|
|
|
switch endpoint.Version {
|
|
|
|
case registry.APIVersion2:
|
|
|
|
return &v2Puller{
|
|
|
|
TagStore: s,
|
|
|
|
endpoint: endpoint,
|
|
|
|
config: imagePullConfig,
|
|
|
|
sf: sf,
|
|
|
|
repoInfo: repoInfo,
|
|
|
|
}, nil
|
|
|
|
case registry.APIVersion1:
|
|
|
|
return &v1Puller{
|
|
|
|
TagStore: s,
|
|
|
|
endpoint: endpoint,
|
|
|
|
config: imagePullConfig,
|
|
|
|
sf: sf,
|
|
|
|
repoInfo: repoInfo,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown version %d for registry %s", endpoint.Version, endpoint.URL)
|
|
|
|
}
|
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
// Pull downloads a image with specified name and tag from the repo.
|
2015-04-21 17:23:48 -04:00
|
|
|
func (s *TagStore) Pull(image string, tag string, imagePullConfig *ImagePullConfig) error {
|
2015-02-12 13:23:22 -05:00
|
|
|
var sf = streamformatter.NewJSONStreamFormatter()
|
2014-10-03 18:24:14 -04:00
|
|
|
|
2014-10-06 21:54:52 -04:00
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
2015-04-15 07:43:15 -04:00
|
|
|
repoInfo, err := s.registryService.ResolveRepository(image)
|
2014-10-06 21:54:52 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
// makes sure name is not empty or `scratch`
|
2015-04-22 17:41:24 -04:00
|
|
|
if err := validateRepoName(repoInfo.LocalName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
endpoints, err := s.registryService.LookupEndpoints(repoInfo.CanonicalName)
|
2014-08-08 02:58:58 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:48:20 -04:00
|
|
|
logName := repoInfo.LocalName
|
|
|
|
if tag != "" {
|
|
|
|
logName = utils.ImageReference(logName, tag)
|
|
|
|
}
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
var (
|
|
|
|
lastErr error
|
|
|
|
|
|
|
|
// 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 lastErr.
|
|
|
|
// As soon as another kind of error is encountered, discardNoSupportErrors is set to true, avoiding the saving of
|
|
|
|
// any subsequent ErrNoSupport errors in lastErr.
|
|
|
|
// 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-05-15 21:35:04 -04:00
|
|
|
)
|
2015-02-12 13:23:22 -05:00
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
logrus.Debugf("Trying to pull %s from %s %s", repoInfo.LocalName, endpoint.URL, endpoint.Version)
|
2014-10-07 14:00:17 -04:00
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
if !endpoint.Mirror && (endpoint.Official || endpoint.Version == registry.APIVersion2) {
|
|
|
|
if repoInfo.Official {
|
|
|
|
s.trustService.UpdateBase()
|
|
|
|
}
|
2014-10-07 14:00:17 -04:00
|
|
|
}
|
2014-12-23 16:40:06 -05:00
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
puller, err := NewPuller(s, endpoint, repoInfo, imagePullConfig, sf)
|
2015-05-15 20:48:20 -04:00
|
|
|
if err != nil {
|
2015-02-12 13:23:22 -05:00
|
|
|
lastErr = err
|
2015-05-15 20:48:20 -04:00
|
|
|
continue
|
|
|
|
}
|
2015-02-12 13:23:22 -05:00
|
|
|
if fallback, err := puller.Pull(tag); err != nil {
|
|
|
|
if fallback {
|
|
|
|
if _, ok := err.(registry.ErrNoSupport); !ok {
|
|
|
|
// Because we found an error that's not ErrNoSupport, discard all subsequent ErrNoSupport errors.
|
|
|
|
discardNoSupportErrors = true
|
|
|
|
// save the current error
|
|
|
|
lastErr = err
|
|
|
|
} else if !discardNoSupportErrors {
|
|
|
|
// Save the ErrNoSupport error, because it's either the first error or all encountered errors
|
|
|
|
// were also ErrNoSupport errors.
|
|
|
|
lastErr = err
|
|
|
|
}
|
|
|
|
continue
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
2015-02-12 13:23:22 -05:00
|
|
|
logrus.Debugf("Not continuing with error: %v", err)
|
2014-08-08 02:58:58 -04:00
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
s.eventsService.Log("pull", logName, "")
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-08 02:58:58 -04:00
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
if lastErr == nil {
|
|
|
|
lastErr = fmt.Errorf("no endpoints found for %s", image)
|
2014-09-23 18:53:43 -04:00
|
|
|
}
|
2015-02-12 13:23:22 -05:00
|
|
|
return lastErr
|
2014-09-23 18:53:43 -04:00
|
|
|
}
|
2014-08-08 02:58:58 -04:00
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
// writeStatus shows status of the pull command.
|
|
|
|
func writeStatus(requestedTag string, out io.Writer, sf *streamformatter.StreamFormatter, layersDownloaded bool) {
|
2015-03-25 21:40:23 -04:00
|
|
|
if layersDownloaded {
|
2014-09-23 18:53:43 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Status: Downloaded newer image for %s", requestedTag))
|
|
|
|
} else {
|
|
|
|
out.Write(sf.FormatStatus("", "Status: Image is up to date for %s", requestedTag))
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
|
|
|
}
|