diff options
author | Luis Rascao <[email protected]> | 2016-11-05 22:22:19 +0000 |
---|---|---|
committer | Luis Rascao <[email protected]> | 2016-11-12 23:59:07 +0000 |
commit | 3905d39d180efe125b9ef5351d1c3b27a3f825b0 (patch) | |
tree | ccfbd1bef3f2e3e2fc0b21bc2652b5a9132d1ff9 /test/rlx_test_utils.erl | |
parent | ad6193884f09d29c7402de45214817dc195f5c6d (diff) | |
download | relx-3905d39d180efe125b9ef5351d1c3b27a3f825b0.tar.gz relx-3905d39d180efe125b9ef5351d1c3b27a3f825b0.tar.bz2 relx-3905d39d180efe125b9ef5351d1c3b27a3f825b0.zip |
Add support for new relx directive that provides start/stop shell script hooks
New 'extended_start_script_hooks' directive that allows the
developer to define six different hook shell scripts to be invoked
at pre/post start/stop/install upgrade phases.
Besides these custom defined scripts, other types of builtin
scripts are also available, these offer pre-packaged functionality
that can be used directly, they are:
pid - writes the beam pid to a configurable file location
(/var/run/<rel_name>.pid by default).
wait_for_vm_start - waits for the vm to start (ie. when it responds to pings)
wait_for_process - waits for a configurable name
to appear in the erlang process registry
The hook scripts are invoked with the 'source' command, therefore
they have access to all the variables in the start script.
Diffstat (limited to 'test/rlx_test_utils.erl')
-rw-r--r-- | test/rlx_test_utils.erl | 100 |
1 files changed, 99 insertions, 1 deletions
diff --git a/test/rlx_test_utils.erl b/test/rlx_test_utils.erl index 3ddc134..f120c75 100644 --- a/test/rlx_test_utils.erl +++ b/test/rlx_test_utils.erl @@ -12,6 +12,13 @@ create_app(Dir, Name, Vsn, Deps, LibDeps) -> rlx_app_info:new(erlang:list_to_atom(Name), Vsn, AppDir, Deps, []). +create_full_app(Dir, Name, Vsn, Deps, LibDeps) -> + AppDir = filename:join([Dir, Name ++ "-" ++ Vsn]), + write_full_app_files(AppDir, Name, Vsn, Deps, LibDeps), + compile_src_files(AppDir), + rlx_app_info:new(erlang:list_to_atom(Name), Vsn, AppDir, + Deps, []). + create_empty_app(Dir, Name, Vsn, Deps, LibDeps) -> AppDir = filename:join([Dir, Name ++ "-" ++ Vsn]), write_app_file(AppDir, Name, Vsn, Deps, LibDeps), @@ -50,6 +57,97 @@ get_app_metadata(Name, Vsn, Deps, LibDeps) -> {registered, []}, {applications, Deps}]}. +write_full_app_files(Dir, Name, Vsn, Deps, LibDeps) -> + %% write out the .app file + AppFilename = filename:join([Dir, "ebin", Name ++ ".app"]), + ok = filelib:ensure_dir(AppFilename), + ok = ec_file:write_term(AppFilename, + get_full_app_metadata(Name, Vsn, Deps, LibDeps)), + %% write out the _app.erl file + ApplicationFilename = filename:join([Dir, "src", Name ++ "_app.erl"]), + ok = filelib:ensure_dir(ApplicationFilename), + ok = file:write_file(ApplicationFilename, full_application_contents(Name)), + %% write out the supervisor + SupervisorFilename = filename:join([Dir, "src", Name ++ "_sup.erl"]), + ok = filelib:ensure_dir(SupervisorFilename), + ok = file:write_file(SupervisorFilename, supervisor_contents(Name)), + %% and finally the gen_server + GenServerFilename = filename:join([Dir, "src", Name ++ "_srv.erl"]), + ok = filelib:ensure_dir(GenServerFilename), + ok = file:write_file(GenServerFilename, gen_server_contents(Name)), + ok. + +compile_src_files(Dir) -> + %% compile all *.erl files in src to ebin + SrcDir = filename:join([Dir, "src"]), + OutputDir = filename:join([Dir, "ebin"]), + lists:foreach(fun(SrcFile) -> + {ok, _} = compile:file(SrcFile, [{outdir, OutputDir}, + return_errors]) + end, ec_file:find(SrcDir, "\\.erl")), + ok. + +get_full_app_metadata(Name, Vsn, Deps, LibDeps) -> + {application, erlang:list_to_atom(Name), + [{description, ""}, + {vsn, Vsn}, + {modules, [goal_app_app,goal_app_sup,goal_app_srv]}, + {mod, {erlang:list_to_atom(Name ++ "_app"), + []}}, + {included_applications, LibDeps}, + {registered, []}, + {applications, Deps}]}. + +full_application_contents(Name) -> + "-module("++Name++"_app).\n" + "-behaviour(application).\n" + "-export([start/2, stop/1]).\n" + "start(_StartType, _StartArgs) ->\n" + " "++Name++"_sup:start_link().\n" + "stop(_State) ->\n" + " ok.\n". + +supervisor_contents(Name) -> + "-module("++Name++"_sup).\n" + "-behaviour(supervisor).\n" + "-export([start_link/0]).\n" + "-export([init/1]).\n" + "-define(SERVER, ?MODULE).\n" + "start_link() ->\n" + " supervisor:start_link({local, ?SERVER}, ?MODULE, []).\n" + "init([]) ->\n" + " {ok, { {one_for_all, 0, 1},\n" + " [{"++Name++"_srv, {"++Name++"_srv, start_link, []},\n" + " transient, 5000, worker, ["++Name++"_srv]}\n" + " ]\n" + " }}.\n". + +gen_server_contents(Name) -> + "-module("++Name++"_srv).\n" + "-behaviour(gen_server).\n" + "-record(state, {}).\n" + "-export([start_link/0]).\n" + "-export([init/1,handle_call/3,handle_cast/2,\n" + " handle_info/2,terminate/2,code_change/3]).\n" + "start_link() ->\n" + " gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).\n" + "init([]) ->\n" + " erlang:send_after(4000, self(), register_signal)," + " {ok, #state{}}.\n" + "handle_call(_Event, _From, State) ->\n" + " {reply, ok, State}.\n" + "handle_cast(_Event, State) ->\n" + " {noreply, State}.\n" + "handle_info(register_signal, State) ->\n" + " erlang:register(goal_app_srv_signal, spawn(fun() -> timer:sleep(200000) end)),\n" + " {noreply, State};\n" + "handle_info(_Info, State) ->\n" + " {noreply, State}.\n" + "terminate(_Reason, _State) ->\n" + " ok.\n" + "code_change(_OldVsn, State, _Extra) ->\n" + " {ok, State}.\n". + create_random_name(Name) -> Name ++ erlang:integer_to_list(random_uniform(1000000)). @@ -113,4 +211,4 @@ list_to_term(String) -> unescape_string(String) -> re:replace(String, "\"", "", - [global, {return, list}]).
\ No newline at end of file + [global, {return, list}]). |