freedombone-utils-postgresql 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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-2018 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. POSTGRESQL_PACKAGES='postgresql-9.6 postgresql-contrib-9.6 postgresql-client'
  33. function store_original_postgresql_password {
  34. if [ ! -f /root/.postgresqloriginal ]; then
  35. echo $'Storing original postgresql password'
  36. ORIGINAL_POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
  37. # We can store this in plaintext because it will soon be of historical interest only
  38. echo -n "$ORIGINAL_POSTGRESQL_PASSWORD" > /root/.postgresqloriginal
  39. fi
  40. }
  41. function get_postgresql_password {
  42. POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
  43. if [[ "$POSTGRESQL_PASSWORD" == *'failed'* ]]; then
  44. echo $'Could not obtain postgresql password'
  45. exit 7835272
  46. fi
  47. }
  48. function mesh_install_postgresql {
  49. chroot "$rootdir" apt-get -yq install $POSTGRESQL_PACKAGES
  50. if [ ! -d $rootdir/etc/postgresql ]; then
  51. echo $"ERROR: postgresql does not appear to have installed."
  52. exit 78352
  53. fi
  54. if [ ! -f $rootdir/usr/bin/psql ]; then
  55. echo $"ERROR: psql command does not appear to have installed."
  56. exit 835290
  57. fi
  58. }
  59. function install_postgresql {
  60. if [[ $VARIANT == "mesh"* ]]; then
  61. mesh_install_postgresql
  62. return
  63. fi
  64. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  65. return
  66. fi
  67. function_check get_postgresql_password
  68. get_postgresql_password
  69. if [ ! $POSTGRESQL_PASSWORD ]; then
  70. if [ -f $IMAGE_PASSWORD_FILE ]; then
  71. POSTGRESQL_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  72. else
  73. POSTGRESQL_PASSWORD="$(openssl rand -base64 32 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
  74. fi
  75. fi
  76. ${PROJECT_NAME}-pass -u root -a postgresql -p "$POSTGRESQL_PASSWORD"
  77. apt-get -yq install $POSTGRESQL_PACKAGES
  78. apt-get -yq remove --purge apache2-bin*
  79. if [ -d /etc/apache2 ]; then
  80. rm -rf /etc/apache2
  81. echo $'Removed Apache installation after postgresql install'
  82. fi
  83. if [ ! -d /etc/postgresql ]; then
  84. echo $"ERROR: postgresql does not appear to have installed."
  85. exit 78352
  86. fi
  87. if [ ! -f /usr/bin/psql ]; then
  88. echo $"ERROR: psql command does not appear to have installed."
  89. exit 835290
  90. fi
  91. mark_completed $FUNCNAME
  92. }
  93. function add_postgresql_user {
  94. postgresql_username=$1
  95. postgresql_password=$2
  96. cd /etc/postgresql
  97. if [[ "$3" != 'encrypt'* ]]; then
  98. sudo -u postgres psql -c "create user $postgresql_username password '$postgresql_password';"
  99. else
  100. sudo -u postgres psql -c "create user $postgresql_username;"
  101. sudo -u postgres psql -c "ALTER user $postgresql_username with encrypted password '$postgresql_password';"
  102. fi
  103. }
  104. function remove_postgresql_user {
  105. postgresql_username=$1
  106. cd /etc/postgresql
  107. sudo -u postgres psql -c "drop user $postgresql_username"
  108. }
  109. function drop_database_postgresql {
  110. database_name="$1"
  111. database_owner_name="$2"
  112. cd /etc/postgresql
  113. sudo -u postgres psql -c "drop database $database_name"
  114. if [ ${#database_owner_name} -gt 0 ]; then
  115. sudo -u postgres psql -c "drop user $database_owner_name"
  116. fi
  117. }
  118. function run_system_query_postgresql {
  119. query=$1
  120. cd /etc/postgresql
  121. sudo -u postgres psql -c "$query"
  122. }
  123. function run_query_postgresql {
  124. database_name=$1
  125. database_query=$2
  126. cd /etc/postgresql
  127. sudo -u postgres psql -d $database_name -c "$database_query"
  128. }
  129. function run_query_postgresql_with_output {
  130. database_name=$1
  131. database_query=$2
  132. cd /etc/postgresql
  133. output=$(sudo -u postgres psql -d $database_name -c "$database_query")
  134. echo "$output"
  135. }
  136. function initialise_database_postgresql {
  137. database_name=$1
  138. database_file=$2
  139. cd /etc/postgresql
  140. sudo -u postgres psql $database_name < $database_file
  141. if [ ! "$?" = "0" ]; then
  142. exit 7238525
  143. fi
  144. }
  145. function create_database_postgresql {
  146. app_name="$1"
  147. app_admin_password="$2"
  148. app_admin_username=$3
  149. if [ ! -d $INSTALL_DIR ]; then
  150. mkdir $INSTALL_DIR
  151. fi
  152. if [ ! $app_admin_username ]; then
  153. app_admin_username=${app_name}admin
  154. fi
  155. echo "create database ${app_name};
  156. CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
  157. GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
  158. flush privileges;
  159. quit" > $INSTALL_DIR/batch.sql
  160. chmod 600 $INSTALL_DIR/batch.sql
  161. cd /etc/postgresql
  162. sudo -u postgres psql -d $database_name --file=$INSTALL_DIR/batch.sql
  163. shred -zu $INSTALL_DIR/batch.sql
  164. }