freedombone-utils-postgresql 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 image_install_postgresql {
  49. # shellcheck disable=SC2154,SC2086
  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. image_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. # shellcheck disable=SC2086
  79. apt-get -yq install $POSTGRESQL_PACKAGES
  80. apt-get -yq remove --purge apache2-bin*
  81. if [ -d /etc/apache2 ]; then
  82. rm -rf /etc/apache2
  83. echo $'Removed Apache installation after postgresql install'
  84. fi
  85. if [ ! -d /etc/postgresql ]; then
  86. echo $"ERROR: postgresql does not appear to have installed."
  87. exit 78352
  88. fi
  89. if [ ! -f /usr/bin/psql ]; then
  90. echo $"ERROR: psql command does not appear to have installed."
  91. exit 835290
  92. fi
  93. mark_completed "${FUNCNAME[0]}"
  94. }
  95. function add_postgresql_user {
  96. postgresql_username=$1
  97. postgresql_password=$2
  98. cd /etc/postgresql || exit 2468246
  99. if [[ "$3" != 'encrypt'* ]]; then
  100. sudo -u postgres psql -c "create user $postgresql_username password '$postgresql_password';"
  101. else
  102. sudo -u postgres psql -c "create user $postgresql_username;"
  103. sudo -u postgres psql -c "ALTER user $postgresql_username with encrypted password '$postgresql_password';"
  104. fi
  105. }
  106. function remove_postgresql_user {
  107. postgresql_username=$1
  108. cd /etc/postgresql || exit 24624624
  109. sudo -u postgres psql -c "drop user $postgresql_username"
  110. }
  111. function drop_database_postgresql {
  112. database_name="$1"
  113. database_owner_name="$2"
  114. cd /etc/postgresql || exit 2482468242
  115. sudo -u postgres psql -c "drop database $database_name"
  116. if [ ${#database_owner_name} -gt 0 ]; then
  117. sudo -u postgres psql -c "drop user $database_owner_name"
  118. fi
  119. }
  120. function run_system_query_postgresql {
  121. query=$1
  122. cd /etc/postgresql || exit 24624649846
  123. sudo -u postgres psql -c "$query"
  124. }
  125. function run_query_postgresql {
  126. database_name=$1
  127. database_query=$2
  128. cd /etc/postgresql || exit 2492464684
  129. sudo -u postgres psql -d "$database_name" -c "$database_query"
  130. }
  131. function run_query_postgresql_with_output {
  132. database_name=$1
  133. database_query=$2
  134. cd /etc/postgresql || exit 2482462846
  135. output=$(sudo -u postgres psql -d "$database_name" -c "$database_query")
  136. echo "$output"
  137. }
  138. function initialise_database_postgresql {
  139. database_name=$1
  140. database_file=$2
  141. cd /etc/postgresql || exit 239246992469
  142. # shellcheck disable=SC2024
  143. if ! sudo -u postgres psql "$database_name" < "$database_file"; then
  144. exit 7238525
  145. fi
  146. }
  147. function create_database_postgresql {
  148. app_name="$1"
  149. app_admin_password="$2"
  150. app_admin_username=$3
  151. if [ ! -d "$INSTALL_DIR" ]; then
  152. mkdir "$INSTALL_DIR"
  153. fi
  154. if [ ! "$app_admin_username" ]; then
  155. app_admin_username=${app_name}admin
  156. fi
  157. echo "create database ${app_name};
  158. CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
  159. GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
  160. flush privileges;
  161. quit" > "$INSTALL_DIR/batch.sql"
  162. chmod 600 "$INSTALL_DIR/batch.sql"
  163. cd /etc/postgresql || exit 247284684
  164. sudo -u postgres psql -d "$database_name" --file="$INSTALL_DIR/batch.sql"
  165. shred -zu "$INSTALL_DIR/batch.sql"
  166. }
  167. # NOTE: deliberately there is no "exit 0"