proxy.go 5.9 KB

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