v-add-database-host 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash
  2. # info: add new database server
  3. # options: TYPE HOST DBUSER DBPASS [MAX_DB] [CHARSETS] [TEMPLATE]
  4. #
  5. # The function add new database server to the server pool. It supports local
  6. # and remote database servers, which is useful for clusters. By adding a host
  7. # you can set limit for number of databases on a host. Template parameter is
  8. # used only for PostgreSQL and has an default value "template1". You can read
  9. # more about templates in official PostgreSQL documentation.
  10. #----------------------------------------------------------#
  11. # Variable&Function #
  12. #----------------------------------------------------------#
  13. # Argument definition
  14. type=$1
  15. host=$2
  16. dbuser=$3
  17. password=$4; HIDE=4
  18. max_db=${6-500}
  19. charsets=${7-UTF8,LATIN1,WIN1250,WIN1251,WIN1252,WIN1256,WIN1258,KOI8}
  20. template=${8-template1}
  21. # Includes
  22. source $VESTA/func/main.sh
  23. source $VESTA/func/db.sh
  24. source $VESTA/conf/vesta.conf
  25. is_mysql_host_alive() {
  26. mycnf=$(mktemp)
  27. echo "[client]">$mycnf
  28. echo "host='$HOST'" >> $mycnf
  29. echo "user='$USER'" >> $mycnf
  30. echo "password='$PASSWORD'" >> $mycnf
  31. chmod 600 $mycnf
  32. mysql --defaults-file=$mycnf -e 'SELECT VERSION()' >/dev/null 2>&1
  33. rm $mycnf
  34. if [ '0' -ne "$?" ]; then
  35. echo "Error: MySQL connection to $host failed"
  36. log_event "$E_CONNECT" "$EVENT"
  37. exit $E_CONNECT
  38. fi
  39. }
  40. is_pgsql_host_alive() {
  41. export PGPASSWORD="$dbpass"
  42. psql -h $host -U $dbuser -c "SELECT VERSION()" > /dev/null 2>&1
  43. if [ '0' -ne "$?" ]; then
  44. echo "Error: PostgreSQL connection to $host failed"
  45. log_event "$E_CONNECT" "$EVENT"
  46. exit $E_CONNECT
  47. fi
  48. }
  49. #----------------------------------------------------------#
  50. # Verifications #
  51. #----------------------------------------------------------#
  52. args_usage='TYPE HOST DBUSER DBPASS [MAX_DB] [CHARSETS] [TPL]'
  53. check_args '4' "$#" "$args_usage"
  54. validate_format 'host' 'dbuser' 'max_db' 'charsets' 'template'
  55. #is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
  56. #is_type_valid "$DB_SYSTEM" "$type"
  57. is_dbhost_new
  58. is_password_valid
  59. dbpass="$password"
  60. case $type in
  61. mysql) is_mysql_host_alive ;;
  62. pgsql) is_pgsql_host_alive ;;
  63. esac
  64. #----------------------------------------------------------#
  65. # Action #
  66. #----------------------------------------------------------#
  67. # Concatenating db host string
  68. case $type in
  69. mysql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass'";
  70. str="$str CHARSETS='$charsets' MAX_DB='$max_db' U_SYS_USERS=''";
  71. str="$str U_DB_BASES='0' SUSPENDED='no' TIME='$TIME' DATE='$DATE'";;
  72. pgsql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass'";
  73. str="$str CHARSETS='$charsets' TPL='$template' MAX_DB='$max_db'";
  74. str="$str U_SYS_USERS='' U_DB_BASES='0' SUSPENDED='no'";
  75. str="$str TIME='$TIME' DATE='$DATE'";;
  76. esac
  77. #----------------------------------------------------------#
  78. # Vesta #
  79. #----------------------------------------------------------#
  80. # Adding host to conf
  81. echo "$str" >> $VESTA/conf/$type.conf
  82. chmod 660 $VESTA/conf/$type.conf
  83. # Updating vesta.conf
  84. if [ -z "$(grep DB_SYSTEM $VESTA/conf/vesta.conf)" ]; then
  85. echo "DB_SYSTEM='$type'" >> $VESTA/conf/vesta.conf
  86. else
  87. db=$(echo "$DB_SYSTEM,$type" |\
  88. sed "s/,/\n/g"|\
  89. sort -r -u |\
  90. sed "/^$/d"|\
  91. sed ':a;N;$!ba;s/\n/,/g')
  92. sed -i "s/DB_SYSTEM=.*/DB_SYSTEM='$db'/g" $VESTA/conf/vesta.conf
  93. fi
  94. # Logging
  95. log_event "$OK" "$EVENT"
  96. exit