github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/api/client/plugin/install.go (about) 1 // +build experimental 2 3 package plugin 4 5 import ( 6 "bufio" 7 "fmt" 8 "strings" 9 10 "github.com/docker/docker/api/client" 11 "github.com/docker/docker/cli" 12 "github.com/docker/docker/reference" 13 "github.com/docker/docker/registry" 14 "github.com/docker/engine-api/types" 15 "github.com/spf13/cobra" 16 "golang.org/x/net/context" 17 ) 18 19 type pluginOptions struct { 20 name string 21 grantPerms bool 22 disable bool 23 } 24 25 func newInstallCommand(dockerCli *client.DockerCli) *cobra.Command { 26 var options pluginOptions 27 cmd := &cobra.Command{ 28 Use: "install PLUGIN", 29 Short: "Install a plugin", 30 Args: cli.RequiresMinArgs(1), // TODO: allow for set args 31 RunE: func(cmd *cobra.Command, args []string) error { 32 options.name = args[0] 33 return runInstall(dockerCli, options) 34 }, 35 } 36 37 flags := cmd.Flags() 38 flags.BoolVar(&options.grantPerms, "grant-all-permissions", false, "grant all permissions necessary to run the plugin") 39 flags.BoolVar(&options.disable, "disable", false, "do not enable the plugin on install") 40 41 return cmd 42 } 43 44 func runInstall(dockerCli *client.DockerCli, opts pluginOptions) error { 45 named, err := reference.ParseNamed(opts.name) // FIXME: validate 46 if err != nil { 47 return err 48 } 49 if reference.IsNameOnly(named) { 50 named = reference.WithDefaultTag(named) 51 } 52 ref, ok := named.(reference.NamedTagged) 53 if !ok { 54 return fmt.Errorf("invalid name: %s", named.String()) 55 } 56 57 ctx := context.Background() 58 59 repoInfo, err := registry.ParseRepositoryInfo(named) 60 authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index) 61 62 encodedAuth, err := client.EncodeAuthToBase64(authConfig) 63 if err != nil { 64 return err 65 } 66 67 registryAuthFunc := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "plugin install") 68 69 options := types.PluginInstallOptions{ 70 RegistryAuth: encodedAuth, 71 Disabled: opts.disable, 72 AcceptAllPermissions: opts.grantPerms, 73 AcceptPermissionsFunc: acceptPrivileges(dockerCli, opts.name), 74 // TODO: Rename PrivilegeFunc, it has nothing to do with privileges 75 PrivilegeFunc: registryAuthFunc, 76 } 77 78 return dockerCli.Client().PluginInstall(ctx, ref.String(), options) 79 } 80 81 func acceptPrivileges(dockerCli *client.DockerCli, name string) func(privileges types.PluginPrivileges) (bool, error) { 82 return func(privileges types.PluginPrivileges) (bool, error) { 83 fmt.Fprintf(dockerCli.Out(), "Plugin %q is requesting the following privileges:\n", name) 84 for _, privilege := range privileges { 85 fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value) 86 } 87 88 fmt.Fprint(dockerCli.Out(), "Do you grant the above permissions? [y/N] ") 89 reader := bufio.NewReader(dockerCli.In()) 90 line, _, err := reader.ReadLine() 91 if err != nil { 92 return false, err 93 } 94 return strings.ToLower(string(line)) == "y", nil 95 } 96 }