#!/usr/bin/env bash set -euo pipefail UNAME="$(uname -s)" PROJ_ROOT="$(git rev-parse --show-toplevel)" cd "$PROJ_ROOT" usage() { cat <&2 exit 1 ;; esac shift 1; done case "${PROFILE}" in ce|emqx) PROFILE='emqx' ;; ee|emqx-enterprise) PROFILE='emqx-enterprise' ;; *) echo "Unknown profile $PROFILE" exit 1 ;; esac export PROFILE case "${PROFILE}" in emqx) SCHEMA_MOD='emqx_conf_schema' ;; emqx-enterprise) SCHEMA_MOD='emqx_ee_conf_schema' ;; esac BASE_DIR="_build/dev-run/$PROFILE" export EMQX_ETC_DIR="$BASE_DIR/etc" export EMQX_DATA_DIR="$BASE_DIR/data" export EMQX_LOG_DIR="$BASE_DIR/log" CONFIGS_DIR="$EMQX_DATA_DIR/configs" # Use your cookie so your IDE can connect to it. COOKIE="${EMQX_NODE__COOKIE:-${EMQX_NODE_COOKIE:-$(cat ~/.erlang.cookie || echo 'emqxsecretcookie')}}" mkdir -p "$EMQX_ETC_DIR" "$EMQX_DATA_DIR/patches" "$EMQX_LOG_DIR" "$CONFIGS_DIR" if [ $EKKA_EPMD -eq 1 ]; then EPMD_ARGS='-start_epmd false -epmd_module ekka_epmd' else EPMD_ARGS='' fi ## build compile the profile is it's not compiled yet prepare_erl_libs() { local profile="$1" local libs_dir="_build/${profile}/lib" local erl_libs='' if [ $FORCE_COMPILE -eq 1 ] || [ ! -d "$libs_dir" ]; then make "compile-${PROFILE}" else echo "Running from code in $libs_dir" fi for app in "${libs_dir}"/*; do erl_libs="${erl_libs}:${app}" done export ERL_LIBS="$erl_libs" } ## poorman's mustache templating mustache() { local name="$1" local value="$2" local file="$3" if [[ "$UNAME" == "Darwin" ]]; then sed -i '' "s|{{[[:space:]]*${name}[[:space:]]*}}|${value}|g" "$file" else sed -i "s|{{\s*${name}\s*}}|${value}|g" "$file" fi } ## render the merged boot conf file. ## the merge action is done before the profile is compiled render_hocon_conf() { input="apps/emqx_conf/etc/emqx.conf.all" output="$EMQX_ETC_DIR/emqx.conf" cp "$input" "$output" mustache emqx_default_erlang_cookie "$COOKIE" "$output" mustache platform_data_dir "${EMQX_DATA_DIR}" "$output" mustache platform_log_dir "${EMQX_LOG_DIR}" "$output" mustache platform_etc_dir "${EMQX_ETC_DIR}" "$output" } call_hocon() { local in=("$@") local args='' for arg in "${in[@]}"; do if [ -z "$args" ]; then args="\"$arg\"" else args="$args, \"$arg\"" fi done erl -noshell -eval "{ok, _} = application:ensure_all_started(hocon), ok = hocon_cli:main([$args]), init:stop()." } # Function to generate app.config and vm.args # sets two environment variables CONF_FILE and ARGS_FILE generate_app_conf() { ## timestamp for each generation local NOW_TIME NOW_TIME="$(date +'%Y.%m.%d.%H.%M.%S')" ## this command populates two files: app.