etcd3-config.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. while getopts ":h:p:" opt
  17. do
  18. case $opt in
  19. h)
  20. host=$OPTARG
  21. ;;
  22. p)
  23. port=$OPTARG
  24. ;;
  25. ?)
  26. echo " USAGE OPTION: $0 [-h host] [-p port] "
  27. exit 1
  28. ;;
  29. esac
  30. done
  31. if [[ -z ${host} ]]; then
  32. host=localhost
  33. fi
  34. if [[ -z ${port} ]]; then
  35. port=2379
  36. fi
  37. etcd3Addr=$host:$port
  38. contentType="content-type:application/json;charset=UTF-8"
  39. echo "Set etcd3Addr=$etcd3Addr"
  40. failCount=0
  41. tempLog=$(mktemp -u)
  42. function addConfig() {
  43. keyBase64=$(printf "%s""$2" | base64)
  44. valueBase64=$(printf "%s""$3" | base64)
  45. curl -X POST -H "${1}" -d "{\"key\": \"$keyBase64\", \"value\": \"$valueBase64\"}" "http://$4/v3/kv/put" >"${tempLog}" 2>/dev/null
  46. if [[ -z $(cat "${tempLog}") ]]; then
  47. echo " Please check the cluster status. "
  48. exit 1
  49. fi
  50. if [[ $(cat "${tempLog}") =~ "error" || $(cat "${tempLog}") =~ "code" ]]; then
  51. echo "Set $2=$3 failure "
  52. (( failCount++ ))
  53. else
  54. echo "Set $2=$3 successfully "
  55. fi
  56. }
  57. count=0
  58. COMMENT_START="#"
  59. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  60. if [[ "$line" =~ ^"${COMMENT_START}".* ]]; then
  61. continue
  62. fi
  63. (( count++ ))
  64. key=${line%%=*}
  65. value=${line#*=}
  66. addConfig "${contentType}" "${key}" "${value}" "${etcd3Addr}"
  67. done
  68. echo "========================================================================="
  69. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  70. echo "========================================================================="
  71. if [[ ${failCount} -eq 0 ]]; then
  72. echo " Init etcd3 config finished, please start seata-server. "
  73. else
  74. echo " Init etcd3 config fail. "
  75. fi