From cf0829b934f58da2faeea8372cfa6b797fc34079 Mon Sep 17 00:00:00 2001 From: Scott Lystig Fritchie Date: Fri, 4 Dec 2015 16:37:05 +0900 Subject: [PATCH] Add rc.d style config dir for FLU server startup --- rel/files/app.config | 6 ++++ rel/reltool.config | 2 ++ src/machi_flu_psup.erl | 2 ++ src/machi_flu_sup.erl | 60 +++++++++++++++++++++++++++++++++++- test/machi_flu_psup_test.erl | 13 ++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) diff --git a/rel/files/app.config b/rel/files/app.config index c65c526..ac3a6a8 100644 --- a/rel/files/app.config +++ b/rel/files/app.config @@ -3,6 +3,12 @@ %% Data directory for all FLUs. {flu_data_dir, "{{platform_data_dir}}"}, + %% FLU config directory + {flu_config_dir, "{{platform_etc_dir}}/flu-config"}, + + %% Chain config directory + {chain_config_dir, "{{platform_etc_dir}}/chain-config"}, + %% FLUs to start at app start. {initial_flus, [ %% Remember, this is a list, so separate all tuples diff --git a/rel/reltool.config b/rel/reltool.config index 1385ca1..ca68025 100644 --- a/rel/reltool.config +++ b/rel/reltool.config @@ -93,6 +93,8 @@ {template, "files/vm.args", "etc/vm.args"}, {template, "files/app.config", "etc/app.config"}, + {mkdir, "etc/chain-config"}, + {mkdir, "etc/flu-config"}, {mkdir, "lib/basho-patches"} %% {copy, "../apps/machi/ebin/etop_txt.beam", "lib/basho-patches"} ]}. diff --git a/src/machi_flu_psup.erl b/src/machi_flu_psup.erl index 9e568cd..d83d559 100644 --- a/src/machi_flu_psup.erl +++ b/src/machi_flu_psup.erl @@ -83,6 +83,8 @@ %% Supervisor callbacks -export([init/1]). +make_package_spec(#p_srvr{name=FluName, port=TcpPort, props=Props}) when is_list(Props) -> + make_package_spec({FluName, TcpPort, Props}); make_package_spec({FluName, TcpPort, Props}) when is_list(Props) -> FluDataDir = get_env(flu_data_dir, undefined_is_invalid), MyDataDir = filename:join(FluDataDir, atom_to_list(FluName)), diff --git a/src/machi_flu_sup.erl b/src/machi_flu_sup.erl index 74995bb..fe72499 100644 --- a/src/machi_flu_sup.erl +++ b/src/machi_flu_sup.erl @@ -29,8 +29,11 @@ -behaviour(supervisor). -include("machi.hrl"). +-include("machi_projection.hrl"). -include("machi_verbose.hrl"). +-ifdef(TEST). +-compile(export_all). -ifdef(PULSE). -compile({parse_transform, pulse_instrument}). -include_lib("pulse_otp/include/pulse_otp.hrl"). @@ -38,6 +41,7 @@ -else. -define(SHUTDOWN, 5000). -endif. +-endif. %TEST %% API -export([start_link/0]). @@ -69,5 +73,59 @@ get_initial_flus() -> []. -else. % PULSE get_initial_flus() -> - application:get_env(machi, initial_flus, []). + DoesNotExist = "/tmp/does/not/exist", + ConfigDir = case application:get_env(machi, flu_config_dir, DoesNotExist) of + DoesNotExist -> + DoesNotExist; + Dir -> + ok = filelib:ensure_dir(Dir ++ "/unused"), + Dir + end, + sanitize_p_srvr_records(load_rc_d_files_from_dir(ConfigDir)). -endif. % PULSE + +load_rc_d_files_from_dir(Dir) -> + Files = filelib:wildcard(Dir ++ "/*"), + lists:append([case file:consult(File) of + {ok, X} -> X; + _ -> [] + end || File <- Files]). + +sanitize_p_srvr_records(Ps) -> + {Sane, _} = lists:foldl(fun sanitize_p_srvr_rec/2, {[], dict:new()}, Ps), + Sane. + +sanitize_p_srvr_rec(Whole, {Acc, D}) -> + try + #p_srvr{name=Name, + proto_mod=PMod, + address=Address, + port=Port, + props=Props} = Whole, + true = is_atom(Name), + NameK = {name, Name}, + error = dict:find(NameK, D), + true = is_atom(PMod), + case code:is_loaded(PMod) of + {file, _} -> ok; + _ -> {module, _} = code:load_file(PMod), ok + end, + if is_list(Address) -> ok; + is_tuple(Address) -> ok % Erlang-style IPv4 or IPv6 + end, + true = is_integer(Port) andalso Port >= 1024 andalso Port =< 65534, + PortK = {port, Port}, + error = dict:find(PortK, D), + true = is_list(Props), + + %% All is sane enough. + D2 = dict:store(NameK, Name, + dict:store(PortK, Port, D)), + {[Whole|Acc], D2} + catch _:_ -> + _ = lager:log(error, self(), + "~s: Bad (or duplicate name/port) p_srvr record, " + "skipping: ~P\n", + [?MODULE, Whole, 15]), + {Acc, D} + end. diff --git a/test/machi_flu_psup_test.erl b/test/machi_flu_psup_test.erl index 1c7b015..fd93b42 100644 --- a/test/machi_flu_psup_test.erl +++ b/test/machi_flu_psup_test.erl @@ -173,6 +173,19 @@ partial_stop_restart2() -> ok end. +p_srvr_rec_test() -> + P = #p_srvr{name=a, address="localhost", port=1024, props=[yo]}, + [P] = machi_flu_sup:sanitize_p_srvr_records([P]), + [P] = machi_flu_sup:sanitize_p_srvr_records([P,P]), + [] = machi_flu_sup:sanitize_p_srvr_records([nope]), + [] = machi_flu_sup:sanitize_p_srvr_records([#p_srvr{proto_mod=does_not_exist}]), + [] = machi_flu_sup:sanitize_p_srvr_records([#p_srvr{proto_mod="lists"}]), + [] = machi_flu_sup:sanitize_p_srvr_records([#p_srvr{address=7}]), + [] = machi_flu_sup:sanitize_p_srvr_records([#p_srvr{port=5}]), + [] = machi_flu_sup:sanitize_p_srvr_records([#p_srvr{port=foo}]), + [] = machi_flu_sup:sanitize_p_srvr_records([#p_srvr{props=foo}]), + ok. + -endif. % !PULSE -endif. % TEST