diff --git a/api/api.go b/api/api.go index 80c03b2564b..20ff1735fad 100644 --- a/api/api.go +++ b/api/api.go @@ -126,6 +126,11 @@ func NewClient(config *Config) (*Client, error) { return client, nil } +// SetRegion sets the region to forward API requests to. +func (c *Client) SetRegion(region string) { + c.config.Region = region +} + // request is used to help build up a request type request struct { config *Config diff --git a/command/meta.go b/command/meta.go index 38120545d1a..354346d832d 100644 --- a/command/meta.go +++ b/command/meta.go @@ -16,6 +16,7 @@ const ( // Names of environment variables used to supply various // config options to the Nomad CLI. EnvNomadAddress = "NOMAD_ADDR" + EnvNomadRegion = "NOMAD_REGION" // Constants for CLI identifier length shortId = 8 @@ -42,6 +43,9 @@ type Meta struct { // Whether to not-colorize output noColor bool + + // The region to send API requests + region string } // FlagSet returns a FlagSet with the common flags that every @@ -55,6 +59,7 @@ func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet { // client connectivity options. if fs&FlagSetClient != 0 { f.StringVar(&m.flagAddress, "address", "", "") + f.StringVar(&m.region, "region", "", "") f.BoolVar(&m.noColor, "no-color", false, "") } @@ -84,6 +89,12 @@ func (m *Meta) Client() (*api.Client, error) { if m.flagAddress != "" { config.Address = m.flagAddress } + if v := os.Getenv(EnvNomadRegion); v != "" { + config.Region = v + } + if m.region != "" { + config.Region = m.region + } return api.NewClient(config) } @@ -102,6 +113,11 @@ func generalOptionsUsage() string { The address of the Nomad server. Overrides the NOMAD_ADDR environment variable if set. Default = http://127.0.0.1:4646 + + -region= + The region of the Nomad servers to forward commands to. + Overrides the NOMAD_REGION environment variable if set. + Defaults to the Agent's local region. ` return strings.TrimSpace(helpText) } diff --git a/command/meta_test.go b/command/meta_test.go index 366d339eb97..d9f402e1fc6 100644 --- a/command/meta_test.go +++ b/command/meta_test.go @@ -18,7 +18,7 @@ func TestMeta_FlagSet(t *testing.T) { }, { FlagSetClient, - []string{"address", "no-color"}, + []string{"address", "no-color", "region"}, }, } diff --git a/command/plan.go b/command/plan.go index 5de76e0aae0..e17bd4622e4 100644 --- a/command/plan.go +++ b/command/plan.go @@ -44,6 +44,9 @@ Usage: nomad plan [options] A structured diff between the local and remote job is displayed to give insight into what the scheduler will attempt to do and why. + If the job has specified the region, the -region flag and NOMAD_REGION + environment variable are overridden and the the job's region is used. + General Options: ` + generalOptionsUsage() + ` @@ -116,6 +119,11 @@ func (c *PlanCommand) Run(args []string) int { return 1 } + // Force the region to be that of the job. + if r := job.Region; r != "" { + client.SetRegion(r) + } + // Submit the job resp, _, err := client.Jobs().Plan(apiJob, diff, nil) if err != nil { diff --git a/command/run.go b/command/run.go index d557d212fa8..30c9fc7d0d4 100644 --- a/command/run.go +++ b/command/run.go @@ -37,6 +37,9 @@ Usage: nomad run [options] exit code will be 2. Any other errors, including client connection issues or internal errors, are indicated by exit code 1. + If the job has specified the region, the -region flag and NOMAD_REGION + environment variable are overridden and the the job's region is used. + General Options: ` + generalOptionsUsage() + ` @@ -134,6 +137,11 @@ func (c *RunCommand) Run(args []string) int { return 1 } + // Force the region to be that of the job. + if r := job.Region; r != "" { + client.SetRegion(r) + } + // Submit the job evalID, _, err := client.Jobs().Register(apiJob, nil) if err != nil { diff --git a/website/helpers/command_helpers.rb b/website/helpers/command_helpers.rb index 0718f2d4846..73e52ee301e 100644 --- a/website/helpers/command_helpers.rb +++ b/website/helpers/command_helpers.rb @@ -4,6 +4,10 @@ def general_options_usage() <`: The address of the Nomad server. Overrides the `NOMAD_ADDR` environment variable if set. Defaults to `http://127.0.0.1:4646`. + +* `-region=`: The region of the Nomad server to forward commands to. + Overrides the `NOMAD_REGION` environment variable if set. Defaults to the + Agent's local region. EOF end end diff --git a/website/source/docs/commands/run.html.md.erb b/website/source/docs/commands/run.html.md.erb index 0c3d7d5f6f7..3e83aa51f66 100644 --- a/website/source/docs/commands/run.html.md.erb +++ b/website/source/docs/commands/run.html.md.erb @@ -32,6 +32,9 @@ there are job placement issues encountered (unsatisfiable constraints, resource exhaustion, etc), then the exit code will be 2. Any other errors, including client connection issues or internal errors, are indicated by exit code 1. +If the job has specified the region, the -region flag and NOMAD_REGION +environment variable are overridden and the the job's region is used. + ## General Options <%= general_options_usage %>