github.com/nullne/docker@v1.13.0-rc1/client/plugin_install.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"golang.org/x/net/context"
    10  )
    11  
    12  // PluginInstall installs a plugin
    13  func (cli *Client) PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) error {
    14  	// FIXME(vdemeester) name is a ref, we might want to parse/validate it here.
    15  	query := url.Values{}
    16  	query.Set("name", name)
    17  	resp, err := cli.tryPluginPull(ctx, query, options.RegistryAuth)
    18  	if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
    19  		newAuthHeader, privilegeErr := options.PrivilegeFunc()
    20  		if privilegeErr != nil {
    21  			ensureReaderClosed(resp)
    22  			return privilegeErr
    23  		}
    24  		resp, err = cli.tryPluginPull(ctx, query, newAuthHeader)
    25  	}
    26  	if err != nil {
    27  		ensureReaderClosed(resp)
    28  		return err
    29  	}
    30  	var privileges types.PluginPrivileges
    31  	if err := json.NewDecoder(resp.body).Decode(&privileges); err != nil {
    32  		ensureReaderClosed(resp)
    33  		return err
    34  	}
    35  	ensureReaderClosed(resp)
    36  
    37  	if !options.AcceptAllPermissions && options.AcceptPermissionsFunc != nil && len(privileges) > 0 {
    38  		accept, err := options.AcceptPermissionsFunc(privileges)
    39  		if err != nil {
    40  			return err
    41  		}
    42  		if !accept {
    43  			resp, _ := cli.delete(ctx, "/plugins/"+name, nil, nil)
    44  			ensureReaderClosed(resp)
    45  			return pluginPermissionDenied{name}
    46  		}
    47  	}
    48  
    49  	if len(options.Args) > 0 {
    50  		if err := cli.PluginSet(ctx, name, options.Args); err != nil {
    51  			return err
    52  		}
    53  	}
    54  
    55  	if options.Disabled {
    56  		return nil
    57  	}
    58  
    59  	return cli.PluginEnable(ctx, name)
    60  }
    61  
    62  func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {
    63  	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
    64  	return cli.post(ctx, "/plugins/pull", query, nil, headers)
    65  }