From 89f271a9da3f77f59240edd738c5a9870fcfdb04 Mon Sep 17 00:00:00 2001 From: lalyos Date: Mon, 15 Dec 2014 19:16:43 +0100 Subject: [PATCH] Add default rpc address configuration option as CONSUL_RPC_ADDR env variable Similar as in serf: https://github.com/hashicorp/serf/pull/210 --- command/rpc.go | 12 +++- command/rpc_test.go | 59 +++++++++++++++++++ .../docs/commands/force-leave.html.markdown | 5 +- .../source/docs/commands/index.html.markdown | 4 +- .../source/docs/commands/info.html.markdown | 5 +- .../source/docs/commands/join.html.markdown | 5 +- .../docs/commands/keyring.html.markdown | 4 +- .../source/docs/commands/leave.html.markdown | 5 +- .../docs/commands/members.html.markdown | 5 +- .../docs/commands/monitor.html.markdown | 6 +- .../source/docs/commands/reload.html.markdown | 5 +- 11 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 command/rpc_test.go diff --git a/command/rpc.go b/command/rpc.go index 0975c6b4e3..d6c47fd59c 100644 --- a/command/rpc.go +++ b/command/rpc.go @@ -2,14 +2,24 @@ package command import ( "flag" + "os" + "github.com/armon/consul-api" "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 // when the given flagset is parsed with the RPC address of the Consul. 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") } diff --git a/command/rpc_test.go b/command/rpc_test.go new file mode 100644 index 0000000000..01d6259487 --- /dev/null +++ b/command/rpc_test.go @@ -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) + } +} diff --git a/website/source/docs/commands/force-leave.html.markdown b/website/source/docs/commands/force-leave.html.markdown index fe01de0aa3..165bae82fa 100644 --- a/website/source/docs/commands/force-leave.html.markdown +++ b/website/source/docs/commands/force-leave.html.markdown @@ -32,6 +32,7 @@ The following command-line options are available for this command. Every option is optional: * `-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. + to send this command. If this isn't specified, the command checks the + CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC + address will be set to "127.0.0.1:8400". diff --git a/website/source/docs/commands/index.html.markdown b/website/source/docs/commands/index.html.markdown index 7a71bca57b..fafca6ed90 100644 --- a/website/source/docs/commands/index.html.markdown +++ b/website/source/docs/commands/index.html.markdown @@ -53,6 +53,8 @@ Usage: consul join [options] address ... 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 ``` diff --git a/website/source/docs/commands/info.html.markdown b/website/source/docs/commands/info.html.markdown index 5df9bf6248..aaf3c7af8d 100644 --- a/website/source/docs/commands/info.html.markdown +++ b/website/source/docs/commands/info.html.markdown @@ -75,6 +75,7 @@ Usage: `consul info` 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 - 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. + to send this command. If this isn't specified, the command checks the + CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC + address will be set to "127.0.0.1:8400". diff --git a/website/source/docs/commands/join.html.markdown b/website/source/docs/commands/join.html.markdown index 8d3100b925..cd9d624fcf 100644 --- a/website/source/docs/commands/join.html.markdown +++ b/website/source/docs/commands/join.html.markdown @@ -38,6 +38,7 @@ The command-line flags are all optional. The list of available flags are: multiple datacenters. * `-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. + to send this command. If this isn't specified, the command checks the + CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC + address will be set to "127.0.0.1:8400". diff --git a/website/source/docs/commands/keyring.html.markdown b/website/source/docs/commands/keyring.html.markdown index 3ecbaebc90..4d85bf0f6f 100644 --- a/website/source/docs/commands/keyring.html.markdown +++ b/website/source/docs/commands/keyring.html.markdown @@ -46,7 +46,9 @@ The list of available flags are: * `-remove` - Remove the given key from the cluster. This operation may only be 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 diff --git a/website/source/docs/commands/leave.html.markdown b/website/source/docs/commands/leave.html.markdown index 80a0e50dba..be55e61cbc 100644 --- a/website/source/docs/commands/leave.html.markdown +++ b/website/source/docs/commands/leave.html.markdown @@ -26,6 +26,7 @@ Usage: `consul leave` 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 - 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. + to send this command. If this isn't specified, the command checks the + CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC + address will be set to "127.0.0.1:8400". diff --git a/website/source/docs/commands/members.html.markdown b/website/source/docs/commands/members.html.markdown index 1e1f4f028e..2c39b02458 100644 --- a/website/source/docs/commands/members.html.markdown +++ b/website/source/docs/commands/members.html.markdown @@ -28,8 +28,9 @@ The command-line flags are all optional. The list of available flags are: about each node. * `-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. + to send this command.If this isn't specified, the command checks the + 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 the regular expression for status diff --git a/website/source/docs/commands/monitor.html.markdown b/website/source/docs/commands/monitor.html.markdown index 8402f9860e..0d609dc3b2 100644 --- a/website/source/docs/commands/monitor.html.markdown +++ b/website/source/docs/commands/monitor.html.markdown @@ -30,6 +30,6 @@ The command-line flags are all optional. The list of available flags are: "warn", and "err". * `-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. - + to send this command. If this isn't specified, the command checks the + CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC + address will be set to "127.0.0.1:8400". diff --git a/website/source/docs/commands/reload.html.markdown b/website/source/docs/commands/reload.html.markdown index b00a0120a8..f1a59f745e 100644 --- a/website/source/docs/commands/reload.html.markdown +++ b/website/source/docs/commands/reload.html.markdown @@ -32,6 +32,7 @@ Usage: `consul reload` 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 - 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. + to send this command. If this isn't specified, the command checks the + CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC + address will be set to "127.0.0.1:8400".