consul-config-interactive.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env bash
  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. echo -e "Please enter the host of consul.\n请输入consul的host [localhost]:"
  19. read -p ">>> " host
  20. echo -e "Please enter the port of consul.\n请输入consul的port [8500]:"
  21. read -p ">>> " port
  22. read -p "Are you sure to continue? [y/n]" input
  23. case $input in
  24. [yY]*)
  25. if [[ -z ${host} ]]; then
  26. host=localhost
  27. fi
  28. if [[ -z ${port} ]]; then
  29. port=8500
  30. fi
  31. ;;
  32. [nN]*)
  33. exit
  34. ;;
  35. *)
  36. echo "Just enter y or n, please."
  37. exit
  38. ;;
  39. esac
  40. consulAddr=$host:$port
  41. contentType="content-type:application/json;charset=UTF-8"
  42. echo "Set consulAddr=$consulAddr"
  43. failCount=0
  44. tempLog=$(mktemp -u)
  45. function addConfig() {
  46. curl -X PUT -H "${1}" -d "${2}" "http://$3/v1/kv/$4" >"${tempLog}" 2>/dev/null
  47. if [[ -z $(cat "${tempLog}") ]]; then
  48. echo " Please check the cluster status. "
  49. exit 1
  50. fi
  51. if [[ $(cat "${tempLog}") =~ "true" ]]; then
  52. echo "Set $4=$2 successfully "
  53. else
  54. echo "Set $4=$2 failure "
  55. (( failCount++ ))
  56. fi
  57. }
  58. count=0
  59. COMMENT_START="#"
  60. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  61. if [[ "$line" =~ ^"${COMMENT_START}".* ]]; then
  62. continue
  63. fi
  64. (( count++ ))
  65. key=${line%%=*}
  66. value=${line#*=}
  67. addConfig "${contentType}" "${value}" "${consulAddr}" "${key}"
  68. done
  69. echo "========================================================================="
  70. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  71. echo "========================================================================="
  72. if [[ ${failCount} -eq 0 ]]; then
  73. echo " Init consul config finished, please start seata-server. "
  74. else
  75. echo " Init consul config fail. "
  76. fi