nacos-config.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/bin/sh
  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. if [ -z ${host} ]; then
  43. host=localhost
  44. fi
  45. if [ -z ${port} ]; then
  46. port=8848
  47. fi
  48. if [ -z ${group} ]; then
  49. group="SEATA_GROUP"
  50. fi
  51. if [ -z ${tenant} ]; then
  52. tenant=""
  53. fi
  54. if [ -z ${username} ]; then
  55. username=""
  56. fi
  57. if [ -z ${password} ]; then
  58. password=""
  59. fi
  60. nacosAddr=$host:$port
  61. contentType="content-type:application/json;charset=UTF-8"
  62. echo "set nacosAddr=$nacosAddr"
  63. echo "set group=$group"
  64. urlencode() {
  65. length="${#1}"
  66. i=0
  67. while [ $length -gt $i ]; do
  68. char="${1:$i:1}"
  69. case $char in
  70. [a-zA-Z0-9.~_-]) printf $char ;;
  71. *) printf '%%%02X' "'$char" ;;
  72. esac
  73. i=`expr $i + 1`
  74. done
  75. }
  76. failCount=0
  77. tempLog=$(mktemp -u)
  78. function addConfig() {
  79. dataId=`urlencode $1`
  80. content=`urlencode $2`
  81. curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$dataId&group=$group&content=$content&tenant=$tenant&username=$username&password=$password" >"${tempLog}" 2>/dev/null
  82. if [ -z $(cat "${tempLog}") ]; then
  83. echo " Please check the cluster status. "
  84. exit 1
  85. fi
  86. if [ "$(cat "${tempLog}")" == "true" ]; then
  87. echo "Set $1=$2 successfully "
  88. else
  89. echo "Set $1=$2 failure "
  90. failCount=`expr $failCount + 1`
  91. fi
  92. }
  93. count=0
  94. COMMENT_START="#"
  95. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  96. if [[ "$line" =~ ^"${COMMENT_START}".* ]]; then
  97. continue
  98. fi
  99. count=`expr $count + 1`
  100. key=${line%%=*}
  101. value=${line#*=}
  102. addConfig "${key}" "${value}"
  103. done
  104. echo "========================================================================="
  105. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  106. echo "========================================================================="
  107. if [ ${failCount} -eq 0 ]; then
  108. echo " Init nacos config finished, please start seata-server. "
  109. else
  110. echo " init nacos config fail. "
  111. fi