etcd3-config.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. while getopts ":h:p:" opt
  20. do
  21. case $opt in
  22. h)
  23. host=$OPTARG
  24. ;;
  25. p)
  26. port=$OPTARG
  27. ;;
  28. ?)
  29. echo " USAGE OPTION: $0 [-h host] [-p port] "
  30. exit 1
  31. ;;
  32. esac
  33. done
  34. if [[ -z ${host} ]]; then
  35. host=localhost
  36. fi
  37. if [[ -z ${port} ]]; then
  38. port=2379
  39. fi
  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