consul-config-interactive.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. echo -e "Please enter the host of consul.\n请输入consul的host [localhost]:"
  16. read -p ">>> " host
  17. echo -e "Please enter the port of consul.\n请输入consul的port [8500]:"
  18. read -p ">>> " port
  19. read -p "Are you sure to continue? [y/n]" input
  20. case $input in
  21. [yY]*)
  22. if [[ -z ${host} ]]; then
  23. host=localhost
  24. fi
  25. if [[ -z ${port} ]]; then
  26. port=8500
  27. fi
  28. ;;
  29. [nN]*)
  30. exit
  31. ;;
  32. *)
  33. echo "Just enter y or n, please."
  34. exit
  35. ;;
  36. esac
  37. consulAddr=$host:$port
  38. contentType="content-type:application/json;charset=UTF-8"
  39. echo "Set consulAddr=$consulAddr"
  40. failCount=0
  41. tempLog=$(mktemp -u)
  42. function addConfig() {
  43. curl -X PUT -H "${1}" -d "${2}" "http://$3/v1/kv/$4" >"${tempLog}" 2>/dev/null
  44. if [[ -z $(cat "${tempLog}") ]]; then
  45. echo " Please check the cluster status. "
  46. exit 1
  47. fi
  48. if [[ $(cat "${tempLog}") =~ "true" ]]; then
  49. echo "Set $4=$2 successfully "
  50. else
  51. echo "Set $4=$2 failure "
  52. (( failCount++ ))
  53. fi
  54. }
  55. count=0
  56. COMMENT_START="#"
  57. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  58. if [[ "$line" =~ ^"${COMMENT_START}".* ]]; then
  59. continue
  60. fi
  61. (( count++ ))
  62. key=${line%%=*}
  63. value=${line#*=}
  64. addConfig "${contentType}" "${value}" "${consulAddr}" "${key}"
  65. done
  66. echo "========================================================================="
  67. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  68. echo "========================================================================="
  69. if [[ ${failCount} -eq 0 ]]; then
  70. echo " Init consul config finished, please start seata-server. "
  71. else
  72. echo " Init consul config fail. "
  73. fi