2018-03-19 18:56:51 -04:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-09-28 17:06:28 -04:00
|
|
|
"strings"
|
2018-03-19 18:56:51 -04:00
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
|
|
"github.com/docker/cli/internal/licenseutils"
|
2018-09-11 08:46:30 -04:00
|
|
|
clitypes "github.com/docker/cli/types"
|
2018-03-19 18:56:51 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/licensing/model"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type activateOptions struct {
|
2018-10-02 14:10:22 -04:00
|
|
|
licenseFile string
|
|
|
|
version string
|
|
|
|
registryPrefix string
|
|
|
|
format string
|
|
|
|
image string
|
|
|
|
quiet bool
|
|
|
|
displayOnly bool
|
|
|
|
sockPath string
|
|
|
|
licenseLoginFunc func(ctx context.Context, authConfig *types.AuthConfig) (licenseutils.HubUser, error)
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// newActivateCommand creates a new `docker engine activate` command
|
|
|
|
func newActivateCommand(dockerCli command.Cli) *cobra.Command {
|
|
|
|
var options activateOptions
|
2018-10-02 14:10:22 -04:00
|
|
|
options.licenseLoginFunc = licenseutils.Login
|
2018-03-19 18:56:51 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "activate [OPTIONS]",
|
|
|
|
Short: "Activate Enterprise Edition",
|
|
|
|
Long: `Activate Enterprise Edition.
|
|
|
|
|
|
|
|
With this command you may apply an existing Docker enterprise license, or
|
|
|
|
interactively download one from Docker. In the interactive exchange, you can
|
|
|
|
sign up for a new trial, or download an existing license. If you are
|
|
|
|
currently running a Community Edition engine, the daemon will be updated to
|
|
|
|
the Enterprise Edition Docker engine with additional capabilities and long
|
|
|
|
term support.
|
|
|
|
|
|
|
|
For more information about different Docker Enterprise license types visit
|
|
|
|
https://www.docker.com/licenses
|
|
|
|
|
|
|
|
For non-interactive scriptable deployments, download your license from
|
|
|
|
https://hub.docker.com/ then specify the file with the '--license' flag.
|
|
|
|
`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runActivate(dockerCli, options)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.StringVar(&options.licenseFile, "license", "", "License File")
|
|
|
|
flags.StringVar(&options.version, "version", "", "Specify engine version (default is to use currently running version)")
|
2018-09-20 14:02:11 -04:00
|
|
|
flags.StringVar(&options.registryPrefix, "registry-prefix", clitypes.RegistryPrefix, "Override the default location where engine images are pulled")
|
2018-09-28 17:06:28 -04:00
|
|
|
flags.StringVar(&options.image, "engine-image", "", "Specify engine image")
|
2018-03-19 18:56:51 -04:00
|
|
|
flags.StringVar(&options.format, "format", "", "Pretty-print licenses using a Go template")
|
2018-10-02 13:24:16 -04:00
|
|
|
flags.BoolVar(&options.displayOnly, "display-only", false, "only display license information and exit")
|
2018-03-19 18:56:51 -04:00
|
|
|
flags.BoolVar(&options.quiet, "quiet", false, "Only display available licenses by ID")
|
|
|
|
flags.StringVar(&options.sockPath, "containerd", "", "override default location of containerd endpoint")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runActivate(cli command.Cli, options activateOptions) error {
|
2018-09-14 13:53:56 -04:00
|
|
|
if !isRoot() {
|
2018-09-20 14:02:11 -04:00
|
|
|
return errors.New("this command must be run as a privileged user")
|
2018-09-13 12:32:19 -04:00
|
|
|
}
|
2018-03-19 18:56:51 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
client, err := cli.NewContainerizedEngineClient(options.sockPath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "unable to access local containerd")
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
authConfig, err := getRegistryAuth(cli, options.registryPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var license *model.IssuedLicense
|
|
|
|
|
|
|
|
// Lookup on hub if no license provided via params
|
|
|
|
if options.licenseFile == "" {
|
|
|
|
if license, err = getLicenses(ctx, authConfig, cli, options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-02 13:24:16 -04:00
|
|
|
if options.displayOnly {
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-19 18:56:51 -04:00
|
|
|
} else {
|
|
|
|
if license, err = licenseutils.LoadLocalIssuedLicense(ctx, options.licenseFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 17:31:45 -04:00
|
|
|
summary, err := licenseutils.GetLicenseSummary(ctx, *license)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cli.Out(), "License: %s\n", summary)
|
|
|
|
if options.displayOnly {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-28 17:06:28 -04:00
|
|
|
dclient := cli.Client()
|
|
|
|
if err = licenseutils.ApplyLicense(ctx, dclient, license); err != nil {
|
2018-03-19 18:56:51 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-28 17:06:28 -04:00
|
|
|
// Short circuit if the user didn't specify a version and we're already running enterprise
|
|
|
|
if options.version == "" {
|
|
|
|
serverVersion, err := dclient.ServerVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if strings.Contains(strings.ToLower(serverVersion.Platform.Name), "enterprise") {
|
|
|
|
fmt.Fprintln(cli.Out(), "Successfully activated engine license on existing enterprise engine.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
options.version = serverVersion.Version
|
|
|
|
}
|
|
|
|
|
2018-09-11 08:46:30 -04:00
|
|
|
opts := clitypes.EngineInitOptions{
|
2018-03-19 18:56:51 -04:00
|
|
|
RegistryPrefix: options.registryPrefix,
|
|
|
|
EngineImage: options.image,
|
|
|
|
EngineVersion: options.version,
|
|
|
|
}
|
|
|
|
|
2018-10-01 18:12:09 -04:00
|
|
|
if err := client.ActivateEngine(ctx, opts, cli.Out(), authConfig); err != nil {
|
2018-09-14 13:53:56 -04:00
|
|
|
return err
|
|
|
|
}
|
2018-09-21 14:01:06 -04:00
|
|
|
fmt.Fprintln(cli.Out(), `Successfully activated engine.
|
2018-09-20 14:02:11 -04:00
|
|
|
Restart docker with 'systemctl restart docker' to complete the activation.`)
|
2018-09-14 13:53:56 -04:00
|
|
|
return nil
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLicenses(ctx context.Context, authConfig *types.AuthConfig, cli command.Cli, options activateOptions) (*model.IssuedLicense, error) {
|
2018-10-02 14:10:22 -04:00
|
|
|
user, err := options.licenseLoginFunc(ctx, authConfig)
|
2018-03-19 18:56:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cli.Out(), "Looking for existing licenses for %s...\n", user.User.Username)
|
|
|
|
subs, err := user.GetAvailableLicenses(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(subs) == 0 {
|
|
|
|
return doTrialFlow(ctx, cli, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
format := options.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
|
|
|
|
updatesCtx := formatter.Context{
|
|
|
|
Output: cli.Out(),
|
2018-10-23 11:05:44 -04:00
|
|
|
Format: NewSubscriptionsFormat(format, options.quiet),
|
2018-03-19 18:56:51 -04:00
|
|
|
Trunc: false,
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
if err := SubscriptionsWrite(updatesCtx, subs); err != nil {
|
2018-03-19 18:56:51 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if options.displayOnly {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cli.Out(), "Please pick a license by number: ")
|
|
|
|
var num int
|
|
|
|
if _, err := fmt.Fscan(cli.In(), &num); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to read user input")
|
|
|
|
}
|
|
|
|
if num < 0 || num >= len(subs) {
|
|
|
|
return nil, fmt.Errorf("invalid choice")
|
|
|
|
}
|
|
|
|
return user.GetIssuedLicense(ctx, subs[num].ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTrialFlow(ctx context.Context, cli command.Cli, user licenseutils.HubUser) (*model.IssuedLicense, error) {
|
|
|
|
if !command.PromptForConfirmation(cli.In(), cli.Out(),
|
|
|
|
"No existing licenses found, would you like to set up a new Enterprise Basic Trial license?") {
|
|
|
|
return nil, fmt.Errorf("you must have an existing enterprise license or generate a new trial to use the Enterprise Docker Engine")
|
|
|
|
}
|
|
|
|
targetID := user.User.ID
|
|
|
|
// If the user is a member of any organizations, allow trials generated against them
|
|
|
|
if len(user.Orgs) > 0 {
|
|
|
|
fmt.Fprintf(cli.Out(), "%d\t%s\n", 0, user.User.Username)
|
|
|
|
for i, org := range user.Orgs {
|
|
|
|
fmt.Fprintf(cli.Out(), "%d\t%s\n", i+1, org.Orgname)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cli.Out(), "Please choose an account to generate the trial in:")
|
|
|
|
var num int
|
|
|
|
if _, err := fmt.Fscan(cli.In(), &num); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to read user input")
|
|
|
|
}
|
|
|
|
if num < 0 || num > len(user.Orgs) {
|
|
|
|
return nil, fmt.Errorf("invalid choice")
|
|
|
|
}
|
|
|
|
if num > 0 {
|
|
|
|
targetID = user.Orgs[num-1].ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return user.GenerateTrialLicense(ctx, targetID)
|
|
|
|
}
|