freedombone-utils-postgresql 6.7KB

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