etcd3-config-interactive.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env bash
  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. # etcd REST API v3.
  16. # author:wangyuewen
  17. # shellcheck disable=SC2039,SC2162,SC2046,SC2013,SC2002
  18. echo -e "Please enter the host of etcd3.\n请输入etcd3的host [localhost]:"
  19. read -p ">>> " host
  20. echo -e "Please enter the port of etcd3.\n请输入etcd3的port [2379]:"
  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=2379
  30. fi
  31. ;;
  32. [nN]*)
  33. exit
  34. ;;
  35. *)
  36. echo "Just enter y or n, please."
  37. exit
  38. ;;
  39. esac
  40. etcd3Addr=$host:$port
  41. contentType="content-type:application/json;charset=UTF-8"
  42. echo "Set etcd3Addr=$etcd3Addr"
  43. failCount=0
  44. tempLog=$(mktemp -u)
  45. function addConfig() {
  46. keyBase64=$(printf "%s""$2" | base64)
  47. valueBase64=$(printf "%s""$3" | base64)
  48. curl -X POST -H "${1}" -d "{\"key\": \"$keyBase64\", \"value\": \"$valueBase64\"}" "http://$4/v3/kv/put" >"${tempLog}" 2>/dev/null
  49. if [[ -z $(cat "${tempLog}") ]]; then
  50. echo " Please check the cluster status. "
  51. exit 1
  52. fi
  53. if [[ $(cat "${tempLog}") =~ "error" || $(cat "${tempLog}") =~ "code" ]]; then
  54. echo "Set $2=$3 failure "
  55. (( failCount++ ))
  56. else
  57. echo "Set $2=$3 successfully "
  58. fi
  59. }
  60. count=0
  61. COMMENT_START="#"
  62. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  63. if [[ "$line" =~ ^"${COMMENT_START}".* ]]; then
  64. continue
  65. fi
  66. (( count++ ))
  67. key=${line%%=*}
  68. value=${line#*=}
  69. addConfig "${contentType}" "${key}" "${value}" "${etcd3Addr}"
  70. done
  71. echo "========================================================================="
  72. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  73. echo "========================================================================="
  74. if [[ ${failCount} -eq 0 ]]; then
  75. echo " Init etcd3 config finished, please start seata-server. "
  76. else
  77. echo " Init etcd3 config fail. "
  78. fi