2016-05-16 11:50:55 -04:00
|
|
|
// +build experimental
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2016-06-16 18:33:21 -04:00
|
|
|
"bufio"
|
2016-05-16 11:50:55 -04:00
|
|
|
"fmt"
|
2016-06-16 18:33:21 -04:00
|
|
|
"strings"
|
2016-05-16 11:50:55 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/reference"
|
|
|
|
"github.com/docker/docker/registry"
|
2016-06-16 18:33:21 -04:00
|
|
|
"github.com/docker/engine-api/types"
|
2016-05-16 11:50:55 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-06-15 13:29:01 -04:00
|
|
|
type pluginOptions struct {
|
|
|
|
name string
|
|
|
|
grantPerms bool
|
2016-06-17 10:44:57 -04:00
|
|
|
disable bool
|
2016-06-15 13:29:01 -04:00
|
|
|
}
|
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
func newInstallCommand(dockerCli *client.DockerCli) *cobra.Command {
|
2016-06-15 13:29:01 -04:00
|
|
|
var options pluginOptions
|
2016-05-16 11:50:55 -04:00
|
|
|
cmd := &cobra.Command{
|
2016-06-17 12:12:37 -04:00
|
|
|
Use: "install PLUGIN",
|
2016-05-16 11:50:55 -04:00
|
|
|
Short: "Install a plugin",
|
|
|
|
Args: cli.RequiresMinArgs(1), // TODO: allow for set args
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-06-15 13:29:01 -04:00
|
|
|
options.name = args[0]
|
|
|
|
return runInstall(dockerCli, options)
|
2016-05-16 11:50:55 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:29:01 -04:00
|
|
|
flags := cmd.Flags()
|
2016-06-17 12:12:37 -04:00
|
|
|
flags.BoolVar(&options.grantPerms, "grant-all-permissions", false, "grant all permissions necessary to run the plugin")
|
2016-06-17 10:44:57 -04:00
|
|
|
flags.BoolVar(&options.disable, "disable", false, "do not enable the plugin on install")
|
2016-06-15 13:29:01 -04:00
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-06-16 18:33:21 -04:00
|
|
|
func runInstall(dockerCli *client.DockerCli, opts pluginOptions) error {
|
|
|
|
named, err := reference.ParseNamed(opts.name) // FIXME: validate
|
2016-05-16 11:50:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
named = reference.WithDefaultTag(named)
|
|
|
|
ref, ok := named.(reference.NamedTagged)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid name: %s", named.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(named)
|
|
|
|
authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
|
|
|
|
|
|
|
|
encodedAuth, err := client.EncodeAuthToBase64(authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-16 18:33:21 -04:00
|
|
|
|
2016-06-17 12:12:37 -04:00
|
|
|
registryAuthFunc := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "plugin install")
|
2016-06-16 18:33:21 -04:00
|
|
|
|
|
|
|
options := types.PluginInstallOptions{
|
|
|
|
RegistryAuth: encodedAuth,
|
2016-06-17 10:44:57 -04:00
|
|
|
Disabled: opts.disable,
|
2016-06-16 18:33:21 -04:00
|
|
|
AcceptAllPermissions: opts.grantPerms,
|
|
|
|
AcceptPermissionsFunc: acceptPrivileges(dockerCli, opts.name),
|
2016-06-17 12:12:37 -04:00
|
|
|
// TODO: Rename PrivilegeFunc, it has nothing to do with privileges
|
|
|
|
PrivilegeFunc: registryAuthFunc,
|
2016-06-16 18:33:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return dockerCli.Client().PluginInstall(ctx, ref.String(), options)
|
|
|
|
}
|
|
|
|
|
|
|
|
func acceptPrivileges(dockerCli *client.DockerCli, name string) func(privileges types.PluginPrivileges) (bool, error) {
|
|
|
|
return func(privileges types.PluginPrivileges) (bool, error) {
|
2016-06-17 12:12:37 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "Plugin %q is requesting the following privileges:\n", name)
|
2016-06-16 18:33:21 -04:00
|
|
|
for _, privilege := range privileges {
|
|
|
|
fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(dockerCli.Out(), "Do you grant the above permissions? [y/N] ")
|
|
|
|
reader := bufio.NewReader(dockerCli.In())
|
|
|
|
line, _, err := reader.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return strings.ToLower(string(line)) == "y", nil
|
|
|
|
}
|
2016-05-16 11:50:55 -04:00
|
|
|
}
|