dev 12 KB

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