-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev
executable file
·303 lines (270 loc) · 7.66 KB
/
dev
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
#!/usr/bin/env bash
set -euo pipefail
PROJ_ROOT="$(git rev-parse --show-toplevel)"
cd "$PROJ_ROOT"
usage() {
cat <<EOF
Run EMQX without building a release (which takes longer time).
Node state is stored in '_build/dev-run/$PROFILE'.
The node is started in interactive mode without a boot file.
USAGE: $0 [OPTION]
OPTIONS:
-h|--help: Print this help usage info.
-p|--profile: emqx | emqx-enterprise, defaults to 'PROFILE' env.
-c|--compile: Force recompile, otherwise starts with the already built libs
in '_build/\$PROFILE/lib/'.
-e|--ekka-epmd: Force to use ekka_epmd.
-n|--name: Node name, defaults to \$EMQX_NODE_NAME env.
-r|--remsh [NAME]: Attach to running node's remote console.
ENVIRONMENT VARIABLES:
PROFILE: Overriden by '-p|--profile' option, defaults to 'emqx'.
EMQX_NODE_NAME: Overriden by '-n|--name' or '-r|--remsh' option.
The node name of the EMQX node. Default to [email protected]'.
EMQX_NODE_COOKIE: Erlang cookie, defaults to ~/.erlang.cookie
EOF
}
if [ -n "${DEBUG:-}" ]; then
set -x
fi
export HOCON_ENV_OVERRIDE_PREFIX='EMQX_'
EMQX_NODE_NAME="${EMQX_NODE_NAME:[email protected]}"
PROFILE="${PROFILE:-emqx}"
FORCE_COMPILE=0
# Do not start using ekka epmd by default, so your IDE can connect to it
EKKA_EPMD=0
REMSH=0
while [ "$#" -gt 0 ]; do
case $1 in
-h|--help)
usage
exit 0
;;
-n|--name)
EMQX_NODE_NAME="$2"
shift 1
;;
-r|--remsh)
REMSH=1
if [[ $2 == *@* ]]; then
EMQX_NODE_NAME="$2"
shift 1
fi
;;
-p|--profile)
PROFILE="${2}"
shift 1;
;;
-c|--compile)
FORCE_COMPILE=1
;;
-e|--ekka-epmd)
EKKA_EPMD=1
;;
*)
echo "Unknown argument $1" >&2
exit 1
;;
esac
shift 1;
done
case "${PROFILE}" in
ce|emqx)
PROFILE='emqx'
;;
ee|emqx-enterprise)
PROFILE='emqx-enterprise'
;;
*)
echo "Unknown profile $PROFILE"
exit 1
;;
esac
export PROFILE
case "${PROFILE}" in
emqx)
SCHEMA_MOD='emqx_conf_schema'
;;
emqx-enterprise)
SCHEMA_MOD='emqx_ee_conf_schema'
;;
esac
BASE_DIR="_build/dev-run/$PROFILE"
export EMQX_ETC_DIR="$BASE_DIR/etc"
export EMQX_DATA_DIR="$BASE_DIR/data"
export EMQX_LOG_DIR="$BASE_DIR/log"
CONFIGS_DIR="$EMQX_DATA_DIR/configs"
# Use your cookie so your IDE can connect to it.
COOKIE="${EMQX_NODE__COOKIE:-${EMQX_NODE_COOKIE:-$(cat ~/.erlang.cookie || echo 'emqxsecretcookie')}}"
mkdir -p "$EMQX_ETC_DIR" "$EMQX_DATA_DIR/patches" "$EMQX_LOG_DIR" "$CONFIGS_DIR"
if [ $EKKA_EPMD -eq 1 ]; then
EPMD_ARGS='-start_epmd false -epmd_module ekka_epmd'
else
EPMD_ARGS=''
fi
## build compile the profile is it's not compiled yet
prepare_erl_libs() {
local profile="$1"
local libs_dir="_build/${profile}/lib"
local erl_libs=''
if [ $FORCE_COMPILE -eq 1 ] || [ ! -d "$libs_dir" ]; then
make "compile-${PROFILE}"
else
echo "Running from code in $libs_dir"
fi
for app in "${libs_dir}"/*; do
erl_libs="${erl_libs}:${app}"
done
export ERL_LIBS="$erl_libs"
}
## poorman's mustache templating
mustache() {
local name="$1"
local value="$2"
local file="$3"
sed -i "s|{{\s*${name}\s*}}|${value}|g" "$file"
}
## render the merged boot conf file.
## the merge action is done before the profile is compiled
render_hocon_conf() {
input="apps/emqx_conf/etc/emqx.conf.all"
output="$EMQX_ETC_DIR/emqx.conf"
cp "$input" "$output"
mustache emqx_default_erlang_cookie "$COOKIE" "$output"
mustache platform_data_dir "${EMQX_DATA_DIR}" "$output"
mustache platform_log_dir "${EMQX_LOG_DIR}" "$output"
mustache platform_etc_dir "${EMQX_ETC_DIR}" "$output"
}
call_hocon() {
local in=("$@")
local args=''
for arg in "${in[@]}"; do
if [ -z "$args" ]; then
args="\"$arg\""
else
args="$args, \"$arg\""
fi
done
erl -noshell -eval "{ok, _} = application:ensure_all_started(hocon), ok = hocon_cli:main([$args]), init:stop()."
}
# Function to generate app.config and vm.args
# sets two environment variables CONF_FILE and ARGS_FILE
generate_app_conf() {
## timestamp for each generation
local NOW_TIME
NOW_TIME="$(date +'%Y.%m.%d.%H.%M.%S')"
## this command populates two files: app.<time>.config and vm.<time>.args
## NOTE: the generate command merges environment variables to the base config (emqx.conf),
## but does not include the cluster-override.conf and local-override.conf
## meaning, certain overrides will not be mapped to app.<time>.config file
call_hocon -v -t "$NOW_TIME" -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf -d "$EMQX_DATA_DIR"/configs generate
## filenames are per-hocon convention
CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config"
ARGS_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args"
}
# apps/emqx/etc/vm.args.cloud
append_args_file() {
## ensure a new line at the end
echo '' >> "$ARGS_FILE"
cat <<EOF >> "$ARGS_FILE"
-name $EMQX_NODE_NAME
-mnesia dir '"$EMQX_DATA_DIR/mnesia/$EMQX_NODE_NAME"'
-stdlib restricted_shell emqx_restricted_shell
+spp true
+A 4
+IOt 4
+SDio 8
-shutdown_time 30000
-pa '"$EMQX_DATA_DIR/patches"'
-mnesia dump_log_write_threshold 5000
-mnesia dump_log_time_threshold 60000
-os_mon start_disksup false
EOF
}
# copy cert files and acl.conf to etc
copy_other_conf_files() {
cp -r apps/emqx/etc/certs "$EMQX_ETC_DIR"/
cp apps/emqx_authz/etc/acl.conf "$EMQX_ETC_DIR"/
}
is_current_profile_app() {
local app="$1"
case "$app" in
lib-ee*)
if [ "$PROFILE" = 'emqx-enterprise' ]; then
return 0
else
return 1
fi
;;
*)
if [ "$PROFILE" = 'emqx' ]; then
if [ -f "$app"/BSL.txt ]; then
return 1
else
return 0
fi
else
return 0
fi
;;
esac
}
## apps to load
apps_to_load() {
local apps csl
apps="$(./scripts/find-apps.sh | xargs)"
csl=""
for app in $apps; do
if ! is_current_profile_app "$app"; then
continue
fi
name="$(basename "$app")"
if [ -z "$csl" ]; then
csl="$name"
else
csl="$csl,$name"
fi
done
echo "$csl"
}
boot() {
## Make erl command aware where to load all the beams
## this should be done before every erl command
prepare_erl_libs "$PROFILE"
render_hocon_conf
generate_app_conf
append_args_file
copy_other_conf_files
APPS="$(apps_to_load)"
BOOT_SEQUENCE="
Apps=[${APPS}],
ok=lists:foreach(fun application:load/1, Apps),
io:format(user, \"~nLoaded ~p apps~n\", [length(Apps)]),
application:ensure_all_started(emqx_machine).
"
# shellcheck disable=SC2086
erl -name "$EMQX_NODE_NAME" \
$EPMD_ARGS \
-proto_dist ekka \
-args_file "$ARGS_FILE" \
-config "$CONF_FILE" \
-s emqx_restricted_shell set_prompt_func \
-eval "$BOOT_SEQUENCE"
}
# Generate a random id
gen_node_id() {
od -t u -N 4 /dev/urandom | head -n1 | awk '{print $2 % 1000}'
}
remsh() {
id="remsh$(gen_node_id)-${EMQX_NODE_NAME}"
# shellcheck disable=SC2086
erl -name "$id" \
-setcookie "$COOKIE" \
-hidden \
-remsh "$EMQX_NODE_NAME" \
$EPMD_ARGS
}
if [ $REMSH -eq 0 ]; then
boot
else
remsh
fi