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