nacos-config.sh 3.0 KB

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