pull.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. repoInfo.Class = "plugin"
  79. if err := dockerdist.ValidateRepoName(repoInfo.Name()); err != nil {
  80. logrus.Debugf("pull.go: error in ValidateRepoName: %v", err)
  81. return nil, err
  82. }
  83. endpoints, err := rs.LookupPullEndpoints(repoInfo.Hostname())
  84. if err != nil {
  85. logrus.Debugf("pull.go: error in LookupPullEndpoints: %v", err)
  86. return nil, err
  87. }
  88. var confirmedV2 bool
  89. var repository distribution.Repository
  90. for _, endpoint := range endpoints {
  91. if confirmedV2 && endpoint.Version == registry.APIVersion1 {
  92. logrus.Debugf("Skipping v1 endpoint %s because v2 registry was detected", endpoint.URL)
  93. continue
  94. }
  95. // TODO: reuse contexts
  96. repository, confirmedV2, err = dockerdist.NewV2Repository(context.Background(), repoInfo, endpoint, metaheader, authConfig, "pull")
  97. if err != nil {
  98. logrus.Debugf("pull.go: error in NewV2Repository: %v", err)
  99. return nil, err
  100. }
  101. if !confirmedV2 {
  102. logrus.Debug("pull.go: !confirmedV2")
  103. return nil, ErrUnsupportedRegistry
  104. }
  105. logrus.Debugf("Trying to pull %s from %s %s", repoInfo.Name(), endpoint.URL, endpoint.Version)
  106. break
  107. }
  108. tag := DefaultTag
  109. if ref, ok := ref.(reference.NamedTagged); ok {
  110. tag = ref.Tag()
  111. }
  112. // tags := repository.Tags(context.Background())
  113. // desc, err := tags.Get(context.Background(), tag)
  114. // if err != nil {
  115. // return nil, err
  116. // }
  117. //
  118. msv, err := repository.Manifests(context.Background())
  119. if err != nil {
  120. logrus.Debugf("pull.go: error in repository.Manifests: %v", err)
  121. return nil, err
  122. }
  123. manifest, err := msv.Get(context.Background(), "", distribution.WithTag(tag))
  124. if err != nil {
  125. logrus.Debugf("pull.go: error in msv.Get(): %v", err)
  126. return nil, dockerdist.TranslatePullError(err, repoInfo)
  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. return nil, ErrUnsupportedMediaType
  140. }
  141. pd := &pullData{
  142. repository: repository,
  143. manifest: m,
  144. }
  145. logrus.Debugf("manifest: %s", pl)
  146. return pd, nil
  147. }
  148. // WritePullData extracts manifest and rootfs to the disk.
  149. func WritePullData(pd PullData, dest string, extract bool) error {
  150. config, err := pd.Config()
  151. if err != nil {
  152. return err
  153. }
  154. var p types.Plugin
  155. if err := json.Unmarshal(config, &p); err != nil {
  156. return err
  157. }
  158. logrus.Debugf("plugin: %#v", p)
  159. if err := os.MkdirAll(dest, 0700); err != nil {
  160. return err
  161. }
  162. if extract {
  163. if err := ioutil.WriteFile(filepath.Join(dest, "config.json"), config, 0600); err != nil {
  164. return err
  165. }
  166. if err := os.MkdirAll(filepath.Join(dest, "rootfs"), 0700); err != nil {
  167. return err
  168. }
  169. }
  170. for i := 0; ; i++ {
  171. l, err := pd.Layer()
  172. if err == io.EOF {
  173. break
  174. }
  175. if err != nil {
  176. return err
  177. }
  178. if !extract {
  179. f, err := os.Create(filepath.Join(dest, fmt.Sprintf("layer%d.tar", i)))
  180. if err != nil {
  181. l.Close()
  182. return err
  183. }
  184. io.Copy(f, l)
  185. l.Close()
  186. f.Close()
  187. continue
  188. }
  189. if _, err := archive.ApplyLayer(filepath.Join(dest, "rootfs"), l); err != nil {
  190. return err
  191. }
  192. }
  193. return nil
  194. }