nacos-config.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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:g:t:u:w:" opt
  16. do
  17. case $opt in
  18. h)
  19. host=$OPTARG
  20. ;;
  21. p)
  22. port=$OPTARG
  23. ;;
  24. g)
  25. group=$OPTARG
  26. ;;
  27. t)
  28. tenant=$OPTARG
  29. ;;
  30. u)
  31. username=$OPTARG
  32. ;;
  33. w)
  34. password=$OPTARG
  35. ;;
  36. ?)
  37. echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
  38. exit 1
  39. ;;
  40. esac
  41. done
  42. urlencode() {
  43. for ((i=0; i < ${#1}; i++))
  44. do
  45. char="${1:$i:1}"
  46. case $char in
  47. [a-zA-Z0-9.~_-]) printf $char ;;
  48. *) printf '%%%02X' "'$char" ;;
  49. esac
  50. done
  51. }
  52. if [[ -z ${host} ]]; then
  53. host=localhost
  54. fi
  55. if [[ -z ${port} ]]; then
  56. port=8848
  57. fi
  58. if [[ -z ${group} ]]; then
  59. group="SEATA_GROUP"
  60. fi
  61. if [[ -z ${tenant} ]]; then
  62. tenant=""
  63. fi
  64. if [[ -z ${username} ]]; then
  65. username=""
  66. fi
  67. if [[ -z ${password} ]]; then
  68. password=""
  69. fi
  70. nacosAddr=$host:$port
  71. contentType="content-type:application/json;charset=UTF-8"
  72. echo "set nacosAddr=$nacosAddr"
  73. echo "set group=$group"
  74. failCount=0
  75. tempLog=$(mktemp -u)
  76. function addConfig() {
  77. curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$(urlencode $1)&group=$group&content=$(urlencode $2)&tenant=$tenant&username=$username&password=$password" >"${tempLog}" 2>/dev/null
  78. if [[ -z $(cat "${tempLog}") ]]; then
  79. echo " Please check the cluster status. "
  80. exit 1
  81. fi
  82. if [[ $(cat "${tempLog}") =~ "true" ]]; then
  83. echo "Set $1=$2 successfully "
  84. else
  85. echo "Set $1=$2 failure "
  86. (( failCount++ ))
  87. fi
  88. }
  89. count=0
  90. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  91. (( count++ ))
  92. key=${line%%=*}
  93. value=${line#*=}
  94. addConfig "${key}" "${value}"
  95. done
  96. echo "========================================================================="
  97. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  98. echo "========================================================================="
  99. if [[ ${failCount} -eq 0 ]]; then
  100. echo " Init nacos config finished, please start seata-server. "
  101. else
  102. echo " init nacos config fail. "
  103. fi