zk-config-interactive.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. echo -e "Please enter the host of zookeeper.\n请输入zookeeper的host [localhost]:"
  18. read -p ">>> " host
  19. echo -e "Please enter the port of zookeeper.\n请输入zookeeper的port [2181]:"
  20. read -p ">>> " port
  21. echo -e "Please enter the zkHome of zookeeper.\n请输入zookeeper的zkHome:"
  22. read -p ">>> " zkHome
  23. read -p "Are you sure to continue? [y/n]" input
  24. case $input in
  25. [yY]*)
  26. if [[ -z ${host} ]]; then
  27. host=localhost
  28. fi
  29. if [[ -z ${port} ]]; then
  30. port=2181
  31. fi
  32. if [[ -z ${zkHome} ]]; then
  33. echo " zk home is empty, please input it "
  34. exit 1
  35. fi
  36. ;;
  37. [nN]*)
  38. exit
  39. ;;
  40. *)
  41. echo "Just enter y or n, please."
  42. exit
  43. ;;
  44. esac
  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