2020-04-21 18:55:31 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-04-22 14:19:51 +00:00
|
|
|
"strings"
|
2020-04-21 18:55:31 +00:00
|
|
|
|
2021-03-23 13:08:14 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2020-04-21 18:55:31 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-03-23 13:08:14 +00:00
|
|
|
"github.com/posener/complete"
|
2020-04-21 18:55:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LicensePutCommand struct {
|
|
|
|
Meta
|
|
|
|
|
|
|
|
testStdin io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LicensePutCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad license put [options]
|
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
Puts a new license in Servers and Clients
|
|
|
|
|
|
|
|
When ACLs are enabled, this command requires a token with the
|
|
|
|
'operator:write' capability.
|
2020-04-21 19:59:39 +00:00
|
|
|
|
2021-03-23 13:08:14 +00:00
|
|
|
Use the -force flag to override the currently installed license with an older
|
|
|
|
license.
|
|
|
|
|
2020-04-21 18:55:31 +00:00
|
|
|
General Options:
|
2020-04-21 19:59:39 +00:00
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
2020-04-21 19:59:39 +00:00
|
|
|
|
2021-03-23 13:08:14 +00:00
|
|
|
License Options:
|
|
|
|
|
|
|
|
-force
|
|
|
|
Force is used to override the currently installed license. By default
|
|
|
|
Nomad will keep the newest license, as determined by the license issue
|
|
|
|
date. Use this flag to apply an older license.
|
|
|
|
|
2020-04-21 18:55:31 +00:00
|
|
|
Install a new license from a file:
|
2020-04-21 19:59:39 +00:00
|
|
|
|
2020-04-28 19:28:35 +00:00
|
|
|
$ nomad license put <path>
|
2020-04-21 19:59:39 +00:00
|
|
|
|
2020-04-21 18:55:31 +00:00
|
|
|
Install a new license from stdin:
|
2020-04-21 19:59:39 +00:00
|
|
|
|
2020-04-21 18:55:31 +00:00
|
|
|
$ nomad license put -
|
2020-04-21 19:59:39 +00:00
|
|
|
|
2020-04-21 18:55:31 +00:00
|
|
|
`
|
2020-04-22 14:19:51 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
2020-04-21 18:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LicensePutCommand) Synopsis() string {
|
|
|
|
return "Install a new Nomad Enterprise License"
|
|
|
|
}
|
|
|
|
|
2021-03-23 13:08:14 +00:00
|
|
|
func (c *LicensePutCommand) AutoCompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-force": complete.PredictNothing,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:55:31 +00:00
|
|
|
func (c *LicensePutCommand) Name() string { return "license put" }
|
|
|
|
|
|
|
|
func (c *LicensePutCommand) Run(args []string) int {
|
2021-03-23 13:08:14 +00:00
|
|
|
var force bool
|
2020-04-21 18:55:31 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2021-03-23 13:08:14 +00:00
|
|
|
flags.BoolVar(&force, "force", false, "")
|
2020-04-21 18:55:31 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = flags.Args()
|
|
|
|
data, err := c.dataFromArgs(args)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(errors.Wrap(err, "Error parsing arguments").Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-03-23 13:08:14 +00:00
|
|
|
opts := &api.ApplyLicenseOptions{
|
|
|
|
Force: force,
|
|
|
|
}
|
|
|
|
_, err = client.Operator().ApplyLicense(data, opts, nil)
|
2020-04-21 18:55:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error putting license: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-03-23 13:08:14 +00:00
|
|
|
lic, _, err := client.Operator().LicenseGet(nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving new license: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if lic.ConfigOutdated {
|
|
|
|
c.Ui.Warn(`
|
|
|
|
WARNING: The server's configured file license is now outdated. Please update or
|
|
|
|
remove the server's license configuration to prevent initialization issues with
|
|
|
|
potentially expired licenses.
|
|
|
|
`) // New line for cli output
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:28:58 +00:00
|
|
|
c.Ui.Output("Successfully applied license")
|
|
|
|
return 0
|
2020-04-21 18:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LicensePutCommand) dataFromArgs(args []string) (string, error) {
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
return "", fmt.Errorf("Missing LICENSE argument")
|
|
|
|
case 1:
|
|
|
|
return LoadDataSource(args[0], c.testStdin)
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("Too many arguments, exptected 1, got %d", len(args))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadFromFile(path string) (string, error) {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to read file: %v", err)
|
|
|
|
}
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadFromStdin(testStdin io.Reader) (string, error) {
|
|
|
|
var stdin io.Reader = os.Stdin
|
|
|
|
if testStdin != nil {
|
|
|
|
stdin = testStdin
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if _, err := io.Copy(&b, stdin); err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to read stdin: %v", err)
|
|
|
|
}
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 19:28:35 +00:00
|
|
|
func LoadDataSource(file string, testStdin io.Reader) (string, error) {
|
2020-04-21 18:55:31 +00:00
|
|
|
// Handle empty quoted shell parameters
|
2020-04-28 19:28:35 +00:00
|
|
|
if len(file) == 0 {
|
2020-04-21 18:55:31 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 19:28:35 +00:00
|
|
|
if file == "-" {
|
|
|
|
if len(file) > 1 {
|
|
|
|
return file, nil
|
2020-04-21 18:55:31 +00:00
|
|
|
}
|
|
|
|
return loadFromStdin(testStdin)
|
|
|
|
}
|
2020-04-28 19:28:35 +00:00
|
|
|
|
|
|
|
return loadFromFile(file)
|
2020-04-21 18:55:31 +00:00
|
|
|
}
|