Dynamically select distribution node

This commit is contained in:
Daniil Fedotov 2016-06-08 15:19:37 +01:00
parent 204e982a94
commit 9b703b2896
48 changed files with 100 additions and 60 deletions

View File

@ -52,7 +52,7 @@ defmodule HelpCommand do
defp print_base_usage() do
IO.puts "Usage:"
IO.puts "rabbitmqctl [-n <node>] [-t <timeout>] [-q] <command> [<command options>]"
IO.puts "rabbitmqctl [-n <node>] [-t <timeout>] [-l] [-q] <command> [<command options>]"
end
def print_base_usage(command) do
@ -74,6 +74,7 @@ Options:
-n node
-q
-t timeout
-l longnames
Default node is \"rabbit@server\", where server is the local host. On a host
named \"server.example.com\", the node name of the RabbitMQ Erlang node will
@ -87,6 +88,10 @@ suppressed when quiet mode is in effect.
Operation timeout in seconds. Only applicable to \"list\" commands. Default is
\"infinity\".
If RabbitMQ broker uses long node names for erlang distribution, \"longnames\"
option should be specified.
Some commands accept an optional virtual host parameter for which
to display results. The default value is \"/\".\n"
end

View File

@ -79,7 +79,7 @@ defmodule Helpers do
def power_as_int(num, x, y), do: round(num * (:math.pow(x, y)))
def global_flags, do: [:node, :quiet, :timeout]
def global_flags, do: [:node, :quiet, :timeout, :longnames]
def nodes_in_cluster(node, timeout \\ :infinity) do
case :rpc.call(node, :rabbit_mnesia, :cluster_nodes, [:running], timeout) do

View File

@ -22,8 +22,12 @@ defmodule Parser do
def parse(command) do
{options, cmd, invalid} = OptionParser.parse(
command,
switches: build_switches([node: :atom, quiet: :boolean, timeout: :integer, vhost: :string]),
aliases: [p: :vhost, n: :node, q: :quiet, t: :timeout]
switches: build_switches([node: :atom,
quiet: :boolean,
timeout: :integer,
vhost: :string,
longnames: :boolean]),
aliases: [p: :vhost, n: :node, q: :quiet, t: :timeout, l: :longnames]
)
{clear_on_empty_command(cmd), options_map(options), invalid}
end

View File

@ -20,19 +20,22 @@ defmodule RabbitMQCtl do
import ExitCodes
def main(unparsed_command) do
:net_kernel.start([:rabbitmqctl, :shortnames])
{parsed_cmd, options, invalid} = parse(unparsed_command)
case {Helpers.is_command?(parsed_cmd), invalid} do
{false, _} -> HelpCommand.all_usage() |> handle_exit(exit_usage);
{_, [_|_]} -> print_standard_messages({:bad_option, invalid}, unparsed_command)
|> handle_exit
{true, []} -> options
|> merge_defaults_defaults
|> run_command(parsed_cmd)
|> StandardCodes.map_to_standard_code
|> print_standard_messages(unparsed_command)
|> handle_exit
{false, _} ->
HelpCommand.all_usage() |> handle_exit(exit_usage);
{_, [_|_]} ->
print_standard_messages({:bad_option, invalid}, unparsed_command)
|> handle_exit
{true, []} ->
effective_options = merge_defaults_defaults(options)
start_distribution(effective_options)
effective_options
|> run_command(parsed_cmd)
|> StandardCodes.map_to_standard_code
|> print_standard_messages(unparsed_command)
|> handle_exit
end
end
@ -40,12 +43,15 @@ defmodule RabbitMQCtl do
options
|> merge_defaults_node
|> merge_defaults_timeout
|> merge_defaults_longnames
end
defp merge_defaults_node(%{} = opts), do: Map.merge(%{node: get_rabbit_hostname}, opts)
defp merge_defaults_timeout(%{} = opts), do: Map.merge(%{timeout: :infinity}, opts)
defp merge_defaults_longnames(%{} = opts), do: Map.merge(%{longnames: false}, opts)
defp maybe_connect_to_rabbitmq("help", _), do: nil
defp maybe_connect_to_rabbitmq(_, node) do
Helpers.connect_to_rabbitmq(node)
@ -202,7 +208,7 @@ defmodule RabbitMQCtl do
defp handle_exit({:validation_failure, {:bad_argument, _}}), do: exit_program(exit_dataerr)
defp handle_exit({:validation_failure, :bad_argument}), do: exit_program(exit_dataerr)
defp handle_exit({:validation_failure, _}), do: exit_program(exit_usage)
defp handle_exit({:bad_option, _}), do: exit_program(exit_usage)
defp handle_exit({:bad_option, _} = err), do: exit_program(exit_usage)
defp handle_exit({:badrpc, :timeout}), do: exit_program(exit_tempfail)
defp handle_exit({:badrpc, :nodedown}), do: exit_program(exit_unavailable)
defp handle_exit({:refused, _, _, _}), do: exit_program(exit_dataerr)
@ -231,4 +237,29 @@ defmodule RabbitMQCtl do
:net_kernel.stop
exit({:shutdown, code})
end
def start_distribution(options) do
names_opt = case options[:longnames] do
true -> [:longnames];
false -> [:shortnames];
nil -> [:shortnames]
end
start_distribution(names_opt, 10, :undefined)
end
defp start_distribution(_opt, 0, last_err) do
{:error, last_err}
end
defp start_distribution(names_opt, attempts, _last_err) do
candidate = String.to_atom("rabbitmqcil" <>
to_string(:rabbit_misc.random(100)))
case :net_kernel.start([candidate | names_opt]) do
{:ok, _} = ok -> ok;
{:error, reason} -> start_distribution(names_opt,
attempts - 1,
reason)
end
end
end

View File

@ -21,7 +21,7 @@ defmodule AddUserCommandTest do
@command AddUserCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -22,7 +22,7 @@ defmodule AddVhostCommandTest do
@vhost "test"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -23,7 +23,7 @@ defmodule AuthenticateUserCommandTest do
@password "password"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -20,7 +20,7 @@ defmodule ChangePasswordCommandTest do
@password "password"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -27,7 +27,7 @@ defmodule ClearParameterCommandTest do
@value "{\"uri\":\"amqp://\"}"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
add_vhost @vhost

View File

@ -20,7 +20,7 @@ defmodule ClearPasswordCommandTest do
@password "password"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -22,7 +22,7 @@ defmodule ClearPermissionsTest do
@specific_vhost "vhost1"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
add_user(@user, @password)
add_vhost(@specific_vhost)

View File

@ -21,7 +21,7 @@ defmodule CloseConnectionCommandTest do
@command CloseConnectionCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
close_all_connections(get_rabbit_hostname)

View File

@ -21,7 +21,7 @@ defmodule ClusterStatusCommandTest do
@command ClusterStatusCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -23,7 +23,7 @@ defmodule DeleteUserCommandTest do
@password "password"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -22,7 +22,7 @@ defmodule DeleteVhostCommandTest do
@vhost "test"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -21,7 +21,7 @@ defmodule EnvironmentCommandTest do
@command EnvironmentCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -21,7 +21,7 @@ defmodule ForceResetCommandTest do
@command ForceResetCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
start_rabbitmq_app

View File

@ -21,7 +21,7 @@ defmodule HelpersTest do
@subject Helpers
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
on_exit([], fn -> :net_kernel.stop() end)
:ok
end

View File

@ -21,7 +21,7 @@ defmodule JoinClusterCommandTest do
@command JoinClusterCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
start_rabbitmq_app

View File

@ -9,7 +9,7 @@ defmodule ListBindingsCommandTest do
@default_timeout :infinity
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -22,7 +22,7 @@ defmodule ListChannelsCommandTest do
@default_timeout :infinity
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
close_all_connections(get_rabbit_hostname)

View File

@ -7,7 +7,7 @@ defmodule ListConnectionsCommandTest do
@default_timeout 15000
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
close_all_connections(get_rabbit_hostname)

View File

@ -7,7 +7,7 @@ defmodule ListConsumersCommandTest do
@default_timeout :infinity
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -20,7 +20,7 @@ defmodule ListExchangesCommandTest do
end
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -26,7 +26,7 @@ defmodule ListParametersCommandTest do
@value "{\"uri\":\"amqp://\"}"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
add_vhost @vhost

View File

@ -24,7 +24,7 @@ defmodule ListPermissionsCommandTest do
@default_timeout :infinity
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
add_vhost @vhost

View File

@ -8,7 +8,7 @@ defmodule ListQueuesCommandTest do
@default_timeout 15000
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
reset_vm_memory_high_watermark()

View File

@ -19,7 +19,7 @@ defmodule ListUserPermissionsCommandTest do
import TestHelper
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -23,7 +23,7 @@ defmodule ListUsersCommandTest do
@guest "guest"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -23,7 +23,7 @@ defmodule ListVhostsCommandTest do
@root "/"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
add_vhost @vhost1

View File

@ -21,7 +21,7 @@ defmodule NodeHealthCheckCommandTest do
@command NodeHealthCheckCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
reset_vm_memory_high_watermark()

View File

@ -22,7 +22,7 @@ defmodule RabbitMQCtlTest do
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -19,7 +19,7 @@ defmodule ReportTest do
import TestHelper
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -21,7 +21,7 @@ defmodule ResetCommandTest do
@command ResetCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
start_rabbitmq_app

View File

@ -21,7 +21,7 @@ defmodule RotateLogsCommandTest do
@command RotateLogsCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -4,7 +4,7 @@ defmodule RpcStreamTest do
import TestHelper
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -21,7 +21,7 @@ defmodule SetDiskFreeLimitCommandTest do
@default_limit 1048576
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
set_disk_free_limit(@default_limit)

View File

@ -26,7 +26,7 @@ defmodule SetParameterCommandTest do
@value "{\"uri\":\"amqp://\"}"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
add_vhost @vhost

View File

@ -23,7 +23,7 @@ defmodule SetPermissionsCommandTest do
@root "/"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
add_vhost @vhost

View File

@ -22,7 +22,7 @@ defmodule SetUserTagsCommandTest do
@password "password"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
add_user @user, @password

View File

@ -21,7 +21,7 @@ defmodule SetVmMemoryHighWatermarkCommandTest do
import SetVmMemoryHighWatermarkCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
reset_vm_memory_high_watermark()

View File

@ -21,7 +21,7 @@ defmodule StartAppCommandTest do
@command StartAppCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
start_rabbitmq_app

View File

@ -21,7 +21,7 @@ defmodule StatusCommandTest do
@command StatusCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -21,7 +21,7 @@ defmodule StopAppCommandTest do
@command StopAppCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
start_rabbitmq_app

View File

@ -21,7 +21,7 @@ defmodule StopCommandTest do
@command StopCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->

View File

@ -22,7 +22,7 @@ defmodule TraceOffCommandTest do
@default_vhost "/"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
add_vhost(@test_vhost)

View File

@ -22,7 +22,7 @@ defmodule TraceOnCommandTest do
@default_vhost "/"
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
add_vhost(@test_vhost)

View File

@ -21,7 +21,7 @@ defmodule WaitCommandTest do
@command WaitCommand
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
RabbitMQCtl.start_distribution(%{})
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->