-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_node
executable file
·179 lines (157 loc) · 4.56 KB
/
make_node
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
#!/usr/bin/env escript
%% -*- erlang -*-
%% Copyright 2011-12 Feuerlabs Inc.
%% License: "BSD New" (http://www.opensource.org/licenses/BSD-3-Clause)
%% Author: Ulf Wiger <[email protected]>
-mode(compile).
-record(state, {target, rel, vm_args = [], node_local = false}).
main(Args) ->
try begin
St = args(Args, #state{}),
make_node(St)
end
catch
error:E ->
rpt_error(E, erlang:get_stacktrace()),
usage(),
exit(1)
end.
rpt_error(E, Stack) ->
io:fwrite("ERROR: ~p~n"
"Stack: ~p~n", [E, Stack]).
usage() ->
io:fwrite("Usage: ~s -rel RelDir -target TargetDir [ -- VmArgs ]~n",
[escript:script_name()]),
%% allow IO time to to through
receive after 500 -> ok end.
make_node(#state{target = T, rel = R, vm_args = As,
node_local = NodeLocal}) when T=/=undefined, R=/=undefined ->
Vsn = read_start_erl_data(R),
RelDir = filename:join([R, "releases", Vsn]),
VmArgsF = filename:join(RelDir, "vm.args"),
SysConfigF = filename:join(RelDir, "sys.config"),
case file:read_file(VmArgsF) of
{ok, Bin} ->
Bin1 = substitute_args(As, Bin),
OutF = filename:join(T, "vm.args"),
ok = filelib:ensure_dir(OutF),
file:write_file(OutF, Bin1);
Error ->
error({vm_args_not_found, Error})
end,
case file:consult(SysConfigF) of
{ok, [Env]} ->
NewEnv = localize_env(T, Env),
{ok, Fd} = file:open(filename:join(T, "sys.config"), [write]),
try
io:fwrite(Fd, "~p.~n", [NewEnv])
after
file:close(Fd)
end;
{error,_} = E ->
error({could_not_consult, [SysConfigF, E]})
end,
_CpR = file:copy(filename:join(R, "ctl"), TCtl = filename:join(T, "ctl")),
set_x_bit(TCtl),
write_env(R, T, NodeLocal).
set_x_bit(F) ->
file:change_mode(F, 8#00744).
write_env(R, T, NodeLocal) ->
OutF = filename:join(T, ".exodm_env"),
Rb = list_to_binary(R),
Tb = list_to_binary(T),
Ex = if NodeLocal -> filename:dirname(filename:dirname(Tb));
true -> Tb
end,
EXODM_DIR = case os:getenv("EXODM_DIR") of
false -> Ex;
DIR -> list_to_binary(DIR)
end,
Bin = <<"EXODM_DIR=",EXODM_DIR/binary,"\n",
"EXODM_RELEASE_DIR=",Rb/binary,"\n">>,
file:write_file(OutF, Bin).
read_start_erl_data(R) ->
io:fwrite("Reading ~s/releases/start_erl.data~n", [R]),
case file:read_file(filename:join(R, "releases/start_erl.data")) of
{ok, B} ->
[_, V, []] = re:split(B,"\\s",[{return,list}]),
V;
_Error ->
error("start_erl.data not found")
end.
substitute_args(Args, B) ->
Lines = re:split(B, "\\n", [{return,list}]),
case lists:foldl(fun({Flag, Vs}, Acc) ->
sub(Acc, Flag, Vs)
end, Lines, Args) of
[] ->
error("empty vm_args");
[L|Ls] ->
list_to_binary([L, "\n", [[L1, "\n"] || L1 <- Ls]])
end.
sub(["-" ++ _ = L | Ls], Flag, Vs) ->
case lists:prefix(Flag, L) of
true ->
[[Flag, [[" ", V] || V <- Vs]] | Ls];
false ->
[L | sub(Ls, Flag, Vs)]
end;
sub([L | Ls], Flag, Vs) ->
[L | sub(Ls, Flag, Vs)];
sub([], Flag, Vs) ->
[[Flag, [[" ", V] || V <- Vs]]].
args(["-target", T | Args], St) ->
args(Args, St#state{target = filename:absname(T)});
args(["-rel", R | Args], St) ->
args(Args, St#state{rel = R});
args(["-node_local", T | Args], St) ->
args(Args, St#state{node_local = mk_bool(T)});
args(["--" | Args], St) ->
VmArgs = vm_args(Args),
St#state{vm_args = VmArgs};
args([Other | _], _) ->
error({bad_argument, Other});
args([], St) ->
St.
vm_args(["-" ++ _ = Flag | Args]) ->
{Vals, Args1} = get_vals(Args),
[{Flag, Vals} | vm_args(Args1)];
vm_args([]) ->
[].
mk_bool("true" ) -> true;
mk_bool("false") -> false.
get_vals(Args) ->
get_vals(Args, []).
get_vals([], Vals) ->
{lists:reverse(Vals), []};
get_vals(["-" ++ _ | _] = Args, Vals) ->
{lists:reverse(Vals), Args};
get_vals([H | T], Vals) ->
get_vals(T, [H|Vals]).
localize_env(Dir, Env) ->
LogD = filename:join(Dir, "log"),
merge_env([
{sasl, [
{sasl_error_logger, {file, filename:join(LogD, "sasl-error.log")}},
{error_logger_mf_dir, filename:join(LogD, "sasl")}
]},
{setup, [
{home, Dir},
{data_dir, filename:join(Dir, "db")},
{log_dir, LogD}
]}
], Env).
merge_env([{App, Vars}|T], Env) ->
case lists:keyfind(App, 1, Env) of
{_, Vars0} ->
NewVars =
lists:foldl(
fun({K,V}, Acc) ->
lists:keystore(K, 1, Acc, {K, V})
end, Vars0, Vars),
merge_env(T, lists:keyreplace(App, 1, Env, {App, NewVars}));
false ->
merge_env(T, Env ++ [{App, Vars}])
end;
merge_env([], Env) ->
Env.