summaryrefslogtreecommitdiff
path: root/src/rabbit_plugin_activator.erl
blob: 34df9c483f14feb5ea538e93a05a514e90413433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
%%   The contents of this file are subject to the Mozilla Public License
%%   Version 1.1 (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.mozilla.org/MPL/
%%
%%   Software distributed under the License is distributed on an "AS IS"
%%   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%%   License for the specific language governing rights and limitations
%%   under the License.
%%
%%   The Original Code is RabbitMQ.
%%
%%   The Initial Developers of the Original Code are LShift Ltd,
%%   Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
%%
%%   Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
%%   Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
%%   are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
%%   Technologies LLC, and Rabbit Technologies Ltd.
%%
%%   Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
%%   Ltd. Portions created by Cohesive Financial Technologies LLC are
%%   Copyright (C) 2007-2009 Cohesive Financial Technologies
%%   LLC. Portions created by Rabbit Technologies Ltd are Copyright
%%   (C) 2007-2009 Rabbit Technologies Ltd.
%%
%%   All Rights Reserved.
%%
%%   Contributor(s): ______________________________________.
%%

-module(rabbit_plugin_activator).

-export([start/0, stop/0]).

-define(DefaultPluginDir, "plugins").
-define(DefaultUnpackedPluginDir, "priv/plugins").
-define(DefaultRabbitEBin, "ebin").
-define(BaseApps, [rabbit]).
-ifdef(enable_debug).
-define(Debug(F, V), io:format("DEBUG: " ++ F, V)).
-endif.
-ifndef(enable_debug).
-define(Debug(_F, _V), ok).
-endif.
-define(Error(F, V), io:format("ERROR: " ++ F, V)).

%%----------------------------------------------------------------------------

start() ->
	% Ensure Rabbit is loaded so we can access it's environment
	application:load(rabbit),
	
	% Determine our various directories
	PluginDir = get_env_or_default(plugins_dir, ?DefaultPluginDir),
	UnpackedPluginDir = get_env_or_default(plugins_expand_dir, ?DefaultUnpackedPluginDir),
	RabbitEBin = get_env_or_default(rabbit_ebin, ?DefaultRabbitEBin),
	?Debug("Got Plugin Dir ~p~n", [PluginDir]),
	?Debug("Got Unpacked Plugin Dir ~p~n", [UnpackedPluginDir]),
	?Debug("Got Rabbit EBin Dir ~p~n", [RabbitEBin]),
	
	% Unpack any .ez plugins
	unpack_ez_plugins(PluginDir, UnpackedPluginDir),
	
	% Build a list of required apps based on the fixed set, and any plugins
	RequiredApps = ?BaseApps ++ find_plugins(PluginDir) ++ find_plugins(UnpackedPluginDir),
	
	% Build the entire set of dependencies - this will load the applications along the way
	AllApps = case catch sets:to_list(expand_dependencies(RequiredApps)) of
		{unknown_app, _} -> 
			?Error("Failed to expand dependencies.~n", []),
			halt(1);
		AppList ->
			AppList
	end,
	AppVersions = [determine_version(App) || App <- AllApps],
	{value, {rabbit, RabbitVersion}} = lists:keysearch(rabbit, 1, AppVersions),
	
	% Build the overall release descriptor
	RDesc = {release, 
			 {"rabbit", RabbitVersion}, 
			 {erts, erlang:system_info(version)},
			 AppVersions},
	
	% Write it out to ebin/rabbit.rel
	?Debug("Generated rabbit.rel: ~p.~n", [RDesc]),
	file:write_file(RabbitEBin ++ "/rabbit.rel", io_lib:format("~p.~n", [RDesc])),
	
	% Compile the script
	case systools:make_script(RabbitEBin ++ "/rabbit", [local, silent]) of
		{ok, Module, Warnings} -> 
			% This gets lots of spurious no-source warnings when we have .ez files, so we want to supress them to prevent
			% hiding real issues.
			WarningStr = Module:format_warning([W || W <- Warnings, 
				case W of 
					{warning, {source_not_found, _}} -> false; 
					_ 								 -> true 
				end]),
			case length(WarningStr) of
				0 -> ok;
				_ -> io:format("~s", [WarningStr])
			end,
			ok;
		{error, Module, Error} ->
			io:format("Boot file generation failed: ~s~n", [Module:format_error(Error)]),
			halt(1)
	end,
	halt(),
	ok.
	
stop() ->
	ok.
	
get_env_or_default(Key, Default) ->
	case application:get_env(rabbit, Key) of
		{ok, V} -> V;
		_		-> Default
	end.
	
determine_version(App) ->
	application:load(App), 
	{ok, Vsn} = application:get_key(App, vsn), 
	{App, Vsn}.
	
assert_dir(Dir) ->
    case filelib:is_dir(Dir) of
        true  -> ok;
        false ->
            ok = filelib:ensure_dir(Dir),
            ok = file:make_dir(Dir)
    end.
delete_dir(Dir) ->
	case filelib:is_dir(Dir) of
		true ->
			case file:list_dir(Dir) of
				{ok, Files} ->
					Paths = [Dir ++ "/" ++ F || F <- Files],
					[delete_dir(F) || F <- Paths, filelib:is_dir(F)],
					[file:delete(F) || F <- Paths, filelib:is_file(F)]
			end,
			ok = file:del_dir(Dir);
		false ->
			ok
	end.

unpack_ez_plugins(PluginSrcDir, PluginDestDir) ->
	% Eliminate the contents of the destination directory
	delete_dir(PluginDestDir),
	
	assert_dir(PluginDestDir),
	[unpack_ez_plugin(PluginName, PluginDestDir) || PluginName <- filelib:wildcard(PluginSrcDir ++ "/*.ez")].
	
unpack_ez_plugin(PluginFn, PluginDestDir) ->
	zip:unzip(PluginFn, [{cwd, PluginDestDir}]),
	ok.


find_plugins(PluginDir) ->
	[prepare_dir_plugin(PluginName) || PluginName <- filelib:wildcard(PluginDir ++ "/*/ebin/*.app")].
	
prepare_dir_plugin(PluginAppDescFn) ->
	% Add the plugin ebin directory to the load path
	PluginEBinDirN = filename:dirname(PluginAppDescFn),
	code:add_path(PluginEBinDirN),
	
	% We want the second-last token
	NameTokens = string:tokens(PluginAppDescFn,"/."),
	PluginNameString = lists:nth(length(NameTokens) - 1, NameTokens),
	list_to_atom(PluginNameString).

expand_dependencies(Pending) ->
	expand_dependencies(sets:new(), Pending).
expand_dependencies(Current, []) ->
	Current;
expand_dependencies(Current, [Next|Rest]) ->
	case sets:is_element(Next, Current) of
		true ->
			expand_dependencies(Current, Rest);
		false ->
			case application:load(Next) of
				ok -> 
					ok;
				{error, {already_loaded, _}} -> 
					ok;
				X -> 
					?Error("Failed to load ~s: ~p~n", [Next, X]),
					throw({unknown_app, Next})
			end,
			{ok, Required} = application:get_key(Next, applications),
			Unique = [A || A <- Required, not(sets:is_element(A, Current))],
			expand_dependencies(sets:add_element(Next, Current), Rest ++ Unique)
	end.