consul-config.sh 2.0 KB

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