install.go 2.8 KB

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