2015-06-01 13:25:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2015-09-14 20:13:52 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2015-06-01 13:25:51 +00:00
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2021-10-06 14:02:42 +00:00
|
|
|
colorable "github.com/mattn/go-colorable"
|
2015-06-01 13:25:51 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2016-05-13 00:17:02 +00:00
|
|
|
"github.com/mitchellh/colorstring"
|
2017-08-23 19:53:15 +00:00
|
|
|
"github.com/posener/complete"
|
2017-08-23 21:35:16 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2015-06-01 13:25:51 +00:00
|
|
|
)
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
const (
|
2016-01-14 20:57:43 +00:00
|
|
|
// Constants for CLI identifier length
|
2016-01-15 22:32:38 +00:00
|
|
|
shortId = 8
|
|
|
|
fullId = 36
|
2015-09-14 20:13:52 +00:00
|
|
|
)
|
|
|
|
|
2015-06-01 13:25:51 +00:00
|
|
|
// FlagSetFlags is an enum to define what flags are present in the
|
|
|
|
// default FlagSet returned by Meta.FlagSet.
|
|
|
|
type FlagSetFlags uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
FlagSetNone FlagSetFlags = 0
|
2015-09-14 20:13:52 +00:00
|
|
|
FlagSetClient FlagSetFlags = 1 << iota
|
|
|
|
FlagSetDefault = FlagSetClient
|
2015-06-01 13:25:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Meta contains the meta-options and functionality that nearly every
|
|
|
|
// Nomad command inherits.
|
|
|
|
type Meta struct {
|
|
|
|
Ui cli.Ui
|
|
|
|
|
|
|
|
// These are set by the command line flags.
|
|
|
|
flagAddress string
|
2016-05-13 00:17:02 +00:00
|
|
|
|
|
|
|
// Whether to not-colorize output
|
|
|
|
noColor bool
|
2016-06-07 18:16:04 +00:00
|
|
|
|
2021-10-06 14:02:42 +00:00
|
|
|
// Whether to force colorized output
|
|
|
|
forceColor bool
|
|
|
|
|
2016-06-07 18:16:04 +00:00
|
|
|
// The region to send API requests
|
|
|
|
region string
|
2016-10-25 00:07:44 +00:00
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
// namespace to send API requests
|
|
|
|
namespace string
|
|
|
|
|
2017-09-26 22:26:33 +00:00
|
|
|
// token is used for ACLs to access privileged information
|
2017-09-12 15:56:55 +00:00
|
|
|
token string
|
|
|
|
|
2019-09-24 14:05:40 +00:00
|
|
|
caCert string
|
|
|
|
caPath string
|
|
|
|
clientCert string
|
|
|
|
clientKey string
|
|
|
|
tlsServerName string
|
|
|
|
insecure bool
|
2015-06-01 13:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlagSet returns a FlagSet with the common flags that every
|
|
|
|
// command implements. The exact behavior of FlagSet can be configured
|
|
|
|
// using the flags as the second parameter, for example to disable
|
|
|
|
// server settings on the commands that don't talk to a server.
|
|
|
|
func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet {
|
|
|
|
f := flag.NewFlagSet(n, flag.ContinueOnError)
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
// FlagSetClient is used to enable the settings for specifying
|
|
|
|
// client connectivity options.
|
|
|
|
if fs&FlagSetClient != 0 {
|
2015-06-01 13:25:51 +00:00
|
|
|
f.StringVar(&m.flagAddress, "address", "", "")
|
2016-06-07 18:16:04 +00:00
|
|
|
f.StringVar(&m.region, "region", "", "")
|
2017-09-07 23:56:15 +00:00
|
|
|
f.StringVar(&m.namespace, "namespace", "", "")
|
2016-05-13 00:17:02 +00:00
|
|
|
f.BoolVar(&m.noColor, "no-color", false, "")
|
2021-10-06 14:02:42 +00:00
|
|
|
f.BoolVar(&m.forceColor, "force-color", false, "")
|
2016-10-25 00:07:44 +00:00
|
|
|
f.StringVar(&m.caCert, "ca-cert", "", "")
|
|
|
|
f.StringVar(&m.caPath, "ca-path", "", "")
|
|
|
|
f.StringVar(&m.clientCert, "client-cert", "", "")
|
|
|
|
f.StringVar(&m.clientKey, "client-key", "", "")
|
|
|
|
f.BoolVar(&m.insecure, "insecure", false, "")
|
2019-09-24 14:05:40 +00:00
|
|
|
f.StringVar(&m.tlsServerName, "tls-server-name", "", "")
|
2016-10-25 00:07:44 +00:00
|
|
|
f.BoolVar(&m.insecure, "tls-skip-verify", false, "")
|
2017-09-12 15:56:55 +00:00
|
|
|
f.StringVar(&m.token, "token", "", "")
|
2016-10-25 00:07:44 +00:00
|
|
|
|
2015-06-01 13:25:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 14:50:10 +00:00
|
|
|
f.SetOutput(&uiErrorWriter{ui: m.Ui})
|
2015-06-01 13:25:51 +00:00
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
2015-09-14 20:13:52 +00:00
|
|
|
|
2017-08-23 19:53:15 +00:00
|
|
|
// AutocompleteFlags returns a set of flag completions for the given flag set.
|
|
|
|
func (m *Meta) AutocompleteFlags(fs FlagSetFlags) complete.Flags {
|
|
|
|
if fs&FlagSetClient == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return complete.Flags{
|
|
|
|
"-address": complete.PredictAnything,
|
|
|
|
"-region": complete.PredictAnything,
|
2017-09-07 23:56:15 +00:00
|
|
|
"-namespace": NamespacePredictor(m.Client, nil),
|
2017-08-23 19:53:15 +00:00
|
|
|
"-no-color": complete.PredictNothing,
|
2021-10-06 14:02:42 +00:00
|
|
|
"-force-color": complete.PredictNothing,
|
2017-08-23 19:53:15 +00:00
|
|
|
"-ca-cert": complete.PredictFiles("*"),
|
|
|
|
"-ca-path": complete.PredictDirs("*"),
|
|
|
|
"-client-cert": complete.PredictFiles("*"),
|
|
|
|
"-client-key": complete.PredictFiles("*"),
|
|
|
|
"-insecure": complete.PredictNothing,
|
2019-09-24 14:05:40 +00:00
|
|
|
"-tls-server-name": complete.PredictNothing,
|
2017-08-23 19:53:15 +00:00
|
|
|
"-tls-skip-verify": complete.PredictNothing,
|
2017-09-12 15:56:55 +00:00
|
|
|
"-token": complete.PredictAnything,
|
2017-08-23 19:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
// ApiClientFactory is the signature of a API client factory
|
|
|
|
type ApiClientFactory func() (*api.Client, error)
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
// Client is used to initialize and return a new API client using
|
|
|
|
// the default command line arguments and env vars.
|
2020-06-17 19:39:50 +00:00
|
|
|
func (m *Meta) clientConfig() *api.Config {
|
2015-09-14 20:13:52 +00:00
|
|
|
config := api.DefaultConfig()
|
2021-12-01 17:07:48 +00:00
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
if m.flagAddress != "" {
|
|
|
|
config.Address = m.flagAddress
|
|
|
|
}
|
2016-06-07 18:16:04 +00:00
|
|
|
if m.region != "" {
|
|
|
|
config.Region = m.region
|
|
|
|
}
|
2017-09-07 23:56:15 +00:00
|
|
|
if m.namespace != "" {
|
|
|
|
config.Namespace = m.namespace
|
|
|
|
}
|
|
|
|
|
2017-09-12 15:56:55 +00:00
|
|
|
if m.token != "" {
|
|
|
|
config.SecretID = m.token
|
|
|
|
}
|
|
|
|
|
2021-12-01 17:07:48 +00:00
|
|
|
// Override TLS configuration fields we may have received from env vars with
|
|
|
|
// flag arguments from the user only if they're provided.
|
|
|
|
if m.caCert != "" {
|
|
|
|
config.TLSConfig.CACert = m.caCert
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.caPath != "" {
|
|
|
|
config.TLSConfig.CAPath = m.caPath
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.clientCert != "" {
|
|
|
|
config.TLSConfig.ClientCert = m.clientCert
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.clientKey != "" {
|
|
|
|
config.TLSConfig.ClientKey = m.clientKey
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.tlsServerName != "" {
|
|
|
|
config.TLSConfig.TLSServerName = m.tlsServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.insecure {
|
|
|
|
config.TLSConfig.Insecure = m.insecure
|
|
|
|
}
|
|
|
|
|
2020-06-17 19:39:50 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Meta) Client() (*api.Client, error) {
|
|
|
|
return api.NewClient(m.clientConfig())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Meta) allNamespaces() bool {
|
|
|
|
return m.clientConfig().Namespace == api.AllNamespacesNamespace
|
2015-09-14 20:13:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 00:17:02 +00:00
|
|
|
func (m *Meta) Colorize() *colorstring.Colorize {
|
2021-09-18 00:37:05 +00:00
|
|
|
_, coloredUi := m.Ui.(*cli.ColoredUi)
|
|
|
|
|
2016-05-13 00:17:02 +00:00
|
|
|
return &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
2021-10-06 14:02:42 +00:00
|
|
|
Disable: !coloredUi,
|
2016-05-13 00:17:02 +00:00
|
|
|
Reset: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 14:02:42 +00:00
|
|
|
func (m *Meta) SetupUi(args []string) {
|
|
|
|
noColor := os.Getenv(EnvNomadCLINoColor) != ""
|
|
|
|
forceColor := os.Getenv(EnvNomadCLIForceColor) != ""
|
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
// Check if color is set
|
|
|
|
if arg == "-no-color" || arg == "--no-color" {
|
|
|
|
noColor = true
|
|
|
|
} else if arg == "-force-color" || arg == "--force-color" {
|
|
|
|
forceColor = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Ui = &cli.BasicUi{
|
|
|
|
Reader: os.Stdin,
|
|
|
|
Writer: colorable.NewColorableStdout(),
|
|
|
|
ErrorWriter: colorable.NewColorableStderr(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only use colored UI if not disabled and stdout is a tty or colors are
|
|
|
|
// forced.
|
|
|
|
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
|
|
|
|
useColor := !noColor && (isTerminal || forceColor)
|
|
|
|
if useColor {
|
|
|
|
m.Ui = &cli.ColoredUi{
|
|
|
|
ErrorColor: cli.UiColorRed,
|
|
|
|
WarnColor: cli.UiColorYellow,
|
|
|
|
InfoColor: cli.UiColorGreen,
|
|
|
|
Ui: m.Ui,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
type usageOptsFlags uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
usageOptsDefault usageOptsFlags = 0
|
|
|
|
usageOptsNoNamespace = 1 << iota
|
|
|
|
)
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
// generalOptionsUsage returns the help string for the global options.
|
2020-11-19 16:15:23 +00:00
|
|
|
func generalOptionsUsage(usageOpts usageOptsFlags) string {
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
helpText := `
|
|
|
|
-address=<addr>
|
|
|
|
The address of the Nomad server.
|
|
|
|
Overrides the NOMAD_ADDR environment variable if set.
|
|
|
|
Default = http://127.0.0.1:4646
|
2016-06-07 18:16:04 +00:00
|
|
|
|
|
|
|
-region=<region>
|
2016-06-07 18:33:55 +00:00
|
|
|
The region of the Nomad servers to forward commands to.
|
2016-06-07 18:16:04 +00:00
|
|
|
Overrides the NOMAD_REGION environment variable if set.
|
|
|
|
Defaults to the Agent's local region.
|
2020-11-19 16:15:23 +00:00
|
|
|
`
|
2017-08-23 19:53:15 +00:00
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
namespaceText := `
|
2017-09-07 23:56:15 +00:00
|
|
|
-namespace=<namespace>
|
|
|
|
The target namespace for queries and actions bound to a namespace.
|
|
|
|
Overrides the NOMAD_NAMESPACE environment variable if set.
|
2022-03-23 11:56:48 +00:00
|
|
|
If set to '*', subcommands which support this functionality query
|
|
|
|
all namespaces authorized to user.
|
2017-09-07 23:56:15 +00:00
|
|
|
Defaults to the "default" namespace.
|
2020-11-19 16:15:23 +00:00
|
|
|
`
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
// note: that although very few commands use color explicitly, all of them
|
2021-10-06 14:02:42 +00:00
|
|
|
// return red-colored text on error so we want the color flags to always be
|
|
|
|
// present in the help messages.
|
2020-11-19 16:15:23 +00:00
|
|
|
remainingText := `
|
2016-06-11 21:40:51 +00:00
|
|
|
-no-color
|
2018-03-21 18:18:26 +00:00
|
|
|
Disables colored command output. Alternatively, NOMAD_CLI_NO_COLOR may be
|
2021-10-06 14:02:42 +00:00
|
|
|
set. This option takes precedence over -force-color.
|
|
|
|
|
|
|
|
-force-color
|
|
|
|
Forces colored command output. This can be used in cases where the usual
|
|
|
|
terminal detection fails. Alternatively, NOMAD_CLI_FORCE_COLOR may be set.
|
|
|
|
This option has no effect if -no-color is also used.
|
2016-10-25 00:07:44 +00:00
|
|
|
|
2017-08-23 19:53:15 +00:00
|
|
|
-ca-cert=<path>
|
|
|
|
Path to a PEM encoded CA cert file to use to verify the
|
2021-08-03 14:05:06 +00:00
|
|
|
Nomad server SSL certificate. Overrides the NOMAD_CACERT
|
2016-10-25 00:07:44 +00:00
|
|
|
environment variable if set.
|
|
|
|
|
2017-08-23 19:53:15 +00:00
|
|
|
-ca-path=<path>
|
|
|
|
Path to a directory of PEM encoded CA cert files to verify
|
|
|
|
the Nomad server SSL certificate. If both -ca-cert and
|
|
|
|
-ca-path are specified, -ca-cert is used. Overrides the
|
2016-10-25 00:07:44 +00:00
|
|
|
NOMAD_CAPATH environment variable if set.
|
|
|
|
|
2017-08-23 19:53:15 +00:00
|
|
|
-client-cert=<path>
|
|
|
|
Path to a PEM encoded client certificate for TLS authentication
|
|
|
|
to the Nomad server. Must also specify -client-key. Overrides
|
2016-10-25 00:07:44 +00:00
|
|
|
the NOMAD_CLIENT_CERT environment variable if set.
|
|
|
|
|
2017-08-23 19:53:15 +00:00
|
|
|
-client-key=<path>
|
|
|
|
Path to an unencrypted PEM encoded private key matching the
|
|
|
|
client certificate from -client-cert. Overrides the
|
2016-10-25 00:07:44 +00:00
|
|
|
NOMAD_CLIENT_KEY environment variable if set.
|
2020-09-28 16:23:25 +00:00
|
|
|
|
2019-09-24 14:05:40 +00:00
|
|
|
-tls-server-name=<value>
|
2020-09-28 16:23:25 +00:00
|
|
|
The server name to use as the SNI host when connecting via
|
2019-09-24 14:05:40 +00:00
|
|
|
TLS. Overrides the NOMAD_TLS_SERVER_NAME environment variable if set.
|
2016-10-25 00:07:44 +00:00
|
|
|
|
2017-08-23 19:53:15 +00:00
|
|
|
-tls-skip-verify
|
2016-10-25 00:07:44 +00:00
|
|
|
Do not verify TLS certificate. This is highly not recommended. Verification
|
|
|
|
will also be skipped if NOMAD_SKIP_VERIFY is set.
|
2017-09-12 15:56:55 +00:00
|
|
|
|
|
|
|
-token
|
2017-09-15 04:30:14 +00:00
|
|
|
The SecretID of an ACL token to use to authenticate API requests with.
|
|
|
|
Overrides the NOMAD_TOKEN environment variable if set.
|
2015-09-14 20:13:52 +00:00
|
|
|
`
|
2020-11-19 16:15:23 +00:00
|
|
|
|
|
|
|
if usageOpts&usageOptsNoNamespace == 0 {
|
|
|
|
helpText = helpText + namespaceText
|
|
|
|
}
|
|
|
|
|
|
|
|
helpText = helpText + remainingText
|
2015-09-14 20:13:52 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
2017-09-15 04:55:25 +00:00
|
|
|
|
|
|
|
// funcVar is a type of flag that accepts a function that is the string given
|
|
|
|
// by the user.
|
|
|
|
type funcVar func(s string) error
|
|
|
|
|
|
|
|
func (f funcVar) Set(s string) error { return f(s) }
|
|
|
|
func (f funcVar) String() string { return "" }
|
|
|
|
func (f funcVar) IsBoolFlag() bool { return false }
|