proxy.go 6.4 KB

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