install.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package plugin
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. distreference "github.com/docker/distribution/reference"
  8. "github.com/docker/docker/api/types"
  9. registrytypes "github.com/docker/docker/api/types/registry"
  10. "github.com/docker/docker/cli"
  11. "github.com/docker/docker/cli/command"
  12. "github.com/docker/docker/pkg/jsonmessage"
  13. "github.com/docker/docker/reference"
  14. "github.com/docker/docker/registry"
  15. "github.com/spf13/cobra"
  16. "golang.org/x/net/context"
  17. )
  18. type pluginOptions struct {
  19. name string
  20. alias string
  21. grantPerms bool
  22. disable bool
  23. args []string
  24. }
  25. func newInstallCommand(dockerCli *command.DockerCli) *cobra.Command {
  26. var options pluginOptions
  27. cmd := &cobra.Command{
  28. Use: "install [OPTIONS] PLUGIN [KEY=VALUE...]",
  29. Short: "Install a plugin",
  30. Args: cli.RequiresMinArgs(1),
  31. RunE: func(cmd *cobra.Command, args []string) error {
  32. options.name = args[0]
  33. if len(args) > 1 {
  34. options.args = args[1:]
  35. }
  36. return runInstall(dockerCli, options)
  37. },
  38. }
  39. flags := cmd.Flags()
  40. flags.BoolVar(&options.grantPerms, "grant-all-permissions", false, "Grant all permissions necessary to run the plugin")
  41. flags.BoolVar(&options.disable, "disable", false, "Do not enable the plugin on install")
  42. flags.StringVar(&options.alias, "alias", "", "Local name for plugin")
  43. return cmd
  44. }
  45. func getRepoIndexFromUnnormalizedRef(ref distreference.Named) (*registrytypes.IndexInfo, error) {
  46. named, err := reference.ParseNamed(ref.Name())
  47. if err != nil {
  48. return nil, err
  49. }
  50. repoInfo, err := registry.ParseRepositoryInfo(named)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return repoInfo.Index, nil
  55. }
  56. func runInstall(dockerCli *command.DockerCli, opts pluginOptions) error {
  57. // Parse name using distribution reference package to support name
  58. // containing both tag and digest. Names with both tag and digest
  59. // will be treated by the daemon as a pull by digest with
  60. // an alias for the tag (if no alias is provided).
  61. ref, err := distreference.ParseNamed(opts.name)
  62. if err != nil {
  63. return err
  64. }
  65. alias := ""
  66. if opts.alias != "" {
  67. aref, err := reference.ParseNamed(opts.alias)
  68. if err != nil {
  69. return err
  70. }
  71. aref = reference.WithDefaultTag(aref)
  72. if _, ok := aref.(reference.NamedTagged); !ok {
  73. return fmt.Errorf("invalid name: %s", opts.alias)
  74. }
  75. alias = aref.String()
  76. }
  77. index, err := getRepoIndexFromUnnormalizedRef(ref)
  78. if err != nil {
  79. return err
  80. }
  81. ctx := context.Background()
  82. authConfig := command.ResolveAuthConfig(ctx, dockerCli, index)
  83. encodedAuth, err := command.EncodeAuthToBase64(authConfig)
  84. if err != nil {
  85. return err
  86. }
  87. registryAuthFunc := command.RegistryAuthenticationPrivilegedFunc(dockerCli, index, "plugin install")
  88. options := types.PluginInstallOptions{
  89. RegistryAuth: encodedAuth,
  90. RemoteRef: ref.String(),
  91. Disabled: opts.disable,
  92. AcceptAllPermissions: opts.grantPerms,
  93. AcceptPermissionsFunc: acceptPrivileges(dockerCli, opts.name),
  94. // TODO: Rename PrivilegeFunc, it has nothing to do with privileges
  95. PrivilegeFunc: registryAuthFunc,
  96. Args: opts.args,
  97. }
  98. responseBody, err := dockerCli.Client().PluginInstall(ctx, alias, options)
  99. if err != nil {
  100. if strings.Contains(err.Error(), "target is image") {
  101. return errors.New(err.Error() + " - Use `docker image pull`")
  102. }
  103. return err
  104. }
  105. defer responseBody.Close()
  106. if err := jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil); err != nil {
  107. return err
  108. }
  109. fmt.Fprintf(dockerCli.Out(), "Installed plugin %s\n", opts.name) // todo: return proper values from the API for this result
  110. return nil
  111. }
  112. func acceptPrivileges(dockerCli *command.DockerCli, name string) func(privileges types.PluginPrivileges) (bool, error) {
  113. return func(privileges types.PluginPrivileges) (bool, error) {
  114. fmt.Fprintf(dockerCli.Out(), "Plugin %q is requesting the following privileges:\n", name)
  115. for _, privilege := range privileges {
  116. fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value)
  117. }
  118. fmt.Fprint(dockerCli.Out(), "Do you grant the above permissions? [y/N] ")
  119. reader := bufio.NewReader(dockerCli.In())
  120. line, _, err := reader.ReadLine()
  121. if err != nil {
  122. return false, err
  123. }
  124. return strings.ToLower(string(line)) == "y", nil
  125. }
  126. }