pull.go 18 KB

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