dev 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. args="$(make_erlang_args "$@")"
  186. erl -noshell \
  187. -eval "{ok, _} = application:ensure_all_started(hocon), \
  188. ok = hocon_cli:main([$args]), init:stop()."
  189. }
  190. # Function to generate app.config and vm.args
  191. # sets two environment variables CONF_FILE and ARGS_FILE
  192. generate_app_conf() {
  193. ## timestamp for each generation
  194. local NOW_TIME
  195. NOW_TIME="$(date +'%Y.%m.%d.%H.%M.%S')"
  196. ## this command populates two files: app.<time>.config and vm.<time>.args
  197. ## NOTE: the generate command merges environment variables to the base config (emqx.conf),
  198. ## but does not include the cluster-override.conf and local-override.conf
  199. ## meaning, certain overrides will not be mapped to app.<time>.config file
  200. call_hocon -v -t "$NOW_TIME" -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf -d "$EMQX_DATA_DIR"/configs generate
  201. ## filenames are per-hocon convention
  202. CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config"
  203. ARGS_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args"
  204. }
  205. # apps/emqx/etc/vm.args.cloud
  206. append_args_file() {
  207. ## ensure a new line at the end
  208. echo '' >> "$ARGS_FILE"
  209. cat <<EOF >> "$ARGS_FILE"
  210. -name $EMQX_NODE_NAME
  211. -mnesia dir '"$EMQX_DATA_DIR/mnesia/$EMQX_NODE_NAME"'
  212. -stdlib restricted_shell emqx_restricted_shell
  213. +spp true
  214. +A 4
  215. +IOt 4
  216. +SDio 8
  217. -shutdown_time 30000
  218. -pa '"$EMQX_DATA_DIR/patches"'
  219. -mnesia dump_log_write_threshold 5000
  220. -mnesia dump_log_time_threshold 60000
  221. -os_mon start_disksup false
  222. EOF
  223. }
  224. # copy cert files and acl.conf to etc
  225. copy_other_conf_files() {
  226. cp -r apps/emqx/etc/certs "$EMQX_ETC_DIR"/
  227. cp apps/emqx_authz/etc/acl.conf "$EMQX_ETC_DIR"/
  228. }
  229. is_current_profile_app() {
  230. local app="$1"
  231. case "$app" in
  232. lib-ee*)
  233. if [ "$PROFILE" = 'emqx-enterprise' ]; then
  234. return 0
  235. else
  236. return 1
  237. fi
  238. ;;
  239. *emqx_telemetry*)
  240. if [ "$PROFILE" = 'emqx-enterprise' ]; then
  241. return 1
  242. else
  243. return 0
  244. fi
  245. ;;
  246. *)
  247. if [ "$PROFILE" = 'emqx' ]; then
  248. if [ -f "$app"/BSL.txt ]; then
  249. return 1
  250. else
  251. return 0
  252. fi
  253. else
  254. return 0
  255. fi
  256. ;;
  257. esac
  258. }
  259. ## apps to load
  260. apps_to_load() {
  261. local apps csl
  262. apps="$(./scripts/find-apps.sh | xargs)"
  263. csl=""
  264. for app in $apps; do
  265. if ! is_current_profile_app "$app"; then
  266. continue
  267. fi
  268. name="$(basename "$app")"
  269. if [ -z "$csl" ]; then
  270. csl="$name"
  271. else
  272. csl="$csl,$name"
  273. fi
  274. done
  275. echo "$csl"
  276. }
  277. boot() {
  278. ## Make erl command aware where to load all the beams
  279. ## this should be done before every erl command
  280. prepare_erl_libs "$PROFILE"
  281. render_hocon_conf
  282. generate_app_conf
  283. append_args_file
  284. copy_other_conf_files
  285. APPS="$(apps_to_load)"
  286. BOOT_SEQUENCE="
  287. Apps=[${APPS}],
  288. ok=lists:foreach(fun application:load/1, Apps),
  289. io:format(user, \"~nLoaded ~p apps~n\", [length(Apps)]),
  290. {ok, _} = application:ensure_all_started(emqx_machine).
  291. "
  292. # shellcheck disable=SC2086
  293. erl -name "$EMQX_NODE_NAME" \
  294. $EPMD_ARGS \
  295. -proto_dist ekka \
  296. -args_file "$ARGS_FILE" \
  297. -config "$CONF_FILE" \
  298. -s emqx_restricted_shell set_prompt_func \
  299. -eval "$BOOT_SEQUENCE"
  300. }
  301. # Generate a random id
  302. gen_tmp_node_name() {
  303. local rnd
  304. rnd="$(od -t u -N 4 /dev/urandom | head -n1 | awk '{print $2 % 1000}')"
  305. echo "remsh${rnd}-$EMQX_NODE_NAME}"
  306. }
  307. remsh() {
  308. local tmpnode
  309. tmpnode="$(gen_tmp_node_name)"
  310. # shellcheck disable=SC2086
  311. erl -name "$tmpnode" \
  312. -hidden \
  313. -setcookie "$COOKIE" \
  314. -remsh "$EMQX_NODE_NAME" \
  315. $EPMD_ARGS
  316. }
  317. ctl() {
  318. if [ -z "${PASSTHROUGH_ARGS:-}" ]; then
  319. logerr "Need at least one argument for ctl command"
  320. logerr "e.g. $0 ctl -- help"
  321. exit 1
  322. fi
  323. local tmpnode args rpc_code output result
  324. tmpnode="$(gen_tmp_node_name)"
  325. args="$(make_erlang_args "${PASSTHROUGH_ARGS[@]}")"
  326. rpc_code="
  327. case rpc:call('$EMQX_NODE_NAME', emqx_ctl, run_command, [[$args]]) of
  328. ok ->
  329. init:stop(0);
  330. Error ->
  331. io:format(\"~p~n\", [Error]),
  332. init:stop(1)
  333. end"
  334. set +e
  335. # shellcheck disable=SC2086
  336. output="$(erl -name "$tmpnode" -setcookie "$COOKIE" -hidden -noshell $EPMD_ARGS -eval "$rpc_code" 2>&1)"
  337. result=$?
  338. if [ $result -eq 0 ]; then
  339. echo -e "$output"
  340. else
  341. logerr "$output"
  342. fi
  343. exit $result
  344. }
  345. case "$COMMAND" in
  346. run)
  347. boot
  348. ;;
  349. remsh)
  350. remsh
  351. ;;
  352. ctl)
  353. ctl
  354. ;;
  355. esac