proxy.go 6.3 KB

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