dev 10 KB

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