dev 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. if [ -n "${DEBUG:-}" ]; then
  4. set -x
  5. fi
  6. UNAME="$(uname -s)"
  7. PROJ_ROOT="$(git rev-parse --show-toplevel)"
  8. cd "$PROJ_ROOT"
  9. logerr() {
  10. if [ "${TERM:-dumb}" = dumb ]; then
  11. echo -e "ERROR: $*" 1>&2
  12. else
  13. echo -e "$(tput setaf 1)ERROR: $*$(tput sgr0)" 1>&2
  14. fi
  15. }
  16. usage() {
  17. cat <<EOF
  18. Run EMQX without building a release (which takes longer time).
  19. Node state is stored in '_build/dev-run/$PROFILE'.
  20. The node is started in interactive mode without a boot file.
  21. USAGE: $0 [COMMAND] [OPTION[]
  22. COMMANDS:
  23. help: Print this usage info.
  24. run: Default command.
  25. remsh: Attach to running node's remote console.
  26. Target node name is to be specified with -n|--name,
  27. otherwise defaults to 'emqx@127.0.0.1'.
  28. ctl: Equivalent to 'emqx ctl'.
  29. ctl command arguments should be passed after '--'
  30. e.g. $0 ctl -- help
  31. eval: Evaluate an Erlang expression
  32. OPTIONS:
  33. -h|--help: Print this usage info.
  34. -p|--profile: emqx | emqx-enterprise, defaults to 'PROFILE' env.
  35. -c|--compile: Force recompile, otherwise starts with the already built libs
  36. in '_build/\$PROFILE/lib/'.
  37. -e|--ekka-epmd: Force to use ekka_epmd.
  38. -n|--name: Node name, defaults to \$EMQX_NODE__NAME or the \$EMQX_NODE_NAME env.
  39. -s|--shortnames: Use shortnames for erlang node name (ie. use -sname parameter).
  40. ENVIRONMENT VARIABLES:
  41. PROFILE: Overridden by '-p|--profile' option, defaults to 'emqx'.
  42. EMQX_NODE_NAME: Overridden by '-n|--name' or '-r|--remsh' option.
  43. The node name of the EMQX node. Default to emqx@127.0.0.1'.
  44. EMQX_NODE_COOKIE: Erlang cookie, defaults to ~/.erlang.cookie
  45. EOF
  46. }
  47. if [ -n "${DEBUG:-}" ]; then
  48. set -x
  49. fi
  50. export HOCON_ENV_OVERRIDE_PREFIX='EMQX_'
  51. export EMQX_LOG__FILE__DEFAULT__ENABLE='false'
  52. export EMQX_LOG__CONSOLE__ENABLE='true'
  53. SYSTEM="$(./scripts/get-distro.sh)"
  54. EMQX_NODE_NAME="${EMQX_NODE__NAME:-${EMQX_NODE_NAME:-emqx@127.0.0.1}}"
  55. PROFILE="${PROFILE:-emqx}"
  56. FORCE_COMPILE=0
  57. # Do not start using ekka epmd by default, so your IDE can connect to it
  58. EKKA_EPMD=0
  59. SHORTNAMES=0
  60. COMMAND='run'
  61. case "${1:-novalue}" in
  62. novalue)
  63. ;;
  64. run)
  65. shift
  66. ;;
  67. remsh)
  68. COMMAND='remsh'
  69. shift
  70. ;;
  71. ctl)
  72. COMMAND='ctl'
  73. shift
  74. ;;
  75. eval)
  76. COMMAND='eval'
  77. shift
  78. ;;
  79. help)
  80. usage
  81. exit 0
  82. ;;
  83. -*)
  84. ;;
  85. esac
  86. while [ "$#" -gt 0 ]; do
  87. case $1 in
  88. -h|--help)
  89. usage
  90. exit 0
  91. ;;
  92. -n|--name)
  93. EMQX_NODE_NAME="$2"
  94. shift 1
  95. ;;
  96. -p|--profile)
  97. PROFILE="${2}"
  98. shift 1;
  99. ;;
  100. -c|--compile)
  101. FORCE_COMPILE=1
  102. ;;
  103. -e|--ekka-epmd)
  104. EKKA_EPMD=1
  105. ;;
  106. -s|--shortnames)
  107. SHORTNAMES=1
  108. ;;
  109. --)
  110. shift
  111. PASSTHROUGH_ARGS=("$@")
  112. break
  113. ;;
  114. *)
  115. logerr "Unknown argument $1"
  116. usage
  117. exit 1
  118. ;;
  119. esac
  120. shift 1;
  121. done
  122. case "${PROFILE}" in
  123. ce|emqx)
  124. PROFILE='emqx'
  125. ;;
  126. ee|emqx-enterprise)
  127. PROFILE='emqx-enterprise'
  128. ;;
  129. ce-ex|emqx-elixir)
  130. export IS_ELIXIR=yes
  131. PROFILE='emqx'
  132. ;;
  133. ee-ex|emqx-enterprise-elixir)
  134. export IS_ELIXIR=yes
  135. PROFILE='emqx-enterprise'
  136. ;;
  137. *)
  138. echo "Unknown profile $PROFILE"
  139. exit 1
  140. ;;
  141. esac
  142. export PROFILE
  143. case "${PROFILE}" in
  144. emqx|emqx-elixir)
  145. export SCHEMA_MOD='emqx_conf_schema'
  146. ;;
  147. emqx-enterprise|emqx-enterprise-elixir)
  148. export SCHEMA_MOD='emqx_enterprise_schema'
  149. ;;
  150. esac
  151. BASE_DIR="_build/dev-run/$PROFILE"
  152. export EMQX_ETC_DIR="$BASE_DIR/etc"
  153. export EMQX_DATA_DIR="$BASE_DIR/data"
  154. export EMQX_LOG_DIR="$BASE_DIR/log"
  155. export EMQX_PLUGINS__INSTALL_DIR="${EMQX_PLUGINS__INSTALL_DIR:-$BASE_DIR/plugins}"
  156. CONFIGS_DIR="$EMQX_DATA_DIR/configs"
  157. # Use your cookie so your IDE can connect to it.
  158. COOKIE="${EMQX_NODE__COOKIE:-${EMQX_NODE_COOKIE:-$(cat ~/.erlang.cookie 2>/dev/null || echo 'emqxsecretcookie')}}"
  159. mkdir -p "$EMQX_ETC_DIR" "$EMQX_DATA_DIR/patches" "$EMQX_DATA_DIR/plugins" "$EMQX_DATA_DIR/certs" "$EMQX_LOG_DIR" "$CONFIGS_DIR"
  160. if [ $EKKA_EPMD -eq 1 ]; then
  161. EPMD_ARGS='-start_epmd false -epmd_module ekka_epmd'
  162. else
  163. EPMD_ARGS=''
  164. fi
  165. if [ $SHORTNAMES -eq 1 ]; then
  166. ERL_NAME_ARG='-sname'
  167. else
  168. ERL_NAME_ARG='-name'
  169. fi
  170. ## build compile the profile is it's not compiled yet
  171. prepare_erl_libs() {
  172. local profile="$1"
  173. local libs_dir="_build/${profile}/lib"
  174. local erl_libs="${ERL_LIBS:-}"
  175. local sep
  176. if [ "${SYSTEM}" = 'windows' ]; then
  177. sep=';'
  178. else
  179. sep=':'
  180. fi
  181. if [ $FORCE_COMPILE -eq 1 ] || [ ! -d "$libs_dir" ]; then
  182. make "compile-${PROFILE}"
  183. else
  184. echo "Running from code in $libs_dir"
  185. fi
  186. for app in "${libs_dir}"/*; do
  187. if [ -n "$erl_libs" ]; then
  188. erl_libs="${erl_libs}${sep}${app}"
  189. else
  190. erl_libs="${app}"
  191. fi
  192. done
  193. if [ "${IS_ELIXIR:-}" = "yes" ]; then
  194. local elixir_libs_root_dir
  195. elixir_libs_root_dir=$(realpath "$(elixir -e ':code.lib_dir(:elixir) |> IO.puts()')"/..)
  196. for app in "${elixir_libs_root_dir}"/*; do
  197. if [ -n "$erl_libs" ]; then
  198. erl_libs="${erl_libs}${sep}${app}"
  199. else
  200. erl_libs="${app}"
  201. fi
  202. done
  203. fi
  204. if [ -e "_build/${profile}/checkouts" ]; then
  205. for app in "_build/${profile}/checkouts"/*; do
  206. erl_libs="${erl_libs}${sep}${app}"
  207. done
  208. else
  209. echo "no checkouts"
  210. fi
  211. export ERL_LIBS="$erl_libs"
  212. }
  213. ## poorman's mustache templating
  214. mustache() {
  215. local name="$1"
  216. local value="$2"
  217. local file="$3"
  218. if [[ "$UNAME" == "Darwin" ]]; then
  219. sed -i '' "s|{{[[:space:]]*${name}[[:space:]]*}}|${value}|g" "$file"
  220. else
  221. sed -i "s|{{\s*${name}\s*}}|${value}|g" "$file"
  222. fi
  223. }
  224. ## render the merged boot conf file.
  225. ## the merge action is done before the profile is compiled
  226. render_hocon_conf() {
  227. input="apps/emqx_conf/etc/emqx.conf.all"
  228. output="$EMQX_ETC_DIR/emqx.conf"
  229. cp "$input" "$output"
  230. mustache emqx_default_erlang_cookie "$COOKIE" "$output"
  231. mustache platform_data_dir "${EMQX_DATA_DIR}" "$output"
  232. mustache platform_log_dir "${EMQX_LOG_DIR}" "$output"
  233. mustache platform_etc_dir "${EMQX_ETC_DIR}" "$output"
  234. }
  235. ## Make comma separated quoted strings
  236. make_erlang_args() {
  237. local in=("$@")
  238. local args=''
  239. for arg in "${in[@]}"; do
  240. if [ -z "$args" ]; then
  241. args="\"$arg\""
  242. else
  243. args="$args, \"$arg\""
  244. fi
  245. done
  246. echo "$args"
  247. }
  248. call_hocon() {
  249. local args erl_code
  250. args="$(make_erlang_args "$@")"
  251. erl_code="
  252. {ok, _} = application:ensure_all_started(hocon), \
  253. try
  254. mnesia_hook:module_info()
  255. catch _:_->
  256. io:format(standard_error, \"Force setting DB backend to 'mnesia', and 'role' to 'core'~n\", []),
  257. os:putenv(\"EMQX_NODE__DB_BACKEND\", \"mnesia\"),
  258. os:putenv(\"EMQX_NODE__DB_ROLE\", \"core\")
  259. end,
  260. ok = hocon_cli:main([$args]),
  261. init:stop().
  262. "
  263. erl -noshell -eval "$erl_code"
  264. }
  265. # Function to generate app.config and vm.args
  266. # sets two environment variables CONF_FILE and ARGS_FILE
  267. generate_app_conf() {
  268. ## timestamp for each generation
  269. local NOW_TIME
  270. NOW_TIME="$(date +'%Y.%m.%d.%H.%M.%S')"
  271. ## this command populates two files: app.<time>.config and vm.<time>.args
  272. ## NOTE: the generate command merges environment variables to the base config (emqx.conf),
  273. ## but does not include the cluster-override.conf and local-override.conf
  274. ## meaning, certain overrides will not be mapped to app.<time>.config file
  275. call_hocon -v -t "$NOW_TIME" -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf -d "$EMQX_DATA_DIR"/configs generate
  276. ## filenames are per-hocon convention
  277. CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config"
  278. ARGS_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args"
  279. }
  280. # apps/emqx/etc/vm.args.cloud
  281. append_args_file() {
  282. ## ensure a new line at the end
  283. echo '' >> "$ARGS_FILE"
  284. cat <<EOF >> "$ARGS_FILE"
  285. $ERL_NAME_ARG $EMQX_NODE_NAME
  286. -mnesia dir '"$EMQX_DATA_DIR/mnesia/$EMQX_NODE_NAME"'
  287. -stdlib restricted_shell emqx_restricted_shell
  288. +spp true
  289. +A 4
  290. +IOt 4
  291. +SDio 8
  292. -shutdown_time 30000
  293. -pa '$EMQX_DATA_DIR/patches'
  294. -mnesia dump_log_write_threshold 5000
  295. -mnesia dump_log_time_threshold 60000
  296. -os_mon start_disksup false
  297. EOF
  298. }
  299. # copy cert files and acl.conf to etc
  300. copy_other_conf_files() {
  301. cp -r apps/emqx/etc/certs "$EMQX_ETC_DIR"/
  302. cp apps/emqx_auth/etc/acl.conf "$EMQX_ETC_DIR"/
  303. }
  304. is_current_profile_app() {
  305. local app="$1"
  306. case "$app" in
  307. lib-ee*)
  308. if [ "$PROFILE" = 'emqx-enterprise' ]; then
  309. return 0
  310. else
  311. return 1
  312. fi
  313. ;;
  314. *emqx_telemetry*)
  315. if [ "$PROFILE" = 'emqx-enterprise' ]; then
  316. return 1
  317. else
  318. return 0
  319. fi
  320. ;;
  321. *)
  322. if [ "$PROFILE" = 'emqx' ]; then
  323. if [ -f "$app"/BSL.txt ]; then
  324. return 1
  325. else
  326. return 0
  327. fi
  328. else
  329. return 0
  330. fi
  331. ;;
  332. esac
  333. }
  334. ## apps to load
  335. apps_to_load() {
  336. local apps csl
  337. apps="$(./scripts/find-apps.sh | xargs)"
  338. csl=""
  339. for app in $apps; do
  340. if ! is_current_profile_app "$app"; then
  341. continue
  342. fi
  343. name="$(basename "$app")"
  344. if [ -z "$csl" ]; then
  345. csl="$name"
  346. else
  347. csl="$csl,$name"
  348. fi
  349. done
  350. echo "$csl"
  351. }
  352. boot() {
  353. ## Make erl command aware where to load all the beams
  354. ## this should be done before every erl command
  355. prepare_erl_libs "$PROFILE"
  356. ## make sure copy acl.conf and certs to etc before render_hocon_conf
  357. ## hocon will check rules inside acl.conf.
  358. copy_other_conf_files
  359. render_hocon_conf
  360. generate_app_conf
  361. append_args_file
  362. APPS="$(apps_to_load)"
  363. if [ "${IS_ELIXIR:-}" = "yes" ]; then
  364. BOOT_SEQUENCE='
  365. apps = System.get_env("APPS") |> String.split(",") |> Enum.map(&String.to_atom/1)
  366. Enum.each(apps, &Application.load/1)
  367. IO.inspect(apps, label: :loaded, limit: :infinity)
  368. {:ok, _} = Application.ensure_all_started(:emqx_machine)
  369. '
  370. if [ -n "${EPMD_ARGS:-}" ]; then
  371. EPMD_ARGS_ELIXIR="$EPMD_ARGS"
  372. else
  373. EPMD_ARGS_ELIXIR="-no_op true"
  374. fi
  375. # shellcheck disable=SC2086
  376. env APPS="$APPS" iex \
  377. -$ERL_NAME_ARG "$EMQX_NODE_NAME" \
  378. --erl "$EPMD_ARGS_ELIXIR" \
  379. --erl '-user Elixir.IEx.CLI' \
  380. --erl '-proto_dist ekka' \
  381. --vm-args "$ARGS_FILE" \
  382. --erl-config "$CONF_FILE" \
  383. -e "$BOOT_SEQUENCE"
  384. else
  385. BOOT_SEQUENCE="
  386. Apps=[${APPS}],
  387. ok=lists:foreach(fun application:load/1, Apps),
  388. io:format(user, \"~nLoaded: ~p~n\", [Apps]),
  389. {ok, _} = application:ensure_all_started(emqx_machine).
  390. "
  391. # shellcheck disable=SC2086
  392. erl $ERL_NAME_ARG "$EMQX_NODE_NAME" \
  393. $EPMD_ARGS \
  394. -proto_dist ekka \
  395. -args_file "$ARGS_FILE" \
  396. -config "$CONF_FILE" \
  397. -s emqx_restricted_shell set_prompt_func \
  398. -eval "$BOOT_SEQUENCE"
  399. fi
  400. }
  401. # Generate a random id
  402. gen_tmp_node_name() {
  403. local rnd
  404. rnd="$(od -t u -N 4 /dev/urandom | head -n1 | awk '{print $2 % 1000}')"
  405. if [ $SHORTNAMES -eq 1 ]; then
  406. echo "remsh${rnd}"
  407. else
  408. echo "remsh${rnd}-${EMQX_NODE_NAME}"
  409. fi
  410. }
  411. remsh() {
  412. local tmpnode
  413. tmpnode="$(gen_tmp_node_name)"
  414. # shellcheck disable=SC2086
  415. erl $ERL_NAME_ARG "$tmpnode" \
  416. -hidden \
  417. -setcookie "$COOKIE" \
  418. -remsh "$EMQX_NODE_NAME" \
  419. $EPMD_ARGS
  420. }
  421. # evaluate erlang expression in remsh node
  422. eval_remsh_erl() {
  423. local tmpnode erl_code
  424. tmpnode="$(gen_tmp_node_name)"
  425. erl_code="$1"
  426. # shellcheck disable=SC2086 # need to expand EMQD_ARGS
  427. erl $ERL_NAME_ARG "$tmpnode" -setcookie "$COOKIE" -hidden -noshell $EPMD_ARGS -eval "$erl_code" 2>&1
  428. }
  429. ctl() {
  430. if [ -z "${PASSTHROUGH_ARGS:-}" ]; then
  431. logerr "Need at least one argument for ctl command"
  432. logerr "e.g. $0 ctl -- help"
  433. exit 1
  434. fi
  435. local args rpc_code output result
  436. args="$(make_erlang_args "${PASSTHROUGH_ARGS[@]}")"
  437. rpc_code="
  438. case rpc:call('$EMQX_NODE_NAME', emqx_ctl, run_command, [[$args]]) of
  439. ok ->
  440. init:stop(0);
  441. Error ->
  442. io:format(\"~p~n\", [Error]),
  443. init:stop(1)
  444. end"
  445. set +e
  446. output="$(eval_remsh_erl "$rpc_code")"
  447. result=$?
  448. if [ $result -eq 0 ]; then
  449. echo -e "$output"
  450. else
  451. logerr "$output"
  452. fi
  453. exit $result
  454. }
  455. case "$COMMAND" in
  456. run)
  457. boot
  458. ;;
  459. remsh)
  460. remsh
  461. ;;
  462. ctl)
  463. ctl
  464. ;;
  465. eval)
  466. PASSTHROUGH_ARGS=('eval_erl' "${PASSTHROUGH_ARGS[@]}")
  467. ctl
  468. ;;
  469. esac