nacos-config-interactive.sh 3.6 KB

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