devstack/lib/databases/postgresql
Ian Wienand 523f488036 Namespace XTRACE commands
I noticed this when debugging some grenade issues failures.

An include of grenade/functions stores the current value of XTRACE
(on) and disables xtrace for the rest of the import.

We then include devstack's "functions" library, which now overwrites
the stored value of XTRACE the current state; i.e. disabled.

When it finishes it restores the prior state (disabled), and then
grenade restores the same value of XTRACE (disabled).

The result is that xtrace is incorrectly disabled until the next time
it just happens to be turned on.

The solution is to name-space the store of the current-value of xtrace
so when we finish sourcing a file, we always restore the tracing value
to what it was when we entered.

Some files had already discovered this.  In general there is
inconsistency around the setting of the variable, and a lot of obvious
copy-paste.  This brings consistency across all files by using
_XTRACE_* prefixes for the sotre/restore of tracing values.

Change-Id: Iba7739eada5711d9c269cb4127fa712e9f961695
2015-11-27 15:36:04 +11:00

127 lines
3.8 KiB
Bash

#!/bin/bash
#
# lib/databases/postgresql
# Functions to control the configuration and operation of the **PostgreSQL** database backend
# Dependencies:
#
# - DATABASE_{HOST,USER,PASSWORD} must be defined
# Save trace setting
_XTRACE_PG=$(set +o | grep xtrace)
set +o xtrace
MAX_DB_CONNECTIONS=${MAX_DB_CONNECTIONS:-200}
register_database postgresql
# Functions
# ---------
function get_database_type_postgresql {
echo postgresql
}
# Get rid of everything enough to cleanly change database backends
function cleanup_database_postgresql {
stop_service postgresql
if is_ubuntu; then
# Get ruthless with mysql
apt_get purge -y postgresql*
return
elif is_fedora || is_suse; then
uninstall_package postgresql-server
else
return
fi
}
function recreate_database_postgresql {
local db=$1
# Avoid unsightly error when calling dropdb when the database doesn't exist
psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db"
createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E utf8 $db
}
function configure_database_postgresql {
local pg_conf pg_dir pg_hba root_roles
echo_summary "Configuring and starting PostgreSQL"
if is_fedora; then
pg_hba=/var/lib/pgsql/data/pg_hba.conf
pg_conf=/var/lib/pgsql/data/postgresql.conf
if ! sudo [ -e $pg_hba ]; then
sudo postgresql-setup initdb
fi
elif is_ubuntu; then
pg_dir=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
pg_hba=$pg_dir/pg_hba.conf
pg_conf=$pg_dir/postgresql.conf
elif is_suse; then
pg_hba=/var/lib/pgsql/data/pg_hba.conf
pg_conf=/var/lib/pgsql/data/postgresql.conf
# initdb is called when postgresql is first started
sudo [ -e $pg_hba ] || start_service postgresql
else
exit_distro_not_supported "postgresql configuration"
fi
# Listen on all addresses
sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $pg_conf
# Set max_connections
sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $pg_conf
# Do password auth from all IPv4 clients
sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $pg_hba
# Do password auth for all IPv6 clients
sudo sed -i "/^host/s/all\s\+::1\/128\s\+ident/$DATABASE_USER\t::0\/0\tpassword/" $pg_hba
restart_service postgresql
# Create the role if it's not here or else alter it.
root_roles=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='root'")
if [[ ${root_roles} == *HERE ]];then
sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
else
sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
fi
}
function install_database_postgresql {
echo_summary "Installing postgresql"
local pgpass=$HOME/.pgpass
if [[ ! -e $pgpass ]]; then
cat <<EOF > $pgpass
*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
EOF
chmod 0600 $pgpass
else
sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $pgpass
fi
if is_ubuntu; then
install_package postgresql
elif is_fedora || is_suse; then
install_package postgresql-server
else
exit_distro_not_supported "postgresql installation"
fi
}
function install_database_python_postgresql {
# Install Python client module
pip_install_gr psycopg2
ADDITIONAL_VENV_PACKAGES+=",psycopg2"
}
function database_connection_url_postgresql {
local db=$1
echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
}
# Restore xtrace
$_XTRACE_PG
# Local variables:
# mode: shell-script
# End: