2016-09-06 14:46:37 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PluginInstall installs a plugin
|
2016-11-16 17:42:46 -05:00
|
|
|
func (cli *Client) PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) (err error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
// FIXME(vdemeester) name is a ref, we might want to parse/validate it here.
|
|
|
|
query := url.Values{}
|
|
|
|
query.Set("name", name)
|
|
|
|
resp, err := cli.tryPluginPull(ctx, query, options.RegistryAuth)
|
|
|
|
if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
|
|
|
|
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
|
|
|
if privilegeErr != nil {
|
|
|
|
ensureReaderClosed(resp)
|
|
|
|
return privilegeErr
|
|
|
|
}
|
|
|
|
resp, err = cli.tryPluginPull(ctx, query, newAuthHeader)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
ensureReaderClosed(resp)
|
|
|
|
return err
|
|
|
|
}
|
2016-11-16 17:42:46 -05:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
delResp, _ := cli.delete(ctx, "/plugins/"+name, nil, nil)
|
|
|
|
ensureReaderClosed(delResp)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
var privileges types.PluginPrivileges
|
|
|
|
if err := json.NewDecoder(resp.body).Decode(&privileges); err != nil {
|
|
|
|
ensureReaderClosed(resp)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ensureReaderClosed(resp)
|
|
|
|
|
|
|
|
if !options.AcceptAllPermissions && options.AcceptPermissionsFunc != nil && len(privileges) > 0 {
|
|
|
|
accept, err := options.AcceptPermissionsFunc(privileges)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !accept {
|
|
|
|
return pluginPermissionDenied{name}
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 20:43:11 -05:00
|
|
|
|
|
|
|
if len(options.Args) > 0 {
|
|
|
|
if err := cli.PluginSet(ctx, name, options.Args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
if options.Disabled {
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-07 20:43:11 -05:00
|
|
|
|
2016-11-21 12:24:01 -05:00
|
|
|
return cli.PluginEnable(ctx, name, types.PluginEnableOptions{Timeout: 0})
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {
|
|
|
|
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
|
|
|
|
return cli.post(ctx, "/plugins/pull", query, nil, headers)
|
|
|
|
}
|