diff --git a/src/setup.app.src b/src/setup.app.src index 8a81e86..e9598ca 100644 --- a/src/setup.app.src +++ b/src/setup.app.src @@ -23,6 +23,7 @@ kernel, stdlib ]}, - {mod, { setup, []}}, + {mod, { setup_app, []}}, + {start_phases, [{run_setup, []}]}, {env, []} ]}. diff --git a/src/setup.erl b/src/setup.erl index 431edae..a42ef93 100644 --- a/src/setup.erl +++ b/src/setup.erl @@ -132,10 +132,6 @@ %% disk storage. %% @end -module(setup). --behaviour(application). - --export([start/2, - stop/1]). -export([home/0, log_dir/0, @@ -159,7 +155,7 @@ -export([ok/1]). -compile(export_all). --export([run_setup/2]). +-export([run_setup/0]). -include_lib("kernel/include/file.hrl"). @@ -167,20 +163,6 @@ -include_lib("eunit/include/eunit.hrl"). -endif. -%% @spec start(Type, Args) -> {ok, pid()} -%% @doc Application start function. -%% @end -%% -start(_, Args) -> - proc_lib:start_link(?MODULE, run_setup, [self(), Args]). - -%% @spec stop(State) -> ok -%% @doc Application stop function -%% end -%% -stop(_) -> - ok. - %% @spec home() -> Directory %% @doc Returns the configured `home' directory, or a best guess (`$CWD') %% @end @@ -860,9 +842,9 @@ intersection(A, B) -> %% Afterwards, setup will either finish and leave the system running, or %% stop, terminating all nodes automatically. %% -run_setup(Parent, Args) -> +run_setup() -> error_logger:info_msg("Setup running ...~n", []), - try run_setup_(Parent, Args) + try run_setup_() catch error:Error -> error_logger:error_msg("Caught exception:~n" @@ -870,21 +852,21 @@ run_setup(Parent, Args) -> "~p~n", [Error, erlang:get_stacktrace()]) end. -run_setup_(Parent, _Args) -> +run_setup_() -> Res = maybe_verify_directories(), error_logger:info_msg("Directories verified. Res = ~p~n", [Res]), - proc_lib:init_ack(Parent, {ok, self()}), Mode = mode(), Hooks = find_hooks(Mode), run_selected_hooks(Hooks), - error_logger:info_msg("Setup finished processing hooks ...~n", []), + error_logger:info_msg( + "Setup finished processing hooks (Mode=~p)...~n", [Mode]), case app_get_env(setup, stop_when_done) of {ok, true} when Mode =/= normal -> error_logger:info_msg("Setup stopping...~n", []), timer:sleep(timer:seconds(5)), rpc:eval_everywhere(init,stop,[0]); _ -> - timer:sleep(infinity) + ok end. %% @spec find_hooks() -> [{PhaseNo, [{M,F,A}]}] diff --git a/src/setup_app.erl b/src/setup_app.erl new file mode 100644 index 0000000..94cab0d --- /dev/null +++ b/src/setup_app.erl @@ -0,0 +1,32 @@ +%% -*- mode: erlang; indent-tabs-mode: nil; -*- +%%============================================================================= +%% Copyright 2014-2016 Ulf Wiger +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%============================================================================= +-module(setup_app). +-behaviour(application). + +-export([start/2, + start_phase/3, + stop/1]). + +start(_Type, _Args) -> + setup_sup:start_link(). + +start_phase(run_setup, _Type, []) -> + _ = setup_srv:run_setup(), + ok. + +stop(_) -> + ok. diff --git a/src/setup_gen.erl b/src/setup_gen.erl index 7a74107..6046c9b 100644 --- a/src/setup_gen.erl +++ b/src/setup_gen.erl @@ -126,6 +126,10 @@ help() -> %% which, if a `{nodes, Ns}' option is given, also configures Erlang %% to wait for all given nodes, and then start the `setup' application %% on the first node. +%% * `{start_setup, true|false}' - Tells whether setup should be started +%% automatically. The default is `true' (as it should be). The best way +%% to include setup, but not start it, would be to add `{setup, load}' to +%% the `apps' list. %% * `{verbose, true|false}' - (Default: `false') Turns on verbose printouts. %% %% == Application entries == @@ -281,6 +285,7 @@ options(["-conf" , F|T]) -> [{conf, F}|options(T)]; options(["-install"]) -> [{install, true}]; options(["-install" | ["-" ++ _|_] = T]) -> [{install, true}|options(T)]; options(["-install" , D|T]) -> [{install, mk_bool(D)}|options(T)]; +options(["-start_setup" , D|T]) -> [{start_setup, mk_bool(D)}|options(T)]; options(["-sys" , D|T]) -> [{sys, D}|options(T)]; options(["-vsn" , D|T]) -> [{vsn, D}|options(T)]; options(["-pa" , D|T]) -> [{pa, D}|options(T)]; @@ -584,7 +589,11 @@ apps(Options, Env) -> end, sort_apps(Options, Apps1)), ?if_verbose(io:fwrite("AppVsns = ~p~n", [AppVsns])), %% setup_is_load_only(replace_versions(AppVsns, Apps1)). - setup_is_load_only(AppVsns). + case proplists:get_value(start_setup, Options, true) of + true -> AppVsns; + false -> + setup_is_load_only(AppVsns) + end. add_remove_apps(Options, _Env) -> lists:foldl( diff --git a/src/setup_srv.erl b/src/setup_srv.erl new file mode 100644 index 0000000..4d7354a --- /dev/null +++ b/src/setup_srv.erl @@ -0,0 +1,49 @@ +%% -*- mode: erlang; indent-tabs-mode: nil; -*- +%%============================================================================= +%% Copyright 2014-2016 Ulf Wiger +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%============================================================================= +-module(setup_srv). +-behaviour(gen_server). + +-export([start_link/0]). +-export([run_setup/0]). + +-export([init/1, + handle_call/3, + handle_cast/2, + handle_info/2, + terminate/2, + code_change/3]). + +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +run_setup() -> + gen_server:call(?MODULE, run_setup). + +init(_) -> + {ok, []}. + +handle_call(run_setup, _From, S) -> + {reply, setup:run_setup(), S}; +handle_call(_, _, S) -> + {reply, {error, badarg}, S}. + +handle_cast(_, S) -> {noreply, S}. +handle_info(_, S) -> {noreply, S}. +terminate(_ , _) -> ok. + +code_change(_FromVsn, S, _Extra) -> + {ok, S}. diff --git a/src/setup_sup.erl b/src/setup_sup.erl new file mode 100644 index 0000000..ca74b1b --- /dev/null +++ b/src/setup_sup.erl @@ -0,0 +1,28 @@ +%% -*- mode: erlang; indent-tabs-mode: nil; -*- +%%============================================================================= +%% Copyright 2014-2016 Ulf Wiger +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%============================================================================= +-module(setup_sup). +-behaviour(supervisor). + +-export([start_link/0]). +-export([init/1]). + +start_link() -> + supervisor:start_link({local, ?MODULE}, ?MODULE, []). + +init(_) -> + {ok, {{one_for_one, 3, 10}, [{setup_srv, {setup_srv, start_link, []}, + permanent, 2000, worker, [setup_srv]}]}}.