Skip to content

Commit

Permalink
Merge pull request #56 from tsloughter/master
Browse files Browse the repository at this point in the history
warn on bad app during discovery and erts in tar fix
  • Loading branch information
jwilberding committed Sep 28, 2013
2 parents 8a60327 + faf8305 commit c2bbc32
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Options
| -l | --lib-dir | string | | Additional dirs to search for OTP apps |
| -p | --path | string | | Additional dirs to add to Erlang code path |
| | --default-libs | boolean | true | Whether to use the default system added lib dirs (means you must add them all manually) |
| -V | --verbose | integer | 0 | The verbosity level between 0 and 2 |
| -V | --verbose | integer | 2 | The verbosity level between 0 and 3 |
| -a | --override_app | string | | An app name and a directory to override in the form appname:dir |
| -c | --config | string | ./relx.config | Config file path |

Expand Down
5 changes: 3 additions & 2 deletions include/relx.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
%%

-define(RLX_ERROR, 0).
-define(RLX_INFO, 1).
-define(RLX_DEBUG, 2).
-define(RLX_WARN, 1).
-define(RLX_INFO, 2).
-define(RLX_DEBUG, 3).

%% This is the default form of error messages for the Relx
%% system. It is expected that everything that returns an error use
Expand Down
4 changes: 2 additions & 2 deletions src/relx.erl
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ opt_spec_list() ->
{default_libs, undefined, "default-libs",
{boolean, true},
"Whether to use the default system added lib dirs (means you must add them all manually). Default is true"},
{log_level, $V, "verbose", {integer, 1},
"Verbosity level, maybe between 0 and 2"},
{log_level, $V, "verbose", {integer, 2},
"Verbosity level, maybe between 0 and 3"},
{override_app, $a, "override_app", string,
"Provide an app name and a directory to override in the form <appname>:<app directory>"},
{config, $c, "config", {string, ""}, "The path to a config file"},
Expand Down
19 changes: 12 additions & 7 deletions src/rlx_app_discovery.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
do(State, LibDirs) ->
rlx_log:info(rlx_state:log(State),
fun() ->
["Resolving OTP Applications from directories:\n",
[[rlx_util:indent(2), LibDir, "\n"] || LibDir <- LibDirs]]
["Resolving OTP Applications from directories:\n",
string:join([[rlx_util:indent(2), LibDir] || LibDir <- LibDirs], "\n")]
end),
resolve_app_metadata(State, LibDirs).

Expand All @@ -62,6 +62,9 @@ resolve_app_metadata(State, LibDirs) ->
case Err of
{error, _} ->
true;
{warning, W} ->
rlx_log:warn(rlx_state:log(State), format_detail(W)),
false;
_ ->
false
end] of
Expand All @@ -79,6 +82,8 @@ resolve_app_metadata(State, LibDirs) ->
?RLX_ERROR(Errors)
end.

app_name({warning, _}) ->
undefined;
app_name({error, _}) ->
undefined;
app_name({ok, AppMeta}) ->
Expand All @@ -103,7 +108,7 @@ resolve_override(AppName, FileName0) ->
end.

-spec format_detail(ErrorDetail::term()) -> iolist().
format_detail({error, {missing_beam_file, Module, BeamFile}}) ->
format_detail({missing_beam_file, Module, BeamFile}) ->
io_lib:format("Missing beam file ~p ~p", [Module, BeamFile]);
format_detail({error, {invalid_override, AppName, FileName}}) ->
io_lib:format("Override {~p, ~p} is not a valid OTP App. Perhaps you forgot to build it?",
Expand Down Expand Up @@ -161,9 +166,9 @@ gather_application_info(EbinDir, File) ->
{ok, [{application, AppName, AppDetail}]} ->
validate_application_info(EbinDir, File, AppName, AppDetail);
{error, Reason} ->
{error, {unable_to_load_app, AppDir, Reason}};
{warning, {unable_to_load_app, AppDir, Reason}};
_ ->
{error, {invalid_app_file, File}}
{warning, {invalid_app_file, File}}
end.

-spec validate_application_info(file:name(),
Expand All @@ -189,7 +194,7 @@ validate_application_info(EbinDir, AppFile, AppName, AppDetail) ->
get_modules_list(AppFile, AppDetail) ->
case proplists:get_value(modules, AppDetail) of
undefined ->
{error, {invalid_app_file, AppFile}};
{warning, {invalid_app_file, AppFile}};
ModulesList ->
{ok, ModulesList}
end.
Expand All @@ -203,7 +208,7 @@ has_all_beams(EbinDir, [Module | ModuleList]) ->
true ->
has_all_beams(EbinDir, ModuleList);
false ->
{error, {missing_beam_file, Module, BeamFile}}
{warning, {missing_beam_file, Module, BeamFile}}
end;
has_all_beams(_, []) ->
ok.
Expand Down
4 changes: 2 additions & 2 deletions src/rlx_cmd_args.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ format_error({not_directory, Dir}) ->
io_lib:format("Library directory does not exist: ~s", [Dir]);
format_error({invalid_log_level, LogLevel}) ->
io_lib:format("Invalid log level specified -V ~p, log level must be in the"
" range 0..2", [LogLevel]);
" range 0..3", [LogLevel]);
format_error({invalid_target, Target}) ->
io_lib:format("Invalid action specified: ~s", [Target]).

Expand Down Expand Up @@ -136,7 +136,7 @@ validate_config(Config) ->
create_log(Opts, Acc) ->
LogLevel = proplists:get_value(log_level, Opts, 0),
if
LogLevel >= 0, LogLevel =< 2 ->
LogLevel >= 0, LogLevel =< 3 ->
create_goals(Opts, [{log, rlx_log:new(LogLevel, command_line)} | Acc]);
true ->
?RLX_ERROR({invalid_log_level, LogLevel})
Expand Down
2 changes: 2 additions & 0 deletions src/rlx_dscv_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ discover_dir(ProcessDir, File, file) ->
[{ok, Result}];
{noresult, _} ->
[];
Warn = {warning, _} ->
[Warn];
Err = {error, _} ->
[Err]
end;
Expand Down
33 changes: 26 additions & 7 deletions src/rlx_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
info/3,
error/2,
error/3,
warn/2,
warn/3,
log_level/1,
atom_log_level/1,
format/1]).
Expand Down Expand Up @@ -63,11 +65,9 @@

-type log_level() :: int_log_level() | atom_log_level().

-type int_log_level() :: 0..2.
-type int_log_level() :: 0..3.

%% Why no warn? because for our purposes there is no difference between error
%% and warn
-type atom_log_level() :: error | info | debug.
-type atom_log_level() :: error | warn | info | debug.

-type log_fun() :: fun(() -> iolist()).

Expand All @@ -83,16 +83,18 @@
new(LogLevel) ->
new(LogLevel, api).

new(LogLevel, Caller) when LogLevel >= 0, LogLevel =< 2 ->
new(LogLevel, Caller) when LogLevel >= 0, LogLevel =< 3 ->
#state_t{mod=?MODULE, log_level=LogLevel, caller=Caller};
new(AtomLogLevel, Caller)
when AtomLogLevel =:= error;
AtomLogLevel =:= warn;
AtomLogLevel =:= info;
AtomLogLevel =:= debug ->
LogLevel = case AtomLogLevel of
error -> 0;
info -> 1;
debug -> 2
warn -> 1;
info -> 2;
debug -> 3
end,
new(LogLevel, Caller).

Expand Down Expand Up @@ -142,6 +144,21 @@ error(LogState, String) ->
error(LogState, FormatString, Args) ->
log(LogState, ?RLX_ERROR, colorize(LogState, ?GREEN, false, FormatString), Args).

%% @doc log at the warn level given the current log state with a string or
%% format string that returns a function
-spec warn(t(), string() | log_fun()) -> ok.
warn(LogState, Fun)
when erlang:is_function(Fun) ->
log(LogState, ?RLX_WARN, fun() -> colorize(LogState, ?MAGENTA, false, Fun()) end);
warn(LogState, String) ->
warn(LogState, "~s~n", [String]).

%% @doc log at the warn level given the current log state with a format string
%% and argements @see io:format/2
-spec warn(t(), string(), [any()]) -> ok.
warn(LogState, FormatString, Args) ->
log(LogState, ?RLX_WARN, colorize(LogState, ?MAGENTA, false, FormatString), Args).

%% @doc Execute the fun passed in if log level is as expected.
-spec log(t(), int_log_level(), log_fun()) -> ok.
log(#state_t{mod=?MODULE, log_level=DetailLogLevel}, LogLevel, Fun)
Expand Down Expand Up @@ -178,6 +195,8 @@ log_level(#state_t{mod=?MODULE, log_level=DetailLogLevel}) ->
-spec atom_log_level(t()) -> atom_log_level().
atom_log_level(#state_t{mod=?MODULE, log_level=?RLX_ERROR}) ->
error;
atom_log_level(#state_t{mod=?MODULE, log_level=?RLX_WARN}) ->
warn;
atom_log_level(#state_t{mod=?MODULE, log_level=?RLX_INFO}) ->
info;
atom_log_level(#state_t{mod=?MODULE, log_level=?RLX_DEBUG}) ->
Expand Down
44 changes: 28 additions & 16 deletions src/rlx_prv_assembler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,16 @@ make_tar(State, Release, OutputDir) ->
Prefix = code:root_dir(),
ErtsVersion = rlx_release:erts(Release),
ErtsDir = filename:join([Prefix]),
Opts = [{path, [filename:join([OutputDir, "lib", "*", "ebin"])]},
{outdir, OutputDir} |
case rlx_state:get(State, include_erts, true) of
true ->
[{erts, ErtsDir}];
false ->
[]
end],
case systools:make_tar(filename:join([OutputDir, "releases", Vsn, Name]),
[{path, [filename:join([OutputDir, "lib", "*", "ebin"])]},
{erts, ErtsDir},
{outdir, OutputDir}]) of
Opts) of
ok ->
TempDir = ec_file:insecure_mkdtemp(),
try
Expand All @@ -456,19 +462,25 @@ update_tar(State, TempDir, OutputDir, Name, Vsn, ErtsVersion) ->
TarFile = filename:join(OutputDir, Name++"-"++Vsn++".tar.gz"),
file:rename(filename:join(OutputDir, Name++".tar.gz"), TarFile),
erl_tar:extract(TarFile, [{cwd, TempDir}, compressed]),
ok = erl_tar:create(TarFile,
[{"erts-"++ErtsVersion, filename:join(TempDir, "erts-"++ErtsVersion)},
{filename:join(["erts-"++ErtsVersion, "bin", "nodetool"]),
hd(nodetool_contents())},
{filename:join(["erts-"++ErtsVersion, "bin", "install_upgrade.escript"]),
hd(install_upgrade_escript_contents())},
{"lib", filename:join(TempDir, "lib")},
{"releases", filename:join(TempDir, "releases")},
{filename:join(["releases", "RELEASES"]),
filename:join([OutputDir, "releases", "RELEASES"])},
{filename:join(["releases", Vsn, "vm.args"]),
filename:join([OutputDir, "releases", Vsn, "vm.args"])},
{"bin", filename:join([OutputDir, "bin"])}], [compressed]),
ok =
erl_tar:create(TarFile,
[{"lib", filename:join(TempDir, "lib")},
{"releases", filename:join(TempDir, "releases")},
{filename:join(["releases", "RELEASES"]),
filename:join([OutputDir, "releases", "RELEASES"])},
{filename:join(["releases", Vsn, "vm.args"]),
filename:join([OutputDir, "releases", Vsn, "vm.args"])},
{"bin", filename:join([OutputDir, "bin"])} |
case rlx_state:get(State, include_erts, true) of
true ->
[{"erts-"++ErtsVersion, filename:join(TempDir, "erts-"++ErtsVersion)},
{filename:join(["erts-"++ErtsVersion, "bin", "nodetool"]),
hd(nodetool_contents())},
{filename:join(["erts-"++ErtsVersion, "bin", "install_upgrade.escript"]),
hd(install_upgrade_escript_contents())}];
false ->
[]
end], [compressed]),
rlx_log:info(rlx_state:log(State),
"tarball ~s successfully created!~n", [TarFile]),
ec_file:remove(TempDir, [recursive]),
Expand Down
2 changes: 1 addition & 1 deletion src/rlx_rel_discovery.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ do(State, LibDirs, AppMeta) ->
rlx_log:info(rlx_state:log(State),
fun() ->
["Resolving available OTP Releases from directories:\n",
[[rlx_util:indent(2), LibDir, "\n"] || LibDir <- LibDirs]]
string:join([[rlx_util:indent(2), LibDir] || LibDir <- LibDirs], "\n")]
end),
resolve_rel_metadata(State, LibDirs, AppMeta).

Expand Down
9 changes: 4 additions & 5 deletions test/rlx_discover_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,11 @@ bad_ebin_case(Config) ->
Filename = filename:join([AppDir, <<"ebin">>, BadName ++ ".app"]),
ok = filelib:ensure_dir(Filename),
ok = ec_file:write_term(Filename, get_bad_app_metadata(BadName, BadVsn)),
write_beam_file(AppDir, BadName),
State0 = proplists:get_value(state, Config),
{DiscoverProvider, {ok, State1}} = rlx_provider:new(rlx_prv_discover, State0),
?assertMatch({error, {_, [{invalid_app_file, Filename}]}},
rlx_provider:do(DiscoverProvider, State1)).

{ok, State2} = rlx_provider:do(DiscoverProvider, State1),
?assertMatch([], [App || App <- rlx_state:available_apps(State2),
BadName =:= rlx_app_info:name(App)]).

%%%===================================================================
%%% Helper functions
Expand Down Expand Up @@ -178,7 +177,7 @@ get_bad_app_metadata(Name, Vsn) ->
["{application, ", Name, ",
[{description, \"\"},
{vsn, \"", Vsn, "\"},
{modules, [],
{modules, [missing],
{applications, [kernel, stdlib]}]}."].


Expand Down
Loading

0 comments on commit c2bbc32

Please sign in to comment.