consul-config.sh 2.2 KB

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