2016-09-06 14:46:37 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-12-12 18:05:53 -05:00
|
|
|
"io"
|
2016-09-06 14:46:37 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-12-12 18:05:53 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-06 14:46:37 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PluginInstall installs a plugin
|
2016-12-12 18:05:53 -05:00
|
|
|
func (cli *Client) PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) (rc io.ReadCloser, err error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
query := url.Values{}
|
2016-12-12 18:05:53 -05:00
|
|
|
if _, err := reference.ParseNamed(options.RemoteRef); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "invalid remote reference")
|
|
|
|
}
|
|
|
|
query.Set("remote", options.RemoteRef)
|
|
|
|
|
2016-11-23 20:29:21 -05:00
|
|
|
resp, err := cli.tryPluginPrivileges(ctx, query, options.RegistryAuth)
|
2016-09-06 14:46:37 -04:00
|
|
|
if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
|
2016-12-12 18:05:53 -05:00
|
|
|
// todo: do inspect before to check existing name before checking privileges
|
2016-09-06 14:46:37 -04:00
|
|
|
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
|
|
|
if privilegeErr != nil {
|
|
|
|
ensureReaderClosed(resp)
|
2016-12-12 18:05:53 -05:00
|
|
|
return nil, privilegeErr
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2016-11-23 20:29:21 -05:00
|
|
|
options.RegistryAuth = newAuthHeader
|
|
|
|
resp, err = cli.tryPluginPrivileges(ctx, query, options.RegistryAuth)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
ensureReaderClosed(resp)
|
2016-12-12 18:05:53 -05:00
|
|
|
return nil, err
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2016-11-16 17:42:46 -05:00
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
var privileges types.PluginPrivileges
|
|
|
|
if err := json.NewDecoder(resp.body).Decode(&privileges); err != nil {
|
|
|
|
ensureReaderClosed(resp)
|
2016-12-12 18:05:53 -05:00
|
|
|
return nil, err
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
ensureReaderClosed(resp)
|
|
|
|
|
|
|
|
if !options.AcceptAllPermissions && options.AcceptPermissionsFunc != nil && len(privileges) > 0 {
|
|
|
|
accept, err := options.AcceptPermissionsFunc(privileges)
|
|
|
|
if err != nil {
|
2016-12-12 18:05:53 -05:00
|
|
|
return nil, err
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
if !accept {
|
2016-12-12 18:05:53 -05:00
|
|
|
return nil, pluginPermissionDenied{options.RemoteRef}
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-07 20:43:11 -05:00
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
// set name for plugin pull, if empty should default to remote reference
|
|
|
|
query.Set("name", name)
|
|
|
|
|
|
|
|
resp, err = cli.tryPluginPull(ctx, query, privileges, options.RegistryAuth)
|
2016-11-23 20:29:21 -05:00
|
|
|
if err != nil {
|
2016-12-12 18:05:53 -05:00
|
|
|
return nil, err
|
2016-11-23 20:29:21 -05:00
|
|
|
}
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
name = resp.header.Get("Docker-Plugin-Name")
|
|
|
|
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() { // todo: the client should probably be designed more around the actual api
|
|
|
|
_, err := io.Copy(pw, resp.body)
|
2016-11-23 20:29:21 -05:00
|
|
|
if err != nil {
|
2016-12-12 18:05:53 -05:00
|
|
|
pw.CloseWithError(err)
|
|
|
|
return
|
2016-11-23 20:29:21 -05:00
|
|
|
}
|
2016-12-12 18:05:53 -05:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
delResp, _ := cli.delete(ctx, "/plugins/"+name, nil, nil)
|
|
|
|
ensureReaderClosed(delResp)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if len(options.Args) > 0 {
|
|
|
|
if err := cli.PluginSet(ctx, name, options.Args); err != nil {
|
|
|
|
pw.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-07 20:43:11 -05:00
|
|
|
}
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
if options.Disabled {
|
|
|
|
pw.Close()
|
|
|
|
return
|
|
|
|
}
|
2016-11-07 20:43:11 -05:00
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
err = cli.PluginEnable(ctx, name, types.PluginEnableOptions{Timeout: 0})
|
|
|
|
pw.CloseWithError(err)
|
|
|
|
}()
|
|
|
|
return pr, nil
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2016-11-23 20:29:21 -05:00
|
|
|
func (cli *Client) tryPluginPrivileges(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {
|
|
|
|
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
|
|
|
|
return cli.get(ctx, "/plugins/privileges", query, headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, privileges types.PluginPrivileges, registryAuth string) (serverResponse, error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
|
2016-11-23 20:29:21 -05:00
|
|
|
return cli.post(ctx, "/plugins/pull", query, privileges, headers)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|