From 92ea016fa0dcdbaf37164e101b087f4afa737765 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 7 Jun 2016 11:16:04 -0700 Subject: [PATCH 1/3] Add region flag and environment variable --- command/meta.go | 16 ++++++++++++++++ command/meta_test.go | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/command/meta.go b/command/meta.go index 38120545d1a..d4095a6bb52 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 too. + 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"}, }, } From cc80c2442fa5646d0dcf6712de55f88aa1ecad9b Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 7 Jun 2016 11:28:02 -0700 Subject: [PATCH 2/3] Run and plan automatically sets the region --- api/api.go | 5 +++++ command/plan.go | 8 ++++++++ command/run.go | 8 ++++++++ 3 files changed, 21 insertions(+) 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/plan.go b/command/plan.go index 5de76e0aae0..b153d1b4bbd 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 to the job's region. + 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..032f54e68a0 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 to the job's region. + 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 { From baff94f3ba4ec0fc03c23c325ce1295952d7ee21 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 7 Jun 2016 11:33:55 -0700 Subject: [PATCH 3/3] docs --- command/meta.go | 2 +- command/plan.go | 2 +- command/run.go | 2 +- website/helpers/command_helpers.rb | 4 ++++ website/source/docs/commands/run.html.md.erb | 3 +++ 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/command/meta.go b/command/meta.go index d4095a6bb52..354346d832d 100644 --- a/command/meta.go +++ b/command/meta.go @@ -115,7 +115,7 @@ func generalOptionsUsage() string { Default = http://127.0.0.1:4646 -region= - The region of the Nomad servers to forward commands too. + 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. ` diff --git a/command/plan.go b/command/plan.go index b153d1b4bbd..e17bd4622e4 100644 --- a/command/plan.go +++ b/command/plan.go @@ -45,7 +45,7 @@ Usage: nomad plan [options] 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 to the job's region. + environment variable are overridden and the the job's region is used. General Options: diff --git a/command/run.go b/command/run.go index 032f54e68a0..30c9fc7d0d4 100644 --- a/command/run.go +++ b/command/run.go @@ -38,7 +38,7 @@ Usage: nomad run [options] 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 to the job's region. + environment variable are overridden and the the job's region is used. General Options: 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 %>