|
| 1 | +package engine |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/docker/cli/cli/command" |
| 8 | + "github.com/docker/cli/cli/command/formatter" |
| 9 | + "github.com/docker/cli/internal/containerizedengine" |
| 10 | + "github.com/docker/cli/internal/licenseutils" |
| 11 | + "github.com/docker/docker/api/types" |
| 12 | + "github.com/docker/licensing/model" |
| 13 | + "github.com/pkg/errors" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +type activateOptions struct { |
| 18 | + licenseFile string |
| 19 | + version string |
| 20 | + registryPrefix string |
| 21 | + format string |
| 22 | + image string |
| 23 | + quiet bool |
| 24 | + displayOnly bool |
| 25 | + sockPath string |
| 26 | +} |
| 27 | + |
| 28 | +// newActivateCommand creates a new `docker engine activate` command |
| 29 | +func newActivateCommand(dockerCli command.Cli) *cobra.Command { |
| 30 | + var options activateOptions |
| 31 | + |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "activate [OPTIONS]", |
| 34 | + Short: "Activate Enterprise Edition", |
| 35 | + Long: `Activate Enterprise Edition. |
| 36 | +
|
| 37 | +With this command you may apply an existing Docker enterprise license, or |
| 38 | +interactively download one from Docker. In the interactive exchange, you can |
| 39 | +sign up for a new trial, or download an existing license. If you are |
| 40 | +currently running a Community Edition engine, the daemon will be updated to |
| 41 | +the Enterprise Edition Docker engine with additional capabilities and long |
| 42 | +term support. |
| 43 | +
|
| 44 | +For more information about different Docker Enterprise license types visit |
| 45 | +https://www.docker.com/licenses |
| 46 | +
|
| 47 | +For non-interactive scriptable deployments, download your license from |
| 48 | +https://hub.docker.com/ then specify the file with the '--license' flag. |
| 49 | +`, |
| 50 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 51 | + return runActivate(dockerCli, options) |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + flags := cmd.Flags() |
| 56 | + |
| 57 | + flags.StringVar(&options.licenseFile, "license", "", "License File") |
| 58 | + flags.StringVar(&options.version, "version", "", "Specify engine version (default is to use currently running version)") |
| 59 | + flags.StringVar(&options.registryPrefix, "registry-prefix", "docker.io/docker", "Override the default location where engine images are pulled") |
| 60 | + flags.StringVar(&options.image, "engine-image", containerizedengine.EnterpriseEngineImage, "Specify engine image") |
| 61 | + flags.StringVar(&options.format, "format", "", "Pretty-print licenses using a Go template") |
| 62 | + flags.BoolVar(&options.displayOnly, "display-only", false, "only display the available licenses and exit") |
| 63 | + flags.BoolVar(&options.quiet, "quiet", false, "Only display available licenses by ID") |
| 64 | + flags.StringVar(&options.sockPath, "containerd", "", "override default location of containerd endpoint") |
| 65 | + |
| 66 | + return cmd |
| 67 | +} |
| 68 | + |
| 69 | +func runActivate(cli command.Cli, options activateOptions) error { |
| 70 | + ctx := context.Background() |
| 71 | + client, err := cli.NewContainerizedEngineClient(options.sockPath) |
| 72 | + if err != nil { |
| 73 | + return errors.Wrap(err, "unable to access local containerd") |
| 74 | + } |
| 75 | + defer client.Close() |
| 76 | + |
| 77 | + authConfig, err := getRegistryAuth(cli, options.registryPrefix) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + var license *model.IssuedLicense |
| 83 | + |
| 84 | + // Lookup on hub if no license provided via params |
| 85 | + if options.licenseFile == "" { |
| 86 | + if license, err = getLicenses(ctx, authConfig, cli, options); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + if options.displayOnly { |
| 90 | + return nil |
| 91 | + } |
| 92 | + } else { |
| 93 | + if license, err = licenseutils.LoadLocalIssuedLicense(ctx, options.licenseFile); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + } |
| 97 | + if err = licenseutils.ApplyLicense(ctx, cli.Client(), license); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + opts := containerizedengine.EngineInitOptions{ |
| 102 | + RegistryPrefix: options.registryPrefix, |
| 103 | + EngineImage: options.image, |
| 104 | + EngineVersion: options.version, |
| 105 | + } |
| 106 | + |
| 107 | + return client.ActivateEngine(ctx, opts, cli.Out(), authConfig, |
| 108 | + func(ctx context.Context) error { |
| 109 | + client := cli.Client() |
| 110 | + _, err := client.Ping(ctx) |
| 111 | + return err |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +func getLicenses(ctx context.Context, authConfig *types.AuthConfig, cli command.Cli, options activateOptions) (*model.IssuedLicense, error) { |
| 116 | + user, err := licenseutils.Login(ctx, authConfig) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + fmt.Fprintf(cli.Out(), "Looking for existing licenses for %s...\n", user.User.Username) |
| 121 | + subs, err := user.GetAvailableLicenses(ctx) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + if len(subs) == 0 { |
| 126 | + return doTrialFlow(ctx, cli, user) |
| 127 | + } |
| 128 | + |
| 129 | + format := options.format |
| 130 | + if len(format) == 0 { |
| 131 | + format = formatter.TableFormatKey |
| 132 | + } |
| 133 | + |
| 134 | + updatesCtx := formatter.Context{ |
| 135 | + Output: cli.Out(), |
| 136 | + Format: formatter.NewSubscriptionsFormat(format, options.quiet), |
| 137 | + Trunc: false, |
| 138 | + } |
| 139 | + if err := formatter.SubscriptionsWrite(updatesCtx, subs); err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + if options.displayOnly { |
| 143 | + return nil, nil |
| 144 | + } |
| 145 | + fmt.Fprintf(cli.Out(), "Please pick a license by number: ") |
| 146 | + var num int |
| 147 | + if _, err := fmt.Fscan(cli.In(), &num); err != nil { |
| 148 | + return nil, errors.Wrap(err, "failed to read user input") |
| 149 | + } |
| 150 | + if num < 0 || num >= len(subs) { |
| 151 | + return nil, fmt.Errorf("invalid choice") |
| 152 | + } |
| 153 | + return user.GetIssuedLicense(ctx, subs[num].ID) |
| 154 | +} |
| 155 | + |
| 156 | +func doTrialFlow(ctx context.Context, cli command.Cli, user licenseutils.HubUser) (*model.IssuedLicense, error) { |
| 157 | + if !command.PromptForConfirmation(cli.In(), cli.Out(), |
| 158 | + "No existing licenses found, would you like to set up a new Enterprise Basic Trial license?") { |
| 159 | + return nil, fmt.Errorf("you must have an existing enterprise license or generate a new trial to use the Enterprise Docker Engine") |
| 160 | + } |
| 161 | + targetID := user.User.ID |
| 162 | + // If the user is a member of any organizations, allow trials generated against them |
| 163 | + if len(user.Orgs) > 0 { |
| 164 | + fmt.Fprintf(cli.Out(), "%d\t%s\n", 0, user.User.Username) |
| 165 | + for i, org := range user.Orgs { |
| 166 | + fmt.Fprintf(cli.Out(), "%d\t%s\n", i+1, org.Orgname) |
| 167 | + } |
| 168 | + fmt.Fprintf(cli.Out(), "Please choose an account to generate the trial in:") |
| 169 | + var num int |
| 170 | + if _, err := fmt.Fscan(cli.In(), &num); err != nil { |
| 171 | + return nil, errors.Wrap(err, "failed to read user input") |
| 172 | + } |
| 173 | + if num < 0 || num > len(user.Orgs) { |
| 174 | + return nil, fmt.Errorf("invalid choice") |
| 175 | + } |
| 176 | + if num > 0 { |
| 177 | + targetID = user.Orgs[num-1].ID |
| 178 | + } |
| 179 | + } |
| 180 | + return user.GenerateTrialLicense(ctx, targetID) |
| 181 | +} |
0 commit comments