postgresql: update to version 9.5.4 and major rework

* convert package build to use host-build for ecpg, pg_config and zic
 * introduce /lib/functions/postgresql.sh to be used by packages
   requiring a postgres database to exist as well as postgres' init
 * no longer require shadow-su, patch pg_ctl to setuid() ifself instead
 * auto-create database directory if there is enough free space
 * auto-create databases configured in UCI
 * remove some dead uci config options
 * grab maintainership

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This commit is contained in:
Daniel Golle
2016-09-23 16:41:49 +02:00
parent 9622ec36dc
commit 63120640e6
5 changed files with 229 additions and 106 deletions
-3
View File
@@ -1,5 +1,2 @@
config postgresql config
option PGUSER postgres
option PGDATA /var/postgresql/data
option PGLOG /var/postgresql/data/postgresql.log
option PG_CTL /usr/bin/pg_ctl
+17 -18
View File
@@ -26,48 +26,47 @@ cleanup() {
}
start_service() {
. /lib/functions/postgresql.sh
config_load "postgresql"
config_get pgdata config PGDATA
config_get pguser config PGUSER
config_get pgctl config PG_CTL
config_get pgopts config PGOPTS
user_exists postgres 5432 || user_add postgres 5432
group_exists postgres 5432 || group_add postgres 5432
if [ ! -d "${pgdata}" ]; then
echo "Create the data directory (${pgdata}) and try again"
return 1
fi
fix_perms
fix_hosts
procd_open_instance
if [ ! -d "${pgdata}" ]; then
pg_init_data ${pgdata}
[ $? -gt 0 ] && return 1
fi
procd_set_param user ${pguser}
cleanup "${pgdata}"
procd_open_instance
procd_set_param user postgres
procd_set_param command $PROG
procd_append_param command -D "${pgdata}"
[ -n "${pgopts}" ] && procd_append_param command -o "${pgopts}"
procd_set_param respawn retry=60
procd_close_instance
procd_open_instance
procd_set_param user postgres
procd_set_param command /lib/functions/postgresql.sh init "${pgdata}"
procd_close_instance
}
reload_service() {
config_load "postgresql"
config_get pgdata config PGDATA
config_get pguser config PGUSER
config_get pgctl config PG_CTL
${pgctl} reload -U ${pguser} -D '${pgdata}' -s
/usr/bin/pg_ctl reload -U postgres -D "${pgdata}" -s
}
status() {
config_load "postgresql"
config_get pgdata config PGDATA
config_get pguser config PGUSER
config_get pgctl config PG_CTL
echo "status postgres..."
${pgctl} status -U ${pguser} -D '${pgdata}'
echo "ok"
/usr/bin/pg_ctl status -U postgres -D "${pgdata}"
}
+60
View File
@@ -0,0 +1,60 @@
#!/bin/sh
PSQL="/usr/bin/psql"
free_megs() {
fsdir=$1
while [ ! -d "$fsdir" ]; do
fsdir=$(dirname $fsdir)
done
df -m $fsdir | while read fs bl us av cap mnt; do [ "$av" = "Available" ] || echo $av; done
}
pg_init_data() {
# make sure we got at least 50MB of free space
[ $(free_megs $1) -lt 50 ] && return 1
pg_ctl initdb -U postgres -D $1
}
pg_server_ready() {
t=0
while [ $t -le 90 ]; do
pg_ctl status -U postgres -D $1 2>/dev/null >/dev/null && return 0
t=$((t+1))
sleep 1
done
return 1
}
# $1: dbname, $2: username, $3: password
pg_require_db() {
pg_test_db $@ && return 0
( echo "CREATE DATABASE $1;"
echo -n "CREATE USER $2"
[ "$3" ] && echo -n " WITH PASSWORD '$3'"
echo ";"
echo "GRANT ALL PRIVILEGES ON DATABASE \"$1\" to $2;" ) |
$PSQL -U postgres -d template1 -e
return $?
}
pg_test_db() {
PGPASSWORD=$3
echo "SHOW ALL;" | $PSQL -U $2 -d $1 -q 2>/dev/null >/dev/null
return $?
}
uci_require_db() {
local dbname dbuser dbpass
config_get dbname $1 name
config_get dbuser $1 user
config_get dbpass $1 pass
pg_require_db $dbname $dbuser $dbpass
}
[ "$1" = "init" ] && {
. /lib/functions.sh
pg_server_ready $2 || exit 1
config_load postgresql
config_foreach uci_require_db postgres-db
}