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