Merge pull request #23570 from vdemeester/change-plugin-install

Update docker plugin install code…
Upstream-commit: 7ef9aec90f4f129945ab2f1e2c79b961a7af2209
Component: engine
This commit is contained in:
Arnaud Porterie
2016-06-17 00:12:56 +00:00
committed by GitHub
12 changed files with 91 additions and 40 deletions

View File

@ -101,7 +101,7 @@ type NodeAPIClient interface {
// ServiceAPIClient defines API client methods for the services
type ServiceAPIClient interface {
ServiceCreate(ctx context.Context, service swarm.ServiceSpec) (types.ServiceCreateResponse, error)
ServiceInspect(ctx context.Context, serviceID string) (swarm.Service, error)
ServiceInspectWithRaw(ctx context.Context, serviceID string) (swarm.Service, []byte, error)
ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
ServiceRemove(ctx context.Context, serviceID string) error
ServiceUpdate(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec) error

View File

@ -3,8 +3,6 @@
package client
import (
"io"
"github.com/docker/engine-api/types"
"golang.org/x/net/context"
)
@ -29,7 +27,7 @@ type PluginAPIClient interface {
PluginRemove(ctx context.Context, name string) error
PluginEnable(ctx context.Context, name string) error
PluginDisable(ctx context.Context, name string) error
PluginInstall(ctx context.Context, name, registryAuth string, acceptAllPermissions, noEnable bool, in io.ReadCloser, out io.Writer) error
PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) error
PluginPush(ctx context.Context, name string, registryAuth string) error
PluginSet(ctx context.Context, name string, args []string) error
PluginInspect(ctx context.Context, name string) (*types.Plugin, error)

View File

@ -3,21 +3,28 @@
package client
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/docker/engine-api/types"
"golang.org/x/net/context"
)
// PluginInstall installs a plugin
func (cli *Client) PluginInstall(ctx context.Context, name, registryAuth string, acceptAllPermissions, noEnable bool, in io.ReadCloser, out io.Writer) error {
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
resp, err := cli.post(ctx, "/plugins/pull", url.Values{"name": []string{name}}, nil, headers)
func (cli *Client) PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) error {
// 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
@ -28,27 +35,24 @@ func (cli *Client) PluginInstall(ctx context.Context, name, registryAuth string,
}
ensureReaderClosed(resp)
if !acceptAllPermissions && len(privileges) > 0 {
fmt.Fprintf(out, "Plugin %q requested the following privileges:\n", name)
for _, privilege := range privileges {
fmt.Fprintf(out, " - %s: %v\n", privilege.Name, privilege.Value)
}
fmt.Fprint(out, "Do you grant the above permissions? [y/N] ")
reader := bufio.NewReader(in)
line, _, err := reader.ReadLine()
if !options.AcceptAllPermissions && options.AcceptPermissionsFunc != nil && len(privileges) > 0 {
accept, err := options.AcceptPermissionsFunc(privileges)
if err != nil {
return err
}
if strings.ToLower(string(line)) != "y" {
if !accept {
resp, _ := cli.delete(ctx, "/plugins/"+name, nil, nil)
ensureReaderClosed(resp)
return pluginPermissionDenied{name}
}
}
if noEnable {
if options.Disabled {
return nil
}
return cli.PluginEnable(ctx, name)
}
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)
}

View File

@ -1,25 +1,33 @@
package client
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/docker/engine-api/types/swarm"
"golang.org/x/net/context"
)
// ServiceInspect returns the service information.
func (cli *Client) ServiceInspect(ctx context.Context, serviceID string) (swarm.Service, error) {
// ServiceInspectWithRaw returns the service information and the raw data.
func (cli *Client) ServiceInspectWithRaw(ctx context.Context, serviceID string) (swarm.Service, []byte, error) {
serverResp, err := cli.get(ctx, "/services/"+serviceID, nil, nil)
if err != nil {
if serverResp.statusCode == http.StatusNotFound {
return swarm.Service{}, serviceNotFoundError{serviceID}
return swarm.Service{}, nil, serviceNotFoundError{serviceID}
}
return swarm.Service{}, err
return swarm.Service{}, nil, err
}
defer ensureReaderClosed(serverResp)
body, err := ioutil.ReadAll(serverResp.body)
if err != nil {
return swarm.Service{}, nil, err
}
var response swarm.Service
err = json.NewDecoder(serverResp.body).Decode(&response)
ensureReaderClosed(serverResp)
return response, err
rdr := bytes.NewReader(body)
err = json.NewDecoder(rdr).Decode(&response)
return response, body, err
}

View File

@ -7,6 +7,15 @@ import (
"fmt"
)
// PluginInstallOptions holds parameters to install a plugin.
type PluginInstallOptions struct {
Disabled bool
AcceptAllPermissions bool
RegistryAuth string // RegistryAuth is the base64 encoded credentials for the registry
PrivilegeFunc RequestPrivilegeFunc
AcceptPermissionsFunc func(PluginPrivileges) (bool, error)
}
// PluginConfig represents the values of settings potentially modifiable by a user
type PluginConfig struct {
Mounts []PluginMount