install.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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",
  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", true, "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. requestPrivilege := 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. PrivilegeFunc: requestPrivilege,
  60. }
  61. return dockerCli.Client().PluginInstall(ctx, ref.String(), options)
  62. }
  63. func acceptPrivileges(dockerCli *client.DockerCli, name string) func(privileges types.PluginPrivileges) (bool, error) {
  64. return func(privileges types.PluginPrivileges) (bool, error) {
  65. fmt.Fprintf(dockerCli.Out(), "Plugin %q requested the following privileges:\n", name)
  66. for _, privilege := range privileges {
  67. fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value)
  68. }
  69. fmt.Fprint(dockerCli.Out(), "Do you grant the above permissions? [y/N] ")
  70. reader := bufio.NewReader(dockerCli.In())
  71. line, _, err := reader.ReadLine()
  72. if err != nil {
  73. return false, err
  74. }
  75. return strings.ToLower(string(line)) == "y", nil
  76. }
  77. }