proxy.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // +build experimental
  2. package graphdriver
  3. import (
  4. "errors"
  5. "fmt"
  6. "github.com/docker/docker/pkg/archive"
  7. )
  8. type graphDriverProxy struct {
  9. name string
  10. client pluginClient
  11. }
  12. type graphDriverRequest struct {
  13. ID string `json:",omitempty"`
  14. Parent string `json:",omitempty"`
  15. MountLabel string `json:",omitempty"`
  16. }
  17. type graphDriverResponse struct {
  18. Err string `json:",omitempty"`
  19. Dir string `json:",omitempty"`
  20. Exists bool `json:",omitempty"`
  21. Status [][2]string `json:",omitempty"`
  22. Changes []archive.Change `json:",omitempty"`
  23. Size int64 `json:",omitempty"`
  24. Metadata map[string]string `json:",omitempty"`
  25. }
  26. type graphDriverInitRequest struct {
  27. Home string
  28. Opts []string
  29. }
  30. func (d *graphDriverProxy) Init(home string, opts []string) error {
  31. args := &graphDriverInitRequest{
  32. Home: home,
  33. Opts: opts,
  34. }
  35. var ret graphDriverResponse
  36. if err := d.client.Call("GraphDriver.Init", args, &ret); err != nil {
  37. return err
  38. }
  39. if ret.Err != "" {
  40. return errors.New(ret.Err)
  41. }
  42. return nil
  43. }
  44. func (d *graphDriverProxy) String() string {
  45. return d.name
  46. }
  47. func (d *graphDriverProxy) Create(id, parent, mountLabel string, storageOpt map[string]string) error {
  48. args := &graphDriverRequest{
  49. ID: id,
  50. Parent: parent,
  51. MountLabel: mountLabel,
  52. }
  53. var ret graphDriverResponse
  54. if err := d.client.Call("GraphDriver.Create", args, &ret); err != nil {
  55. return err
  56. }
  57. if ret.Err != "" {
  58. return errors.New(ret.Err)
  59. }
  60. return nil
  61. }
  62. func (d *graphDriverProxy) Remove(id string) error {
  63. args := &graphDriverRequest{ID: id}
  64. var ret graphDriverResponse
  65. if err := d.client.Call("GraphDriver.Remove", args, &ret); err != nil {
  66. return err
  67. }
  68. if ret.Err != "" {
  69. return errors.New(ret.Err)
  70. }
  71. return nil
  72. }
  73. func (d *graphDriverProxy) Get(id, mountLabel string) (string, error) {
  74. args := &graphDriverRequest{
  75. ID: id,
  76. MountLabel: mountLabel,
  77. }
  78. var ret graphDriverResponse
  79. if err := d.client.Call("GraphDriver.Get", args, &ret); err != nil {
  80. return "", err
  81. }
  82. var err error
  83. if ret.Err != "" {
  84. err = errors.New(ret.Err)
  85. }
  86. return ret.Dir, err
  87. }
  88. func (d *graphDriverProxy) Put(id string) error {
  89. args := &graphDriverRequest{ID: id}
  90. var ret graphDriverResponse
  91. if err := d.client.Call("GraphDriver.Put", args, &ret); err != nil {
  92. return err
  93. }
  94. if ret.Err != "" {
  95. return errors.New(ret.Err)
  96. }
  97. return nil
  98. }
  99. func (d *graphDriverProxy) Exists(id string) bool {
  100. args := &graphDriverRequest{ID: id}
  101. var ret graphDriverResponse
  102. if err := d.client.Call("GraphDriver.Exists", args, &ret); err != nil {
  103. return false
  104. }
  105. return ret.Exists
  106. }
  107. func (d *graphDriverProxy) Status() [][2]string {
  108. args := &graphDriverRequest{}
  109. var ret graphDriverResponse
  110. if err := d.client.Call("GraphDriver.Status", args, &ret); err != nil {
  111. return nil
  112. }
  113. return ret.Status
  114. }
  115. func (d *graphDriverProxy) GetMetadata(id string) (map[string]string, error) {
  116. args := &graphDriverRequest{
  117. ID: id,
  118. }
  119. var ret graphDriverResponse
  120. if err := d.client.Call("GraphDriver.GetMetadata", args, &ret); err != nil {
  121. return nil, err
  122. }
  123. if ret.Err != "" {
  124. return nil, errors.New(ret.Err)
  125. }
  126. return ret.Metadata, nil
  127. }
  128. func (d *graphDriverProxy) Cleanup() error {
  129. args := &graphDriverRequest{}
  130. var ret graphDriverResponse
  131. if err := d.client.Call("GraphDriver.Cleanup", args, &ret); err != nil {
  132. return nil
  133. }
  134. if ret.Err != "" {
  135. return errors.New(ret.Err)
  136. }
  137. return nil
  138. }
  139. func (d *graphDriverProxy) Diff(id, parent string) (archive.Archive, error) {
  140. args := &graphDriverRequest{
  141. ID: id,
  142. Parent: parent,
  143. }
  144. body, err := d.client.Stream("GraphDriver.Diff", args)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return archive.Archive(body), nil
  149. }
  150. func (d *graphDriverProxy) Changes(id, parent string) ([]archive.Change, error) {
  151. args := &graphDriverRequest{
  152. ID: id,
  153. Parent: parent,
  154. }
  155. var ret graphDriverResponse
  156. if err := d.client.Call("GraphDriver.Changes", args, &ret); err != nil {
  157. return nil, err
  158. }
  159. if ret.Err != "" {
  160. return nil, errors.New(ret.Err)
  161. }
  162. return ret.Changes, nil
  163. }
  164. func (d *graphDriverProxy) ApplyDiff(id, parent string, diff archive.Reader) (int64, error) {
  165. var ret graphDriverResponse
  166. if err := d.client.SendFile(fmt.Sprintf("GraphDriver.ApplyDiff?id=%s&parent=%s", id, parent), diff, &ret); err != nil {
  167. return -1, err
  168. }
  169. if ret.Err != "" {
  170. return -1, errors.New(ret.Err)
  171. }
  172. return ret.Size, nil
  173. }
  174. func (d *graphDriverProxy) DiffSize(id, parent string) (int64, error) {
  175. args := &graphDriverRequest{
  176. ID: id,
  177. Parent: parent,
  178. }
  179. var ret graphDriverResponse
  180. if err := d.client.Call("GraphDriver.DiffSize", args, &ret); err != nil {
  181. return -1, err
  182. }
  183. if ret.Err != "" {
  184. return -1, errors.New(ret.Err)
  185. }
  186. return ret.Size, nil
  187. }