zk-config.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env bash
  2. # Copyright 1999-2019 Seata.io Group.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at、
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # The purpose is to sync the local configuration(config.txt) to zk.
  16. # This script need to rely on zk.
  17. while getopts ":h:p:z:" opt
  18. do
  19. case $opt in
  20. h)
  21. host=$OPTARG
  22. ;;
  23. p)
  24. port=$OPTARG
  25. ;;
  26. z)
  27. zkHome=$OPTARG
  28. ;;
  29. ?)
  30. echo " USAGE OPTION: $0 [-h host] [-p port] [-z zkHome] "
  31. exit 1
  32. ;;
  33. esac
  34. done
  35. if [[ -z ${host} ]]; then
  36. host=localhost
  37. fi
  38. if [[ -z ${port} ]]; then
  39. port=2181
  40. fi
  41. if [[ -z ${zkHome} ]]; then
  42. echo " zk home is empty, please usage option: [-z zkHome] "
  43. exit 1
  44. fi
  45. zkAddr=$host:$port
  46. root="/seata"
  47. tempLog=$(mktemp -u)
  48. echo "ZK address is $zkAddr"
  49. echo "ZK home is $zkHome"
  50. echo "ZK config root node is $root"
  51. function check_node() {
  52. "$2"/bin/zkCli.sh -server "$1" ls ${root} >/dev/null 2>"${tempLog}"
  53. }
  54. function create_node() {
  55. "$2"/bin/zkCli.sh -server "$1" create ${root} "" >/dev/null
  56. }
  57. function create_subNode() {
  58. "$2"/bin/zkCli.sh -server "$1" create "${root}/$3" "$4" >/dev/null
  59. }
  60. function delete_node() {
  61. "$2"/bin/zkCli.sh -server $1 rmr ${root} "" >/dev/null
  62. }
  63. check_node "${zkAddr}" "${zkHome}"
  64. if [[ $(cat "${tempLog}") =~ "No such file or directory" ]]; then
  65. echo " ZK home is error, please enter correct zk home! "
  66. exit 1
  67. elif [[ $(cat "${tempLog}") =~ "Exception" ]]; then
  68. echo " Exception error, please check zk cluster status or if the zk address is entered correctly! "
  69. exit 1
  70. elif [[ $(cat "${tempLog}") =~ "Node does not exist" ]]; then
  71. create_node "${zkAddr}" "${zkHome}"
  72. else
  73. read -p "${root} node already exists, now delete ${root} node in zk, y/n: " result
  74. if [[ ${result} == "y" ]]; then
  75. echo "Delete ${root} node..."
  76. delete_node "${zkAddr}" "${zkHome}"
  77. create_node "${zkAddr}" "${zkHome}"
  78. else
  79. exit 0
  80. fi
  81. fi
  82. COMMENT_START="#"
  83. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  84. if [[ "$line" =~ ^"${COMMENT_START}".* ]]; then
  85. continue
  86. fi
  87. key=${line%%=*}
  88. value=${line#*=}
  89. echo "Set" "${key}" "=" "${value}"
  90. create_subNode "${zkAddr}" "${zkHome}" "${key}" "${value}"
  91. done