zk-config.sh 2.8 KB

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