v-add-database-host 4.7 KB

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