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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et
%% -------------------------------------------------------------------
%%
%% rebar: Erlang Build Tools
%%
%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%% THE SOFTWARE.
%% -------------------------------------------------------------------
-module(rebar_reltool).
-export([generate/2,
clean/2]).
-include("rebar.hrl").
-include_lib("reltool/src/reltool.hrl").
-include_lib("kernel/include/file.hrl").
%% ===================================================================
%% Public API
%% ===================================================================
generate(Config, ReltoolFile) ->
%% Make sure we have decent version of reltool available
check_vsn(),
%% Load the reltool configuration from the file
ReltoolConfig = load_config(ReltoolFile),
%% Spin up reltool server and load our config into it
{ok, Server} = reltool:start_server([sys_tuple(ReltoolConfig)]),
%% Do some validation of the reltool configuration; error messages out of
%% reltool are still pretty cryptic
validate_rel_apps(Server, sys_tuple(ReltoolConfig)),
%% Finally, run reltool
case catch(run_reltool(Server, Config, ReltoolConfig)) of
ok ->
ok;
{error, failed} ->
?FAIL;
Other2 ->
?ERROR("Unexpected error: ~p\n", [Other2]),
?FAIL
end.
clean(_Config, ReltoolFile) ->
ReltoolConfig = load_config(ReltoolFile),
TargetDir = target_dir(ReltoolConfig),
rebar_file_utils:rm_rf(TargetDir),
rebar_file_utils:delete_each(["reltool.spec"]).
%% ===================================================================
%% Internal functions
%% ===================================================================
check_vsn() ->
case code:lib_dir(reltool) of
{error, bad_name} ->
?ABORT("Reltool support requires the reltool application to be installed!", []);
Path ->
ReltoolVsn = filename:basename(Path),
case ReltoolVsn < "reltool-0.5.2" of
true ->
?ABORT("Reltool support requires at least reltool-0.5.2; this VM is using ~s\n",
[ReltoolVsn]);
false ->
ok
end
end.
%%
%% Load terms from reltool.config
%%
load_config(ReltoolFile) ->
case file:consult(ReltoolFile) of
{ok, Terms} ->
Terms;
Other ->
?ABORT("Failed to load expected config from ~s: ~p\n", [ReltoolFile, Other])
end.
%%
%% Look for the {sys, [...]} tuple in the reltool.config file. Without this present, we
%% can't run reltool.
%%
sys_tuple(ReltoolConfig) ->
case lists:keyfind(sys, 1, ReltoolConfig) of
{sys, _} = SysTuple ->
SysTuple;
false ->
?ABORT("Failed to find {sys, [...]} tuple in reltool.config.", [])
end.
%%
%% Look for {target_dir, TargetDir} in the reltool config file; if none is
%% found, use the name of the release as the default target directory.
%%
target_dir(ReltoolConfig) ->
case rebar_config:get_global(target_dir, undefined) of
undefined ->
case lists:keyfind(target_dir, 1, ReltoolConfig) of
{target_dir, TargetDir} ->
filename:absname(TargetDir);
false ->
{sys, SysInfo} = sys_tuple(ReltoolConfig),
case lists:keyfind(rel, 1, SysInfo) of
{rel, Name, _Vsn, _Apps} ->
filename:absname(Name);
false ->
filename:absname("target")
end
end;
TargetDir ->
filename:absname(TargetDir)
end.
%%
%% Look for overlay_vars file reference. The user can override this from the
%% command line (i.e. globals), so we check there first and then fall back to
%% what is present in the reltool.config file
%%
overlay_vars(ReltoolConfig) ->
case rebar_config:get_global(overlay_vars, undefined) of
undefined ->
case lists:keyfind(overlay_vars, 1, ReltoolConfig) of
{overlay_vars, File} ->
File;
false ->
undefined
end;
File ->
File
end.
validate_rel_apps(ReltoolServer, {sys, ReltoolConfig}) ->
case lists:keyfind(rel, 1, ReltoolConfig) of
false ->
ok;
{rel, _Name, _Vsn, Apps} ->
%% Identify all the apps that do NOT exist, based on what's available
%% from the reltool server
Missing = lists:sort([App || App <- Apps,
app_exists(App, ReltoolServer) == false]),
case Missing of
[] ->
ok;
_ ->
?ABORT("Apps in {rel, ...} section not found by reltool: ~p\n", [Missing])
end;
Rel ->
%% Invalid release format!
?ABORT("Invalid {rel, ...} section in reltools.config: ~p\n", [Rel])
end.
app_exists(App, Server) when is_atom(App) ->
case reltool_server:get_app(Server, App) of
{ok, _} ->
true;
_ ->
false
end;
app_exists(AppTuple, Server) when is_tuple(AppTuple) ->
app_exists(element(1, AppTuple), Server).
run_reltool(Server, _Config, ReltoolConfig) ->
case reltool:get_target_spec(Server) of
{ok, Spec} ->
%% Pull the target dir and make sure it exists
TargetDir = target_dir(ReltoolConfig),
mk_target_dir(TargetDir),
%% Dump the spec, if necessary
dump_spec(Spec),
%% Have reltool actually run
case reltool:eval_target_spec(Spec, code:root_dir(), TargetDir) of
ok ->
ok;
{error, Reason} ->
?ABORT("Failed to generate target from spec: ~p\n", [Reason])
end,
%% Initialize overlay vars with some basics (that can get overwritten)
OverlayVars0 = [{erts_vsn, "erts-" ++ erlang:system_info(version)}],
%% Load up any variables specified by overlay_vars
OverlayVars = case overlay_vars(ReltoolConfig) of
undefined ->
dict:from_list(OverlayVars0);
File ->
case file:consult(File) of
{ok, Terms} ->
dict:from_list(OverlayVars0 ++ Terms);
{error, Reason2} ->
?ABORT("Unable to load overlay_vars from ~s: ~p\n",
[File, Reason2])
end
end,
%% Finally, overlay the files specified by the overlay section
case lists:keyfind(overlay, 1, ReltoolConfig) of
{overlay, Overlay} when is_list(Overlay) ->
execute_overlay(Overlay, OverlayVars, rebar_utils:get_cwd(),
TargetDir);
false ->
?INFO("No {overlay, [...]} found in reltool.config.\n", []);
_ ->
?ABORT("{overlay, [...]} entry in reltool.config "
"must be a list.\n", [])
end;
{error, Reason} ->
?ABORT("Unable to generate spec: ~s\n", [Reason])
end.
mk_target_dir(TargetDir) ->
case file:make_dir(TargetDir) of
ok ->
ok;
{error, eexist} ->
%% Output directory already exists; if force=1, wipe it out
case rebar_config:get_global(force, "0") of
"1" ->
rebar_file_utils:rm_rf(TargetDir),
ok = file:make_dir(TargetDir);
_ ->
?ERROR("Release target directory ~p already exists!\n", [TargetDir]),
?FAIL
end
end.
dump_spec(Spec) ->
case rebar_config:get_global(dump_spec, "0") of
"1" ->
SpecBin = list_to_binary(io_lib:print(Spec, 1, 120, -1)),
ok = file:write_file("reltool.spec", SpecBin);
_ ->
ok
end.
execute_overlay([], _Vars, _BaseDir, _TargetDir) ->
ok;
execute_overlay([{mkdir, Out} | Rest], Vars, BaseDir, TargetDir) ->
OutFile = render(filename:join([TargetDir, Out, "dummy"]), Vars),
ok = filelib:ensure_dir(OutFile),
?DEBUG("Created dir ~s\n", [filename:dirname(OutFile)]),
execute_overlay(Rest, Vars, BaseDir, TargetDir);
execute_overlay([{copy, In} | Rest], _Vars, BaseDir, TargetDir) ->
execute_overlay([{copy, In, ""} | Rest], _Vars, BaseDir, TargetDir);
execute_overlay([{copy, In, Out} | Rest], Vars, BaseDir, TargetDir) ->
InFile = render(filename:join(BaseDir, In), Vars),
OutFile = render(filename:join(TargetDir, Out), Vars),
case filelib:is_dir(InFile) of
true ->
ok;
false ->
ok = filelib:ensure_dir(OutFile)
end,
rebar_file_utils:cp_r([InFile], OutFile),
execute_overlay(Rest, Vars, BaseDir, TargetDir);
execute_overlay([{template, In, Out} | Rest], Vars, BaseDir, TargetDir) ->
InFile = render(filename:join(BaseDir, In), Vars),
{ok, InFileData} = file:read_file(InFile),
OutFile = render(filename:join(TargetDir, Out), Vars),
ok = filelib:ensure_dir(OutFile),
case file:write_file(OutFile, render(InFileData, Vars)) of
ok ->
ok = apply_file_info(InFile, OutFile),
?DEBUG("Templated ~p\n", [OutFile]),
execute_overlay(Rest, Vars, BaseDir, TargetDir);
{error, Reason} ->
?ABORT("Failed to template ~p: ~p\n", [OutFile, Reason])
end;
execute_overlay([{create, Out, Contents} | Rest], Vars, BaseDir, TargetDir) ->
OutFile = render(filename:join(TargetDir, Out), Vars),
ok = filelib:ensure_dir(OutFile),
case file:write_file(OutFile, Contents) of
ok ->
?DEBUG("Created ~p\n", [OutFile]),
execute_overlay(Rest, Vars, BaseDir, TargetDir);
{error, Reason} ->
?ABORT("Failed to create ~p: ~p\n", [OutFile, Reason])
end;
execute_overlay([{replace, Out, Regex, Replacement} | Rest],
Vars, BaseDir, TargetDir) ->
execute_overlay([{replace, Out, Regex, Replacement, []} | Rest], Vars, BaseDir, TargetDir);
execute_overlay([{replace, Out, Regex, Replacement, Opts} | Rest],
Vars, BaseDir, TargetDir) ->
Filename = render(filename:join(TargetDir, Out), Vars),
{ok, OrigData} = file:read_file(Filename),
Data = re:replace(OrigData, Regex, Replacement, [global, {return, binary}] ++ Opts),
case file:write_file(Filename, Data) of
ok ->
?DEBUG("Edited ~s: s/~s/~s/\n", [Filename, Regex, Replacement]),
execute_overlay(Rest, Vars, BaseDir, TargetDir);
{error, Reason} ->
?ABORT("Failed to edit ~p: ~p\n", [Filename, Reason])
end;
execute_overlay([Other | _Rest], _Vars, _BaseDir, _TargetDir) ->
{error, {unsupported_operation, Other}}.
%%
%% Render a binary to a string, using mustache and the specified context
%%
render(Bin, Context) ->
ReOpts = [global, {return, list}],
%% Be sure to escape any double-quotes before rendering...
Str0 = re:replace(Bin, "\\\\", "\\\\\\", ReOpts),
Str1 = re:replace(Str0, "\"", "\\\\\"", ReOpts),
mustache:render(Str1, Context).
apply_file_info(InFile, OutFile) ->
{ok, FileInfo} = file:read_file_info(InFile),
ok = file:write_file_info(OutFile, FileInfo).
|