github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/daemon/graphdriver/proxy.go (about)

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