github.com/scorpionis/docker@v1.6.0-rc7/graph/pull.go (about) 1 package graph 2 3 import ( 4 "fmt" 5 "io" 6 "io/ioutil" 7 "net" 8 "net/url" 9 "os" 10 "strings" 11 "time" 12 13 log "github.com/Sirupsen/logrus" 14 "github.com/docker/distribution/digest" 15 "github.com/docker/docker/engine" 16 "github.com/docker/docker/image" 17 "github.com/docker/docker/pkg/common" 18 "github.com/docker/docker/pkg/progressreader" 19 "github.com/docker/docker/registry" 20 "github.com/docker/docker/utils" 21 ) 22 23 func (s *TagStore) CmdPull(job *engine.Job) engine.Status { 24 if n := len(job.Args); n != 1 && n != 2 { 25 return job.Errorf("Usage: %s IMAGE [TAG|DIGEST]", job.Name) 26 } 27 28 var ( 29 localName = job.Args[0] 30 tag string 31 sf = utils.NewStreamFormatter(job.GetenvBool("json")) 32 authConfig = ®istry.AuthConfig{} 33 metaHeaders map[string][]string 34 ) 35 36 // Resolve the Repository name from fqn to RepositoryInfo 37 repoInfo, err := registry.ResolveRepositoryInfo(job, localName) 38 if err != nil { 39 return job.Error(err) 40 } 41 42 if len(job.Args) > 1 { 43 tag = job.Args[1] 44 } 45 46 job.GetenvJson("authConfig", authConfig) 47 job.GetenvJson("metaHeaders", &metaHeaders) 48 49 c, err := s.poolAdd("pull", utils.ImageReference(repoInfo.LocalName, tag)) 50 if err != nil { 51 if c != nil { 52 // Another pull of the same repository is already taking place; just wait for it to finish 53 job.Stdout.Write(sf.FormatStatus("", "Repository %s already being pulled by another client. Waiting.", repoInfo.LocalName)) 54 <-c 55 return engine.StatusOK 56 } 57 return job.Error(err) 58 } 59 defer s.poolRemove("pull", utils.ImageReference(repoInfo.LocalName, tag)) 60 61 log.Debugf("pulling image from host %q with remote name %q", repoInfo.Index.Name, repoInfo.RemoteName) 62 endpoint, err := repoInfo.GetEndpoint() 63 if err != nil { 64 return job.Error(err) 65 } 66 67 r, err := registry.NewSession(authConfig, registry.HTTPRequestFactory(metaHeaders), endpoint, true) 68 if err != nil { 69 return job.Error(err) 70 } 71 72 logName := repoInfo.LocalName 73 if tag != "" { 74 logName = utils.ImageReference(logName, tag) 75 } 76 77 if len(repoInfo.Index.Mirrors) == 0 && (repoInfo.Index.Official || endpoint.Version == registry.APIVersion2) { 78 if repoInfo.Official { 79 j := job.Eng.Job("trust_update_base") 80 if err = j.Run(); err != nil { 81 log.Errorf("error updating trust base graph: %s", err) 82 } 83 } 84 85 log.Debugf("pulling v2 repository with local name %q", repoInfo.LocalName) 86 if err := s.pullV2Repository(job.Eng, r, job.Stdout, repoInfo, tag, sf, job.GetenvBool("parallel")); err == nil { 87 if err = job.Eng.Job("log", "pull", logName, "").Run(); err != nil { 88 log.Errorf("Error logging event 'pull' for %s: %s", logName, err) 89 } 90 return engine.StatusOK 91 } else if err != registry.ErrDoesNotExist && err != ErrV2RegistryUnavailable { 92 log.Errorf("Error from V2 registry: %s", err) 93 } 94 95 log.Debug("image does not exist on v2 registry, falling back to v1") 96 } 97 98 log.Debugf("pulling v1 repository with local name %q", repoInfo.LocalName) 99 if err = s.pullRepository(r, job.Stdout, repoInfo, tag, sf, job.GetenvBool("parallel")); err != nil { 100 return job.Error(err) 101 } 102 103 if err = job.Eng.Job("log", "pull", logName, "").Run(); err != nil { 104 log.Errorf("Error logging event 'pull' for %s: %s", logName, err) 105 } 106 107 return engine.StatusOK 108 } 109 110 func (s *TagStore) pullRepository(r *registry.Session, out io.Writer, repoInfo *registry.RepositoryInfo, askedTag string, sf *utils.StreamFormatter, parallel bool) error { 111 out.Write(sf.FormatStatus("", "Pulling repository %s", repoInfo.CanonicalName)) 112 113 repoData, err := r.GetRepositoryData(repoInfo.RemoteName) 114 if err != nil { 115 if strings.Contains(err.Error(), "HTTP code: 404") { 116 return fmt.Errorf("Error: image %s not found", utils.ImageReference(repoInfo.RemoteName, askedTag)) 117 } 118 // Unexpected HTTP error 119 return err 120 } 121 122 log.Debugf("Retrieving the tag list") 123 tagsList, err := r.GetRemoteTags(repoData.Endpoints, repoInfo.RemoteName, repoData.Tokens) 124 if err != nil { 125 log.Errorf("unable to get remote tags: %s", err) 126 return err 127 } 128 129 for tag, id := range tagsList { 130 repoData.ImgList[id] = ®istry.ImgData{ 131 ID: id, 132 Tag: tag, 133 Checksum: "", 134 } 135 } 136 137 log.Debugf("Registering tags") 138 // If no tag has been specified, pull them all 139 if askedTag == "" { 140 for tag, id := range tagsList { 141 repoData.ImgList[id].Tag = tag 142 } 143 } else { 144 // Otherwise, check that the tag exists and use only that one 145 id, exists := tagsList[askedTag] 146 if !exists { 147 return fmt.Errorf("Tag %s not found in repository %s", askedTag, repoInfo.CanonicalName) 148 } 149 repoData.ImgList[id].Tag = askedTag 150 } 151 152 errors := make(chan error) 153 154 layers_downloaded := false 155 for _, image := range repoData.ImgList { 156 downloadImage := func(img *registry.ImgData) { 157 if askedTag != "" && img.Tag != askedTag { 158 if parallel { 159 errors <- nil 160 } 161 return 162 } 163 164 if img.Tag == "" { 165 log.Debugf("Image (id: %s) present in this repository but untagged, skipping", img.ID) 166 if parallel { 167 errors <- nil 168 } 169 return 170 } 171 172 // ensure no two downloads of the same image happen at the same time 173 if c, err := s.poolAdd("pull", "img:"+img.ID); err != nil { 174 if c != nil { 175 out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Layer already being pulled by another client. Waiting.", nil)) 176 <-c 177 out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Download complete", nil)) 178 } else { 179 log.Debugf("Image (id: %s) pull is already running, skipping: %v", img.ID, err) 180 } 181 if parallel { 182 errors <- nil 183 } 184 return 185 } 186 defer s.poolRemove("pull", "img:"+img.ID) 187 188 out.Write(sf.FormatProgress(common.TruncateID(img.ID), fmt.Sprintf("Pulling image (%s) from %s", img.Tag, repoInfo.CanonicalName), nil)) 189 success := false 190 var lastErr, err error 191 var is_downloaded bool 192 for _, ep := range repoInfo.Index.Mirrors { 193 out.Write(sf.FormatProgress(common.TruncateID(img.ID), fmt.Sprintf("Pulling image (%s) from %s, mirror: %s", img.Tag, repoInfo.CanonicalName, ep), nil)) 194 if is_downloaded, err = s.pullImage(r, out, img.ID, ep, repoData.Tokens, sf); err != nil { 195 // Don't report errors when pulling from mirrors. 196 log.Debugf("Error pulling image (%s) from %s, mirror: %s, %s", img.Tag, repoInfo.CanonicalName, ep, err) 197 continue 198 } 199 layers_downloaded = layers_downloaded || is_downloaded 200 success = true 201 break 202 } 203 if !success { 204 for _, ep := range repoData.Endpoints { 205 out.Write(sf.FormatProgress(common.TruncateID(img.ID), fmt.Sprintf("Pulling image (%s) from %s, endpoint: %s", img.Tag, repoInfo.CanonicalName, ep), nil)) 206 if is_downloaded, err = s.pullImage(r, out, img.ID, ep, repoData.Tokens, sf); err != nil { 207 // It's not ideal that only the last error is returned, it would be better to concatenate the errors. 208 // As the error is also given to the output stream the user will see the error. 209 lastErr = err 210 out.Write(sf.FormatProgress(common.TruncateID(img.ID), fmt.Sprintf("Error pulling image (%s) from %s, endpoint: %s, %s", img.Tag, repoInfo.CanonicalName, ep, err), nil)) 211 continue 212 } 213 layers_downloaded = layers_downloaded || is_downloaded 214 success = true 215 break 216 } 217 } 218 if !success { 219 err := fmt.Errorf("Error pulling image (%s) from %s, %v", img.Tag, repoInfo.CanonicalName, lastErr) 220 out.Write(sf.FormatProgress(common.TruncateID(img.ID), err.Error(), nil)) 221 if parallel { 222 errors <- err 223 return 224 } 225 } 226 out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Download complete", nil)) 227 228 if parallel { 229 errors <- nil 230 } 231 } 232 233 if parallel { 234 go downloadImage(image) 235 } else { 236 downloadImage(image) 237 } 238 } 239 if parallel { 240 var lastError error 241 for i := 0; i < len(repoData.ImgList); i++ { 242 if err := <-errors; err != nil { 243 lastError = err 244 } 245 } 246 if lastError != nil { 247 return lastError 248 } 249 250 } 251 for tag, id := range tagsList { 252 if askedTag != "" && tag != askedTag { 253 continue 254 } 255 if err := s.Set(repoInfo.LocalName, tag, id, true); err != nil { 256 return err 257 } 258 } 259 260 requestedTag := repoInfo.CanonicalName 261 if len(askedTag) > 0 { 262 requestedTag = utils.ImageReference(repoInfo.CanonicalName, askedTag) 263 } 264 WriteStatus(requestedTag, out, sf, layers_downloaded) 265 return nil 266 } 267 268 func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint string, token []string, sf *utils.StreamFormatter) (bool, error) { 269 history, err := r.GetRemoteHistory(imgID, endpoint, token) 270 if err != nil { 271 return false, err 272 } 273 out.Write(sf.FormatProgress(common.TruncateID(imgID), "Pulling dependent layers", nil)) 274 // FIXME: Try to stream the images? 275 // FIXME: Launch the getRemoteImage() in goroutines 276 277 layers_downloaded := false 278 for i := len(history) - 1; i >= 0; i-- { 279 id := history[i] 280 281 // ensure no two downloads of the same layer happen at the same time 282 if c, err := s.poolAdd("pull", "layer:"+id); err != nil { 283 log.Debugf("Image (id: %s) pull is already running, skipping: %v", id, err) 284 <-c 285 } 286 defer s.poolRemove("pull", "layer:"+id) 287 288 if !s.graph.Exists(id) { 289 out.Write(sf.FormatProgress(common.TruncateID(id), "Pulling metadata", nil)) 290 var ( 291 imgJSON []byte 292 imgSize int 293 err error 294 img *image.Image 295 ) 296 retries := 5 297 for j := 1; j <= retries; j++ { 298 imgJSON, imgSize, err = r.GetRemoteImageJSON(id, endpoint, token) 299 if err != nil && j == retries { 300 out.Write(sf.FormatProgress(common.TruncateID(id), "Error pulling dependent layers", nil)) 301 return layers_downloaded, err 302 } else if err != nil { 303 time.Sleep(time.Duration(j) * 500 * time.Millisecond) 304 continue 305 } 306 img, err = image.NewImgJSON(imgJSON) 307 layers_downloaded = true 308 if err != nil && j == retries { 309 out.Write(sf.FormatProgress(common.TruncateID(id), "Error pulling dependent layers", nil)) 310 return layers_downloaded, fmt.Errorf("Failed to parse json: %s", err) 311 } else if err != nil { 312 time.Sleep(time.Duration(j) * 500 * time.Millisecond) 313 continue 314 } else { 315 break 316 } 317 } 318 319 for j := 1; j <= retries; j++ { 320 // Get the layer 321 status := "Pulling fs layer" 322 if j > 1 { 323 status = fmt.Sprintf("Pulling fs layer [retries: %d]", j) 324 } 325 out.Write(sf.FormatProgress(common.TruncateID(id), status, nil)) 326 layer, err := r.GetRemoteImageLayer(img.ID, endpoint, token, int64(imgSize)) 327 if uerr, ok := err.(*url.Error); ok { 328 err = uerr.Err 329 } 330 if terr, ok := err.(net.Error); ok && terr.Timeout() && j < retries { 331 time.Sleep(time.Duration(j) * 500 * time.Millisecond) 332 continue 333 } else if err != nil { 334 out.Write(sf.FormatProgress(common.TruncateID(id), "Error pulling dependent layers", nil)) 335 return layers_downloaded, err 336 } 337 layers_downloaded = true 338 defer layer.Close() 339 340 err = s.graph.Register(img, 341 progressreader.New(progressreader.Config{ 342 In: layer, 343 Out: out, 344 Formatter: sf, 345 Size: imgSize, 346 NewLines: false, 347 ID: common.TruncateID(id), 348 Action: "Downloading", 349 })) 350 if terr, ok := err.(net.Error); ok && terr.Timeout() && j < retries { 351 time.Sleep(time.Duration(j) * 500 * time.Millisecond) 352 continue 353 } else if err != nil { 354 out.Write(sf.FormatProgress(common.TruncateID(id), "Error downloading dependent layers", nil)) 355 return layers_downloaded, err 356 } else { 357 break 358 } 359 } 360 } 361 out.Write(sf.FormatProgress(common.TruncateID(id), "Download complete", nil)) 362 } 363 return layers_downloaded, nil 364 } 365 366 func WriteStatus(requestedTag string, out io.Writer, sf *utils.StreamFormatter, layers_downloaded bool) { 367 if layers_downloaded { 368 out.Write(sf.FormatStatus("", "Status: Downloaded newer image for %s", requestedTag)) 369 } else { 370 out.Write(sf.FormatStatus("", "Status: Image is up to date for %s", requestedTag)) 371 } 372 } 373 374 // downloadInfo is used to pass information from download to extractor 375 type downloadInfo struct { 376 imgJSON []byte 377 img *image.Image 378 digest digest.Digest 379 tmpFile *os.File 380 length int64 381 downloaded bool 382 err chan error 383 } 384 385 func (s *TagStore) pullV2Repository(eng *engine.Engine, r *registry.Session, out io.Writer, repoInfo *registry.RepositoryInfo, tag string, sf *utils.StreamFormatter, parallel bool) error { 386 endpoint, err := r.V2RegistryEndpoint(repoInfo.Index) 387 if err != nil { 388 if repoInfo.Index.Official { 389 log.Debugf("Unable to pull from V2 registry, falling back to v1: %s", err) 390 return ErrV2RegistryUnavailable 391 } 392 return fmt.Errorf("error getting registry endpoint: %s", err) 393 } 394 auth, err := r.GetV2Authorization(endpoint, repoInfo.RemoteName, true) 395 if err != nil { 396 return fmt.Errorf("error getting authorization: %s", err) 397 } 398 var layersDownloaded bool 399 if tag == "" { 400 log.Debugf("Pulling tag list from V2 registry for %s", repoInfo.CanonicalName) 401 tags, err := r.GetV2RemoteTags(endpoint, repoInfo.RemoteName, auth) 402 if err != nil { 403 return err 404 } 405 if len(tags) == 0 { 406 return registry.ErrDoesNotExist 407 } 408 for _, t := range tags { 409 if downloaded, err := s.pullV2Tag(eng, r, out, endpoint, repoInfo, t, sf, parallel, auth); err != nil { 410 return err 411 } else if downloaded { 412 layersDownloaded = true 413 } 414 } 415 } else { 416 if downloaded, err := s.pullV2Tag(eng, r, out, endpoint, repoInfo, tag, sf, parallel, auth); err != nil { 417 return err 418 } else if downloaded { 419 layersDownloaded = true 420 } 421 } 422 423 requestedTag := repoInfo.CanonicalName 424 if len(tag) > 0 { 425 requestedTag = utils.ImageReference(repoInfo.CanonicalName, tag) 426 } 427 WriteStatus(requestedTag, out, sf, layersDownloaded) 428 return nil 429 } 430 431 func (s *TagStore) pullV2Tag(eng *engine.Engine, r *registry.Session, out io.Writer, endpoint *registry.Endpoint, repoInfo *registry.RepositoryInfo, tag string, sf *utils.StreamFormatter, parallel bool, auth *registry.RequestAuthorization) (bool, error) { 432 log.Debugf("Pulling tag from V2 registry: %q", tag) 433 434 manifestBytes, manifestDigest, err := r.GetV2ImageManifest(endpoint, repoInfo.RemoteName, tag, auth) 435 if err != nil { 436 return false, err 437 } 438 439 // loadManifest ensures that the manifest payload has the expected digest 440 // if the tag is a digest reference. 441 manifest, verified, err := s.loadManifest(eng, manifestBytes, manifestDigest, tag) 442 if err != nil { 443 return false, fmt.Errorf("error verifying manifest: %s", err) 444 } 445 446 if err := checkValidManifest(manifest); err != nil { 447 return false, err 448 } 449 450 if verified { 451 log.Printf("Image manifest for %s has been verified", utils.ImageReference(repoInfo.CanonicalName, tag)) 452 } 453 out.Write(sf.FormatStatus(tag, "Pulling from %s", repoInfo.CanonicalName)) 454 455 downloads := make([]downloadInfo, len(manifest.FSLayers)) 456 457 for i := len(manifest.FSLayers) - 1; i >= 0; i-- { 458 var ( 459 sumStr = manifest.FSLayers[i].BlobSum 460 imgJSON = []byte(manifest.History[i].V1Compatibility) 461 ) 462 463 img, err := image.NewImgJSON(imgJSON) 464 if err != nil { 465 return false, fmt.Errorf("failed to parse json: %s", err) 466 } 467 downloads[i].img = img 468 469 // Check if exists 470 if s.graph.Exists(img.ID) { 471 log.Debugf("Image already exists: %s", img.ID) 472 continue 473 } 474 475 dgst, err := digest.ParseDigest(sumStr) 476 if err != nil { 477 return false, err 478 } 479 downloads[i].digest = dgst 480 481 out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Pulling fs layer", nil)) 482 483 downloadFunc := func(di *downloadInfo) error { 484 log.Debugf("pulling blob %q to V1 img %s", sumStr, img.ID) 485 486 if c, err := s.poolAdd("pull", "img:"+img.ID); err != nil { 487 if c != nil { 488 out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Layer already being pulled by another client. Waiting.", nil)) 489 <-c 490 out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Download complete", nil)) 491 } else { 492 log.Debugf("Image (id: %s) pull is already running, skipping: %v", img.ID, err) 493 } 494 } else { 495 defer s.poolRemove("pull", "img:"+img.ID) 496 tmpFile, err := ioutil.TempFile("", "GetV2ImageBlob") 497 if err != nil { 498 return err 499 } 500 501 r, l, err := r.GetV2ImageBlobReader(endpoint, repoInfo.RemoteName, di.digest.Algorithm(), di.digest.Hex(), auth) 502 if err != nil { 503 return err 504 } 505 defer r.Close() 506 507 verifier, err := digest.NewDigestVerifier(di.digest) 508 if err != nil { 509 return err 510 } 511 512 if _, err := io.Copy(tmpFile, progressreader.New(progressreader.Config{ 513 In: ioutil.NopCloser(io.TeeReader(r, verifier)), 514 Out: out, 515 Formatter: sf, 516 Size: int(l), 517 NewLines: false, 518 ID: common.TruncateID(img.ID), 519 Action: "Downloading", 520 })); err != nil { 521 return fmt.Errorf("unable to copy v2 image blob data: %s", err) 522 } 523 524 out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Verifying Checksum", nil)) 525 526 if !verifier.Verified() { 527 log.Infof("Image verification failed: checksum mismatch for %q", di.digest.String()) 528 verified = false 529 } 530 531 out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Download complete", nil)) 532 533 log.Debugf("Downloaded %s to tempfile %s", img.ID, tmpFile.Name()) 534 di.tmpFile = tmpFile 535 di.length = l 536 di.downloaded = true 537 } 538 di.imgJSON = imgJSON 539 540 return nil 541 } 542 543 if parallel { 544 downloads[i].err = make(chan error) 545 go func(di *downloadInfo) { 546 di.err <- downloadFunc(di) 547 }(&downloads[i]) 548 } else { 549 err := downloadFunc(&downloads[i]) 550 if err != nil { 551 return false, err 552 } 553 } 554 } 555 556 var tagUpdated bool 557 for i := len(downloads) - 1; i >= 0; i-- { 558 d := &downloads[i] 559 if d.err != nil { 560 err := <-d.err 561 if err != nil { 562 return false, err 563 } 564 } 565 if d.downloaded { 566 // if tmpFile is empty assume download and extracted elsewhere 567 defer os.Remove(d.tmpFile.Name()) 568 defer d.tmpFile.Close() 569 d.tmpFile.Seek(0, 0) 570 if d.tmpFile != nil { 571 err = s.graph.Register(d.img, 572 progressreader.New(progressreader.Config{ 573 In: d.tmpFile, 574 Out: out, 575 Formatter: sf, 576 Size: int(d.length), 577 ID: common.TruncateID(d.img.ID), 578 Action: "Extracting", 579 })) 580 if err != nil { 581 return false, err 582 } 583 584 // FIXME: Pool release here for parallel tag pull (ensures any downloads block until fully extracted) 585 } 586 out.Write(sf.FormatProgress(common.TruncateID(d.img.ID), "Pull complete", nil)) 587 tagUpdated = true 588 } else { 589 out.Write(sf.FormatProgress(common.TruncateID(d.img.ID), "Already exists", nil)) 590 } 591 592 } 593 594 // Check for new tag if no layers downloaded 595 if !tagUpdated { 596 repo, err := s.Get(repoInfo.LocalName) 597 if err != nil { 598 return false, err 599 } 600 if repo != nil { 601 if _, exists := repo[tag]; !exists { 602 tagUpdated = true 603 } 604 } 605 } 606 607 if verified && tagUpdated { 608 out.Write(sf.FormatStatus(utils.ImageReference(repoInfo.CanonicalName, tag), "The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.")) 609 } 610 611 if manifestDigest != "" { 612 out.Write(sf.FormatStatus("", "Digest: %s", manifestDigest)) 613 } 614 615 if utils.DigestReference(tag) { 616 if err = s.SetDigest(repoInfo.LocalName, tag, downloads[0].img.ID); err != nil { 617 return false, err 618 } 619 } else { 620 // only set the repository/tag -> image ID mapping when pulling by tag (i.e. not by digest) 621 if err = s.Set(repoInfo.LocalName, tag, downloads[0].img.ID, true); err != nil { 622 return false, err 623 } 624 } 625 626 return tagUpdated, nil 627 }