install.go 2.7 KB

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