dev 10.0 KB

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