freedombone-utils-postgresql 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. # shellcheck disable=SC2154
  50. chroot "$rootdir" apt-get -yq install "$POSTGRESQL_PACKAGES"
  51. if [ ! -d "$rootdir/etc/postgresql" ]; then
  52. echo $"ERROR: postgresql does not appear to have installed."
  53. exit 78352
  54. fi
  55. if [ ! -f "$rootdir/usr/bin/psql" ]; then
  56. echo $"ERROR: psql command does not appear to have installed."
  57. exit 835290
  58. fi
  59. }
  60. function install_postgresql {
  61. if [[ $VARIANT == "mesh"* ]]; then
  62. mesh_install_postgresql
  63. return
  64. fi
  65. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  66. return
  67. fi
  68. function_check get_postgresql_password
  69. get_postgresql_password
  70. if [ ! "$POSTGRESQL_PASSWORD" ]; then
  71. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  72. POSTGRESQL_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  73. else
  74. POSTGRESQL_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  75. fi
  76. fi
  77. "${PROJECT_NAME}-pass" -u root -a postgresql -p "$POSTGRESQL_PASSWORD"
  78. apt-get -yq install "$POSTGRESQL_PACKAGES"
  79. apt-get -yq remove --purge apache2-bin*
  80. if [ -d /etc/apache2 ]; then
  81. rm -rf /etc/apache2
  82. echo $'Removed Apache installation after postgresql install'
  83. fi
  84. if [ ! -d /etc/postgresql ]; then
  85. echo $"ERROR: postgresql does not appear to have installed."
  86. exit 78352
  87. fi
  88. if [ ! -f /usr/bin/psql ]; then
  89. echo $"ERROR: psql command does not appear to have installed."
  90. exit 835290
  91. fi
  92. mark_completed "${FUNCNAME[0]}"
  93. }
  94. function add_postgresql_user {
  95. postgresql_username=$1
  96. postgresql_password=$2
  97. cd /etc/postgresql || exit 2468246
  98. if [[ "$3" != 'encrypt'* ]]; then
  99. sudo -u postgres psql -c "create user $postgresql_username password '$postgresql_password';"
  100. else
  101. sudo -u postgres psql -c "create user $postgresql_username;"
  102. sudo -u postgres psql -c "ALTER user $postgresql_username with encrypted password '$postgresql_password';"
  103. fi
  104. }
  105. function remove_postgresql_user {
  106. postgresql_username=$1
  107. cd /etc/postgresql || exit 24624624
  108. sudo -u postgres psql -c "drop user $postgresql_username"
  109. }
  110. function drop_database_postgresql {
  111. database_name="$1"
  112. database_owner_name="$2"
  113. cd /etc/postgresql || exit 2482468242
  114. sudo -u postgres psql -c "drop database $database_name"
  115. if [ ${#database_owner_name} -gt 0 ]; then
  116. sudo -u postgres psql -c "drop user $database_owner_name"
  117. fi
  118. }
  119. function run_system_query_postgresql {
  120. query=$1
  121. cd /etc/postgresql || exit 24624649846
  122. sudo -u postgres psql -c "$query"
  123. }
  124. function run_query_postgresql {
  125. database_name=$1
  126. database_query=$2
  127. cd /etc/postgresql || exit 2492464684
  128. sudo -u postgres psql -d "$database_name" -c "$database_query"
  129. }
  130. function run_query_postgresql_with_output {
  131. database_name=$1
  132. database_query=$2
  133. cd /etc/postgresql || exit 2482462846
  134. output=$(sudo -u postgres psql -d "$database_name" -c "$database_query")
  135. echo "$output"
  136. }
  137. function initialise_database_postgresql {
  138. database_name=$1
  139. database_file=$2
  140. cd /etc/postgresql || exit 239246992469
  141. # shellcheck disable=SC2024
  142. if ! sudo -u postgres psql "$database_name" < "$database_file"; then
  143. exit 7238525
  144. fi
  145. }
  146. function create_database_postgresql {
  147. app_name="$1"
  148. app_admin_password="$2"
  149. app_admin_username=$3
  150. if [ ! -d "$INSTALL_DIR" ]; then
  151. mkdir "$INSTALL_DIR"
  152. fi
  153. if [ ! "$app_admin_username" ]; then
  154. app_admin_username=${app_name}admin
  155. fi
  156. echo "create database ${app_name};
  157. CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
  158. GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
  159. flush privileges;
  160. quit" > "$INSTALL_DIR/batch.sql"
  161. chmod 600 "$INSTALL_DIR/batch.sql"
  162. cd /etc/postgresql || exit 247284684
  163. sudo -u postgres psql -d "$database_name" --file="$INSTALL_DIR/batch.sql"
  164. shred -zu "$INSTALL_DIR/batch.sql"
  165. }
  166. # NOTE: deliberately there is no "exit 0"