v-add-database-host 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 defenition
  14. type=$1
  15. host=$2
  16. dbuser=$3
  17. password=$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. # Hiding password
  26. A4='******'
  27. EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
  28. is_mysql_host_alive() {
  29. mycnf=$(mktemp)
  30. echo "[client]">$mycnf
  31. echo "host='$HOST'" >> $mycnf
  32. echo "user='$USER'" >> $mycnf
  33. echo "password='$PASSWORD'" >> $mycnf
  34. chmod 600 $mycnf
  35. mysql --defaults-file=$mycnf -e 'SELECT VERSION()' >/dev/null 2>&1
  36. rm $mycnf
  37. if [ '0' -ne "$?" ]; then
  38. echo "Error: MySQL connection to $host failed"
  39. log_event "$E_CONNECT" "$EVENT"
  40. exit $E_CONNECT
  41. fi
  42. }
  43. is_pgsql_host_alive() {
  44. export PGPASSWORD="$dbpass"
  45. psql -h $host -U $dbuser -c "SELECT VERSION()" > /dev/null 2>&1
  46. if [ '0' -ne "$?" ]; then
  47. echo "Error: PostgreSQL connection to $host failed"
  48. log_event "$E_CONNECT" "$EVENT"
  49. exit $E_CONNECT
  50. fi
  51. }
  52. #----------------------------------------------------------#
  53. # Verifications #
  54. #----------------------------------------------------------#
  55. args_usage='TYPE HOST DBUSER DBPASS [MAX_DB] [CHARSETS] [TPL]'
  56. check_args '4' "$#" "$args_usage"
  57. validate_format 'host' 'dbuser' 'max_db' 'charsets' 'template'
  58. #is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
  59. #is_type_valid "$DB_SYSTEM" "$type"
  60. is_dbhost_new
  61. is_password_valid
  62. dbpass="$password"
  63. case $type in
  64. mysql) is_mysql_host_alive ;;
  65. pgsql) is_pgsql_host_alive ;;
  66. esac
  67. #----------------------------------------------------------#
  68. # Action #
  69. #----------------------------------------------------------#
  70. # Concatentating db host string
  71. case $type in
  72. mysql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass'";
  73. str="$str CHARSETS='$charsets' MAX_DB='$max_db' U_SYS_USERS=''";
  74. str="$str U_DB_BASES='0' SUSPENDED='no' TIME='$TIME' DATE='$DATE'";;
  75. pgsql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass'";
  76. str="$str CHARSETS='$charsets' TPL='$template' MAX_DB='$max_db'";
  77. str="$str U_SYS_USERS='' U_DB_BASES='0' SUSPENDED='no'";
  78. str="$str TIME='$TIME' DATE='$DATE'";;
  79. esac
  80. #----------------------------------------------------------#
  81. # Vesta #
  82. #----------------------------------------------------------#
  83. # Adding host to conf
  84. echo "$str" >> $VESTA/conf/$type.conf
  85. chmod 660 $VESTA/conf/$type.conf
  86. # Updating vesta.conf
  87. if [ -z "$(grep DB_SYSTEM $VESTA/conf/vesta.conf)" ]; then
  88. echo "DB_SYSTEM='$type'" >> $VESTA/conf/vesta.conf
  89. else
  90. db=$(echo "$DB_SYSTEM,$type" |\
  91. sed "s/,/\n/g"|\
  92. sort -r -u |\
  93. sed "/^$/d"|\
  94. sed ':a;N;$!ba;s/\n/,/g')
  95. sed -i "s/DB_SYSTEM=.*/DB_SYSTEM='$db'/g" $VESTA/conf/vesta.conf
  96. fi
  97. # Logging
  98. log_event "$OK" "$EVENT"
  99. exit