aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEric <[email protected]>2013-01-31 15:26:30 -0800
committerEric <[email protected]>2013-01-31 15:26:30 -0800
commit8b43038c86a2b4d7329434dab85dd5aac17fc9fe (patch)
tree3a1924bf355644f4809602fafa4d80ddf6fbd3b5 /src
parent367d9fdf1d577dd67990cdd4ad4480ba1130bc92 (diff)
downloadrelx-8b43038c86a2b4d7329434dab85dd5aac17fc9fe.tar.gz
relx-8b43038c86a2b4d7329434dab85dd5aac17fc9fe.tar.bz2
relx-8b43038c86a2b4d7329434dab85dd5aac17fc9fe.zip
support release discovery in relcool along with app discovery
Diffstat (limited to 'src')
-rw-r--r--src/rcl_prv_discover.erl17
-rw-r--r--src/rcl_rel_discovery.erl166
-rw-r--r--src/rcl_release.erl11
3 files changed, 188 insertions, 6 deletions
diff --git a/src/rcl_prv_discover.erl b/src/rcl_prv_discover.erl
index 1ffbece..c5a625d 100644
--- a/src/rcl_prv_discover.erl
+++ b/src/rcl_prv_discover.erl
@@ -41,11 +41,20 @@ init(State) ->
%% @doc recursively dig down into the library directories specified in the state
%% looking for OTP Applications
-spec do(rcl_state:t()) -> {ok, rcl_state:t()} | relcool:error().
-do(State) ->
- LibDirs = get_lib_dirs(State),
- case rcl_app_discovery:do(State, LibDirs) of
+do(State0) ->
+ LibDirs = get_lib_dirs(State0),
+ case rcl_app_discovery:do(State0, LibDirs) of
{ok, AppMeta} ->
- {ok, rcl_state:available_apps(State, AppMeta)};
+ case rcl_rel_discovery:do(State0, LibDirs, AppMeta) of
+ {ok, Releases} ->
+ State1 = rcl_state:available_apps(State0, AppMeta),
+ State3 = lists:foldl(fun(Rel, State2) ->
+ rcl_state:add_release(State2, Rel)
+ end, State1, Releases),
+ {ok, State3};
+ Error ->
+ Error
+ end;
Error ->
Error
end.
diff --git a/src/rcl_rel_discovery.erl b/src/rcl_rel_discovery.erl
new file mode 100644
index 0000000..0fbcf23
--- /dev/null
+++ b/src/rcl_rel_discovery.erl
@@ -0,0 +1,166 @@
+%% -*- erlang-indent-level: 4; indent-tabs-mode: nil; fill-column: 80 -*-
+%%% Copyright 2012 Erlware, LLC. All Rights Reserved.
+%%%
+%%% This file is provided to you 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.
+%%%---------------------------------------------------------------------------
+%%% @author Eric Merritt <[email protected]>
+%%% @copyright (C) 2012 Erlware, LLC.
+%%%
+%%% @doc This provider uses the lib_dir setting of the state. It searches the
+%%% Lib Dirs looking for all OTP Applications that are available. When it finds
+%%% those OTP Applications it loads the information about them and adds them to
+%%% the state of available apps. This implements the rcl_provider behaviour.
+-module(rcl_rel_discovery).
+
+-export([do/3,
+ format_error/1]).
+
+-include_lib("relcool/include/relcool.hrl").
+
+%%============================================================================
+%% API
+%%============================================================================
+
+%% @doc recursively dig down into the library directories specified in the state
+%% looking for OTP Applications
+-spec do(rcl_state:t(), [filename:name()], [rcl_app_info:t()]) ->
+ {ok, [rcl_release:t()]} | relcool:error().
+do(State, LibDirs, AppMeta) ->
+ rcl_log:info(rcl_state:log(State),
+ fun() ->
+ ["Resolving available releases from directories:\n",
+ [[rcl_util:indent(1), LibDir, "\n"] || LibDir <- LibDirs]]
+ end),
+ resolve_rel_metadata(State, LibDirs, AppMeta).
+
+-spec format_error([ErrorDetail::term()]) -> iolist().
+format_error(ErrorDetails)
+ when erlang:is_list(ErrorDetails) ->
+ [[format_detail(ErrorDetail), "\n"] || ErrorDetail <- ErrorDetails].
+
+%%%===================================================================
+%%% Internal Functions
+%%%===================================================================
+resolve_rel_metadata(State, LibDirs, AppMeta) ->
+ ReleaseMeta0 =
+ lists:flatten(ec_plists:map(fun(LibDir) ->
+ discover_dir([], LibDir, AppMeta)
+ end, LibDirs)),
+ Errors = [case El of
+ {error, Ret} -> Ret;
+ _ -> El
+ end
+ || El <- ReleaseMeta0,
+ case El of
+ {error, _} ->
+ true;
+ _ ->
+ false
+ end],
+
+ case Errors of
+ [] ->
+ ReleaseMeta1 = lists:flatten(ReleaseMeta0),
+ rcl_log:debug(rcl_state:log(State),
+ fun() ->
+ ["Resolved the following OTP Releases from the system: \n",
+ [[rcl_release:format(1, Rel), "\n"] || Rel <- ReleaseMeta1]]
+ end),
+ {ok, ReleaseMeta1};
+ _ ->
+ ?RCL_ERROR(Errors)
+ end.
+
+-spec format_detail(ErrorDetail::term()) -> iolist().
+format_detail({accessing, File, eaccess}) ->
+ io_lib:format("permission denied accessing file ~s", [File]);
+format_detail({accessing, File, Type}) ->
+ io_lib:format("error (~p) accessing file ~s", [Type, File]).
+
+-spec discover_dir([file:name()], file:name(), [rcl_app_info:t()]) ->
+ [rcl_release:t() | {error, Reason::term()}]
+ | rcl_release:t()
+ | {error, Reason::term()}.
+discover_dir(IgnoreDirs, File, AppMeta) ->
+ case (not lists:member(File, IgnoreDirs))
+ andalso filelib:is_dir(File) of
+ true ->
+ case file:list_dir(File) of
+ {error, Reason} ->
+ {error, {accessing, File, Reason}};
+ {ok, List} ->
+ ec_plists:map(fun(LibDir) ->
+ discover_dir(IgnoreDirs, LibDir, AppMeta)
+ end,
+ [filename:join([File, Dir]) || Dir <- List])
+ end;
+ false ->
+ is_valid_release(File, AppMeta)
+ end.
+
+-spec is_valid_release(file:name(),
+ [rcl_app_info:t()]) ->
+ rcl_release:t()
+ | {error, Reason::term()}
+ | [].
+is_valid_release(File, AppMeta) ->
+ case lists:suffix(".rel", File) of
+ true ->
+ resolve_release(File, AppMeta);
+ false ->
+ []
+ end.
+
+resolve_release(RelFile, AppMeta) ->
+ case file:consult(RelFile) of
+ {ok, [{release, {RelName, RelVsn},
+ {erts, ErtsVsn},
+ Apps}]} ->
+ build_release(RelName, RelVsn, ErtsVsn, Apps, AppMeta);
+ {ok, InvalidRelease} ->
+ ?RCL_ERROR({invalid_release_information, InvalidRelease});
+ {error, Reason} ->
+ ?RCL_ERROR({unable_to_read, RelFile, Reason})
+ end.
+
+build_release(RelName, RelVsn, ErtsVsn, Apps, AppMeta) ->
+ Release = rcl_release:erts(rcl_release:new(RelName, RelVsn),
+ ErtsVsn),
+ resolve_apps(Apps, AppMeta, Release, []).
+
+resolve_apps([], _AppMeta, Release, Acc) ->
+ rcl_release:application_details(Release, Acc);
+resolve_apps([AppInfo | Apps], AppMeta, Release, Acc) ->
+ AppName = erlang:element(1, AppInfo),
+ AppVsn = erlang:element(2, AppInfo),
+ case find_app(AppName, AppVsn, AppMeta) of
+ Error = {error, _} ->
+ Error;
+ ResolvedApp ->
+ resolve_apps(Apps, AppMeta, Release, [ResolvedApp | Acc])
+ end.
+
+find_app(AppName, AppVsn, AppMeta) ->
+ case ec_lists:find(fun(App) ->
+ NAppName = rcl_app:name(App),
+ NAppVsn = rcl_app:version(App),
+ AppName == NAppName andalso
+ AppVsn == NAppVsn
+ end, AppMeta) of
+ {ok, Head} ->
+ Head;
+ error ->
+ ?RCL_ERROR({could_not_find, {AppName, AppVsn}})
+ end.
diff --git a/src/rcl_release.erl b/src/rcl_release.erl
index 8cd3033..ea20f1f 100644
--- a/src/rcl_release.erl
+++ b/src/rcl_release.erl
@@ -32,6 +32,7 @@
realize/3,
applications/1,
application_details/1,
+ application_details/2,
realized/1,
metadata/1,
format/1,
@@ -134,12 +135,18 @@ applications(#release_t{applications=Apps}) ->
Apps.
%% @doc this gives the rcl_app_info objects representing the applications in
-%% this release. These can only be populated by the 'realize' call in this
-%% module.
+%% this release. These should only be populated by the 'realize' call in this
+%% module or by reading an existing rel file.
-spec application_details(t()) -> [rcl_app_info:t()].
application_details(#release_t{app_detail=App}) ->
App.
+%% @doc this is only expected to be called by a process building a new release
+%% from an existing rel file.
+-spec application_details(t(), [rcl_app_info:t()]) -> t().
+application_details(Release, AppDetail) ->
+ Release#release_t{app_detail=AppDetail}.
+
-spec realized(t()) -> boolean().
realized(#release_t{realized=Realized}) ->
Realized.