Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

89 linhas
2.7 KiB

  1. %%% Common functions to boot/stop distributed setups for
  2. %%% the rebar3 script.
  3. -module(rebar_dist_utils).
  4. -export([either/3, short/2, long/2, find_options/1]).
  5. -include("rebar.hrl").
  6. %%%%%%%%%%%%%%%%%%
  7. %%% PUBLIC API %%%
  8. %%%%%%%%%%%%%%%%%%
  9. -spec either(Name::atom(), SName::atom(), Opts::[{setcookie,term()}]) -> atom().
  10. either(undefined, undefined, _) ->
  11. 'nonode@nohost';
  12. either(Name, undefined, Opts) ->
  13. long(Name, Opts),
  14. node();
  15. either(undefined, SName, Opts) ->
  16. short(SName, Opts),
  17. node();
  18. either(_, _, _) ->
  19. ?ABORT("Cannot have both short and long node names defined", []).
  20. short(Name, Opts) ->
  21. start(Name, shortnames, Opts).
  22. long(Name, Opts) ->
  23. start(Name, longnames, Opts).
  24. -spec find_options(rebar_state:state()) -> {Long, Short, Opts} when
  25. Long :: atom(),
  26. Short :: atom(),
  27. Opts :: [{setcookie,term()}].
  28. find_options(State) ->
  29. {Long, Short} = find_name_options(State),
  30. case find_cookie_option(State) of
  31. nocookie ->
  32. {Long, Short, []};
  33. Cookie ->
  34. {Long, Short, [{setcookie, Cookie}]}
  35. end.
  36. %%%%%%%%%%%%%%%
  37. %%% PRIVATE %%%
  38. %%%%%%%%%%%%%%%
  39. start(Name, Type, Opts) ->
  40. check_epmd(net_kernel:start([Name, Type])),
  41. setup_cookie(Opts).
  42. check_epmd({error,{{shutdown, {_,net_kernel,{'EXIT',nodistribution}}},_}}) ->
  43. ?ERROR("Erlang Distribution failed, falling back to nonode@nohost. "
  44. "Verify that epmd is running and try again.",[]);
  45. check_epmd(_) ->
  46. ok.
  47. setup_cookie(Opts) ->
  48. case {node(), proplists:get_value(setcookie, Opts, nocookie)} of
  49. {'nonode@nohost', _} -> nocookie;
  50. {_, nocookie} -> nocookie;
  51. {Node, Name} -> erlang:set_cookie(Node, Name)
  52. end.
  53. find_name_options(State) ->
  54. {Opts, _} = rebar_state:command_parsed_args(State),
  55. %% First try the CLI
  56. case {proplists:get_value(name, Opts), proplists:get_value(sname, Opts)} of
  57. {undefined, undefined} ->
  58. %% Else try the config file
  59. DistOpts = rebar_state:get(State, dist_node, []),
  60. %% Pick the first one seen to support profile merges
  61. find_first_name(DistOpts);
  62. Res ->
  63. Res
  64. end.
  65. find_first_name([]) -> {undefined, undefined};
  66. find_first_name([{sname,Val}|_]) -> {undefined, Val};
  67. find_first_name([{name,Val}|_]) -> {Val, undefined};
  68. find_first_name([_|Opts]) -> find_first_name(Opts).
  69. find_cookie_option(State) ->
  70. {Opts, _} = rebar_state:command_parsed_args(State),
  71. %% First try the CLI
  72. case proplists:get_value(setcookie, Opts) of
  73. undefined ->
  74. %% Else try the config file
  75. DistOpts = rebar_state:get(State, dist_node, []),
  76. proplists:get_value(setcookie, DistOpts, nocookie);
  77. Res ->
  78. Res
  79. end.