Created
April 18, 2022 17:21
-
-
Save maxlapshin/3dc66cd73c7539c5e795f51f5573ce3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env escript | |
%% | |
%%! -env ERL_LIBS _build/default/lib | |
main(Args) -> | |
inet_db:set_lookup([file, dns]), | |
inet_db:add_host({127,0,0,1}, ["server.l"]), | |
ensure_epmd_is_working(), | |
Names = case net_adm:names("server.l") of | |
{error, _} -> []; | |
{ok, NameList} -> NameList | |
end, | |
GoodName = first_good_name(Names), | |
{ok, _} = net_kernel:start([GoodName, longnames]), | |
{ok, CookieBin} = file:read_file("/etc/myservice/.erlang.cookie"), | |
erlang:set_cookie(erlang:node(), binary_to_atom(CookieBin,latin1)), | |
Node = '[email protected]', | |
M = .., | |
F = .., | |
A = .., | |
ok = rpc:call(Node, M, F, A), | |
ok. | |
ensure_epmd_is_working() -> | |
ensure_epmd_is_working(20). | |
ensure_epmd_is_working(0) -> | |
ok; | |
ensure_epmd_is_working(N) -> | |
case net_adm:names("server.l") of | |
{error, _} -> timer:sleep(100), ensure_epmd_is_working(N-1); | |
{ok, _} -> ok | |
end. | |
ensure_streamer_working(#{} = Opts) -> | |
case is_running(Opts) of | |
false -> | |
io:format("Server is down\n"), | |
erlang:halt(2); | |
_ -> | |
ok | |
end. | |
first_good_name(Names) -> | |
first_good_name(Names, 0). | |
first_good_name(_Names, N) when N > 10 -> | |
error_logger:error_msg("Too many launched control nodes"), | |
timer:sleep(1000), | |
erlang:halt(5); | |
first_good_name(Names, N) -> | |
CheckName = "control" ++ integer_to_list(N), | |
case lists:keymember(CheckName, 1, Names) of | |
true -> first_good_name(Names, N+1); | |
false -> list_to_atom(CheckName++"@server.l") | |
end. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment