dev 14 KB

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