freedombone/src/freedombone-utils-postgresql

209 lines
6.7 KiB
Plaintext
Raw Permalink Normal View History

2017-11-05 15:39:32 +01:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2017-11-05 15:39:32 +01:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2017-11-05 15:39:32 +01:00
#
# postgresql database functions
#
# License
# =======
#
2018-01-25 19:35:39 +01:00
# Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
2017-11-05 15:39:32 +01:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Set this when calling backup and restore commands
USE_POSTGRESQL=
2017-12-03 23:03:55 +01:00
POSTGRESQL_PACKAGES='postgresql-9.6 postgresql-contrib-9.6 postgresql-client'
2018-04-19 12:39:26 +02:00
POSTGRESQL_VERSION=9.6
function turn_off_postgresql_logging {
if [ ! -f /etc/postgresql/$POSTGRESQL_VERSION/main/postgresql.conf ]; then
return
fi
sed -i 's|#log_destination|log_destination|g' /etc/postgresql/$POSTGRESQL_VERSION/main/postgresql.conf
sed -i "s|log_destination.*|log_destination = 'syslog'|g" /etc/postgresql/$POSTGRESQL_VERSION/main/postgresql.conf
if [ -d /var/log/postgresql ]; then
$REMOVE_FILES_COMMAND /var/log/postgresql/*
fi
}
function turn_on_postgresql_logging {
if [ ! -f /etc/postgresql/$POSTGRESQL_VERSION/main/postgresql.conf ]; then
return
fi
sed -i 's|log_destination|#log_destination|g' /etc/postgresql/$POSTGRESQL_VERSION/main/postgresql.conf
sed -i "s|log_destination.*|log_destination = 'stderr'|g" /etc/postgresql/$POSTGRESQL_VERSION/main/postgresql.conf
}
2017-11-05 15:39:32 +01:00
2017-11-05 17:21:13 +01:00
function store_original_postgresql_password {
if [ ! -f /root/.postgresqloriginal ]; then
echo $'Storing original postgresql password'
2018-03-02 23:20:49 +01:00
ORIGINAL_POSTGRESQL_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a postgresql)
2017-11-05 17:21:13 +01:00
# We can store this in plaintext because it will soon be of historical interest only
echo -n "$ORIGINAL_POSTGRESQL_PASSWORD" > /root/.postgresqloriginal
fi
}
2017-11-05 15:39:32 +01:00
function get_postgresql_password {
2018-03-02 23:20:49 +01:00
POSTGRESQL_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a postgresql)
2017-11-05 15:39:32 +01:00
if [[ "$POSTGRESQL_PASSWORD" == *'failed'* ]]; then
echo $'Could not obtain postgresql password'
exit 7835272
fi
}
function image_install_postgresql {
2018-03-04 22:32:40 +01:00
# shellcheck disable=SC2154,SC2086
chroot "$rootdir" apt-get -yq install $POSTGRESQL_PACKAGES
2017-12-03 23:03:55 +01:00
2018-03-02 23:20:49 +01:00
if [ ! -d "$rootdir/etc/postgresql" ]; then
2017-12-03 23:03:55 +01:00
echo $"ERROR: postgresql does not appear to have installed."
exit 78352
fi
2018-03-02 23:20:49 +01:00
if [ ! -f "$rootdir/usr/bin/psql" ]; then
2017-12-03 23:03:55 +01:00
echo $"ERROR: psql command does not appear to have installed."
exit 835290
fi
}
2017-11-05 15:39:32 +01:00
function install_postgresql {
2017-12-03 23:03:55 +01:00
if [[ $VARIANT == "mesh"* ]]; then
image_install_postgresql
2017-12-03 23:03:55 +01:00
return
fi
2018-02-25 13:50:46 +01:00
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
2017-11-05 15:39:32 +01:00
return
fi
function_check get_postgresql_password
get_postgresql_password
2018-03-02 23:20:49 +01:00
if [ ! "$POSTGRESQL_PASSWORD" ]; then
if [ -f "$IMAGE_PASSWORD_FILE" ]; then
POSTGRESQL_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
2017-11-05 15:39:32 +01:00
else
2018-03-02 23:20:49 +01:00
POSTGRESQL_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
2017-11-05 15:39:32 +01:00
fi
fi
2018-03-02 23:20:49 +01:00
"${PROJECT_NAME}-pass" -u root -a postgresql -p "$POSTGRESQL_PASSWORD"
2017-11-05 15:39:32 +01:00
2018-03-04 22:32:40 +01:00
# shellcheck disable=SC2086
apt-get -yq install $POSTGRESQL_PACKAGES
2017-11-05 15:39:32 +01:00
apt-get -yq remove --purge apache2-bin*
if [ -d /etc/apache2 ]; then
rm -rf /etc/apache2
echo $'Removed Apache installation after postgresql install'
fi
if [ ! -d /etc/postgresql ]; then
2017-12-03 23:03:55 +01:00
echo $"ERROR: postgresql does not appear to have installed."
2017-11-05 15:39:32 +01:00
exit 78352
fi
if [ ! -f /usr/bin/psql ]; then
2017-12-03 23:03:55 +01:00
echo $"ERROR: psql command does not appear to have installed."
2017-11-05 15:39:32 +01:00
exit 835290
fi
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
2017-11-05 15:39:32 +01:00
}
function add_postgresql_user {
postgresql_username=$1
postgresql_password=$2
2018-03-02 23:20:49 +01:00
cd /etc/postgresql || exit 2468246
2017-11-06 12:29:20 +01:00
if [[ "$3" != 'encrypt'* ]]; then
sudo -u postgres psql -c "create user $postgresql_username password '$postgresql_password';"
else
sudo -u postgres psql -c "create user $postgresql_username;"
sudo -u postgres psql -c "ALTER user $postgresql_username with encrypted password '$postgresql_password';"
fi
2017-11-05 15:39:32 +01:00
}
function remove_postgresql_user {
postgresql_username=$1
2018-03-02 23:20:49 +01:00
cd /etc/postgresql || exit 24624624
2017-11-05 15:39:32 +01:00
sudo -u postgres psql -c "drop user $postgresql_username"
}
2017-11-06 12:29:20 +01:00
function drop_database_postgresql {
2017-11-05 15:39:32 +01:00
database_name="$1"
2018-02-11 13:06:08 +01:00
database_owner_name="$2"
2018-03-02 23:20:49 +01:00
cd /etc/postgresql || exit 2482468242
2017-11-05 15:39:32 +01:00
sudo -u postgres psql -c "drop database $database_name"
2018-02-11 13:06:08 +01:00
if [ ${#database_owner_name} -gt 0 ]; then
sudo -u postgres psql -c "drop user $database_owner_name"
fi
2017-11-05 15:39:32 +01:00
}
2017-11-06 12:29:20 +01:00
function run_system_query_postgresql {
query=$1
2018-03-02 23:20:49 +01:00
cd /etc/postgresql || exit 24624649846
2017-11-06 12:29:20 +01:00
sudo -u postgres psql -c "$query"
}
2017-11-05 15:39:32 +01:00
function run_query_postgresql {
database_name=$1
database_query=$2
2018-03-02 23:20:49 +01:00
cd /etc/postgresql || exit 2492464684
sudo -u postgres psql -d "$database_name" -c "$database_query"
2017-11-05 15:39:32 +01:00
}
function run_query_postgresql_with_output {
database_name=$1
database_query=$2
2018-03-02 23:20:49 +01:00
cd /etc/postgresql || exit 2482462846
output=$(sudo -u postgres psql -d "$database_name" -c "$database_query")
2017-11-05 15:39:32 +01:00
echo "$output"
}
function initialise_database_postgresql {
database_name=$1
database_file=$2
2018-03-02 23:20:49 +01:00
cd /etc/postgresql || exit 239246992469
# shellcheck disable=SC2024
if ! sudo -u postgres psql "$database_name" < "$database_file"; then
2017-11-05 15:39:32 +01:00
exit 7238525
fi
}
function create_database_postgresql {
app_name="$1"
app_admin_password="$2"
app_admin_username=$3
2018-03-02 23:20:49 +01:00
if [ ! -d "$INSTALL_DIR" ]; then
mkdir "$INSTALL_DIR"
2017-11-05 15:39:32 +01:00
fi
2018-03-02 23:20:49 +01:00
if [ ! "$app_admin_username" ]; then
2017-11-05 15:39:32 +01:00
app_admin_username=${app_name}admin
fi
echo "create database ${app_name};
CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
flush privileges;
2018-03-02 23:20:49 +01:00
quit" > "$INSTALL_DIR/batch.sql"
chmod 600 "$INSTALL_DIR/batch.sql"
cd /etc/postgresql || exit 247284684
sudo -u postgres psql -d "$database_name" --file="$INSTALL_DIR/batch.sql"
rm "$INSTALL_DIR/batch.sql"
2017-11-05 15:39:32 +01:00
}
2018-03-02 23:20:49 +01:00
# NOTE: deliberately there is no "exit 0"