Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI can forward request to different regions #1237

Merged
merged 3 commits into from
Jun 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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, "")
}

Expand Down Expand Up @@ -84,6 +89,12 @@ func (m *Meta) Client() (*api.Client, error) {
if m.flagAddress != "" {
config.Address = m.flagAddress
}
if v := os.Getenv(EnvNomadRegion); v != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this in an else below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is actually distinct from flagAddress. The precedent is the env-var -> flag -> job's region in the case of run and plan

config.Region = v
}
if m.region != "" {
config.Region = m.region
}
return api.NewClient(config)
}

Expand All @@ -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=<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)
}
2 changes: 1 addition & 1 deletion command/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestMeta_FlagSet(t *testing.T) {
},
{
FlagSetClient,
[]string{"address", "no-color"},
[]string{"address", "no-color", "region"},
},
}

Expand Down
8 changes: 8 additions & 0 deletions command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Usage: nomad plan [options] <file>
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() + `
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Usage: nomad run [options] <file>
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() + `
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions website/helpers/command_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ def general_options_usage()
<<EOF
* `-address=<addr>`: The address of the Nomad server. Overrides the `NOMAD_ADDR`
environment variable if set. Defaults to `http://127.0.0.1:4646`.

* `-region=<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
3 changes: 3 additions & 0 deletions website/source/docs/commands/run.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down