proxy.go 6.2 KB

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