v-add-database-host 4.0 KB

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