Merge pull request #1237 from hashicorp/b-regions

CLI can forward request to different regions
This commit is contained in:
Alex Dadgar
2016-06-08 11:00:23 -07:00
7 changed files with 45 additions and 1 deletions

View File

@@ -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=<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)
}

View File

@@ -18,7 +18,7 @@ func TestMeta_FlagSet(t *testing.T) {
},
{
FlagSetClient,
[]string{"address", "no-color"},
[]string{"address", "no-color", "region"},
},
}

View File

@@ -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() + `
@@ -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 {

View File

@@ -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() + `
@@ -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 {