v-update-sys-ip 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. # info: update system ip
  3. # options: [USER] [IP_STATUS]
  4. #
  5. # The function scans configured ip in the system and register them with vesta
  6. # internal database. This call is intended for use on vps servers, where ip is
  7. # set by hypervizor.
  8. #----------------------------------------------------------#
  9. # Variable&Function #
  10. #----------------------------------------------------------#
  11. # Argument defenition
  12. user=${1-admin}
  13. ip_status=${2-shared}
  14. # Includes
  15. source /etc/profile.d/vesta.sh
  16. source $VESTA/func/main.sh
  17. source $VESTA/func/ip.sh
  18. source $VESTA/conf/vesta.conf
  19. #----------------------------------------------------------#
  20. # Verifications #
  21. #----------------------------------------------------------#
  22. check_args '0' "$#" '[USER] [IP_STATUS]'
  23. validate_format 'user' 'ip_status'
  24. is_object_valid 'user' 'USER' "$user" "$user"
  25. #----------------------------------------------------------#
  26. # Action #
  27. #----------------------------------------------------------#
  28. # Get list of ip addresses
  29. ip_list=$(/sbin/ip addr|grep 'inet '|grep global|awk '{print $2}')
  30. ip_list=$(echo "$ip_list"|cut -f 1 -d /)
  31. ip_num=$(echo "$ip_list" | wc -l)
  32. # WorkAround for DHCP IP address
  33. vst_ip_list=$(ls $VESTA/data/ips/)
  34. vst_ip_num=$(echo "$vst_ip_list" | wc -l)
  35. if [ ! -z "$vst_ip_list" ] && [ "$vst_ip_num" -eq '1' ]; then
  36. if [ $ip_num -eq 1 ] && [ "$ip_list" != "$vst_ip_list" ]; then
  37. new=$ip_list
  38. old=$vst_ip_list
  39. mv $VESTA/data/ips/$old $VESTA/data/ips/$new
  40. if [ ! -z "$PROXY_SYSTEM" ]; then
  41. mv /etc/$PROXY_SYSTEM/conf.d/$old.conf \
  42. /etc/$PROXY_SYSTEM/conf.d/$new.conf
  43. sed -i "s/$old/$new/g" /etc/$PROXY_SYSTEM/conf.d/$new.conf
  44. fi
  45. if [ ! -z "$WEB_SYSTEM" ]; then
  46. mv /etc/$WEB_SYSTEM/conf.d/$old.conf \
  47. /etc/$WEB_SYSTEM/conf.d/$new.conf
  48. sed -i "s/$old/$new/g" /etc/$WEB_SYSTEM/conf.d/$new.conf
  49. sed -i "s/$old/$new/g" $VESTA/data/users/*/web.conf
  50. # Rebuild web domains
  51. for user in $(ls $VESTA/data/users/); do
  52. $BIN/v-rebuild-web-domains $user no
  53. done
  54. fi
  55. # Restarting web server
  56. $BIN/v-restart-web
  57. # Restarting proxy server
  58. if [ ! -z "$PROXY_SYSTEM" ]; then
  59. $BIN/v-restart-proxy
  60. fi
  61. # Restarting firewall
  62. if [ ! -z "$FIREWALL_SYSTEM" ]; then
  63. $BIN/v-update-firewall
  64. fi
  65. if [ ! -z "$DNS_SYSTEM" ]; then
  66. # Rebuild dns domains
  67. for user in $(ls $VESTA/data/users/); do
  68. sed -i "s/$old/$new/g" $VESTA/data/users/$user/dns.conf
  69. sed -i "s/$old/$new/g" $VESTA/data/users/$user/dns/*.conf
  70. $BIN/v-rebuild-dns-domains $user no
  71. done
  72. $BIN/v-restart-dns
  73. check_result $? "dns restart failed" >/dev/null
  74. fi
  75. # No further comparation is needed
  76. exit
  77. fi
  78. fi
  79. # Compare ips
  80. for ip in $ip_list; do
  81. if [ ! -e "$VESTA/data/ips/$ip" ]; then
  82. interface=$(/sbin/ip addr |grep $ip |awk '{print $NF}')
  83. interface=$(echo $interface |cut -f 1 -d :)
  84. netmask=$(/sbin/ip addr |grep $ip |cut -f 2 -d / |cut -f 1 -d \ )
  85. netmask=$(convert_cidr $netmask)
  86. $BIN/v-add-sys-ip $ip $netmask $interface
  87. fi
  88. done
  89. #----------------------------------------------------------#
  90. # Vesta #
  91. #----------------------------------------------------------#
  92. exit