pull.go 16 KB

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