dev 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. fi
  209. export ERL_LIBS="$erl_libs"
  210. }
  211. ## poorman's mustache templating
  212. mustache() {
  213. local name="$1"
  214. local value="$2"
  215. local file="$3"
  216. if [[ "$UNAME" == "Darwin" ]]; then
  217. sed -i '' "s|{{[[:space:]]*${name}[[:space:]]*}}|${value}|g" "$file"
  218. else
  219. sed -i "s|{{\s*${name}\s*}}|${value}|g" "$file"
  220. fi
  221. }
  222. ## render the merged boot conf file.
  223. ## the merge action is done before the profile is compiled
  224. render_hocon_conf() {
  225. input="apps/emqx_conf/etc/emqx.conf.all"
  226. output="$EMQX_ETC_DIR/emqx.conf"
  227. cp "$input" "$output"
  228. mustache emqx_default_erlang_cookie "$COOKIE" "$output"
  229. mustache platform_data_dir "${EMQX_DATA_DIR}" "$output"
  230. mustache platform_log_dir "${EMQX_LOG_DIR}" "$output"
  231. mustache platform_etc_dir "${EMQX_ETC_DIR}" "$output"
  232. }
  233. ## Make comma separated quoted strings
  234. make_erlang_args() {
  235. local in=("$@")
  236. local args=''
  237. for arg in "${in[@]}"; do
  238. if [ -z "$args" ]; then
  239. args="\"$arg\""
  240. else
  241. args="$args, \"$arg\""
  242. fi
  243. done
  244. echo "$args"
  245. }
  246. call_hocon() {
  247. local args erl_code
  248. args="$(make_erlang_args "$@")"
  249. erl_code="
  250. {ok, _} = application:ensure_all_started(hocon), \
  251. try
  252. mnesia_hook:module_info()
  253. catch _:_->
  254. io:format(standard_error, \"Force setting DB backend to 'mnesia', and 'role' to 'core'~n\", []),
  255. os:putenv(\"EMQX_NODE__DB_BACKEND\", \"mnesia\"),
  256. os:putenv(\"EMQX_NODE__DB_ROLE\", \"core\")
  257. end,
  258. ok = hocon_cli:main([$args]),
  259. init:stop().
  260. "
  261. erl -noshell -eval "$erl_code"
  262. }
  263. # Function to generate app.config and vm.args
  264. # sets two environment variables CONF_FILE and ARGS_FILE
  265. generate_app_conf() {
  266. ## timestamp for each generation
  267. local NOW_TIME
  268. NOW_TIME="$(date +'%Y.%m.%d.%H.%M.%S')"
  269. ## this command populates two files: app.<time>.config and vm.<time>.args
  270. ## NOTE: the generate command merges environment variables to the base config (emqx.conf),
  271. ## but does not include the cluster-override.conf and local-override.conf
  272. ## meaning, certain overrides will not be mapped to app.<time>.config file
  273. call_hocon -v -t "$NOW_TIME" -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf -d "$EMQX_DATA_DIR"/configs generate
  274. ## filenames are per-hocon convention
  275. CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config"
  276. ARGS_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args"
  277. }
  278. # apps/emqx/etc/vm.args.cloud
  279. append_args_file() {
  280. ## ensure a new line at the end
  281. echo '' >> "$ARGS_FILE"
  282. cat <<EOF >> "$ARGS_FILE"
  283. $ERL_NAME_ARG $EMQX_NODE_NAME
  284. -mnesia dir '"$EMQX_DATA_DIR/mnesia/$EMQX_NODE_NAME"'
  285. -stdlib restricted_shell emqx_restricted_shell
  286. +spp true
  287. +A 4
  288. +IOt 4
  289. +SDio 8
  290. -shutdown_time 30000
  291. -pa '$EMQX_DATA_DIR/patches'
  292. -mnesia dump_log_write_threshold 5000
  293. -mnesia dump_log_time_threshold 60000
  294. -os_mon start_disksup false
  295. EOF
  296. }
  297. # copy cert files and acl.conf to etc
  298. copy_other_conf_files() {
  299. cp -r apps/emqx/etc/certs "$EMQX_ETC_DIR"/
  300. cp apps/emqx_auth/etc/acl.conf "$EMQX_ETC_DIR"/
  301. }
  302. is_current_profile_app() {
  303. local app="$1"
  304. case "$app" in
  305. *emqx_telemetry*)
  306. if [ "$PROFILE" = 'emqx-enterprise' ]; then
  307. return 1
  308. else
  309. return 0
  310. fi
  311. ;;
  312. *)
  313. if [ "$PROFILE" = 'emqx' ]; then
  314. if [ -f "$app"/BSL.txt ]; then
  315. return 1
  316. else
  317. return 0
  318. fi
  319. else
  320. return 0
  321. fi
  322. ;;
  323. esac
  324. }
  325. ## apps to load
  326. apps_to_load() {
  327. local apps csl
  328. apps="$(./scripts/find-apps.sh | xargs)"
  329. csl=""
  330. for app in $apps; do
  331. if ! is_current_profile_app "$app"; then
  332. continue
  333. fi
  334. name="$(basename "$app")"
  335. if [ -z "$csl" ]; then
  336. csl="$name"
  337. else
  338. csl="$csl,$name"
  339. fi
  340. done
  341. echo "$csl"
  342. }
  343. boot() {
  344. ## Make erl command aware where to load all the beams
  345. ## this should be done before every erl command
  346. prepare_erl_libs "$PROFILE"
  347. ## make sure copy acl.conf and certs to etc before render_hocon_conf
  348. ## hocon will check rules inside acl.conf.
  349. copy_other_conf_files
  350. render_hocon_conf
  351. generate_app_conf
  352. append_args_file
  353. APPS="$(apps_to_load)"
  354. if [ "${IS_ELIXIR:-}" = "yes" ]; then
  355. BOOT_SEQUENCE='
  356. apps = System.get_env("APPS") |> String.split(",") |> Enum.map(&String.to_atom/1)
  357. Enum.each(apps, &Application.load/1)
  358. IO.inspect(apps, label: :loaded, limit: :infinity)
  359. {:ok, _} = Application.ensure_all_started(:emqx_machine)
  360. '
  361. if [ -n "${EPMD_ARGS:-}" ]; then
  362. EPMD_ARGS_ELIXIR="$EPMD_ARGS"
  363. else
  364. EPMD_ARGS_ELIXIR="-no_op true"
  365. fi
  366. local OTP_VSN USER_MOD_ARG
  367. OTP_VSN=$(./scripts/get-otp-vsn.sh)
  368. case "$OTP_VSN" in
  369. 25*)
  370. USER_MOD_ARG='-user Elixir.IEx.CLI'
  371. ;;
  372. *)
  373. USER_MOD_ARG='-user elixir'
  374. ;;
  375. esac
  376. # shellcheck disable=SC2086
  377. env APPS="$APPS" iex \
  378. -$ERL_NAME_ARG "$EMQX_NODE_NAME" \
  379. --erl "$EPMD_ARGS_ELIXIR" \
  380. --erl "$USER_MOD_ARG" \
  381. --erl '-proto_dist ekka' \
  382. --vm-args "$ARGS_FILE" \
  383. --erl-config "$CONF_FILE" \
  384. -e "$BOOT_SEQUENCE"
  385. else
  386. BOOT_SEQUENCE="
  387. Apps=[${APPS}],
  388. ok=lists:foreach(fun application:load/1, Apps),
  389. io:format(user, \"~nLoaded: ~p~n\", [Apps]),
  390. {ok, _} = application:ensure_all_started(emqx_machine).
  391. "
  392. # shellcheck disable=SC2086
  393. erl $ERL_NAME_ARG "$EMQX_NODE_NAME" \
  394. $EPMD_ARGS \
  395. -proto_dist ekka \
  396. -args_file "$ARGS_FILE" \
  397. -config "$CONF_FILE" \
  398. -s emqx_restricted_shell set_prompt_func \
  399. -eval "$BOOT_SEQUENCE"
  400. fi
  401. }
  402. # Generate a random id
  403. gen_tmp_node_name() {
  404. local rnd
  405. rnd="$(od -t u -N 4 /dev/urandom | head -n1 | awk '{print $2 % 1000}')"
  406. if [ $SHORTNAMES -eq 1 ]; then
  407. echo "remsh${rnd}"
  408. else
  409. echo "remsh${rnd}-${EMQX_NODE_NAME}"
  410. fi
  411. }
  412. remsh() {
  413. local tmpnode
  414. tmpnode="$(gen_tmp_node_name)"
  415. # shellcheck disable=SC2086
  416. erl $ERL_NAME_ARG "$tmpnode" \
  417. -hidden \
  418. -setcookie "$COOKIE" \
  419. -remsh "$EMQX_NODE_NAME" \
  420. $EPMD_ARGS
  421. }
  422. # evaluate erlang expression in remsh node
  423. eval_remsh_erl() {
  424. local tmpnode erl_code
  425. tmpnode="$(gen_tmp_node_name)"
  426. erl_code="$1"
  427. # shellcheck disable=SC2086 # need to expand EMQD_ARGS
  428. erl $ERL_NAME_ARG "$tmpnode" -setcookie "$COOKIE" -hidden -noshell $EPMD_ARGS -eval "$erl_code" 2>&1
  429. }
  430. ctl() {
  431. if [ -z "${PASSTHROUGH_ARGS:-}" ]; then
  432. logerr "Need at least one argument for ctl command"
  433. logerr "e.g. $0 ctl -- help"
  434. exit 1
  435. fi
  436. local args rpc_code output result
  437. args="$(make_erlang_args "${PASSTHROUGH_ARGS[@]}")"
  438. rpc_code="
  439. case rpc:call('$EMQX_NODE_NAME', emqx_ctl, run_command, [[$args]]) of
  440. ok ->
  441. init:stop(0);
  442. Error ->
  443. io:format(\"~p~n\", [Error]),
  444. init:stop(1)
  445. end"
  446. set +e
  447. output="$(eval_remsh_erl "$rpc_code")"
  448. result=$?
  449. if [ $result -eq 0 ]; then
  450. echo -e "$output"
  451. else
  452. logerr "$output"
  453. fi
  454. exit $result
  455. }
  456. case "$COMMAND" in
  457. run)
  458. boot
  459. ;;
  460. remsh)
  461. remsh
  462. ;;
  463. ctl)
  464. ctl
  465. ;;
  466. eval)
  467. PASSTHROUGH_ARGS=('eval_erl' "${PASSTHROUGH_ARGS[@]}")
  468. ctl
  469. ;;
  470. esac