diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/rebar_dist_utils.erl | 89 | ||||
-rw-r--r-- | src/rebar_prv_common_test.erl | 6 | ||||
-rw-r--r-- | src/rebar_prv_shell.erl | 29 |
3 files changed, 95 insertions, 29 deletions
diff --git a/src/rebar_dist_utils.erl b/src/rebar_dist_utils.erl new file mode 100644 index 0000000..f462826 --- /dev/null +++ b/src/rebar_dist_utils.erl @@ -0,0 +1,89 @@ +%%% Common functions to boot/stop distributed setups for +%%% the rebar3 script. +-module(rebar_dist_utils). +-export([either/3, short/2, long/2, find_options/1]). +-include("rebar.hrl"). + +%%%%%%%%%%%%%%%%%% +%%% PUBLIC API %%% +%%%%%%%%%%%%%%%%%% +-spec either(Name::atom(), SName::atom(), Opts::[{setcookie,term()}]) -> atom(). +either(undefined, undefined, _) -> + 'nonode@nohost'; +either(Name, undefined, Opts) -> + long(Name, Opts), + node(); +either(undefined, SName, Opts) -> + short(SName, Opts), + node(); +either(_, _, _) -> + ?ABORT("Cannot have both short and long node names defined", []). + +short(Name, Opts) -> + start(Name, shortnames, Opts). + +long(Name, Opts) -> + start(Name, longnames, Opts). + +-spec find_options(rebar_state:state()) -> {Long, Short, Opts} when + Long :: atom(), + Short :: atom(), + Opts :: [{setcookie,term()}]. +find_options(State) -> + {Long, Short} = find_name_options(State), + case find_cookie_option(State) of + nocookie -> + {Long, Short, []}; + Cookie -> + {Long, Short, [{setcookie, Cookie}]} + end. + +%%%%%%%%%%%%%%% +%%% PRIVATE %%% +%%%%%%%%%%%%%%% +start(Name, Type, Opts) -> + check_epmd(net_kernel:start([Name, Type])), + setup_cookie(Opts). + +check_epmd({error,{{shutdown, {_,net_kernel,{'EXIT',nodistribution}}},_}}) -> + ?ERROR("Erlang Distribution failed, falling back to nonode@nohost. " + "Verify that epmd is running and try again.",[]); +check_epmd(_) -> + ok. + +setup_cookie(Opts) -> + case {node(), proplists:get_value(setcookie, Opts, nocookie)} of + {'nonode@nohost', _} -> nocookie; + {_, nocookie} -> nocookie; + {Node, Name} -> erlang:set_cookie(Node, Name) + end. + +find_name_options(State) -> + {Opts, _} = rebar_state:command_parsed_args(State), + %% First try the CLI + case {proplists:get_value(name, Opts), proplists:get_value(sname, Opts)} of + {undefined, undefined} -> + %% Else try the config file + DistOpts = rebar_state:get(State, dist_node, []), + %% Pick the first one seen to support profile merges + find_first_name(DistOpts); + Res -> + Res + end. + +find_first_name([]) -> {undefined, undefined}; +find_first_name([{sname,Val}|_]) -> {undefined, Val}; +find_first_name([{name,Val}|_]) -> {Val, undefined}; +find_first_name([_|Opts]) -> find_first_name(Opts). + +find_cookie_option(State) -> + {Opts, _} = rebar_state:command_parsed_args(State), + %% First try the CLI + case proplists:get_value(setcookie, Opts) of + undefined -> + %% Else try the config file + DistOpts = rebar_state:get(State, dist_node, []), + proplists:get_value(setcookie, DistOpts, nocookie); + Res -> + Res + end. diff --git a/src/rebar_prv_common_test.erl b/src/rebar_prv_common_test.erl index 6b94f79..14dc4cc 100644 --- a/src/rebar_prv_common_test.erl +++ b/src/rebar_prv_common_test.erl @@ -189,8 +189,10 @@ ensure_opts([{suite, Suites}|Rest], Acc) -> ensure_opts(Rest, [NewSuites|Acc]); ensure_opts([{K, V}|Rest], Acc) -> ensure_opts(Rest, [{K, V}|Acc]); -ensure_opts([V|_Rest], _Acc) -> - ?PRV_ERROR({badconfig, {"Member `~p' of option `~p' must be a 2-tuple", {V, ct_opts}}}). +%% pass through other options, in case of things like config terms +%% in `ct_opts` +ensure_opts([V|Rest], Acc) -> + ensure_opts(Rest, [V|Acc]). add_hooks(Opts, State) -> case {readable(State), lists:keyfind(ct_hooks, 1, Opts)} of diff --git a/src/rebar_prv_shell.erl b/src/rebar_prv_shell.erl index d67f940..365ed66 100644 --- a/src/rebar_prv_shell.erl +++ b/src/rebar_prv_shell.erl @@ -269,33 +269,8 @@ simulate_proc_lib() -> put('$initial_call', {rebar_agent, init, 1}). setup_name(State) -> - {Opts, _} = rebar_state:command_parsed_args(State), - case {proplists:get_value(name, Opts), proplists:get_value(sname, Opts)} of - {undefined, undefined} -> - ok; - {Name, undefined} -> - check_epmd(net_kernel:start([Name, longnames])), - setup_cookie(Opts); - {undefined, SName} -> - check_epmd(net_kernel:start([SName, shortnames])), - setup_cookie(Opts); - {_, _} -> - ?ABORT("Cannot have both short and long node names defined", []) - end. - -check_epmd({error,{{shutdown, {_,net_kernel,{'EXIT',nodistribution}}},_}}) -> - ?ERROR("Erlang Distribution failed, falling back to nonode@nohost. " - "Verify that epmd is running and try again.",[]); -check_epmd(_) -> - ok. - -setup_cookie(Opts) -> - case {node(), proplists:get_value(setcookie, Opts, nocookie)} of - {'nonode@nohost', _} -> nocookie; - {_, nocookie} -> nocookie; - {Node, Name} -> erlang:set_cookie(Node, Name) - end. - + {Long, Short, Opts} = rebar_dist_utils:find_options(State), + rebar_dist_utils:either(Long, Short, Opts). find_apps_to_boot(State) -> %% Try the shell_apps option |