etcd.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with 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. ## @description download etcd bin
  18. ## @audience public
  19. ## @stability stable
  20. function download_etcd_bin()
  21. {
  22. # my download http server
  23. if [[ -n "$DOWNLOAD_HTTP" ]]; then
  24. MY_ETCD_DOWNLOAD_URL="${DOWNLOAD_HTTP}/downloads/etcd/${ETCD_TAR_GZ}"
  25. else
  26. MY_ETCD_DOWNLOAD_URL=${ETCD_DOWNLOAD_URL}
  27. fi
  28. if [[ -f "${DOWNLOAD_DIR}/etcd/${ETCD_TAR_GZ}" ]]; then
  29. echo "${DOWNLOAD_DIR}/etcd/${ETCD_TAR_GZ} is exist."
  30. else
  31. echo "download ${MY_ETCD_DOWNLOAD_URL} ..."
  32. wget -P ${DOWNLOAD_DIR}/etcd ${MY_ETCD_DOWNLOAD_URL}
  33. fi
  34. }
  35. ## @description install etcd bin
  36. ## @audience public
  37. ## @stability stable
  38. function install_etcd_bin()
  39. {
  40. download_etcd_bin
  41. # install etcd bin
  42. mkdir -p ${INSTALL_TEMP_DIR}
  43. rm -rf ${INSTALL_TEMP_DIR}/etcd-*-linux-amd6
  44. tar zxvf ${DOWNLOAD_DIR}/etcd/${ETCD_TAR_GZ} -C ${INSTALL_TEMP_DIR}
  45. cp -f ${INSTALL_TEMP_DIR}/etcd-*-linux-amd64/etcd /usr/bin
  46. cp -f ${INSTALL_TEMP_DIR}/etcd-*-linux-amd64/etcdctl /usr/bin
  47. mkdir -p /var/lib/etcd
  48. chmod -R a+rw /var/lib/etcd
  49. }
  50. ## @description install etcd config
  51. ## @audience public
  52. ## @stability stable
  53. function install_etcd_config()
  54. {
  55. # config etcd.service
  56. rm -rf ${INSTALL_TEMP_DIR}/etcd
  57. cp -rf ${PACKAGE_DIR}/etcd ${INSTALL_TEMP_DIR}/
  58. # 1. Replace name with ETCD_NODE_NAME_REPLACE based on the location of the local IP in $ETCD_HOSTS
  59. indexEtcdList=$(indexByEtcdHosts ${LOCAL_HOST_IP})
  60. # echo ${indexEtcdList}
  61. etcdNodeName="etcdnode${indexEtcdList}"
  62. # echo ${etcdNodeName}
  63. sed -i "s/ETCD_NODE_NAME_REPLACE/${etcdNodeName}/g" $INSTALL_TEMP_DIR/etcd/etcd.service >>$LOG
  64. # 2. Replace local IP address
  65. sed -i "s/LOCAL_HOST_REPLACE/${LOCAL_HOST_IP}/g" $INSTALL_TEMP_DIR/etcd/etcd.service >>$LOG
  66. # 3. Replace the initial-cluster parameter
  67. # --initial-cluster=etcdnode1=http://10.196.69.173:2380,etcdnode2=http://10.196.69.174:2380,etcdnode3=http://10.196.69.175:2380 \
  68. initialCluster=''
  69. index=0
  70. etcdHostsSize=${#ETCD_HOSTS[@]}
  71. for item in ${ETCD_HOSTS[@]}
  72. do
  73. # char '/' need to escape '\/'
  74. initialCluster="${initialCluster}etcdnode${index}=http:\/\/${item}:2380"
  75. if [[ ${index} -lt ${etcdHostsSize}-1 ]]; then
  76. initialCluster=${initialCluster}","
  77. fi
  78. index=$(($index+1))
  79. done
  80. #echo "initialCluster=${initialCluster}"
  81. sed -i "s/INITIAL_CLUSTER_REPLACE/${initialCluster}/g" $INSTALL_TEMP_DIR/etcd/etcd.service >>$LOG
  82. cp $INSTALL_TEMP_DIR/etcd/etcd.service /etc/systemd/system/ >>$LOG
  83. }
  84. ## @description install etcd
  85. ## @audience public
  86. ## @stability stable
  87. function install_etcd()
  88. {
  89. index=$(indexByEtcdHosts ${LOCAL_HOST_IP})
  90. if [ -z "$index" ]; then
  91. echo -e "STOP: This host\033[31m[${LOCAL_HOST_IP}]\033[0m is not in the ETCD server list\033[31m[${ETCD_HOSTS[@]}]\033[0m"
  92. return 1
  93. fi
  94. install_etcd_bin
  95. install_etcd_config
  96. systemctl daemon-reload
  97. systemctl enable etcd.service
  98. }
  99. ## @description uninstall etcd
  100. ## @audience public
  101. ## @stability stable
  102. function uninstall_etcd()
  103. {
  104. echo "stop etcd.service"
  105. systemctl stop etcd.service
  106. echo "rm etcd ..."
  107. rm /usr/bin/etcd
  108. rm /usr/bin/etcdctl
  109. rm -rf /var/lib/etcd
  110. rm /etc/systemd/system/etcd.service
  111. systemctl daemon-reload
  112. }
  113. ## @description start etcd
  114. ## @audience public
  115. ## @stability stable
  116. function start_etcd()
  117. {
  118. systemctl restart etcd.service
  119. echo " ===== Check the status of the etcd service ====="
  120. echo " exec etcdctl cluster-health"
  121. etcdctl cluster-health
  122. echo " exec etcdctl cluster-health"
  123. etcdctl member list
  124. }
  125. ## @description stop etcd
  126. ## @audience public
  127. ## @stability stable
  128. function stop_etcd()
  129. {
  130. systemctl stop etcd.service
  131. }