pull.go 5.3 KB

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