2016-05-16 11:50:55 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2016-06-16 18:33:21 -04:00
|
|
|
"bufio"
|
2016-12-12 18:05:53 -05:00
|
|
|
"errors"
|
2016-05-16 11:50:55 -04:00
|
|
|
"fmt"
|
2016-06-16 18:33:21 -04:00
|
|
|
"strings"
|
2016-05-16 11:50:55 -04:00
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
distreference "github.com/docker/distribution/reference"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-12-12 18:05:53 -05:00
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2016-05-16 11:50:55 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 14:54:01 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-12-12 18:05:53 -05:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2016-05-16 11:50:55 -04:00
|
|
|
"github.com/docker/docker/reference"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-06-15 13:29:01 -04:00
|
|
|
type pluginOptions struct {
|
|
|
|
name string
|
2016-12-12 18:05:53 -05:00
|
|
|
alias string
|
2016-06-15 13:29:01 -04:00
|
|
|
grantPerms bool
|
2016-06-17 10:44:57 -04:00
|
|
|
disable bool
|
2016-11-07 20:43:11 -05:00
|
|
|
args []string
|
2016-06-15 13:29:01 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
func newInstallCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-15 13:29:01 -04:00
|
|
|
var options pluginOptions
|
2016-05-16 11:50:55 -04:00
|
|
|
cmd := &cobra.Command{
|
2016-11-07 20:43:11 -05:00
|
|
|
Use: "install [OPTIONS] PLUGIN [KEY=VALUE...]",
|
2016-05-16 11:50:55 -04:00
|
|
|
Short: "Install a plugin",
|
2016-11-07 20:43:11 -05:00
|
|
|
Args: cli.RequiresMinArgs(1),
|
2016-05-16 11:50:55 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-06-15 13:29:01 -04:00
|
|
|
options.name = args[0]
|
2016-11-07 20:43:11 -05:00
|
|
|
if len(args) > 1 {
|
|
|
|
options.args = args[1:]
|
|
|
|
}
|
2016-06-15 13:29:01 -04:00
|
|
|
return runInstall(dockerCli, options)
|
2016-05-16 11:50:55 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:29:01 -04:00
|
|
|
flags := cmd.Flags()
|
2016-08-30 06:45:06 -04:00
|
|
|
flags.BoolVar(&options.grantPerms, "grant-all-permissions", false, "Grant all permissions necessary to run the plugin")
|
|
|
|
flags.BoolVar(&options.disable, "disable", false, "Do not enable the plugin on install")
|
2016-12-12 18:05:53 -05:00
|
|
|
flags.StringVar(&options.alias, "alias", "", "Local name for plugin")
|
2016-06-15 13:29:01 -04:00
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
func getRepoIndexFromUnnormalizedRef(ref distreference.Named) (*registrytypes.IndexInfo, error) {
|
|
|
|
named, err := reference.ParseNamed(ref.Name())
|
2016-05-16 11:50:55 -04:00
|
|
|
if err != nil {
|
2016-12-12 18:05:53 -05:00
|
|
|
return nil, err
|
2016-05-16 11:50:55 -04:00
|
|
|
}
|
2016-12-12 18:05:53 -05:00
|
|
|
|
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(named)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-06-17 12:13:24 -04:00
|
|
|
}
|
2016-12-12 18:05:53 -05:00
|
|
|
|
|
|
|
return repoInfo.Index, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runInstall(dockerCli *command.DockerCli, opts pluginOptions) error {
|
|
|
|
// Parse name using distribution reference package to support name
|
|
|
|
// containing both tag and digest. Names with both tag and digest
|
|
|
|
// will be treated by the daemon as a pull by digest with
|
|
|
|
// an alias for the tag (if no alias is provided).
|
|
|
|
ref, err := distreference.ParseNamed(opts.name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-05-16 11:50:55 -04:00
|
|
|
}
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
alias := ""
|
|
|
|
if opts.alias != "" {
|
|
|
|
aref, err := reference.ParseNamed(opts.alias)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
aref = reference.WithDefaultTag(aref)
|
|
|
|
if _, ok := aref.(reference.NamedTagged); !ok {
|
|
|
|
return fmt.Errorf("invalid name: %s", opts.alias)
|
|
|
|
}
|
|
|
|
alias = aref.String()
|
|
|
|
}
|
2016-05-16 11:50:55 -04:00
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
index, err := getRepoIndexFromUnnormalizedRef(ref)
|
2016-06-29 05:26:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
authConfig := command.ResolveAuthConfig(ctx, dockerCli, index)
|
2016-05-16 11:50:55 -04:00
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
|
2016-05-16 11:50:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-16 18:33:21 -04:00
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
registryAuthFunc := command.RegistryAuthenticationPrivilegedFunc(dockerCli, index, "plugin install")
|
2016-06-16 18:33:21 -04:00
|
|
|
|
|
|
|
options := types.PluginInstallOptions{
|
|
|
|
RegistryAuth: encodedAuth,
|
2016-12-12 18:05:53 -05:00
|
|
|
RemoteRef: ref.String(),
|
2016-06-17 10:44:57 -04:00
|
|
|
Disabled: opts.disable,
|
2016-06-16 18:33:21 -04:00
|
|
|
AcceptAllPermissions: opts.grantPerms,
|
|
|
|
AcceptPermissionsFunc: acceptPrivileges(dockerCli, opts.name),
|
2016-06-17 12:12:37 -04:00
|
|
|
// TODO: Rename PrivilegeFunc, it has nothing to do with privileges
|
|
|
|
PrivilegeFunc: registryAuthFunc,
|
2016-11-07 20:43:11 -05:00
|
|
|
Args: opts.args,
|
2016-06-16 18:33:21 -04:00
|
|
|
}
|
2016-12-12 18:05:53 -05:00
|
|
|
|
|
|
|
responseBody, err := dockerCli.Client().PluginInstall(ctx, alias, options)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "target is image") {
|
|
|
|
return errors.New(err.Error() + " - Use `docker image pull`")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
|
|
|
if err := jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil); err != nil {
|
2016-07-15 13:37:17 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-12-12 18:05:53 -05:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "Installed plugin %s\n", opts.name) // todo: return proper values from the API for this result
|
2016-07-15 13:37:17 -04:00
|
|
|
return nil
|
2016-06-16 18:33:21 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
func acceptPrivileges(dockerCli *command.DockerCli, name string) func(privileges types.PluginPrivileges) (bool, error) {
|
2016-06-16 18:33:21 -04:00
|
|
|
return func(privileges types.PluginPrivileges) (bool, error) {
|
2016-06-17 12:12:37 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "Plugin %q is requesting the following privileges:\n", name)
|
2016-06-16 18:33:21 -04:00
|
|
|
for _, privilege := range privileges {
|
|
|
|
fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(dockerCli.Out(), "Do you grant the above permissions? [y/N] ")
|
|
|
|
reader := bufio.NewReader(dockerCli.In())
|
|
|
|
line, _, err := reader.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return strings.ToLower(string(line)) == "y", nil
|
|
|
|
}
|
2016-05-16 11:50:55 -04:00
|
|
|
}
|