forked from outroll/vesta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv-change-web-domain-backend-tpl
executable file
·174 lines (141 loc) · 4.94 KB
/
v-change-web-domain-backend-tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
# info: change web domain backend template
# options: USER DOMAIN TEMPLATE [RESTART]
#
# The function changes backend template
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain_idn=$(idn -t --quiet -a "$domain")
template=$3
restart="$4"
# Includes
source $VESTA/func/main.sh
source $VESTA/func/domain.sh
source $VESTA/func/ip.sh
source $VESTA/conf/vesta.conf
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '3' "$#" 'USER DOMAIN TEMPLATE [RESTART]'
validate_format 'user' 'domain' 'template'
is_system_enabled "$WEB_BACKEND" 'WEB_BACKEND'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"
is_object_unsuspended 'web' 'DOMAIN' "$domain"
is_web_backend_template_valid $template
is_web_backend_pool_valid
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Deleting backend
rm -f $pool/$backend.conf
# Allocating backend port
backend_port=9000
ports=$(grep -v '^;' $pool/* 2>/dev/null |grep listen |grep -o :[0-9].*)
ports=$(echo "$ports" |sed "s/://" |sort -n)
for port in $ports; do
if [ "$backend_port" -eq "$port" ]; then
backend_port=$((backend_port + 1))
fi
done
# Changing backend config
cat $WEBTPL/$WEB_BACKEND/$template.tpl |\
sed -e "s|%backend_port%|$backend_port|" \
-e "s|%user%|$user|"\
-e "s|%domain%|$domain|"\
-e "s|%domain_idn%|$domain_idn|"\
-e "s|%backend%|$backend|g" > $pool/$backend.conf
# Checking backend pool configuration
if [ "$backend" = "$user" ]; then
conf=$USER_DATA/web.conf
fields='$DOMAIN'
nohead=1
for domain in $(shell_list); do
# Parsing domain values
get_domain_values 'web'
ip=$(get_real_ip $IP)
# Deleting old vhost
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.tpl"
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
del_web_config
# Deleting old ssl vhost
if [ "$SSL" = 'yes' ]; then
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.stpl"
conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
del_web_config
fi
# Adding new vhost
upd_web_domain_values
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.tpl"
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
add_web_config
# Adding new ssl vhost
if [ "$SSL" = 'yes' ]; then
conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.stpl"
add_web_config
fi
# Update config
add_object_key "web" 'DOMAIN' "$domain" 'BACKEND' 'PROXY'
update_object_value 'web' 'DOMAIN' "$domain" '$BACKEND' "$template"
done
# Chaning template in user config
old_template=$(grep BACKEND_TEMPLATE $USER_DATA/user.conf)
if [ -z "$old_template" ]; then
sed -i "s/^WEB_DOMAINS/BACKEND_TEMPLATE='$template'\nWEB_DOMAINS/g" \
$USER_DATA/user.conf
else
update_user_value "$user" '$BACKEND_TEMPLATE' "$template"
fi
else
# Parsing domain values
get_domain_values 'web'
ip=$(get_real_ip $IP)
# Deleting old vhost
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.tpl"
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
del_web_config
# Deleting old ssl vhost
if [ "$SSL" = 'yes' ]; then
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.stpl"
conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
del_web_config
fi
# Adding new vhost
upd_web_domain_values
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.tpl"
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
add_web_config
# Adding new ssl vhost
if [ "$SSL" = 'yes' ]; then
conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.stpl"
add_web_config
fi
# Update config
add_object_key "web" 'DOMAIN' "$domain" 'BACKEND' 'PROXY'
update_object_value 'web' 'DOMAIN' "$domain" '$BACKEND' "$template"
fi
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Restart web
if [ "$restart" != 'no' ]; then
$BIN/v-restart-web
if [ $? -ne 0 ]; then
exit $E_RESTART
fi
$BIN/v-restart-web-backend
if [ $? -ne 0 ]; then
exit $E_RESTART
fi
fi
# Logging
log_history "changed backend template for $domain to $template"
log_event "$OK" "$EVENT"
exit