Skip to content

Commit

Permalink
Add supervisor, app module. Add 'start_setup' option (default: true)
Browse files Browse the repository at this point in the history
The 'start_setup' option was added to address the weird behavior that
setup_gen always sets setup to load-only in the .rel file. This can
now be achieved by giving -start_setup false, but the appropriate
way would be to give {setup,load} in the 'apps' list.
  • Loading branch information
uwiger committed Jan 25, 2016
1 parent 5c7d635 commit 370acf1
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/setup.app.src
Expand Up @@ -23,6 +23,7 @@
kernel,
stdlib
]},
{mod, { setup, []}},
{mod, { setup_app, []}},
{start_phases, [{run_setup, []}]},
{env, []}
]}.
32 changes: 7 additions & 25 deletions src/setup.erl
Expand Up @@ -132,10 +132,6 @@
%% disk storage.
%% @end
-module(setup).
-behaviour(application).

-export([start/2,
stop/1]).

-export([home/0,
log_dir/0,
Expand All @@ -159,28 +155,14 @@
-export([ok/1]).
-compile(export_all).

-export([run_setup/2]).
-export([run_setup/0]).

-include_lib("kernel/include/file.hrl").

-ifdef(TEST).
-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
Expand Down Expand Up @@ -860,31 +842,31 @@ 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"
"~p~n"
"~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}]}]
Expand Down
32 changes: 32 additions & 0 deletions 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.
11 changes: 10 additions & 1 deletion src/setup_gen.erl
Expand Up @@ -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 ==
Expand Down Expand Up @@ -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)];
Expand Down Expand Up @@ -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(
Expand Down
49 changes: 49 additions & 0 deletions 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}.
28 changes: 28 additions & 0 deletions 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]}]}}.

0 comments on commit 370acf1

Please sign in to comment.