v-add-sys-ip 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. is_format_valid '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. is_format_valid 'ip_name'
  37. fi
  38. if [ ! -z "$nat_ip" ] ; then
  39. is_format_valid '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. # Generating timestamp
  73. time_n_date=$(date +'%T %F')
  74. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  75. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  76. # Adding vesta ip
  77. echo "OWNER='$user'
  78. STATUS='$ip_status'
  79. NAME='$ip_name'
  80. U_SYS_USERS=''
  81. U_WEB_DOMAINS='0'
  82. INTERFACE='$interface'
  83. NETMASK='$netmask'
  84. NAT='$nat_ip'
  85. TIME='$time'
  86. DATE='$date'" > $VESTA/data/ips/$ip
  87. chmod 660 $VESTA/data/ips/$ip
  88. # WEB support
  89. if [ ! -z "$WEB_SYSTEM" ]; then
  90. web_conf="/etc/$WEB_SYSTEM/conf.d/$ip.conf"
  91. rm -f $web_conf
  92. if [ "$WEB_SYSTEM" = 'httpd' ] || [ "$WEB_SYSTEM" = 'apache2' ]; then
  93. if [ -z "$(/usr/sbin/apachectl -v | grep Apache/2.4)" ]; then
  94. echo "NameVirtualHost $ip:$WEB_PORT" > $web_conf
  95. fi
  96. echo "Listen $ip:$WEB_PORT" >> $web_conf
  97. fi
  98. if [ "$WEB_SSL" = 'mod_ssl' ]; then
  99. if [ -z "$(/usr/sbin/apachectl -v | grep Apache/2.4)" ]; then
  100. echo "NameVirtualHost $ip:$WEB_SSL_PORT" >> $web_conf
  101. fi
  102. echo "Listen $ip:$WEB_SSL_PORT" >> $web_conf
  103. fi
  104. fi
  105. # Proxy support
  106. if [ ! -z "$PROXY_SYSTEM" ]; then
  107. cat $WEBTPL/$PROXY_SYSTEM/proxy_ip.tpl |\
  108. sed -e "s/%ip%/$ip/g" \
  109. -e "s/%web_port%/$WEB_PORT/g" \
  110. -e "s/%proxy_port%/$PROXY_PORT/g" \
  111. > /etc/$PROXY_SYSTEM/conf.d/$ip.conf
  112. # mod_extract_forwarded
  113. fw_conf="/etc/$WEB_SYSTEM/conf.d/mod_extract_forwarded.conf"
  114. if [ -e "$fw_conf" ]; then
  115. ips=$(grep 'MEFaccept ' $fw_conf | grep -v '#' | head -n1)
  116. sed -i "s/$ips/$ips $ip/g" $fw_conf
  117. fi
  118. # mod_rpaf
  119. rpaf_conf="/etc/$WEB_SYSTEM/mods-enabled/rpaf.conf"
  120. if [ -e "$rpaf_conf" ]; then
  121. rpaf_str=$(grep RPAFproxy_ips $rpaf_conf)
  122. rpaf_str="$rpaf_str $ip"
  123. sed -i "s/.*RPAFproxy_ips.*/$rpaf_str/" $rpaf_conf
  124. fi
  125. #mod_remoteip
  126. remoteip_conf="/etc/$WEB_SYSTEM/mods-enabled/remoteip.conf"
  127. if [ -e "$remoteip_conf" ]; then
  128. if [ $( grep -ic "$ip" $remoteip_conf ) -eq 0 ]; then
  129. sed -i "s/<\/IfModule>/RemoteIPInternalProxy $ip\n<\/IfModule>/g" $remoteip_conf
  130. fi
  131. fi
  132. fi
  133. #----------------------------------------------------------#
  134. # Vesta #
  135. #----------------------------------------------------------#
  136. # Updating user counters
  137. increase_user_value "$user" '$IP_OWNED'
  138. if [ "$user" = 'admin' ]; then
  139. if [ "$ip_status" = 'shared' ]; then
  140. for user in $(ls $VESTA/data/users); do
  141. increase_user_value "$user" '$IP_AVAIL'
  142. done
  143. else
  144. increase_user_value 'admin' '$IP_AVAIL'
  145. fi
  146. else
  147. increase_user_value "$user" '$IP_AVAIL'
  148. increase_user_value 'admin' '$IP_AVAIL'
  149. fi
  150. # Restarting web server
  151. $BIN/v-restart-web
  152. check_result $? "Web restart failed" >/dev/null
  153. # Restarting proxy server
  154. if [ ! -z "$PROXY_SYSTEM" ]; then
  155. $BIN/v-restart-proxy
  156. check_result $? "Proxy restart failed" >/dev/null
  157. fi
  158. # Restarting firewall
  159. if [ ! -z "$FIREWALL_SYSTEM" ]; then
  160. $BIN/v-update-firewall
  161. fi
  162. # Logging
  163. log_history "added system ip address $ip" '' 'admin'
  164. log_event "$OK" "$ARGUMENTS"
  165. exit