diff options
author | Simon MacMullen <simon@rabbitmq.com> | 2012-02-02 14:16:48 +0000 |
---|---|---|
committer | Simon MacMullen <simon@rabbitmq.com> | 2012-02-02 14:16:48 +0000 |
commit | e299e2a17188948c5878e787cfb3bd740650d505 (patch) | |
tree | eae0bb0dd93d108f35309d6d5539454ffd703c45 | |
parent | 6ede957195ca5c2b66ddcb79d00e30d69568c019 (diff) | |
download | rabbitmq-server-e299e2a17188948c5878e787cfb3bd740650d505.tar.gz |
Unify the more resilient hostname check, refactor out this code into a new module.
-rw-r--r-- | src/rabbit.erl | 36 | ||||
-rw-r--r-- | src/rabbit_control.erl | 2 | ||||
-rw-r--r-- | src/rabbit_nodes.erl | 77 | ||||
-rw-r--r-- | src/rabbit_prelaunch.erl | 17 |
4 files changed, 82 insertions, 50 deletions
diff --git a/src/rabbit.erl b/src/rabbit.erl index 293864ea..5ada38d2 100644 --- a/src/rabbit.erl +++ b/src/rabbit.erl @@ -22,7 +22,7 @@ status/0, is_running/0, is_running/1, environment/0, rotate_logs/1, force_event_refresh/0]). --export([start/2, stop/1, diagnostics/1]). +-export([start/2, stop/1]). -export([log_location/1]). %% for testing @@ -236,7 +236,6 @@ {'required',[any(),...]}}} | {'ok',pid()}). -spec(stop/1 :: (_) -> 'ok'). --spec(diagnostics/1 :: ([node()]) -> string()). -endif. @@ -510,7 +509,7 @@ boot_step_error({error, {timeout_waiting_for_tables, _}}, _Stacktrace) -> Ns -> {format("Timeout contacting cluster nodes: ~p.~n", [Ns]), Ns} end, - boot_error(Err ++ diagnostics(Nodes) ++ "~n~n", []); + boot_error(Err ++ rabbit_nodes:diagnostics(Nodes) ++ "~n~n", []); boot_step_error(Reason, Stacktrace) -> boot_error("Error description:~n ~p~n~n" @@ -524,37 +523,6 @@ boot_error(Format, Args) -> timer:sleep(1000), exit({?MODULE, failure_during_boot}). - -diagnostics(Nodes) -> - Hosts = lists:usort([element(2, rabbit_misc:nodeparts(Node)) || - Node <- Nodes]), - NodeDiags = [{"~nDIAGNOSTICS~n===========~n~n" - "nodes to contact: ~p~n~n" - "hosts, their running nodes and ports:", [Nodes]}] ++ - [diagnostics_host(Host) || Host <- Hosts] ++ - diagnostics0(), - lists:flatten([io_lib:format(F ++ "~n", A) || NodeDiag <- NodeDiags, - {F, A} <- [NodeDiag]]). - -diagnostics0() -> - [{"~ncurrent node details:~n- node name: ~w", [node()]}, - case init:get_argument(home) of - {ok, [[Home]]} -> {"- home dir: ~s", [Home]}; - Other -> {"- no home dir: ~p", [Other]} - end, - {"- cookie hash: ~s", [rabbit_misc:cookie_hash()]}]. - -diagnostics_host(Host) -> - case net_adm:names(Host) of - {error, EpmdReason} -> - {"- unable to connect to epmd on ~s: ~w", - [Host, EpmdReason]}; - {ok, NamePorts} -> - {"- ~s: ~p", - [Host, [{list_to_atom(Name), Port} || - {Name, Port} <- NamePorts]]} - end. - format(F, A) -> lists:flatten(io_lib:format(F, A)). %%--------------------------------------------------------------------------- diff --git a/src/rabbit_control.erl b/src/rabbit_control.erl index b31661b3..48d319c8 100644 --- a/src/rabbit_control.erl +++ b/src/rabbit_control.erl @@ -142,7 +142,7 @@ print_report0(Node, {Module, InfoFun, KeysFun}, VHostArg) -> print_error(Format, Args) -> fmt_stderr("Error: " ++ Format, Args). print_badrpc_diagnostics(Node) -> - fmt_stderr(rabbit:diagnostics([Node]), []). + fmt_stderr(rabbit_nodes:diagnostics([Node]), []). stop() -> ok. diff --git a/src/rabbit_nodes.erl b/src/rabbit_nodes.erl new file mode 100644 index 00000000..6cfa5fa9 --- /dev/null +++ b/src/rabbit_nodes.erl @@ -0,0 +1,77 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is VMware, Inc. +%% Copyright (c) 2007-2011 VMware, Inc. All rights reserved. +%% + +-module(rabbit_nodes). + +-export([names/1, diagnostics/1]). + +-define(EPMD_TIMEOUT, 30000). + +%%---------------------------------------------------------------------------- +%% Specs +%%---------------------------------------------------------------------------- + +-ifdef(use_specs). + +-spec(names/1 :: (string()) -> rabbit_types:ok_or_error2( + [{string(), integer()}], term())). +-spec(diagnostics/1 :: ([node()]) -> string()). + +-endif. + +%%---------------------------------------------------------------------------- + +names(Hostname) -> + Self = self(), + process_flag(trap_exit, true), + Pid = spawn_link(fun () -> Self ! {names, net_adm:names(Hostname)} end), + timer:exit_after(?EPMD_TIMEOUT, Pid, timeout), + Res = receive + {names, Names} -> Names; + {'EXIT', Pid, Reason} -> {error, Reason} + end, + process_flag(trap_exit, false), + Res. + +diagnostics(Nodes) -> + Hosts = lists:usort([element(2, rabbit_misc:nodeparts(Node)) || + Node <- Nodes]), + NodeDiags = [{"~nDIAGNOSTICS~n===========~n~n" + "nodes to contact: ~p~n~n" + "hosts, their running nodes and ports:", [Nodes]}] ++ + [diagnostics_host(Host) || Host <- Hosts] ++ + diagnostics0(), + lists:flatten([io_lib:format(F ++ "~n", A) || NodeDiag <- NodeDiags, + {F, A} <- [NodeDiag]]). + +diagnostics0() -> + [{"~ncurrent node details:~n- node name: ~w", [node()]}, + case init:get_argument(home) of + {ok, [[Home]]} -> {"- home dir: ~s", [Home]}; + Other -> {"- no home dir: ~p", [Other]} + end, + {"- cookie hash: ~s", [rabbit_misc:cookie_hash()]}]. + +diagnostics_host(Host) -> + case names(Host) of + {error, EpmdReason} -> + {"- unable to connect to epmd on ~s: ~w", + [Host, EpmdReason]}; + {ok, NamePorts} -> + {"- ~s: ~p", + [Host, [{list_to_atom(Name), Port} || + {Name, Port} <- NamePorts]]} + end. diff --git a/src/rabbit_prelaunch.erl b/src/rabbit_prelaunch.erl index 5fc67662..2dc1b156 100644 --- a/src/rabbit_prelaunch.erl +++ b/src/rabbit_prelaunch.erl @@ -22,7 +22,6 @@ -define(BaseApps, [rabbit]). -define(ERROR_CODE, 1). --define(EPMD_TIMEOUT, 30000). %%---------------------------------------------------------------------------- %% Specs @@ -246,13 +245,13 @@ duplicate_node_check([]) -> duplicate_node_check(NodeStr) -> Node = rabbit_misc:makenode(NodeStr), {NodeName, NodeHost} = rabbit_misc:nodeparts(Node), - case names(NodeHost) of + case rabbit_nodes:names(NodeHost) of {ok, NamePorts} -> case proplists:is_defined(NodeName, NamePorts) of true -> io:format("node with name ~p " "already running on ~p~n", [NodeName, NodeHost]), - io:format(rabbit:diagnostics([Node]) ++ "~n"), + io:format(rabbit_nodes:diagnostics([Node]) ++ "~n"), terminate(?ERROR_CODE); false -> ok end; @@ -278,15 +277,3 @@ terminate(Status) -> after infinity -> ok end end. - -names(Hostname) -> - Self = self(), - process_flag(trap_exit, true), - Pid = spawn_link(fun () -> Self ! {names, net_adm:names(Hostname)} end), - timer:exit_after(?EPMD_TIMEOUT, Pid, timeout), - Res = receive - {names, Names} -> Names; - {'EXIT', Pid, Reason} -> {error, Reason} - end, - process_flag(trap_exit, false), - Res. |