pull.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // +build experimental
  2. package distribution
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "github.com/Sirupsen/logrus"
  12. "github.com/docker/distribution"
  13. "github.com/docker/distribution/manifest/schema2"
  14. dockerdist "github.com/docker/docker/distribution"
  15. archive "github.com/docker/docker/pkg/chrootarchive"
  16. "github.com/docker/docker/reference"
  17. "github.com/docker/docker/registry"
  18. "github.com/docker/engine-api/types"
  19. "golang.org/x/net/context"
  20. )
  21. // PullData is the plugin manifest and the rootfs
  22. type PullData interface {
  23. Config() ([]byte, error)
  24. Layer() (io.ReadCloser, error)
  25. }
  26. type pullData struct {
  27. repository distribution.Repository
  28. manifest schema2.Manifest
  29. index int
  30. }
  31. func (pd *pullData) Config() ([]byte, error) {
  32. blobs := pd.repository.Blobs(context.Background())
  33. config, err := blobs.Get(context.Background(), pd.manifest.Config.Digest)
  34. if err != nil {
  35. return nil, err
  36. }
  37. // validate
  38. var p types.Plugin
  39. if err := json.Unmarshal(config, &p); err != nil {
  40. return nil, err
  41. }
  42. return config, nil
  43. }
  44. func (pd *pullData) Layer() (io.ReadCloser, error) {
  45. if pd.index >= len(pd.manifest.Layers) {
  46. return nil, io.EOF
  47. }
  48. blobs := pd.repository.Blobs(context.Background())
  49. rsc, err := blobs.Open(context.Background(), pd.manifest.Layers[pd.index].Digest)
  50. if err != nil {
  51. return nil, err
  52. }
  53. pd.index++
  54. return rsc, nil
  55. }
  56. // Pull downloads the plugin from Store
  57. func Pull(name string, rs registry.Service, metaheader http.Header, authConfig *types.AuthConfig) (PullData, error) {
  58. ref, err := reference.ParseNamed(name)
  59. if err != nil {
  60. logrus.Debugf("pull.go: error in ParseNamed: %v", err)
  61. return nil, err
  62. }
  63. repoInfo, err := rs.ResolveRepository(ref)
  64. if err != nil {
  65. logrus.Debugf("pull.go: error in ResolveRepository: %v", err)
  66. return nil, err
  67. }
  68. if err := dockerdist.ValidateRepoName(repoInfo.Name()); err != nil {
  69. logrus.Debugf("pull.go: error in ValidateRepoName: %v", err)
  70. return nil, err
  71. }
  72. endpoints, err := rs.LookupPullEndpoints(repoInfo.Hostname())
  73. if err != nil {
  74. logrus.Debugf("pull.go: error in LookupPullEndpoints: %v", err)
  75. return nil, err
  76. }
  77. var confirmedV2 bool
  78. var repository distribution.Repository
  79. for _, endpoint := range endpoints {
  80. if confirmedV2 && endpoint.Version == registry.APIVersion1 {
  81. logrus.Debugf("Skipping v1 endpoint %s because v2 registry was detected", endpoint.URL)
  82. continue
  83. }
  84. // TODO: reuse contexts
  85. repository, confirmedV2, err = dockerdist.NewV2Repository(context.Background(), repoInfo, endpoint, metaheader, authConfig, "pull")
  86. if err != nil {
  87. logrus.Debugf("pull.go: error in NewV2Repository: %v", err)
  88. return nil, err
  89. }
  90. if !confirmedV2 {
  91. logrus.Debugf("pull.go: !confirmedV2")
  92. return nil, ErrUnsupportedRegistry
  93. }
  94. logrus.Debugf("Trying to pull %s from %s %s", repoInfo.Name(), endpoint.URL, endpoint.Version)
  95. break
  96. }
  97. tag := DefaultTag
  98. if ref, ok := ref.(reference.NamedTagged); ok {
  99. tag = ref.Tag()
  100. }
  101. // tags := repository.Tags(context.Background())
  102. // desc, err := tags.Get(context.Background(), tag)
  103. // if err != nil {
  104. // return nil, err
  105. // }
  106. //
  107. msv, err := repository.Manifests(context.Background())
  108. if err != nil {
  109. logrus.Debugf("pull.go: error in repository.Manifests: %v", err)
  110. return nil, err
  111. }
  112. manifest, err := msv.Get(context.Background(), "", distribution.WithTag(tag))
  113. if err != nil {
  114. // TODO: change 401 to 404
  115. logrus.Debugf("pull.go: error in msv.Get(): %v", err)
  116. return nil, err
  117. }
  118. _, pl, err := manifest.Payload()
  119. if err != nil {
  120. logrus.Debugf("pull.go: error in manifest.Payload(): %v", err)
  121. return nil, err
  122. }
  123. var m schema2.Manifest
  124. if err := json.Unmarshal(pl, &m); err != nil {
  125. logrus.Debugf("pull.go: error in json.Unmarshal(): %v", err)
  126. return nil, err
  127. }
  128. if m.Config.MediaType != MediaTypeConfig {
  129. return nil, ErrUnsupportedMediaType
  130. }
  131. pd := &pullData{
  132. repository: repository,
  133. manifest: m,
  134. }
  135. logrus.Debugf("manifest: %s", pl)
  136. return pd, nil
  137. }
  138. // WritePullData extracts manifest and rootfs to the disk.
  139. func WritePullData(pd PullData, dest string, extract bool) error {
  140. config, err := pd.Config()
  141. if err != nil {
  142. return err
  143. }
  144. var p types.Plugin
  145. if err := json.Unmarshal(config, &p); err != nil {
  146. return err
  147. }
  148. logrus.Debugf("%#v", p)
  149. if err := os.MkdirAll(dest, 0700); err != nil {
  150. return err
  151. }
  152. if extract {
  153. if err := ioutil.WriteFile(filepath.Join(dest, "manifest.json"), config, 0600); err != nil {
  154. return err
  155. }
  156. if err := os.MkdirAll(filepath.Join(dest, "rootfs"), 0700); err != nil {
  157. return err
  158. }
  159. }
  160. for i := 0; ; i++ {
  161. l, err := pd.Layer()
  162. if err == io.EOF {
  163. break
  164. }
  165. if err != nil {
  166. return err
  167. }
  168. if !extract {
  169. f, err := os.Create(filepath.Join(dest, fmt.Sprintf("layer%d.tar", i)))
  170. if err != nil {
  171. l.Close()
  172. return err
  173. }
  174. io.Copy(f, l)
  175. l.Close()
  176. f.Close()
  177. continue
  178. }
  179. if _, err := archive.ApplyLayer(filepath.Join(dest, "rootfs"), l); err != nil {
  180. return err
  181. }
  182. }
  183. return nil
  184. }