2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-04-21 18:55:31 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2020-04-21 19:59:39 +00:00
|
|
|
var _ cli.Command = &LicenseCommand{}
|
|
|
|
|
2020-04-21 18:55:31 +00:00
|
|
|
type LicenseCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LicenseCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad license <subcommand> [options] [args]
|
2021-04-27 20:50:07 +00:00
|
|
|
|
2020-04-21 18:55:31 +00:00
|
|
|
This command has subcommands for managing the Nomad Enterprise license.
|
|
|
|
For more detailed examples see:
|
|
|
|
https://www.nomadproject.io/docs/commands/license/
|
|
|
|
|
2021-04-27 20:50:07 +00:00
|
|
|
Retrieve the server's license:
|
2020-04-21 18:55:31 +00:00
|
|
|
|
|
|
|
$ nomad license get
|
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LicenseCommand) Synopsis() string {
|
|
|
|
return "Interact with Nomad Enterprise License"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LicenseCommand) Name() string { return "license" }
|
|
|
|
|
|
|
|
func (l *LicenseCommand) Run(args []string) int {
|
|
|
|
return cli.RunResultHelp
|
|
|
|
}
|
|
|
|
|
|
|
|
func OutputLicenseReply(ui cli.Ui, resp *api.LicenseReply) int {
|
2020-05-05 14:28:58 +00:00
|
|
|
now := time.Now()
|
2021-03-23 13:08:14 +00:00
|
|
|
expired := resp.License.ExpirationTime.Before(now)
|
2023-04-17 13:01:43 +00:00
|
|
|
terminated := resp.License.TerminationTime.Before(now)
|
|
|
|
outputLicenseInfo(ui, resp.License, expired, terminated)
|
2021-03-23 13:08:14 +00:00
|
|
|
if expired {
|
2020-04-21 18:55:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
2020-05-05 14:28:58 +00:00
|
|
|
return 0
|
2020-04-21 18:55:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 13:01:43 +00:00
|
|
|
func outputLicenseInfo(ui cli.Ui, lic *api.License, expired, terminated bool) {
|
2020-04-28 19:28:35 +00:00
|
|
|
expStr := ""
|
2020-04-21 18:55:31 +00:00
|
|
|
if expired {
|
2020-04-30 12:56:40 +00:00
|
|
|
expStr = fmt.Sprintf("Expired At|%s", lic.ExpirationTime.String())
|
2020-04-21 18:55:31 +00:00
|
|
|
} else {
|
2020-04-30 12:56:40 +00:00
|
|
|
expStr = fmt.Sprintf("Expires At|%s", lic.ExpirationTime.String())
|
2020-04-21 18:55:31 +00:00
|
|
|
}
|
2023-04-17 13:01:43 +00:00
|
|
|
termStr := ""
|
|
|
|
if terminated {
|
|
|
|
termStr = fmt.Sprintf("Terminated At|%s", lic.TerminationTime.String())
|
|
|
|
} else {
|
|
|
|
termStr = fmt.Sprintf("Terminates At|%s", lic.TerminationTime.String())
|
|
|
|
}
|
2020-04-28 19:28:35 +00:00
|
|
|
|
2021-03-23 13:08:14 +00:00
|
|
|
validity := "valid"
|
|
|
|
if expired {
|
|
|
|
validity = "expired!"
|
|
|
|
}
|
2020-04-28 19:28:35 +00:00
|
|
|
output := []string{
|
2020-05-05 14:28:58 +00:00
|
|
|
fmt.Sprintf("Product|%s", lic.Product),
|
2020-04-30 12:56:40 +00:00
|
|
|
fmt.Sprintf("License Status|%s", validity),
|
|
|
|
fmt.Sprintf("License ID|%s", lic.LicenseID),
|
|
|
|
fmt.Sprintf("Customer ID|%s", lic.CustomerID),
|
2021-03-23 13:08:14 +00:00
|
|
|
fmt.Sprintf("Issued At|%s", lic.IssueTime),
|
2020-04-28 19:28:35 +00:00
|
|
|
expStr,
|
2023-04-17 13:01:43 +00:00
|
|
|
termStr,
|
2020-04-30 12:56:40 +00:00
|
|
|
fmt.Sprintf("Datacenter|%s", lic.InstallationID),
|
2020-04-28 19:28:35 +00:00
|
|
|
}
|
|
|
|
ui.Output(formatKV(output))
|
2020-04-30 12:56:40 +00:00
|
|
|
|
2020-04-21 18:55:31 +00:00
|
|
|
if len(lic.Modules) > 0 {
|
|
|
|
ui.Output("Modules:")
|
|
|
|
for _, mod := range lic.Modules {
|
2020-05-07 16:14:15 +00:00
|
|
|
ui.Output(fmt.Sprintf("\t%s", mod))
|
2020-04-21 18:55:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-07 16:14:15 +00:00
|
|
|
if len(lic.Features) > 0 {
|
|
|
|
ui.Output("Licensed Features:")
|
|
|
|
for _, f := range lic.Features {
|
|
|
|
ui.Output(fmt.Sprintf("\t%s", f))
|
|
|
|
}
|
2020-04-21 18:55:31 +00:00
|
|
|
}
|
|
|
|
}
|