freedombone-utils-postgresql 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # postgresql database functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # Set this when calling backup and restore commands
  31. USE_POSTGRESQL=
  32. function store_original_postgresql_password {
  33. if [ ! -f /root/.postgresqloriginal ]; then
  34. echo $'Storing original postgresql password'
  35. ORIGINAL_POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
  36. # We can store this in plaintext because it will soon be of historical interest only
  37. echo -n "$ORIGINAL_POSTGRESQL_PASSWORD" > /root/.postgresqloriginal
  38. fi
  39. }
  40. function get_postgresql_password {
  41. POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
  42. if [[ "$POSTGRESQL_PASSWORD" == *'failed'* ]]; then
  43. echo $'Could not obtain postgresql password'
  44. exit 7835272
  45. fi
  46. }
  47. function install_postgresql {
  48. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  49. return
  50. fi
  51. function_check get_postgresql_password
  52. get_postgresql_password
  53. if [ ! $POSTGRESQL_PASSWORD ]; then
  54. if [ -f $IMAGE_PASSWORD_FILE ]; then
  55. POSTGRESQL_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  56. else
  57. POSTGRESQL_PASSWORD="$(openssl rand -base64 32 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
  58. fi
  59. fi
  60. ${PROJECT_NAME}-pass -u root -a postgresql -p "$POSTGRESQL_PASSWORD"
  61. apt-get -yq install postgresql postgresql-contrib postgresql-client
  62. apt-get -yq remove --purge apache2-bin*
  63. if [ -d /etc/apache2 ]; then
  64. rm -rf /etc/apache2
  65. echo $'Removed Apache installation after postgresql install'
  66. fi
  67. if [ ! -d /etc/postgresql ]; then
  68. echo $"ERROR: postgresql does not appear to have installed. $CHECK_MESSAGE"
  69. exit 78352
  70. fi
  71. if [ ! -f /usr/bin/psql ]; then
  72. echo $"ERROR: psql command does not appear to have installed. $CHECK_MESSAGE"
  73. exit 835290
  74. fi
  75. mark_completed $FUNCNAME
  76. }
  77. function add_postgresql_user {
  78. postgresql_username=$1
  79. postgresql_password=$2
  80. if [[ "$3" != 'encrypt'* ]]; then
  81. sudo -u postgres psql -c "create user $postgresql_username password '$postgresql_password';"
  82. else
  83. sudo -u postgres psql -c "create user $postgresql_username;"
  84. sudo -u postgres psql -c "ALTER user $postgresql_username with encrypted password '$postgresql_password';"
  85. fi
  86. }
  87. function remove_postgresql_user {
  88. postgresql_username=$1
  89. sudo -u postgres psql -c "drop user $postgresql_username"
  90. }
  91. function drop_database_postgresql {
  92. database_name="$1"
  93. sudo -u postgres psql -c "drop database $database_name"
  94. }
  95. function run_system_query_postgresql {
  96. query=$1
  97. sudo -u postgres psql -c "$query"
  98. }
  99. function run_query_postgresql {
  100. database_name=$1
  101. database_query=$2
  102. sudo -u postgres psql -d $database_name -c "$database_query"
  103. }
  104. function run_query_postgresql_with_output {
  105. database_name=$1
  106. database_query=$2
  107. output=$(sudo -u postgres psql -d $database_name -c << EOF
  108. use $database_name;
  109. $database_query
  110. EOF
  111. )
  112. echo "$output"
  113. }
  114. function initialise_database_postgresql {
  115. database_name=$1
  116. database_file=$2
  117. sudo -u postgres psql $database_name < $database_file
  118. if [ ! "$?" = "0" ]; then
  119. exit 7238525
  120. fi
  121. }
  122. function create_database_postgresql {
  123. app_name="$1"
  124. app_admin_password="$2"
  125. app_admin_username=$3
  126. if [ ! -d $INSTALL_DIR ]; then
  127. mkdir $INSTALL_DIR
  128. fi
  129. if [ ! $app_admin_username ]; then
  130. app_admin_username=${app_name}admin
  131. fi
  132. echo "create database ${app_name};
  133. CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
  134. GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
  135. flush privileges;
  136. quit" > $INSTALL_DIR/batch.sql
  137. chmod 600 $INSTALL_DIR/batch.sql
  138. sudo -u postgres psql -d $database_name --file=$INSTALL_DIR/batch.sql
  139. shred -zu $INSTALL_DIR/batch.sql
  140. }