v-add-sys-ip 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/bin/bash
  2. # info: add system ip address
  3. # options: IP NETMASK [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 automatically 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 definition
  15. ip=${1// /}
  16. netmask=$2
  17. interface="${3-eth0}"
  18. user="${4-admin}"
  19. ip_status="${5-shared}"
  20. ip_name=$6
  21. nat_ip=$7
  22. # Includes
  23. source $VESTA/func/main.sh
  24. source $VESTA/func/ip.sh
  25. source $VESTA/func/domain.sh
  26. source $VESTA/conf/vesta.conf
  27. #----------------------------------------------------------#
  28. # Verifications #
  29. #----------------------------------------------------------#
  30. check_args '2' "$#" 'IP NETMASK [INTERFACE] [USER] [STATUS] [NAME] [NATED_IP]'
  31. validate_format 'ip' 'netmask' '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. iface=$(get_ip_iface)
  45. cidr=$(convert_netmask $netmask)
  46. broadcast=$(get_broadcast $ip $netmask)
  47. sys_ip_check=$(/sbin/ip addr | grep "$ip")
  48. if [ -z "$sys_ip_check" ]; then
  49. # Adding sys ip
  50. /sbin/ip addr add $ip/$cidr dev $interface \
  51. broadcast $broadcast label $iface
  52. # Adding RHEL/CentOS/Fedora startup script
  53. if [ -e "/etc/redhat-release" ]; then
  54. sys_ip="# Added by vesta"
  55. sys_ip="$sys_ip\nDEVICE=$iface"
  56. sys_ip="$sys_ip\nBOOTPROTO=static"
  57. sys_ip="$sys_ip\nONBOOT=yes"
  58. sys_ip="$sys_ip\nIPADDR=$ip"
  59. sys_ip="$sys_ip\nNETMASK=$netmask"
  60. echo -e $sys_ip > /etc/sysconfig/network-scripts/ifcfg-$iface
  61. fi
  62. # Adding Debian/Ubuntu startup script
  63. if [ -e "/etc/debian_version" ]; then
  64. sys_ip="\n# Added by vesta"
  65. sys_ip="$sys_ip\nauto $iface"
  66. sys_ip="$sys_ip\niface $iface inet static"
  67. sys_ip="$sys_ip\naddress $ip"
  68. sys_ip="$sys_ip\nnetmask $netmask"
  69. echo -e $sys_ip >> /etc/network/interfaces
  70. fi
  71. fi
  72. # Adding vesta ip
  73. echo "OWNER='$user'
  74. STATUS='$ip_status'
  75. NAME='$ip_name'
  76. U_SYS_USERS=''
  77. U_WEB_DOMAINS='0'
  78. INTERFACE='$interface'
  79. NETMASK='$netmask'
  80. NAT='$nat_ip'
  81. TIME='$TIME'
  82. DATE='$DATE'" > $VESTA/data/ips/$ip
  83. chmod 660 $VESTA/data/ips/$ip
  84. # WEB support
  85. if [ ! -z "$WEB_SYSTEM" ]; then
  86. web_conf="/etc/$WEB_SYSTEM/conf.d/$ip.conf"
  87. rm -f $web_conf
  88. if [ "$WEB_SYSTEM" = 'httpd' ] || [ "$WEB_SYSTEM" = 'apache2' ]; then
  89. if [ -z "$(/usr/sbin/apachectl -v | grep Apache/2.4)" ]; then
  90. echo "NameVirtualHost $ip:$WEB_PORT" > $web_conf
  91. fi
  92. echo "Listen $ip:$WEB_PORT" >> $web_conf
  93. fi
  94. if [ "$WEB_SSL" = 'mod_ssl' ]; then
  95. if [ -z "$(/usr/sbin/apachectl -v | grep Apache/2.4)" ]; then
  96. echo "NameVirtualHost $ip:$WEB_SSL_PORT" >> $web_conf
  97. fi
  98. echo "Listen $ip:$WEB_SSL_PORT" >> $web_conf
  99. fi
  100. fi
  101. # Proxy support
  102. if [ ! -z "$PROXY_SYSTEM" ]; then
  103. cat $WEBTPL/$PROXY_SYSTEM/proxy_ip.tpl |\
  104. sed -e "s/%ip%/$ip/g" \
  105. -e "s/%web_port%/$WEB_PORT/g" \
  106. -e "s/%proxy_port%/$PROXY_PORT/g" \
  107. > /etc/$PROXY_SYSTEM/conf.d/$ip.conf
  108. # mod_extract_forwarded
  109. fw_conf="/etc/$WEB_SYSTEM/conf.d/mod_extract_forwarded.conf"
  110. if [ -e "$fw_conf" ]; then
  111. ips=$(grep 'MEFaccept ' $fw_conf | grep -v '#' | head -n1)
  112. sed -i "s/$ips/$ips $ip/g" $fw_conf
  113. fi
  114. # mod_rpaf
  115. rpaf_conf="/etc/$WEB_SYSTEM/mods-enabled/rpaf.conf"
  116. if [ -e "$rpaf_conf" ]; then
  117. rpaf_str=$(grep RPAFproxy_ips $rpaf_conf)
  118. rpaf_str="$rpaf_str $ip"
  119. sed -i "s/.*RPAFproxy_ips.*/$rpaf_str/" $rpaf_conf
  120. fi
  121. fi
  122. #----------------------------------------------------------#
  123. # Vesta #
  124. #----------------------------------------------------------#
  125. # Updating user counters
  126. increase_user_value "$user" '$IP_OWNED'
  127. if [ "$user" = 'admin' ]; then
  128. if [ "$ip_status" = 'shared' ]; then
  129. for user in $(ls $VESTA/data/users); do
  130. increase_user_value "$user" '$IP_AVAIL'
  131. done
  132. else
  133. increase_user_value 'admin' '$IP_AVAIL'
  134. fi
  135. else
  136. increase_user_value "$user" '$IP_AVAIL'
  137. increase_user_value 'admin' '$IP_AVAIL'
  138. fi
  139. # Restarting web server
  140. $BIN/v-restart-web
  141. check_result $? "Web restart failed" >/dev/null
  142. # Restarting proxy server
  143. if [ ! -z "$PROXY_SYSTEM" ]; then
  144. $BIN/v-restart-proxy
  145. check_result $? "Proxy restart failed" >/dev/null
  146. fi
  147. # Restarting firewall
  148. if [ ! -z "$FIREWALL_SYSTEM" ]; then
  149. $BIN/v-update-firewall
  150. fi
  151. # Logging
  152. log_history "added system ip address $ip" '' 'admin'
  153. log_event "$OK" "$EVENT"
  154. exit