proxy.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package graphdriver
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "path/filepath"
  7. "github.com/docker/docker/pkg/archive"
  8. "github.com/docker/docker/pkg/idtools"
  9. "github.com/docker/docker/pkg/plugingetter"
  10. "github.com/docker/docker/pkg/plugins"
  11. )
  12. type graphDriverProxy struct {
  13. name string
  14. p plugingetter.CompatPlugin
  15. caps Capabilities
  16. }
  17. type graphDriverRequest struct {
  18. ID string `json:",omitempty"`
  19. Parent string `json:",omitempty"`
  20. MountLabel string `json:",omitempty"`
  21. StorageOpt map[string]string `json:",omitempty"`
  22. }
  23. type graphDriverResponse struct {
  24. Err string `json:",omitempty"`
  25. Dir string `json:",omitempty"`
  26. Exists bool `json:",omitempty"`
  27. Status [][2]string `json:",omitempty"`
  28. Changes []archive.Change `json:",omitempty"`
  29. Size int64 `json:",omitempty"`
  30. Metadata map[string]string `json:",omitempty"`
  31. Capabilities Capabilities `json:",omitempty"`
  32. }
  33. type graphDriverInitRequest struct {
  34. Home string
  35. Opts []string `json:"Opts"`
  36. UIDMaps []idtools.IDMap `json:"UIDMaps"`
  37. GIDMaps []idtools.IDMap `json:"GIDMaps"`
  38. }
  39. func (d *graphDriverProxy) Init(home string, opts []string, uidMaps, gidMaps []idtools.IDMap) error {
  40. if !d.p.IsV1() {
  41. if cp, ok := d.p.(plugingetter.CountedPlugin); ok {
  42. // always acquire here, it will be cleaned up on daemon shutdown
  43. cp.Acquire()
  44. }
  45. }
  46. args := &graphDriverInitRequest{
  47. Home: home,
  48. Opts: opts,
  49. UIDMaps: uidMaps,
  50. GIDMaps: gidMaps,
  51. }
  52. var ret graphDriverResponse
  53. if err := d.p.Client().Call("GraphDriver.Init", args, &ret); err != nil {
  54. return err
  55. }
  56. if ret.Err != "" {
  57. return errors.New(ret.Err)
  58. }
  59. caps, err := d.fetchCaps()
  60. if err != nil {
  61. return err
  62. }
  63. d.caps = caps
  64. return nil
  65. }
  66. func (d *graphDriverProxy) fetchCaps() (Capabilities, error) {
  67. args := &graphDriverRequest{}
  68. var ret graphDriverResponse
  69. if err := d.p.Client().Call("GraphDriver.Capabilities", args, &ret); err != nil {
  70. if !plugins.IsNotFound(err) {
  71. return Capabilities{}, err
  72. }
  73. }
  74. return ret.Capabilities, nil
  75. }
  76. func (d *graphDriverProxy) String() string {
  77. return d.name
  78. }
  79. func (d *graphDriverProxy) Capabilities() Capabilities {
  80. return d.caps
  81. }
  82. func (d *graphDriverProxy) CreateReadWrite(id, parent string, opts *CreateOpts) error {
  83. return d.create("GraphDriver.CreateReadWrite", id, parent, opts)
  84. }
  85. func (d *graphDriverProxy) Create(id, parent string, opts *CreateOpts) error {
  86. return d.create("GraphDriver.Create", id, parent, opts)
  87. }
  88. func (d *graphDriverProxy) create(method, id, parent string, opts *CreateOpts) error {
  89. args := &graphDriverRequest{
  90. ID: id,
  91. Parent: parent,
  92. }
  93. if opts != nil {
  94. args.MountLabel = opts.MountLabel
  95. args.StorageOpt = opts.StorageOpt
  96. }
  97. var ret graphDriverResponse
  98. if err := d.p.Client().Call(method, args, &ret); err != nil {
  99. return err
  100. }
  101. if ret.Err != "" {
  102. return errors.New(ret.Err)
  103. }
  104. return nil
  105. }
  106. func (d *graphDriverProxy) Remove(id string) error {
  107. args := &graphDriverRequest{ID: id}
  108. var ret graphDriverResponse
  109. if err := d.p.Client().Call("GraphDriver.Remove", args, &ret); err != nil {
  110. return err
  111. }
  112. if ret.Err != "" {
  113. return errors.New(ret.Err)
  114. }
  115. return nil
  116. }
  117. func (d *graphDriverProxy) Get(id, mountLabel string) (string, error) {
  118. args := &graphDriverRequest{
  119. ID: id,
  120. MountLabel: mountLabel,
  121. }
  122. var ret graphDriverResponse
  123. if err := d.p.Client().Call("GraphDriver.Get", args, &ret); err != nil {
  124. return "", err
  125. }
  126. var err error
  127. if ret.Err != "" {
  128. err = errors.New(ret.Err)
  129. }
  130. return filepath.Join(d.p.BasePath(), ret.Dir), err
  131. }
  132. func (d *graphDriverProxy) Put(id string) error {
  133. args := &graphDriverRequest{ID: id}
  134. var ret graphDriverResponse
  135. if err := d.p.Client().Call("GraphDriver.Put", args, &ret); err != nil {
  136. return err
  137. }
  138. if ret.Err != "" {
  139. return errors.New(ret.Err)
  140. }
  141. return nil
  142. }
  143. func (d *graphDriverProxy) Exists(id string) bool {
  144. args := &graphDriverRequest{ID: id}
  145. var ret graphDriverResponse
  146. if err := d.p.Client().Call("GraphDriver.Exists", args, &ret); err != nil {
  147. return false
  148. }
  149. return ret.Exists
  150. }
  151. func (d *graphDriverProxy) Status() [][2]string {
  152. args := &graphDriverRequest{}
  153. var ret graphDriverResponse
  154. if err := d.p.Client().Call("GraphDriver.Status", args, &ret); err != nil {
  155. return nil
  156. }
  157. return ret.Status
  158. }
  159. func (d *graphDriverProxy) GetMetadata(id string) (map[string]string, error) {
  160. args := &graphDriverRequest{
  161. ID: id,
  162. }
  163. var ret graphDriverResponse
  164. if err := d.p.Client().Call("GraphDriver.GetMetadata", args, &ret); err != nil {
  165. return nil, err
  166. }
  167. if ret.Err != "" {
  168. return nil, errors.New(ret.Err)
  169. }
  170. return ret.Metadata, nil
  171. }
  172. func (d *graphDriverProxy) Cleanup() error {
  173. if !d.p.IsV1() {
  174. if cp, ok := d.p.(plugingetter.CountedPlugin); ok {
  175. // always release
  176. defer cp.Release()
  177. }
  178. }
  179. args := &graphDriverRequest{}
  180. var ret graphDriverResponse
  181. if err := d.p.Client().Call("GraphDriver.Cleanup", args, &ret); err != nil {
  182. return nil
  183. }
  184. if ret.Err != "" {
  185. return errors.New(ret.Err)
  186. }
  187. return nil
  188. }
  189. func (d *graphDriverProxy) Diff(id, parent string) (io.ReadCloser, error) {
  190. args := &graphDriverRequest{
  191. ID: id,
  192. Parent: parent,
  193. }
  194. body, err := d.p.Client().Stream("GraphDriver.Diff", args)
  195. if err != nil {
  196. return nil, err
  197. }
  198. return body, nil
  199. }
  200. func (d *graphDriverProxy) Changes(id, parent string) ([]archive.Change, error) {
  201. args := &graphDriverRequest{
  202. ID: id,
  203. Parent: parent,
  204. }
  205. var ret graphDriverResponse
  206. if err := d.p.Client().Call("GraphDriver.Changes", args, &ret); err != nil {
  207. return nil, err
  208. }
  209. if ret.Err != "" {
  210. return nil, errors.New(ret.Err)
  211. }
  212. return ret.Changes, nil
  213. }
  214. func (d *graphDriverProxy) ApplyDiff(id, parent string, diff io.Reader) (int64, error) {
  215. var ret graphDriverResponse
  216. if err := d.p.Client().SendFile(fmt.Sprintf("GraphDriver.ApplyDiff?id=%s&parent=%s", id, parent), diff, &ret); err != nil {
  217. return -1, err
  218. }
  219. if ret.Err != "" {
  220. return -1, errors.New(ret.Err)
  221. }
  222. return ret.Size, nil
  223. }
  224. func (d *graphDriverProxy) DiffSize(id, parent string) (int64, error) {
  225. args := &graphDriverRequest{
  226. ID: id,
  227. Parent: parent,
  228. }
  229. var ret graphDriverResponse
  230. if err := d.p.Client().Call("GraphDriver.DiffSize", args, &ret); err != nil {
  231. return -1, err
  232. }
  233. if ret.Err != "" {
  234. return -1, errors.New(ret.Err)
  235. }
  236. return ret.Size, nil
  237. }