Skip to content

Commit 2850492

Browse files
committed
optimized web domain adding (almost 2x faster
1 parent 1246ded commit 2850492

File tree

10 files changed

+192
-177
lines changed

10 files changed

+192
-177
lines changed

bin/v-add-dns-on-web-alias

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
2-
# info: add dns domain or dns record based on web domain alias restart
3-
# options: USER DOMAIN
2+
# info: add dns domain or dns record after web domain alias
3+
# options: USER ALIAS IP [RESTART]
44
#
55
# The function adds dns domain or dns record based on web domain alias.
66

@@ -11,13 +11,9 @@
1111

1212
# Argument defenition
1313
user=$1
14-
domain=$(echo $2 | sed -e 's/\.*$//g' -e 's/^\.*//g')
15-
domain_idn=$(idn -t --quiet -a "$domain")
16-
dom_alias=$(idn -t --quiet -u "$3" )
17-
dom_alias=$(echo $dom_alias | sed -e 's/\.*$//g' -e 's/^\.*//g')
18-
dom_alias=$(echo $dom_alias | tr '[:upper:]' '[:lower:]')
19-
dom_alias_idn=$(idn -t --quiet -a "$dom_alias" )
20-
restart="$4"
14+
alias=$2
15+
ip=$3
16+
restart=$4
2117

2218
# Includes
2319
source $VESTA/func/main.sh
@@ -29,65 +25,61 @@ source $VESTA/conf/vesta.conf
2925
# Verifications #
3026
#----------------------------------------------------------#
3127

32-
check_args '3' "$#" 'USER DOMAIN ALIAS'
33-
validate_format 'user' 'domain'
34-
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
28+
check_args '3' "$#" 'USER ALIAS IP [RESTART]'
29+
validate_format 'user' 'alias' 'ip'
3530
is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
3631
is_object_valid 'user' 'USER' "$user"
3732
is_object_unsuspended 'user' 'USER' "$user"
38-
is_object_valid 'web' 'DOMAIN' "$domain"
39-
is_object_unsuspended 'web' 'DOMAIN' "$domain"
33+
if [ -e "$USER_DATA/dns/$alias.conf" ]; then
34+
exit
35+
fi
4036

4137

4238
#----------------------------------------------------------#
4339
# Action #
4440
#----------------------------------------------------------#
4541

46-
# Parsing domain values
47-
get_domain_values 'web'
42+
# Logging
43+
log_event "$OK" "$EVENT"
44+
45+
# Define additional vars
46+
sub_domain=$(echo "$alias" |awk -F '.' '{print $1}')
47+
top_domain=$(echo "$alias" |sed -e "s/^$sub_domain.//")
48+
domain_lvl=$(echo "$alias" |grep -o "\." |wc -l)
49+
50+
# Adding second level domain
51+
if [ "$domain_lvl" -eq 1 ] || [ "${#top_domain}" -le '6' ]; then
52+
$BIN/v-add-dns-domain \
53+
$user $alias $ip '' '' '' '' '' $restart >> /dev/null
54+
exit
55+
fi
56+
57+
# Adding toplevel domain and then its sub
58+
$BIN/v-add-dns-domain $user $top_domain $ip '' '' '' '' $restart >> /dev/null
4859

49-
# Check if it a simple domain
50-
if [ $(echo -e "${dom_alias//\./\n}" | wc -l) -le 2 ]; then
51-
if [ ! -e "$USER_DATA/dns/$dom_alias.conf" ]; then
52-
$BIN/v-add-dns-domain \
53-
$user $dom_alias $IP '' '' '' '' '' $restart > /dev/null
54-
fi
60+
# Checking top-level domain
61+
if [ ! -e "$USER_DATA/dns/$top_domain.conf" ]; then
62+
exit
63+
fi
64+
65+
# Checking subdomain record
66+
if [ "$sub_domain" == '*' ]; then
67+
check_record=$(grep -w "RECORD='\*'" $USER_DATA/dns/$top_domain.conf)
5568
else
56-
# Check subdomain
57-
sub=$(echo "$dom_alias" | cut -f1 -d . -s)
58-
dom=$(echo "$dom_alias" | sed -e "s/^$sub.//" )
59-
60-
# Ignore short domains like co.uk, com.au and so on
61-
if [ "${#dom}" -le '6' ]; then
62-
exit
63-
fi
64-
65-
if [ ! -e "$USER_DATA/dns/$dom.conf" ]; then
66-
$BIN/v-add-dns-domain \
67-
$user $dom $IP '' '' '' '' $restart > /dev/null
68-
69-
if [ $? -eq 0 ]; then
70-
$BIN/v-add-dns-record \
71-
$user $dom "$sub" A $IP '' '' $restart
72-
fi
73-
else
74-
if [ "$sub" == '*' ]; then
75-
rec=$(grep -w "RECORD='\*'" $USER_DATA/dns/$dom.conf)
76-
else
77-
rec=$(grep -w "RECORD='$sub'" $USER_DATA/dns/$dom.conf)
78-
fi
79-
if [ -z "$rec" ]; then
80-
$BIN/v-add-dns-record \
81-
$user $dom "$sub" A $IP '' '' $restart > /dev/null
82-
fi
83-
fi
69+
check_record=$(grep -w "RECORD='$sub_domain'" $USER_DATA/dns/$top_domain.conf)
70+
fi
71+
72+
# Adding subdomain record
73+
if [ -z "$check_record" ]; then
74+
$BIN/v-add-dns-record \
75+
$user $top_domain "$sub_domain" A $ip '' '' $restart >> /dev/null
8476
fi
8577

8678

8779
#----------------------------------------------------------#
8880
# Vesta #
8981
#----------------------------------------------------------#
9082

91-
# No Logging
83+
# No logging
9284

9385
exit

bin/v-add-web-domain

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# info: add web domain
3-
# options: USER DOMAIN IP [RESTART]
3+
# options: USER DOMAIN IP [RESTART] [ALIASES] [PROXY_EXTENTIONS]
44
#
55
# The function adds virtual host to a server. In cases when a template is
66
# undefined in the script, the template "default" will be used. The alias of
@@ -20,9 +20,12 @@ domain=$(idn -t --quiet -u "$2" )
2020
domain=$(echo $domain | sed -e 's/\.*$//g' -e 's/^\.*//g')
2121
domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
2222
domain_idn=$(idn -t --quiet -a "$domain")
23-
ip=$3
23+
ip=$3; IP=$3
2424
restart=$4
25-
alises=$5
25+
aliases=$5
26+
default_extentions="jpg,jpeg,gif,png,ico,svg,css,zip,tgz,gz,rar,bz2,doc,xls,\
27+
exe,pdf,ppt,txt,odt,ods,odp,odf,tar,wav,bmp,rtf,js,mp3,avi,mpeg,flv,html,htm"
28+
extentions=${6-$default_extentions}
2629

2730
# Includes
2831
source $VESTA/func/main.sh
@@ -35,8 +38,8 @@ source $VESTA/conf/vesta.conf
3538
# Verifications #
3639
#----------------------------------------------------------#
3740

38-
check_args '3' "$#" 'USER DOMAIN IP [RESTART]'
39-
validate_format 'user' 'domain' 'ip'
41+
check_args '3' "$#" 'USER DOMAIN IP [RESTART] [ALIASES] [PROXY_EXTENTIONS]'
42+
validate_format 'user' 'domain' 'ip' 'extentions'
4043
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
4144
is_object_valid 'user' 'USER' "$user"
4245
is_object_unsuspended 'user' 'USER' "$user"
@@ -46,50 +49,56 @@ is_ip_avalable
4649
is_package_full 'WEB_DOMAINS'
4750
template=$(get_user_value '$WEB_TEMPLATE')
4851
is_web_template_valid
52+
if [ ! -z "$aliases" ]; then
53+
for domain_alias in $(echo "${aliases//,/ }"); do
54+
is_domain_new 'web' "$domain_alias" 'alias'
55+
done
56+
fi
57+
if [ ! -z "$PROXY_SYSTEM" ]; then
58+
proxy=$(get_user_value '$PROXY_TEMPLATE')
59+
is_proxy_template_valid $proxy
60+
fi
4961

5062

5163
#----------------------------------------------------------#
5264
# Action #
5365
#----------------------------------------------------------#
5466

55-
# Define real ip in case of NAT
56-
IP=$ip
57-
ip=$(get_real_ip $ip)
58-
59-
# Defining domain aliases
60-
ip_name=$(get_ip_name)
61-
ip_name_idn=$(idn -t --quiet -a "$ip_name")
62-
domain_alias="www.$domain"
63-
domain_alias_idn="www.$domain_idn"
64-
if [ ! -z "$ip_name" ]; then
65-
domain_alias_dash="${domain//./-}.$ip_name"
66-
domain_alias_dash_idn="${domain_idn//./-}.$ip_name_idn"
67-
aliases="$domain_alias,$domain_alias_dash"
68-
aliases_idn="$domain_alias_idn,$domain_alias_dash_idn"
69-
alias_string="ServerAlias $domain_alias_idn $domain_alias_dash_idn"
70-
else
71-
aliases="$domain_alias"
72-
aliases_idn="$domain_alias_idn"
73-
alias_string="ServerAlias $domain_alias_idn"
67+
# Checking domain backend in case PHP-FPM is configured
68+
if [ ! -z "$WEB_BACKEND" ]; then
69+
is_web_backend_pool_valid
70+
$BIN/v-add-web-domain-backend $user $domain
71+
rc=$?
72+
if [ $rc -ne 0 ]; then
73+
exit $rc
74+
fi
75+
get_domain_backend_values
76+
backend=$(get_user_value '$BACKEND_TEMPLATE')
7477
fi
7578

76-
# Defining vars for add_config function
79+
# Defining variables for add_config function
80+
ip=$(get_real_ip $ip)
7781
group="$user"
7882
email="info@$domain"
7983
docroot="$HOMEDIR/$user/web/$domain/public_html"
8084
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$template.tpl"
8185
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
8286

83-
# Checking backend
84-
if [ ! -z "$WEB_BACKEND" ]; then
85-
is_web_backend_pool_valid
86-
$BIN/v-add-web-domain-backend $user $domain
87-
rc=$?
88-
if [ $rc -ne 0 ]; then
89-
exit $rc
87+
# Defining domain aliases
88+
ip_name=$(get_ip_name)
89+
if [ -z "$aliases" ]; then
90+
if [ -z "$ip_name" ]; then
91+
aliases="www.$domain"
92+
else
93+
aliases="www.$domain,${domain//./-}.$ip_name"
94+
fi
95+
else
96+
if [ ! -z "$ip_name" ]; then
97+
aliases="$aliases,${domain//./-}.$ip_name"
9098
fi
91-
get_domain_backend_values
9299
fi
100+
aliases_idn=$(idn -t --quiet -a $aliases)
101+
alias_string="ServerAlias ${aliases_idn//,/ }"
93102

94103
# Adding web config
95104
add_web_config
@@ -166,6 +175,23 @@ if [ -z "$web_include" ] && [ "$WEB_SYSTEM" = 'nginx' ]; then
166175
echo "include $conf;" >> $web_conf
167176
fi
168177

178+
# Checking proxy system
179+
if [ ! -z "$PROXY_SYSTEM" ]; then
180+
PROXY_EXT="$extentions"
181+
tpl_file="$WEBTPL/$PROXY_SYSTEM/$proxy.tpl"
182+
conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
183+
add_web_config
184+
chown root:$user $conf
185+
chmod 640 $conf
186+
proxy_conf="/etc/$PROXY_SYSTEM/conf.d/vesta.conf"
187+
if [ -z "$(grep "$conf" $proxy_conf)" ]; then
188+
echo "include $conf;" >> $proxy_conf
189+
fi
190+
if [ -x $WEBTPL/$PROXY_SYSTEM/$template.sh ]; then
191+
$WEBTPL/$PROXY_SYSTEM/$template.sh $user $domain $ip $HOMEDIR $docroot
192+
fi
193+
fi
194+
169195

170196
#----------------------------------------------------------#
171197
# Vesta #
@@ -177,10 +203,9 @@ increase_user_value "$user" '$U_WEB_DOMAINS'
177203
increase_user_value "$user" '$U_WEB_ALIASES'
178204

179205
# Defining domain variables
180-
BACKEND_TPL=$(grep BACKEND_TEMPLATE $USER_DATA/user.conf |cut -f 2 -d \')
181206
str="DOMAIN='$domain' IP='$IP' IP6='' ALIAS='$aliases' TPL='$template'"
182-
str="$str SSL='no' SSL_HOME='same' FTP_USER='' FTP_MD5=''"
183-
str="$str BACKEND='$BACKEND_TPL' PROXY='' PROXY_EXT='' STATS='' STATS_USER=''"
207+
str="$str SSL='no' SSL_HOME='same' FTP_USER='' FTP_MD5='' BACKEND='$backend'"
208+
str="$str PROXY='$proxy' PROXY_EXT='$extentions' STATS='' STATS_USER=''"
184209
str="$str STATS_CRYPT='' U_DISK='0' U_BANDWIDTH='0' SUSPENDED='no'"
185210
str="$str TIME='$TIME' DATE='$DATE'"
186211

@@ -193,6 +218,12 @@ if [ "$restart" != 'no' ]; then
193218
if [ $? -ne 0 ]; then
194219
exit $E_RESTART
195220
fi
221+
if [ ! -z "$PROXY_SYSTEM" ]; then
222+
$BIN/v-restart-proxy
223+
if [ $? -ne 0 ]; then
224+
exit $E_RESTART
225+
fi
226+
fi
196227
fi
197228

198229
# Logging

bin/v-add-web-domain-alias

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ is_object_valid 'user' 'USER' "$user"
3939
is_object_unsuspended 'user' 'USER' "$user"
4040
is_object_valid 'web' 'DOMAIN' "$domain"
4141
is_object_unsuspended 'web' 'DOMAIN' "$domain"
42-
is_domain_new 'web' "$dom_alias"
42+
is_domain_new 'web' "$dom_alias" 'alias'
4343
is_package_full 'WEB_ALIASES'
4444

4545

bin/v-add-web-domain-proxy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ is_object_value_empty 'web' 'DOMAIN' "$domain" '$PROXY'
4242
if [ -z $template ]; then
4343
template=$(get_user_value '$PROXY_TEMPLATE')
4444
fi
45-
is_proxy_template_valid
45+
is_proxy_template_valid $template
4646

4747

4848
#----------------------------------------------------------#

bin/v-change-user-template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ is_object_unsuspended 'user' 'USER' "$user"
3737
case $type in
3838
WEB) is_web_template_valid;
3939
update_user_value "$user" '$WEB_TEMPLATE' "$template";;
40-
PROXY) is_proxy_template_valid;
40+
PROXY) is_proxy_template_valid $template;
4141
update_user_value "$user" '$PROXY_TEMPLATE' "$template";;
4242
DNS) is_dns_template_valid;
4343
update_user_value "$user" '$DNS_TEMPLATE' "$template";;

bin/v-change-web-domain-proxy-tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ is_object_unsuspended 'user' 'USER' "$user"
3939
is_object_valid 'web' 'DOMAIN' "$domain"
4040
is_object_unsuspended 'web' 'DOMAIN' "$domain"
4141
is_object_value_exist 'web' 'DOMAIN' "$domain" '$PROXY'
42-
is_proxy_template_valid
42+
is_proxy_template_valid $template
4343

4444

4545
#----------------------------------------------------------#

0 commit comments

Comments
 (0)