Matt Riedemann | b14665f | 2019-10-17 19:34:05 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # lib/databases/postgresql |
| 4 | # Functions to control the configuration and operation of the **PostgreSQL** database backend |
| 5 | |
| 6 | # Dependencies: |
| 7 | # |
| 8 | # - DATABASE_{HOST,USER,PASSWORD} must be defined |
| 9 | |
| 10 | # Save trace setting |
| 11 | _XTRACE_PG=$(set +o | grep xtrace) |
| 12 | set +o xtrace |
| 13 | |
| 14 | |
| 15 | MAX_DB_CONNECTIONS=${MAX_DB_CONNECTIONS:-200} |
| 16 | |
| 17 | |
| 18 | register_database postgresql |
| 19 | |
| 20 | |
| 21 | # Functions |
| 22 | # --------- |
| 23 | |
| 24 | function get_database_type_postgresql { |
| 25 | echo postgresql |
| 26 | } |
| 27 | |
| 28 | # Get rid of everything enough to cleanly change database backends |
| 29 | function cleanup_database_postgresql { |
| 30 | stop_service postgresql |
| 31 | if is_ubuntu; then |
| 32 | # Get ruthless with mysql |
| 33 | apt_get purge -y postgresql* |
| 34 | return |
| 35 | elif is_fedora || is_suse; then |
| 36 | uninstall_package postgresql-server |
| 37 | else |
| 38 | return |
| 39 | fi |
| 40 | } |
| 41 | |
| 42 | function recreate_database_postgresql { |
| 43 | local db=$1 |
| 44 | # Avoid unsightly error when calling dropdb when the database doesn't exist |
| 45 | psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db" |
| 46 | createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E utf8 $db |
| 47 | } |
| 48 | |
| 49 | function configure_database_postgresql { |
| 50 | local pg_conf pg_dir pg_hba check_role version |
| 51 | echo_summary "Configuring and starting PostgreSQL" |
| 52 | if is_fedora; then |
| 53 | pg_hba=/var/lib/pgsql/data/pg_hba.conf |
| 54 | pg_conf=/var/lib/pgsql/data/postgresql.conf |
| 55 | if ! sudo [ -e $pg_hba ]; then |
| 56 | sudo postgresql-setup initdb |
| 57 | fi |
| 58 | elif is_ubuntu; then |
| 59 | version=`psql --version | cut -d ' ' -f3 | cut -d. -f1-2` |
| 60 | if vercmp $version '>=' 9.3; then |
| 61 | if [ -z "`pg_lsclusters -h`" ]; then |
| 62 | echo 'No PostgreSQL clusters exist; will create one' |
| 63 | sudo pg_createcluster $version main --start |
| 64 | fi |
| 65 | fi |
| 66 | pg_dir=`find /etc/postgresql -name pg_hba.conf|xargs dirname` |
| 67 | pg_hba=$pg_dir/pg_hba.conf |
| 68 | pg_conf=$pg_dir/postgresql.conf |
| 69 | elif is_suse; then |
| 70 | pg_hba=/var/lib/pgsql/data/pg_hba.conf |
| 71 | pg_conf=/var/lib/pgsql/data/postgresql.conf |
| 72 | # initdb is called when postgresql is first started |
| 73 | sudo [ -e $pg_hba ] || start_service postgresql |
| 74 | else |
| 75 | exit_distro_not_supported "postgresql configuration" |
| 76 | fi |
| 77 | # Listen on all addresses |
| 78 | sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $pg_conf |
| 79 | # Set max_connections |
| 80 | sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $pg_conf |
| 81 | # Do password auth from all IPv4 clients |
| 82 | sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $pg_hba |
| 83 | # Do password auth for all IPv6 clients |
| 84 | sudo sed -i "/^host/s/all\s\+::1\/128\s\+ident/$DATABASE_USER\t::0\/0\tpassword/" $pg_hba |
| 85 | restart_service postgresql |
| 86 | |
| 87 | # Create the role if it's not here or else alter it. |
| 88 | check_role=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='$DATABASE_USER'") |
| 89 | if [[ ${check_role} == *HERE ]];then |
| 90 | sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" |
| 91 | else |
| 92 | sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" |
| 93 | fi |
| 94 | } |
| 95 | |
| 96 | function install_database_postgresql { |
| 97 | echo_summary "Installing postgresql" |
| 98 | deprecated "Use of postgresql in devstack is deprecated, and will be removed during the Pike cycle" |
| 99 | local pgpass=$HOME/.pgpass |
| 100 | if [[ ! -e $pgpass ]]; then |
| 101 | cat <<EOF > $pgpass |
| 102 | *:*:*:$DATABASE_USER:$DATABASE_PASSWORD |
| 103 | EOF |
| 104 | chmod 0600 $pgpass |
| 105 | else |
| 106 | sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $pgpass |
| 107 | fi |
| 108 | if is_ubuntu; then |
| 109 | install_package postgresql |
| 110 | elif is_fedora || is_suse; then |
| 111 | install_package postgresql-server |
| 112 | if is_fedora; then |
| 113 | sudo systemctl enable postgresql |
| 114 | fi |
| 115 | else |
| 116 | exit_distro_not_supported "postgresql installation" |
| 117 | fi |
| 118 | } |
| 119 | |
| 120 | function install_database_python_postgresql { |
| 121 | # Install Python client module |
| 122 | pip_install_gr psycopg2 |
| 123 | ADDITIONAL_VENV_PACKAGES+=",psycopg2" |
| 124 | } |
| 125 | |
| 126 | function database_connection_url_postgresql { |
| 127 | local db=$1 |
| 128 | echo "$BASE_SQL_CONN/$db?client_encoding=utf8" |
| 129 | } |
| 130 | |
| 131 | |
| 132 | # Restore xtrace |
| 133 | $_XTRACE_PG |
| 134 | |
| 135 | # Local variables: |
| 136 | # mode: shell-script |
| 137 | # End: |