one-more-emqx-ee.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env bash
  2. # shellcheck disable=2090
  3. ###############
  4. ## args and env validation
  5. ###############
  6. if ! [ -d "emqx" ]; then
  7. echo "[error] this script must be run at the same dir as the emqx"
  8. exit 1
  9. fi
  10. if [ $# -eq 0 ]
  11. then
  12. echo "[error] a new emqx name should be provided!"
  13. echo "Usage: ./one_more_emqx <new_name>"
  14. echo " e.g. ./one_more_emqx emqx2"
  15. exit 1
  16. fi
  17. NEW_EMQX=$1
  18. if [ -d "$NEW_EMQX" ]; then
  19. echo "[error] a dir named ${NEW_EMQX} already exists!"
  20. exit 2
  21. fi
  22. echo creating "$NEW_EMQX" ...
  23. SED_REPLACE="sed -i "
  24. # shellcheck disable=2089
  25. case $(sed --help 2>&1) in
  26. *GNU*) SED_REPLACE="sed -i ";;
  27. *) SED_REPLACE="sed -i ''";;
  28. esac
  29. PORT_INC_=$(cksum <<< "$NEW_EMQX" | cut -f 1 -d ' ')
  30. PORT_INC=$((PORT_INC_ % 1000))
  31. echo using increment factor: $PORT_INC
  32. ###############
  33. ## helpers
  34. ###############
  35. process_emqx_conf() {
  36. echo "processing config file: $1"
  37. $SED_REPLACE '/^#/d' "$1"
  38. $SED_REPLACE '/^$/d' "$1"
  39. for entry_ in "${entries_to_be_inc[@]}"
  40. do
  41. echo inc port for "$entry_"
  42. ip_port_=$(grep -E "$entry_"'[ \t]*=' "$1" 2> /dev/null | tail -1 | cut -d = -f 2- | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  43. echo -- from: "$ip_port_"
  44. ip_=$(echo "$ip_port_" | cut -sd : -f 1)
  45. port_=$(echo "$ip_port_" | cut -sd : -f 2)
  46. if [ -z "$ip_" ]
  47. then
  48. new_ip_port=$(( ip_port_ + PORT_INC ))
  49. else
  50. new_ip_port="${ip_}:$(( port_ + PORT_INC ))"
  51. fi
  52. echo -- to: "$new_ip_port"
  53. $SED_REPLACE 's|'"$entry_"'[ \t]*=.*|'"$entry_"' = '"$new_ip_port"'|g' "$1"
  54. done
  55. }
  56. ###############
  57. ## main
  58. ###############
  59. cp -r emqx "$NEW_EMQX"
  60. ## change the rpc ports
  61. $SED_REPLACE 's|tcp_server_port[ \t]*=.*|tcp_server_port = 5369|g' emqx/etc/rpc.conf
  62. $SED_REPLACE 's|tcp_client_port[ \t]*=.*|tcp_client_port = 5370|g' emqx/etc/rpc.conf
  63. $SED_REPLACE 's|tcp_client_port[ \t]*=.*|tcp_client_port = 5369|g' "$NEW_EMQX/etc/rpc.conf"
  64. $SED_REPLACE 's|tcp_server_port[ \t]*=.*|tcp_server_port = 5370|g' "$NEW_EMQX/etc/rpc.conf"
  65. $SED_REPLACE 's|.*node\.name.*|node.name='"$NEW_EMQX"'@127.0.0.1|g' "$NEW_EMQX/etc/emqx.conf"
  66. conf_ext="*.conf"
  67. find "$NEW_EMQX" -name "${conf_ext}" | while read -r conf; do
  68. if [ "${conf##*/}" = 'emqx.conf' ]
  69. then
  70. declare -a entries_to_be_inc=("node.dist_listen_min"
  71. "node.dist_listen_max")
  72. process_emqx_conf "$conf" "${entries_to_be_inc[@]}"
  73. elif [ "${conf##*/}" = 'listeners.conf' ]
  74. then
  75. declare -a entries_to_be_inc=("listener.tcp.external"
  76. "listener.tcp.internal"
  77. "listener.ssl.external"
  78. "listener.ws.external"
  79. "listener.wss.external")
  80. process_emqx_conf "$conf" "${entries_to_be_inc[@]}"
  81. elif [ "${conf##*/}" = 'emqx_management.conf' ]
  82. then
  83. declare -a entries_to_be_inc=("management.listener.http"
  84. "management.listener.https")
  85. process_emqx_conf "$conf" "${entries_to_be_inc[@]}"
  86. elif [ "${conf##*/}" = 'emqx_dashboard.conf' ]
  87. then
  88. declare -a entries_to_be_inc=("dashboard.listener.http"
  89. "dashboard.listener.https")
  90. process_emqx_conf "$conf" "${entries_to_be_inc[@]}"
  91. else
  92. echo "."
  93. fi
  94. done