freedombone-utils-postgresql 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # postgresql database functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. # Set this when calling backup and restore commands
  29. USE_POSTGRESQL=
  30. POSTGRESQL_PACKAGES='postgresql-9.6 postgresql-contrib-9.6 postgresql-client'
  31. function store_original_postgresql_password {
  32. if [ ! -f /root/.postgresqloriginal ]; then
  33. echo $'Storing original postgresql password'
  34. ORIGINAL_POSTGRESQL_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a postgresql)
  35. # We can store this in plaintext because it will soon be of historical interest only
  36. echo -n "$ORIGINAL_POSTGRESQL_PASSWORD" > /root/.postgresqloriginal
  37. fi
  38. }
  39. function get_postgresql_password {
  40. POSTGRESQL_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a postgresql)
  41. if [[ "$POSTGRESQL_PASSWORD" == *'failed'* ]]; then
  42. echo $'Could not obtain postgresql password'
  43. exit 7835272
  44. fi
  45. }
  46. function image_install_postgresql {
  47. # shellcheck disable=SC2154,SC2086
  48. chroot "$rootdir" apt-get -yq install $POSTGRESQL_PACKAGES
  49. if [ ! -d "$rootdir/etc/postgresql" ]; then
  50. echo $"ERROR: postgresql does not appear to have installed."
  51. exit 78352
  52. fi
  53. if [ ! -f "$rootdir/usr/bin/psql" ]; then
  54. echo $"ERROR: psql command does not appear to have installed."
  55. exit 835290
  56. fi
  57. }
  58. function install_postgresql {
  59. if [[ $VARIANT == "mesh"* ]]; then
  60. image_install_postgresql
  61. return
  62. fi
  63. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  64. return
  65. fi
  66. function_check get_postgresql_password
  67. get_postgresql_password
  68. if [ ! "$POSTGRESQL_PASSWORD" ]; then
  69. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  70. POSTGRESQL_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  71. else
  72. POSTGRESQL_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  73. fi
  74. fi
  75. "${PROJECT_NAME}-pass" -u root -a postgresql -p "$POSTGRESQL_PASSWORD"
  76. # shellcheck disable=SC2086
  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[0]}"
  92. }
  93. function add_postgresql_user {
  94. postgresql_username=$1
  95. postgresql_password=$2
  96. cd /etc/postgresql || exit 2468246
  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 || exit 24624624
  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 || exit 2482468242
  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 || exit 24624649846
  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 || exit 2492464684
  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 || exit 2482462846
  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 || exit 239246992469
  140. # shellcheck disable=SC2024
  141. if ! sudo -u postgres psql "$database_name" < "$database_file"; 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 || exit 247284684
  162. sudo -u postgres psql -d "$database_name" --file="$INSTALL_DIR/batch.sql"
  163. shred -zu "$INSTALL_DIR/batch.sql"
  164. }
  165. # NOTE: deliberately there is no "exit 0"