utils.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 check install user
  18. ## @audience public
  19. ## @stability stable
  20. function check_install_user()
  21. {
  22. if [[ $(id -u) -ne 0 ]];then
  23. echo "This script must be run with a ROOT user!"
  24. exit # don't call exit_install()
  25. fi
  26. }
  27. ## @description exit install
  28. ## @audience public
  29. ## @stability stable
  30. function exit_install()
  31. {
  32. echo "Exit the installation!" | tee -a $LOG
  33. exit $1
  34. }
  35. ## @description Check if the IP address format is correct
  36. ## @audience public
  37. ## @stability stable
  38. function valid_ip()
  39. {
  40. local ip=$1
  41. local stat=1
  42. if [[ $ip =~ ^[0-9]{1,3\}.[0-9]{1,3\}.[0-9]{1,3\}.[0-9]{1,3\}$ ]]; then
  43. OIFS=$IFS
  44. IFS='.'
  45. ip=($ip)
  46. IFS=$OIFS
  47. if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]; then
  48. stat=$?
  49. fi
  50. fi
  51. return $stat
  52. }
  53. ## @description Check if the configuration file configuration is correct
  54. ## @audience public
  55. ## @stability stable
  56. function check_install_conf()
  57. {
  58. echo "Check if the configuration file configuration is correct ..." | tee -a $LOG
  59. # check etcd conf
  60. hostCount=${#ETCD_HOSTS[@]}
  61. if [[ $hostCount -lt 3 && hostCount -ne 0 ]]; then # <>2
  62. echo "Number of nodes = [$hostCount], must be configured to be greater than or equal to 3 servers! " | tee -a $LOG
  63. exit_install
  64. fi
  65. for ip in ${ETCD_HOSTS[@]}
  66. do
  67. if ! valid_ip $ip; then
  68. echo "]ETCD_HOSTS=[$ip], IP address format is incorrect! " | tee -a $LOG
  69. exit_install
  70. fi
  71. done
  72. echo "Check if the configuration file configuration is correct [ Done ]" | tee -a $LOG
  73. }
  74. ## @description index by EtcdHosts list
  75. ## @audience public
  76. ## @stability stable
  77. function indexByEtcdHosts() {
  78. index=0
  79. while [ "$index" -lt "${#ETCD_HOSTS[@]}" ]; do
  80. if [ "${ETCD_HOSTS[$index]}" = "$1" ]; then
  81. echo $index
  82. return
  83. fi
  84. let "index++"
  85. done
  86. echo ""
  87. }
  88. ## @description get local IP
  89. ## @audience public
  90. ## @stability stable
  91. function getLocalIP()
  92. {
  93. local _ip _myip _line _nl=$'\n'
  94. while IFS=$': \t' read -a _line ;do
  95. [ -z "${_line%inet}" ] &&
  96. _ip=${_line[${#_line[1]}>4?1:2]} &&
  97. [ "${_ip#127.0.0.1}" ] && _myip=$_ip
  98. done< <(LANG=C /sbin/ifconfig)
  99. printf ${1+-v} $1 "%s${_nl:0:$[${#1}>0?0:1]}" $_myip
  100. }
  101. ## @description get ip list
  102. ## @audience public
  103. ## @stability stable
  104. function get_ip_list()
  105. {
  106. array=$(ifconfig | grep inet | grep -v inet6 | grep -v 127 | sed 's/^[ \t]*//g' | cut -d ' ' -f2)
  107. for ip in ${array[@]}
  108. do
  109. LOCAL_HOST_IP_LIST+=(${ip})
  110. done
  111. }