14 "github.com/Sirupsen/logrus"
15 "github.com/docker/distribution/reference"
16 "github.com/docker/distribution/registry/client/transport"
17 "github.com/docker/docker/distribution/metadata"
18 "github.com/docker/docker/distribution/xfer"
19 "github.com/docker/docker/dockerversion"
20 "github.com/docker/docker/image"
21 "github.com/docker/docker/image/v1"
22 "github.com/docker/docker/layer"
23 "github.com/docker/docker/pkg/ioutils"
24 "github.com/docker/docker/pkg/progress"
25 "github.com/docker/docker/pkg/stringid"
26 "github.com/docker/docker/registry"
27 "golang.org/x/net/context"
30 type v1Puller struct {
31 v1IDService *metadata.V1IDService
32 endpoint registry.APIEndpoint
33 config *ImagePullConfig
34 repoInfo *registry.RepositoryInfo
35 session *registry.Session
38 func (p *v1Puller) Pull(ctx context.Context, ref reference.Named) error {
39 if _, isCanonical := ref.(reference.Canonical); isCanonical {
40 // Allowing fallback, because HTTPS v1 is before HTTP v2
41 return fallbackError{err: ErrNoSupport{Err: errors.New("Cannot pull by digest with v1 registry")}}
44 tlsConfig, err := p.config.RegistryService.TLSConfig(p.repoInfo.Index.Name)
48 // Adds Docker-specific headers as well as user-specified headers (metaHeaders)
49 tr := transport.NewTransport(
50 // TODO(tiborvass): was ReceiveTimeout
51 registry.NewTransport(tlsConfig),
52 registry.DockerHeaders(dockerversion.DockerUserAgent(ctx), p.config.MetaHeaders)...,
54 client := registry.HTTPClient(tr)
55 v1Endpoint, err := p.endpoint.ToV1Endpoint(dockerversion.DockerUserAgent(ctx), p.config.MetaHeaders)
57 logrus.Debugf("Could not get v1 endpoint: %v", err)
58 return fallbackError{err: err}
60 p.session, err = registry.NewSession(client, p.config.AuthConfig, v1Endpoint)
62 // TODO(dmcgowan): Check if should fallback
63 logrus.Debugf("Fallback from error: %s", err)
64 return fallbackError{err: err}
66 if err := p.pullRepository(ctx, ref); err != nil {
67 // TODO(dmcgowan): Check if should fallback
70 progress.Message(p.config.ProgressOutput, "", p.repoInfo.Name.Name()+": this image was pulled from a legacy registry. Important: This registry version will not be supported in future versions of docker.")
75 func (p *v1Puller) pullRepository(ctx context.Context, ref reference.Named) error {
76 progress.Message(p.config.ProgressOutput, "", "Pulling repository "+p.repoInfo.Name.Name())
78 tagged, isTagged := ref.(reference.NamedTagged)
80 repoData, err := p.session.GetRepositoryData(p.repoInfo.Name)
82 if strings.Contains(err.Error(), "HTTP code: 404") {
84 return fmt.Errorf("Error: image %s:%s not found", reference.Path(p.repoInfo.Name), tagged.Tag())
86 return fmt.Errorf("Error: image %s not found", reference.Path(p.repoInfo.Name))
88 // Unexpected HTTP error
92 logrus.Debug("Retrieving the tag list")
93 var tagsList map[string]string
95 tagsList, err = p.session.GetRemoteTags(repoData.Endpoints, p.repoInfo.Name)
98 tagsList = make(map[string]string)
99 tagID, err = p.session.GetRemoteTag(repoData.Endpoints, p.repoInfo.Name, tagged.Tag())
100 if err == registry.ErrRepoNotFound {
101 return fmt.Errorf("Tag %s not found in repository %s", tagged.Tag(), p.repoInfo.Name.Name())
103 tagsList[tagged.Tag()] = tagID
106 logrus.Errorf("unable to get remote tags: %s", err)
110 for tag, id := range tagsList {
111 repoData.ImgList[id] = ®istry.ImgData{
118 layersDownloaded := false
119 for _, imgData := range repoData.ImgList {
120 if isTagged && imgData.Tag != tagged.Tag() {
124 err := p.downloadImage(ctx, repoData, imgData, &layersDownloaded)
130 writeStatus(reference.FamiliarString(ref), p.config.ProgressOutput, layersDownloaded)
134 func (p *v1Puller) downloadImage(ctx context.Context, repoData *registry.RepositoryData, img *registry.ImgData, layersDownloaded *bool) error {
136 logrus.Debugf("Image (id: %s) present in this repository but untagged, skipping", img.ID)
140 localNameRef, err := reference.WithTag(p.repoInfo.Name, img.Tag)
142 retErr := fmt.Errorf("Image (id: %s) has invalid tag: %s", img.ID, img.Tag)
143 logrus.Debug(retErr.Error())
147 if err := v1.ValidateID(img.ID); err != nil {
151 progress.Updatef(p.config.ProgressOutput, stringid.TruncateID(img.ID), "Pulling image (%s) from %s", img.Tag, p.repoInfo.Name.Name())
154 for _, ep := range p.repoInfo.Index.Mirrors {
156 progress.Updatef(p.config.ProgressOutput, stringid.TruncateID(img.ID), fmt.Sprintf("Pulling image (%s) from %s, mirror: %s", img.Tag, p.repoInfo.Name.Name(), ep))
157 if err = p.pullImage(ctx, img.ID, ep, localNameRef, layersDownloaded); err != nil {
158 // Don't report errors when pulling from mirrors.
159 logrus.Debugf("Error pulling image (%s) from %s, mirror: %s, %s", img.Tag, p.repoInfo.Name.Name(), ep, err)
166 for _, ep := range repoData.Endpoints {
167 progress.Updatef(p.config.ProgressOutput, stringid.TruncateID(img.ID), "Pulling image (%s) from %s, endpoint: %s", img.Tag, p.repoInfo.Name.Name(), ep)
168 if err = p.pullImage(ctx, img.ID, ep, localNameRef, layersDownloaded); err != nil {
169 // It's not ideal that only the last error is returned, it would be better to concatenate the errors.
170 // As the error is also given to the output stream the user will see the error.
172 progress.Updatef(p.config.ProgressOutput, stringid.TruncateID(img.ID), "Error pulling image (%s) from %s, endpoint: %s, %s", img.Tag, p.repoInfo.Name.Name(), ep, err)
180 err := fmt.Errorf("Error pulling image (%s) from %s, %v", img.Tag, p.repoInfo.Name.Name(), lastErr)
181 progress.Update(p.config.ProgressOutput, stringid.TruncateID(img.ID), err.Error())
187 func (p *v1Puller) pullImage(ctx context.Context, v1ID, endpoint string, localNameRef reference.Named, layersDownloaded *bool) (err error) {
189 history, err = p.session.GetRemoteHistory(v1ID, endpoint)
193 if len(history) < 1 {
194 return fmt.Errorf("empty history for image %s", v1ID)
196 progress.Update(p.config.ProgressOutput, stringid.TruncateID(v1ID), "Pulling dependent layers")
199 descriptors []xfer.DownloadDescriptor
200 newHistory []image.History
205 // Iterate over layers, in order from bottom-most to top-most. Download
206 // config for all layers and create descriptors.
207 for i := len(history) - 1; i >= 0; i-- {
208 v1LayerID := history[i]
209 imgJSON, imgSize, err = p.downloadLayerConfig(v1LayerID, endpoint)
214 // Create a new-style config from the legacy configs
215 h, err := v1.HistoryFromConfig(imgJSON, false)
219 newHistory = append(newHistory, h)
221 layerDescriptor := &v1LayerDescriptor{
222 v1LayerID: v1LayerID,
223 indexName: p.repoInfo.Index.Name,
225 v1IDService: p.v1IDService,
226 layersDownloaded: layersDownloaded,
231 descriptors = append(descriptors, layerDescriptor)
234 rootFS := image.NewRootFS()
235 resultRootFS, release, err := p.config.DownloadManager.Download(ctx, *rootFS, "", descriptors, p.config.ProgressOutput)
241 config, err := v1.MakeConfigFromV1Config(imgJSON, &resultRootFS, newHistory)
246 imageID, err := p.config.ImageStore.Put(config)
251 if p.config.ReferenceStore != nil {
252 if err := p.config.ReferenceStore.AddTag(localNameRef, imageID, true); err != nil {
260 func (p *v1Puller) downloadLayerConfig(v1LayerID, endpoint string) (imgJSON []byte, imgSize int64, err error) {
261 progress.Update(p.config.ProgressOutput, stringid.TruncateID(v1LayerID), "Pulling metadata")
264 for j := 1; j <= retries; j++ {
265 imgJSON, imgSize, err := p.session.GetRemoteImageJSON(v1LayerID, endpoint)
266 if err != nil && j == retries {
267 progress.Update(p.config.ProgressOutput, stringid.TruncateID(v1LayerID), "Error pulling layer metadata")
269 } else if err != nil {
270 time.Sleep(time.Duration(j) * 500 * time.Millisecond)
274 return imgJSON, imgSize, nil
281 type v1LayerDescriptor struct {
285 v1IDService *metadata.V1IDService
286 layersDownloaded *bool
288 session *registry.Session
292 func (ld *v1LayerDescriptor) Key() string {
293 return "v1:" + ld.v1LayerID
296 func (ld *v1LayerDescriptor) ID() string {
297 return stringid.TruncateID(ld.v1LayerID)
300 func (ld *v1LayerDescriptor) DiffID() (layer.DiffID, error) {
301 return ld.v1IDService.Get(ld.v1LayerID, ld.indexName)
304 func (ld *v1LayerDescriptor) Download(ctx context.Context, progressOutput progress.Output) (io.ReadCloser, int64, error) {
305 progress.Update(progressOutput, ld.ID(), "Pulling fs layer")
306 layerReader, err := ld.session.GetRemoteImageLayer(ld.v1LayerID, ld.endpoint, ld.layerSize)
308 progress.Update(progressOutput, ld.ID(), "Error pulling dependent layers")
309 if uerr, ok := err.(*url.Error); ok {
312 if terr, ok := err.(net.Error); ok && terr.Timeout() {
315 return nil, 0, xfer.DoNotRetry{Err: err}
317 *ld.layersDownloaded = true
319 ld.tmpFile, err = ioutil.TempFile("", "GetImageBlob")
325 reader := progress.NewProgressReader(ioutils.NewCancelReadCloser(ctx, layerReader), progressOutput, ld.layerSize, ld.ID(), "Downloading")
328 _, err = io.Copy(ld.tmpFile, reader)
334 progress.Update(progressOutput, ld.ID(), "Download complete")
336 logrus.Debugf("Downloaded %s to tempfile %s", ld.ID(), ld.tmpFile.Name())
338 ld.tmpFile.Seek(0, 0)
340 // hand off the temporary file to the download manager, so it will only
342 tmpFile := ld.tmpFile
345 return ioutils.NewReadCloserWrapper(tmpFile, func() error {
347 err := os.RemoveAll(tmpFile.Name())
349 logrus.Errorf("Failed to remove temp file: %s", tmpFile.Name())
352 }), ld.layerSize, nil
355 func (ld *v1LayerDescriptor) DeltaBase() io.ReadSeeker {
359 func (ld *v1LayerDescriptor) Close() {
360 if ld.tmpFile != nil {
362 if err := os.RemoveAll(ld.tmpFile.Name()); err != nil {
363 logrus.Errorf("Failed to remove temp file: %s", ld.tmpFile.Name())
369 func (ld *v1LayerDescriptor) Registered(diffID layer.DiffID) {
370 // Cache mapping from this layer's DiffID to the blobsum
371 ld.v1IDService.Set(ld.v1LayerID, ld.indexName, diffID)
374 func (ld *v1LayerDescriptor) Size() int64 {