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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 definition
  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. is_format_valid '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_backend_template_valid $template
  31. #----------------------------------------------------------#
  32. # Action #
  33. #----------------------------------------------------------#
  34. prepare_web_backend
  35. # Deleting backend
  36. rm -f $pool/$backend_type.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_type|g" > $pool/$backend_type.conf
  53. # Checking backend pool configuration
  54. if [ "$backend_type" = "$user" ]; then
  55. conf=$USER_DATA/web.conf
  56. fields='$DOMAIN'
  57. nohead=1
  58. for domain in $(shell_list); do
  59. get_domain_values 'web'
  60. local_ip=$(get_real_ip $IP)
  61. prepare_web_domain_values
  62. # Rebuilding vhost
  63. del_web_config "$WEB_SYSTEM" "$TPL.tpl"
  64. add_web_config "$WEB_SYSTEM" "$TPL.tpl"
  65. if [ "$SSL" = 'yes' ]; then
  66. del_web_config "$WEB_SYSTEM" "$TPL.stpl"
  67. add_web_config "$WEB_SYSTEM" "$TPL.stpl"
  68. fi
  69. # Rebuilding proxy configuration
  70. if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then
  71. del_web_config "$PROXY_SYSTEM" "$PROXY.tpl"
  72. add_web_config "$PROXY_SYSTEM" "$PROXY.tpl"
  73. if [ "$SSL" = 'yes' ]; then
  74. del_web_config "$PROXY_SYSTEM" "$PROXY.stpl"
  75. add_web_config "$PROXY_SYSTEM" "$PROXY.stpl"
  76. fi
  77. fi
  78. # Update config
  79. add_object_key "web" 'DOMAIN' "$domain" 'BACKEND' 'PROXY'
  80. update_object_value 'web' 'DOMAIN' "$domain" '$BACKEND' "$template"
  81. done
  82. # Chaning template in user config
  83. old_template=$(grep BACKEND_TEMPLATE $USER_DATA/user.conf)
  84. if [ -z "$old_template" ]; then
  85. sed -i "s/^WEB_DOMAINS/BACKEND_TEMPLATE='$template'\nWEB_DOMAINS/g" \
  86. $USER_DATA/user.conf
  87. else
  88. update_user_value "$user" '$BACKEND_TEMPLATE' "$template"
  89. fi
  90. else
  91. # Parsing domain values
  92. get_domain_values 'web'
  93. local_ip=$(get_real_ip $IP)
  94. prepare_web_domain_values
  95. # Rebuilding vhost
  96. del_web_config "$WEB_SYSTEM" "$TPL.tpl"
  97. add_web_config "$WEB_SYSTEM" "$TPL.tpl"
  98. if [ "$SSL" = 'yes' ]; then
  99. del_web_config "$WEB_SYSTEM" "$TPL.stpl"
  100. add_web_config "$WEB_SYSTEM" "$TPL.stpl"
  101. fi
  102. # Rebuilding proxy configuration
  103. if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then
  104. del_web_config "$PROXY_SYSTEM" "$PROXY.tpl"
  105. add_web_config "$PROXY_SYSTEM" "$PROXY.tpl"
  106. if [ "$SSL" = 'yes' ]; then
  107. del_web_config "$PROXY_SYSTEM" "$PROXY.stpl"
  108. add_web_config "$PROXY_SYSTEM" "$PROXY.stpl"
  109. fi
  110. fi
  111. # Update config
  112. add_object_key "web" 'DOMAIN' "$domain" 'BACKEND' 'PROXY'
  113. update_object_value 'web' 'DOMAIN' "$domain" '$BACKEND' "$template"
  114. fi
  115. #----------------------------------------------------------#
  116. # Vesta #
  117. #----------------------------------------------------------#
  118. # Restarting web
  119. if [ "$restart" != 'no' ]; then
  120. $BIN/v-restart-web
  121. check_result $? "Web restart failed" >/dev/null
  122. $BIN/v-restart-web-backend
  123. check_result $? "Web backend restart failed" >/dev/null
  124. fi
  125. # Logging
  126. log_history "changed backend template for $domain to $template"
  127. log_event "$OK" "$ARGUMENTS"
  128. exit