You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.5 KiB
120 lines
3.5 KiB
# -*- mode: shell-script -*-
|
|
|
|
include pretty
|
|
|
|
export DB_NAME="postgres"
|
|
export DB_DATADIR=/var/lib/postgresql/data
|
|
export DB_PASSFILE=/root/.pgpass
|
|
|
|
|
|
is_db_locked() {
|
|
require lsof || apt-get install -y lsof </dev/null || {
|
|
err "Couldn't install command 'lsof'."
|
|
return 1
|
|
}
|
|
|
|
# "$lsof" +D "$host_db_working_dir" || true
|
|
|
|
## We need:
|
|
## 1- to protect against set -e
|
|
## 2- to detect if errorlevel 1 because lsof failed for good reason: permissions error, directory not found
|
|
## 3- to detect if errorlevel 1 because no process found
|
|
|
|
[ -d "$host_db_working_dir" ] || {
|
|
err "Directory '$host_db_working_dir' not found. Might be normal at this stage."
|
|
return 1
|
|
}
|
|
|
|
## We choose to ignore (2), and use stdout emptyness to ensure (3)
|
|
open_files=$(lsof +D "$host_db_working_dir") || true
|
|
|
|
[ "$open_files" ]
|
|
}
|
|
|
|
|
|
_set_db_params() {
|
|
local docker_ip="$1" docker_network="$2"
|
|
|
|
export db_docker_opts="--network $docker_network -e PGHOST=$docker_ip -e PGUSER=postgres"
|
|
export db_cmd_opts=
|
|
}
|
|
|
|
ddb () { dcmd psql -qAt "$@"; }
|
|
|
|
|
|
##
|
|
## Entrypoints
|
|
##
|
|
|
|
|
|
db_drop () {
|
|
local dbname="$1"
|
|
dcmd dropdb --if-exists "$1"
|
|
}
|
|
|
|
db_create () {
|
|
local dbname="$1"
|
|
dcmd createdb "$dbname" || return 1
|
|
info "Database '$dbname' created."
|
|
if [ "$POSTGIS" ]; then
|
|
ddb -d "$dbname" < <(echo "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;") || return 1
|
|
dcmd /bin/bash -c "psql -d '$dbname' -f /usr/share/postgresql/*/contrib/postgis-2.1/legacy.sql" || return 1
|
|
info "Installed postgis extensions on database '$dbname'."
|
|
fi
|
|
if [ "$UNACCENT" ]; then
|
|
ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS unaccent;") || return 1
|
|
info "Installed unaccent extension on database '$dbname'."
|
|
fi
|
|
}
|
|
|
|
|
|
## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
|
|
db_has_database() {
|
|
local dbname="$1"
|
|
if [ "$(ddb < <(echo "SELECT 1 FROM pg_database WHERE datname = '$dbname'"))" ]; then
|
|
debug "Database $dbname exists."
|
|
return 0
|
|
else
|
|
debug "Database $dbname exists."
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
|
|
db_has_user() {
|
|
local user="$1" users
|
|
users=$(ddb < <(echo "select u.usename from pg_catalog.pg_user u")) || {
|
|
err "Failed to get user list"
|
|
return 1
|
|
}
|
|
echo "$users" | grep "^${user}$" >/dev/null 2>&1
|
|
}
|
|
|
|
db_create_user() {
|
|
local user="$1" password="$2"
|
|
## Using this instead of pipes is important so that trap works
|
|
ddb < <(echo "CREATE USER $user WITH PASSWORD '$password' CREATEDB NOCREATEROLE;")
|
|
}
|
|
|
|
db_change_password() {
|
|
local user="$1" password="$2"
|
|
## Using this instead of pipes is important so that trap works
|
|
ddb < <(echo "ALTER USER $user WITH PASSWORD '$password';")
|
|
}
|
|
|
|
db_grant_rights () {
|
|
local dbname="$1" user="$2"
|
|
require psql || apt-get install -y postgresql-client </dev/null
|
|
require pgm || {
|
|
(
|
|
cd /opt/apps
|
|
git clone https://github.com/0k/pgm.git
|
|
ln -sf /opt/apps/pgm/bin/* /usr/local/bin/
|
|
apt-get install -y --force-yes pv buffer < /dev/null
|
|
# cd /opt/apps/0k-docker
|
|
# git checkout master
|
|
)
|
|
}
|
|
debug PGHOST="$DOCKER_IP" PGUSER=postgres pgm chown "$user" "$dbname"
|
|
PGHOST="$DOCKER_IP" PGUSER=postgres prefix_pg_local_command=" " pgm chown "$user" "$dbname"
|
|
}
|