optimized web domain adding (almost 2x faster

This commit is contained in:
Serghey Rodin 2015-09-07 16:42:32 +03:00
commit 2850492f55
10 changed files with 194 additions and 179 deletions

View file

@ -1,6 +1,6 @@
#!/bin/bash
# info: add web domain
# options: USER DOMAIN IP [RESTART]
# options: USER DOMAIN IP [RESTART] [ALIASES] [PROXY_EXTENTIONS]
#
# The function adds virtual host to a server. In cases when a template is
# undefined in the script, the template "default" will be used. The alias of
@ -20,9 +20,12 @@ domain=$(idn -t --quiet -u "$2" )
domain=$(echo $domain | sed -e 's/\.*$//g' -e 's/^\.*//g')
domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
domain_idn=$(idn -t --quiet -a "$domain")
ip=$3
ip=$3; IP=$3
restart=$4
alises=$5
aliases=$5
default_extentions="jpg,jpeg,gif,png,ico,svg,css,zip,tgz,gz,rar,bz2,doc,xls,\
exe,pdf,ppt,txt,odt,ods,odp,odf,tar,wav,bmp,rtf,js,mp3,avi,mpeg,flv,html,htm"
extentions=${6-$default_extentions}
# Includes
source $VESTA/func/main.sh
@ -35,8 +38,8 @@ source $VESTA/conf/vesta.conf
# Verifications #
#----------------------------------------------------------#
check_args '3' "$#" 'USER DOMAIN IP [RESTART]'
validate_format 'user' 'domain' 'ip'
check_args '3' "$#" 'USER DOMAIN IP [RESTART] [ALIASES] [PROXY_EXTENTIONS]'
validate_format 'user' 'domain' 'ip' 'extentions'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
@ -46,41 +49,22 @@ is_ip_avalable
is_package_full 'WEB_DOMAINS'
template=$(get_user_value '$WEB_TEMPLATE')
is_web_template_valid
if [ ! -z "$aliases" ]; then
for domain_alias in $(echo "${aliases//,/ }"); do
is_domain_new 'web' "$domain_alias" 'alias'
done
fi
if [ ! -z "$PROXY_SYSTEM" ]; then
proxy=$(get_user_value '$PROXY_TEMPLATE')
is_proxy_template_valid $proxy
fi
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Define real ip in case of NAT
IP=$ip
ip=$(get_real_ip $ip)
# Defining domain aliases
ip_name=$(get_ip_name)
ip_name_idn=$(idn -t --quiet -a "$ip_name")
domain_alias="www.$domain"
domain_alias_idn="www.$domain_idn"
if [ ! -z "$ip_name" ]; then
domain_alias_dash="${domain//./-}.$ip_name"
domain_alias_dash_idn="${domain_idn//./-}.$ip_name_idn"
aliases="$domain_alias,$domain_alias_dash"
aliases_idn="$domain_alias_idn,$domain_alias_dash_idn"
alias_string="ServerAlias $domain_alias_idn $domain_alias_dash_idn"
else
aliases="$domain_alias"
aliases_idn="$domain_alias_idn"
alias_string="ServerAlias $domain_alias_idn"
fi
# Defining vars for add_config function
group="$user"
email="info@$domain"
docroot="$HOMEDIR/$user/web/$domain/public_html"
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$template.tpl"
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
# Checking backend
# Checking domain backend in case PHP-FPM is configured
if [ ! -z "$WEB_BACKEND" ]; then
is_web_backend_pool_valid
$BIN/v-add-web-domain-backend $user $domain
@ -89,8 +73,33 @@ if [ ! -z "$WEB_BACKEND" ]; then
exit $rc
fi
get_domain_backend_values
backend=$(get_user_value '$BACKEND_TEMPLATE')
fi
# Defining variables for add_config function
ip=$(get_real_ip $ip)
group="$user"
email="info@$domain"
docroot="$HOMEDIR/$user/web/$domain/public_html"
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$template.tpl"
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
# Defining domain aliases
ip_name=$(get_ip_name)
if [ -z "$aliases" ]; then
if [ -z "$ip_name" ]; then
aliases="www.$domain"
else
aliases="www.$domain,${domain//./-}.$ip_name"
fi
else
if [ ! -z "$ip_name" ]; then
aliases="$aliases,${domain//./-}.$ip_name"
fi
fi
aliases_idn=$(idn -t --quiet -a $aliases)
alias_string="ServerAlias ${aliases_idn//,/ }"
# Adding web config
add_web_config
@ -166,6 +175,23 @@ if [ -z "$web_include" ] && [ "$WEB_SYSTEM" = 'nginx' ]; then
echo "include $conf;" >> $web_conf
fi
# Checking proxy system
if [ ! -z "$PROXY_SYSTEM" ]; then
PROXY_EXT="$extentions"
tpl_file="$WEBTPL/$PROXY_SYSTEM/$proxy.tpl"
conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
add_web_config
chown root:$user $conf
chmod 640 $conf
proxy_conf="/etc/$PROXY_SYSTEM/conf.d/vesta.conf"
if [ -z "$(grep "$conf" $proxy_conf)" ]; then
echo "include $conf;" >> $proxy_conf
fi
if [ -x $WEBTPL/$PROXY_SYSTEM/$template.sh ]; then
$WEBTPL/$PROXY_SYSTEM/$template.sh $user $domain $ip $HOMEDIR $docroot
fi
fi
#----------------------------------------------------------#
# Vesta #
@ -177,10 +203,9 @@ increase_user_value "$user" '$U_WEB_DOMAINS'
increase_user_value "$user" '$U_WEB_ALIASES'
# Defining domain variables
BACKEND_TPL=$(grep BACKEND_TEMPLATE $USER_DATA/user.conf |cut -f 2 -d \')
str="DOMAIN='$domain' IP='$IP' IP6='' ALIAS='$aliases' TPL='$template'"
str="$str SSL='no' SSL_HOME='same' FTP_USER='' FTP_MD5=''"
str="$str BACKEND='$BACKEND_TPL' PROXY='' PROXY_EXT='' STATS='' STATS_USER=''"
str="$str SSL='no' SSL_HOME='same' FTP_USER='' FTP_MD5='' BACKEND='$backend'"
str="$str PROXY='$proxy' PROXY_EXT='$extentions' STATS='' STATS_USER=''"
str="$str STATS_CRYPT='' U_DISK='0' U_BANDWIDTH='0' SUSPENDED='no'"
str="$str TIME='$TIME' DATE='$DATE'"
@ -193,6 +218,12 @@ if [ "$restart" != 'no' ]; then
if [ $? -ne 0 ]; then
exit $E_RESTART
fi
if [ ! -z "$PROXY_SYSTEM" ]; then
$BIN/v-restart-proxy
if [ $? -ne 0 ]; then
exit $E_RESTART
fi
fi
fi
# Logging