v-add-sys-ip 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/bash
  2. # info: add system ip address
  3. # options: IP MASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]
  4. #
  5. # The function adds ip address into a system. It also creates rc scripts. You
  6. # can specify ip name which will be used as root domain for temporary aliases.
  7. # For example, if you set a1.myhosting.com as name, each new domain created on
  8. # this ip will automaticaly receive alias $domain.a1.myhosting.com. Of course
  9. # you must have wildcard record *.a1.myhosting.com pointed to ip. This feature
  10. # is very handy when customer wants to test domain before dns migration.
  11. #----------------------------------------------------------#
  12. # Variable&Function #
  13. #----------------------------------------------------------#
  14. # Argument defenition
  15. ip=${1// /}
  16. mask=$2
  17. interface="${3-eth0}"
  18. user="${4-admin}"
  19. ip_status="${5-shared}" # status can be dedicated
  20. ip_name=$6
  21. nat_ip=$7
  22. # Includes
  23. source $VESTA/conf/vesta.conf
  24. source $VESTA/func/main.sh
  25. source $VESTA/func/ip.sh
  26. source $VESTA/func/domain.sh
  27. #----------------------------------------------------------#
  28. # Verifications #
  29. #----------------------------------------------------------#
  30. check_args '2' "$#" 'IP MASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]'
  31. validate_format 'ip' 'mask' 'interface' 'user' 'ip_status'
  32. is_ip_free
  33. is_object_valid 'user' 'USER' "$user"
  34. is_object_unsuspended 'user' 'USER' "$user"
  35. if [ ! -z "$ip_name" ] ; then
  36. validate_format 'ip_name'
  37. fi
  38. if [ ! -z "$nat_ip" ] ; then
  39. validate_format 'nat_ip'
  40. fi
  41. #----------------------------------------------------------#
  42. # Action #
  43. #----------------------------------------------------------#
  44. get_ip_iface
  45. sys_ip_check=$(/sbin/ifconfig | grep "addr:$ip ")
  46. if [ -z "$sys_ip_check" ]; then
  47. /sbin/ifconfig "$iface" "$ip" netmask "$mask"
  48. # Adding startup script
  49. sys_ip="# Added by vesta"
  50. sys_ip="$sys_ip\nDEVICE=$iface"
  51. sys_ip="$sys_ip\nBOOTPROTO=static"
  52. sys_ip="$sys_ip\nONBOOT=yes"
  53. sys_ip="$sys_ip\nIPADDR=$ip"
  54. sys_ip="$sys_ip\nNETMASK=$mask"
  55. echo -e $sys_ip > /etc/sysconfig/network-scripts/ifcfg-$iface
  56. fi
  57. # Adding vesta ip
  58. echo "OWNER='$user'
  59. STATUS='$ip_status'
  60. NAME='$ip_name'
  61. U_SYS_USERS=''
  62. U_WEB_DOMAINS='0'
  63. INTERFACE='$interface'
  64. NETMASK='$mask'
  65. NAT='$nat_ip'
  66. TIME='$TIME'
  67. DATE='$DATE'" > $VESTA/data/ips/$ip
  68. chmod 660 $VESTA/data/ips/$ip
  69. # WEB support
  70. web_conf="/etc/$WEB_SYSTEM/conf.d/vesta.conf"
  71. if [ "$WEB_SYSTEM" = 'httpd' ]; then
  72. conf_ins='1'
  73. conf_line=$(grep -n NameVirtual $web_conf | tail -n1 | cut -f1 -d:)
  74. if [ ! -z "$conf_line" ]; then
  75. conf_ins=$((conf_line + 1))
  76. fi
  77. if [ "$WEB_SSL" = 'mod_ssl' ]; then
  78. sed -i "$conf_ins i NameVirtualHost $ip:$WEB_SSL_PORT" $web_conf
  79. sed -i "$conf_ins i Listen $ip:$WEB_SSL_PORT" $web_conf
  80. fi
  81. sed -i "$conf_ins i NameVirtualHost $ip:$WEB_PORT" $web_conf
  82. sed -i "$conf_ins i Listen $ip:$WEB_PORT" $web_conf
  83. fi
  84. # Proxy support
  85. if [ ! -z "$PROXY_SYSTEM" ]; then
  86. cat $WEBTPL/$PROXY_SYSTEM/proxy_ip.tpl |\
  87. sed -e "s/%ip%/$ip/g" \
  88. -e "s/%web_port%/$WEB_PORT/g" \
  89. -e "s/%proxy_port%/$PROXY_PORT/g" \
  90. > /etc/$PROXY_SYSTEM/conf.d/$ip.conf
  91. fw_conf="/etc/$WEB_SYSTEM/conf.d/mod_extract_forwarded.conf"
  92. if [ -e "$fw_conf" ]; then
  93. ips=$(grep 'MEFaccept ' $fw_conf | grep -v '#' | head -n1)
  94. sed -i "s/$ips/$ips $ip/g" $fw_conf
  95. fi
  96. fi
  97. #----------------------------------------------------------#
  98. # Vesta #
  99. #----------------------------------------------------------#
  100. # Updating user counters
  101. increase_user_value "$user" '$IP_OWNED'
  102. if [ "$user" = 'admin' ]; then
  103. if [ "$ip_status" = 'shared' ]; then
  104. for user in $(ls $VESTA/data/users); do
  105. increase_user_value "$user" '$IP_AVAIL'
  106. done
  107. else
  108. increase_user_value 'admin' '$IP_AVAIL'
  109. fi
  110. else
  111. increase_user_value "$user" '$IP_AVAIL'
  112. increase_user_value 'admin' '$IP_AVAIL'
  113. fi
  114. # Restart web server
  115. $BIN/v-restart-web
  116. $BIN/v-restart-proxy
  117. # Logging
  118. log_history "added system ip address $ip" '' 'admin'
  119. log_event "$OK" "$EVENT"
  120. exit