dev 13 KB

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