v-change-web-domain-backend-tpl 4.9 KB

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