Add default rpc address configuration option as CONSUL_RPC_ADDR env variable

Similar as in serf: https://github.com/hashicorp/serf/pull/210
pull/542/head
lalyos 2014-12-15 19:16:43 +01:00
parent b3b9e860ec
commit 89f271a9da
11 changed files with 97 additions and 18 deletions

View File

@ -2,14 +2,24 @@ package command
import ( import (
"flag" "flag"
"os"
"github.com/armon/consul-api" "github.com/armon/consul-api"
"github.com/hashicorp/consul/command/agent" "github.com/hashicorp/consul/command/agent"
) )
// RPCAddrEnvName defines the environment variable name, which can set
// a default RPC address in case there is no -rpc-addr specified.
const RPCAddrEnvName = "CONSUL_RPC_ADDR"
// RPCAddrFlag returns a pointer to a string that will be populated // RPCAddrFlag returns a pointer to a string that will be populated
// when the given flagset is parsed with the RPC address of the Consul. // when the given flagset is parsed with the RPC address of the Consul.
func RPCAddrFlag(f *flag.FlagSet) *string { func RPCAddrFlag(f *flag.FlagSet) *string {
return f.String("rpc-addr", "127.0.0.1:8400", defaultRPCAddr := os.Getenv(RPCAddrEnvName)
if defaultRPCAddr == "" {
defaultRPCAddr = "127.0.0.1:8400"
}
return f.String("rpc-addr", defaultRPCAddr,
"RPC address of the Consul agent") "RPC address of the Consul agent")
} }

59
command/rpc_test.go Normal file
View File

@ -0,0 +1,59 @@
package command
import (
"flag"
"os"
"testing"
)
const defaultRPC = "127.0.0.1:8400"
func getParsedRPC(t *testing.T, cliRPC, envRPC string) string {
args := []string{}
if cliRPC != "" {
args = append(args, "-rpc-addr="+cliRPC)
}
os.Clearenv()
if envRPC != "" {
os.Setenv(RPCAddrEnvName, envRPC)
}
cmdFlags := flag.NewFlagSet("rpc", flag.ContinueOnError)
rpc := RPCAddrFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil {
t.Fatal("Parse error", err)
}
return *rpc
}
func TestRPCAddrFlag_default(t *testing.T) {
rpc := getParsedRPC(t, "", "")
if rpc != defaultRPC {
t.Fatalf("Expected rpc addr: %s, got: %s", defaultRPC, rpc)
}
}
func TestRPCAddrFlag_onlyEnv(t *testing.T) {
envRPC := "4.4.4.4:8400"
rpc := getParsedRPC(t, "", envRPC)
if rpc != envRPC {
t.Fatalf("Expected rpc addr: %s, got: %s", envRPC, rpc)
}
}
func TestRPCAddrFlag_precedence(t *testing.T) {
cliRPC := "8.8.8.8:8400"
envRPC := "4.4.4.4:8400"
rpc := getParsedRPC(t, cliRPC, envRPC)
if rpc != cliRPC {
t.Fatalf("Expected rpc addr: %s, got: %s", cliRPC, rpc)
}
}

View File

@ -32,6 +32,7 @@ The following command-line options are available for this command.
Every option is optional: Every option is optional:
* `-rpc-addr` - Address to the RPC server of the agent you want to contact * `-rpc-addr` - Address to the RPC server of the agent you want to contact
to send this command. If this isn't specified, the command will contact to send this command. If this isn't specified, the command checks the
"127.0.0.1:8400" which is the default RPC address of a Consul agent. CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC
address will be set to "127.0.0.1:8400".

View File

@ -53,6 +53,8 @@ Usage: consul join [options] address ...
Options: Options:
-rpc-addr=127.0.0.1:8400 RPC address of the Consul agent. -rpc-addr=127.0.0.1:8400 Address to the RPC server of the agent you want to contact
to send this command. If this isn't specified, the command checks the
CONSUL_RPC_ADDR env variable.
-wan Joins a server to another server in the WAN pool -wan Joins a server to another server in the WAN pool
``` ```

View File

@ -75,6 +75,7 @@ Usage: `consul info`
The command-line flags are all optional. The list of available flags are: The command-line flags are all optional. The list of available flags are:
* `-rpc-addr` - Address to the RPC server of the agent you want to contact * `-rpc-addr` - Address to the RPC server of the agent you want to contact
to send this command. If this isn't specified, the command will contact to send this command. If this isn't specified, the command checks the
"127.0.0.1:8400" which is the default RPC address of a Consul agent. CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC
address will be set to "127.0.0.1:8400".

View File

@ -38,6 +38,7 @@ The command-line flags are all optional. The list of available flags are:
multiple datacenters. multiple datacenters.
* `-rpc-addr` - Address to the RPC server of the agent you want to contact * `-rpc-addr` - Address to the RPC server of the agent you want to contact
to send this command. If this isn't specified, the command will contact to send this command. If this isn't specified, the command checks the
"127.0.0.1:8400" which is the default RPC address of a Consul agent. CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC
address will be set to "127.0.0.1:8400".

View File

@ -46,7 +46,9 @@ The list of available flags are:
* `-remove` - Remove the given key from the cluster. This operation may only be * `-remove` - Remove the given key from the cluster. This operation may only be
performed on keys which are not currently the primary key. performed on keys which are not currently the primary key.
* `-rpc-addr` - RPC address of the Consul agent. * `-rpc-addr` - Address to the RPC server of the agent you want to contact
to send this command. If this isn't specified, the command will contact
"127.0.0.1:8400" which is the default RPC address of a Consul agent.
## Output ## Output

View File

@ -26,6 +26,7 @@ Usage: `consul leave`
The command-line flags are all optional. The list of available flags are: The command-line flags are all optional. The list of available flags are:
* `-rpc-addr` - Address to the RPC server of the agent you want to contact * `-rpc-addr` - Address to the RPC server of the agent you want to contact
to send this command. If this isn't specified, the command will contact to send this command. If this isn't specified, the command checks the
"127.0.0.1:8400" which is the default RPC address of a Consul agent. CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC
address will be set to "127.0.0.1:8400".

View File

@ -28,8 +28,9 @@ The command-line flags are all optional. The list of available flags are:
about each node. about each node.
* `-rpc-addr` - Address to the RPC server of the agent you want to contact * `-rpc-addr` - Address to the RPC server of the agent you want to contact
to send this command. If this isn't specified, the command will contact to send this command.If this isn't specified, the command checks the
"127.0.0.1:8400 " which is the default RPC address of a Consul agent. CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC
address will be set to "127.0.0.1:8400".
* `-status` - If provided, output is filtered to only nodes matching * `-status` - If provided, output is filtered to only nodes matching
the regular expression for status the regular expression for status

View File

@ -30,6 +30,6 @@ The command-line flags are all optional. The list of available flags are:
"warn", and "err". "warn", and "err".
* `-rpc-addr` - Address to the RPC server of the agent you want to contact * `-rpc-addr` - Address to the RPC server of the agent you want to contact
to send this command. If this isn't specified, the command will contact to send this command. If this isn't specified, the command checks the
"127.0.0.1:8400" which is the default RPC address of a Consul agent. CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC
address will be set to "127.0.0.1:8400".

View File

@ -32,6 +32,7 @@ Usage: `consul reload`
The command-line flags are all optional. The list of available flags are: The command-line flags are all optional. The list of available flags are:
* `-rpc-addr` - Address to the RPC server of the agent you want to contact * `-rpc-addr` - Address to the RPC server of the agent you want to contact
to send this command. If this isn't specified, the command will contact to send this command. If this isn't specified, the command checks the
"127.0.0.1:8400" which is the default RPC address of a Consul agent. CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC
address will be set to "127.0.0.1:8400".