install.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }
  19. func newInstallCommand(dockerCli *client.DockerCli) *cobra.Command {
  20. var options pluginOptions
  21. cmd := &cobra.Command{
  22. Use: "install",
  23. Short: "Install a plugin",
  24. Args: cli.RequiresMinArgs(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", true, "grant all permissions necessary to run the plugin")
  32. return cmd
  33. }
  34. func runInstall(dockerCli *client.DockerCli, opts pluginOptions) error {
  35. named, err := reference.ParseNamed(opts.name) // FIXME: validate
  36. if err != nil {
  37. return err
  38. }
  39. named = reference.WithDefaultTag(named)
  40. ref, ok := named.(reference.NamedTagged)
  41. if !ok {
  42. return fmt.Errorf("invalid name: %s", named.String())
  43. }
  44. ctx := context.Background()
  45. repoInfo, err := registry.ParseRepositoryInfo(named)
  46. authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
  47. encodedAuth, err := client.EncodeAuthToBase64(authConfig)
  48. if err != nil {
  49. return err
  50. }
  51. requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "plugin install")
  52. // TODO: pass acceptAllPermissions and noEnable flag
  53. options := types.PluginInstallOptions{
  54. RegistryAuth: encodedAuth,
  55. Disabled: false,
  56. AcceptAllPermissions: opts.grantPerms,
  57. AcceptPermissionsFunc: acceptPrivileges(dockerCli, opts.name),
  58. PrivilegeFunc: requestPrivilege,
  59. }
  60. return dockerCli.Client().PluginInstall(ctx, ref.String(), options)
  61. }
  62. func acceptPrivileges(dockerCli *client.DockerCli, name string) func(privileges types.PluginPrivileges) (bool, error) {
  63. return func(privileges types.PluginPrivileges) (bool, error) {
  64. fmt.Fprintf(dockerCli.Out(), "Plugin %q requested the following privileges:\n", name)
  65. for _, privilege := range privileges {
  66. fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value)
  67. }
  68. fmt.Fprint(dockerCli.Out(), "Do you grant the above permissions? [y/N] ")
  69. reader := bufio.NewReader(dockerCli.In())
  70. line, _, err := reader.ReadLine()
  71. if err != nil {
  72. return false, err
  73. }
  74. return strings.ToLower(string(line)) == "y", nil
  75. }
  76. }