etcd3-config-interactive.sh 2.7 KB

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