summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Klishin <mklishin@pivotal.io>2017-07-04 21:19:57 +0300
committerMichael Klishin <mklishin@pivotal.io>2017-07-04 21:19:57 +0300
commitc07b7cacc326cbec273b04d0d3eb1a351223d73a (patch)
tree6c44393edc89315b73cbd7dcd5f97377b0421de8
parent5d45fc999335451eeee3f9dd4c6f87b9fbfe4f3f (diff)
downloadrabbitmq-server-git-c07b7cacc326cbec273b04d0d3eb1a351223d73a.tar.gz
rabbit_nodes: move a few more functions to rabbit_nodes_common
they are required by or can be useful to CLI tools.
-rw-r--r--src/rabbit_nodes.erl150
1 files changed, 7 insertions, 143 deletions
diff --git a/src/rabbit_nodes.erl b/src/rabbit_nodes.erl
index 850ec48bdc..6b5ab499cc 100644
--- a/src/rabbit_nodes.erl
+++ b/src/rabbit_nodes.erl
@@ -24,10 +24,6 @@
-include_lib("kernel/include/inet.hrl").
--define(EPMD_TIMEOUT, 30000).
--define(TCP_DIAGNOSTIC_TIMEOUT, 5000).
--define(ERROR_LOGGER_HANDLER, rabbit_error_logger_handler).
-
%%----------------------------------------------------------------------------
%% Specs
%%----------------------------------------------------------------------------
@@ -45,135 +41,10 @@
%%----------------------------------------------------------------------------
names(Hostname) ->
- Self = self(),
- Ref = make_ref(),
- {Pid, MRef} = spawn_monitor(
- fun () -> Self ! {Ref, net_adm:names(Hostname)} end),
- timer:exit_after(?EPMD_TIMEOUT, Pid, timeout),
- receive
- {Ref, Names} -> erlang:demonitor(MRef, [flush]),
- Names;
- {'DOWN', MRef, process, Pid, Reason} -> {error, Reason}
- end.
+ rabbit_nodes_common:names(Hostname).
diagnostics(Nodes) ->
- verbose_erlang_distribution(true),
- NodeDiags = [{"~nDIAGNOSTICS~n===========~n~n"
- "attempted to contact: ~p~n", [Nodes]}] ++
- [diagnostics_node(Node) || Node <- Nodes] ++
- current_node_details(),
- verbose_erlang_distribution(false),
- rabbit_misc:format_many(lists:flatten(NodeDiags)).
-
-verbose_erlang_distribution(true) ->
- net_kernel:verbose(1),
- error_logger:add_report_handler(?ERROR_LOGGER_HANDLER);
-verbose_erlang_distribution(false) ->
- net_kernel:verbose(0),
- error_logger:delete_report_handler(?ERROR_LOGGER_HANDLER).
-
-current_node_details() ->
- [{"~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", [cookie_hash()]}].
-
-diagnostics_node(Node) ->
- {Name, Host} = parts(Node),
- [{"~s:", [Node]} |
- case names(Host) of
- {error, Reason} ->
- [{" * unable to connect to epmd (port ~s) on ~s: ~s~n",
- [epmd_port(), Host, rabbit_misc:format_inet_error(Reason)]}];
- {ok, NamePorts} ->
- [{" * connected to epmd (port ~s) on ~s",
- [epmd_port(), Host]}] ++
- case net_adm:ping(Node) of
- pong -> dist_working_diagnostics(Node);
- pang -> dist_broken_diagnostics(Name, Host, NamePorts)
- end
- end].
-
-epmd_port() ->
- case init:get_argument(epmd_port) of
- {ok, [[Port | _] | _]} when is_list(Port) -> Port;
- error -> "4369"
- end.
-
-dist_working_diagnostics(Node) ->
- case is_process_running(Node, rabbit) of
- true -> [{" * node ~s up, 'rabbit' application running", [Node]}];
- false -> [{" * node ~s up, 'rabbit' application not running~n"
- " * running applications on ~s: ~p~n"
- " * suggestion: start_app on ~s",
- [Node, Node, remote_apps(Node), Node]}]
- end.
-
-remote_apps(Node) ->
- %% We want a timeout here because really, we don't trust the node,
- %% the last thing we want to do is hang.
- case rpc:call(Node, application, which_applications, [5000]) of
- {badrpc, _} = E -> E;
- Apps -> [App || {App, _, _} <- Apps]
- end.
-
-dist_broken_diagnostics(Name, Host, NamePorts) ->
- case [{N, P} || {N, P} <- NamePorts, N =:= Name] of
- [] ->
- {SelfName, SelfHost} = parts(node()),
- Others = [list_to_atom(N) || {N, _} <- NamePorts,
- N =/= case SelfHost of
- Host -> SelfName;
- _ -> never_matches
- end],
- OthersDiag = case Others of
- [] -> [{" no other nodes on ~s",
- [Host]}];
- _ -> [{" other nodes on ~s: ~p",
- [Host, Others]}]
- end,
- [{" * epmd reports: node '~s' not running at all", [Name]},
- OthersDiag, {" * suggestion: start the node", []}];
- [{Name, Port}] ->
- [{" * epmd reports node '~s' running on port ~b", [Name, Port]} |
- case diagnose_connect(Host, Port) of
- ok ->
- connection_succeeded_diagnostics();
- {error, Reason} ->
- [{" * can't establish TCP connection, reason: ~s~n"
- " * suggestion: blocked by firewall?",
- [rabbit_misc:format_inet_error(Reason)]}]
- end]
- end.
-
-connection_succeeded_diagnostics() ->
- case gen_event:call(error_logger, ?ERROR_LOGGER_HANDLER, get_connection_report) of
- [] ->
- [{" * TCP connection succeeded but Erlang distribution "
- "failed~n"
- " * suggestion: hostname mismatch?~n"
- " * suggestion: is the cookie set correctly?~n"
- " * suggestion: is the Erlang distribution using TLS?", []}];
- Report ->
- [{" * TCP connection succeeded but Erlang distribution "
- "failed~n", []}]
- ++ Report
- end.
-
-diagnose_connect(Host, Port) ->
- case inet:gethostbyname(Host) of
- {ok, #hostent{h_addrtype = Family}} ->
- case gen_tcp:connect(Host, Port, [Family],
- ?TCP_DIAGNOSTIC_TIMEOUT) of
- {ok, Socket} -> gen_tcp:close(Socket),
- ok;
- {error, _} = E -> E
- end;
- {error, _} = E ->
- E
- end.
+ rabbit_nodes_common:diagnostics(Nodes).
make(NodeStr) ->
rabbit_nodes_common:make(NodeStr).
@@ -182,30 +53,23 @@ parts(NodeStr) ->
rabbit_nodes_common:parts(NodeStr).
cookie_hash() ->
- base64:encode_to_string(erlang:md5(atom_to_list(erlang:get_cookie()))).
+ rabbit_nodes_common:cookie_hash().
is_running(Node, Application) ->
- case rpc:call(Node, rabbit_misc, which_applications, []) of
- {badrpc, _} -> false;
- Apps -> proplists:is_defined(Application, Apps)
- end.
+ rabbit_nodes_common:is_running(Node, Application).
is_process_running(Node, Process) ->
- case rpc:call(Node, erlang, whereis, [Process]) of
- {badrpc, _} -> false;
- undefined -> false;
- P when is_pid(P) -> true
- end.
+ rabbit_nodes_common:is_process_running(Node, Process).
cluster_name() ->
rabbit_runtime_parameters:value_global(
cluster_name, cluster_name_default()).
cluster_name_default() ->
- {ID, _} = rabbit_nodes:parts(node()),
+ {ID, _} = parts(node()),
{ok, Host} = inet:gethostname(),
{ok, #hostent{h_name = FQDN}} = inet:gethostbyname(Host),
- list_to_binary(atom_to_list(rabbit_nodes:make({ID, FQDN}))).
+ list_to_binary(atom_to_list(make({ID, FQDN}))).
set_cluster_name(Name, Username) ->
%% Cluster name should be binary