2016-05-16 11:50:55 -04:00
|
|
|
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
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-05-16 11:50:55 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 14:54:01 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-05-16 11:50:55 -04:00
|
|
|
"github.com/docker/docker/reference"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"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-09-08 14:54:01 -04:00
|
|
|
func newInstallCommand(dockerCli *command.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-07-16 10:44:10 -04:00
|
|
|
Use: "install [OPTIONS] PLUGIN",
|
2016-05-16 11:50:55 -04:00
|
|
|
Short: "Install a plugin",
|
2016-07-29 13:41:52 -04:00
|
|
|
Args: cli.ExactArgs(1), // TODO: allow for set args
|
2016-05-16 11:50:55 -04:00
|
|
|
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-08-30 06:45:06 -04:00
|
|
|
flags.BoolVar(&options.grantPerms, "grant-all-permissions", false, "Grant all permissions necessary to run the plugin")
|
|
|
|
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-09-08 14:54:01 -04:00
|
|
|
func runInstall(dockerCli *command.DockerCli, opts pluginOptions) error {
|
2016-06-16 18:33:21 -04:00
|
|
|
named, err := reference.ParseNamed(opts.name) // FIXME: validate
|
2016-05-16 11:50:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-17 12:13:24 -04:00
|
|
|
if reference.IsNameOnly(named) {
|
|
|
|
named = reference.WithDefaultTag(named)
|
|
|
|
}
|
2016-05-16 11:50:55 -04:00
|
|
|
ref, ok := named.(reference.NamedTagged)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid name: %s", named.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(named)
|
2016-06-29 05:26:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-09 15:38:00 -04:00
|
|
|
authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
|
2016-05-16 11:50:55 -04:00
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
|
2016-05-16 11:50:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-16 18:33:21 -04:00
|
|
|
|
2016-09-09 15:38:00 -04:00
|
|
|
registryAuthFunc := command.RegistryAuthenticationPrivilegedFunc(dockerCli, 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
|
|
|
}
|
2016-07-15 13:37:17 -04:00
|
|
|
if err := dockerCli.Client().PluginInstall(ctx, ref.String(), options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(dockerCli.Out(), opts.name)
|
|
|
|
return nil
|
2016-06-16 18:33:21 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
func acceptPrivileges(dockerCli *command.DockerCli, name string) func(privileges types.PluginPrivileges) (bool, error) {
|
2016-06-16 18:33:21 -04:00
|
|
|
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
|
|
|
}
|