summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPavel Raiskup <praiskup@redhat.com>2015-03-17 11:34:21 +0100
committerPavel Raiskup <praiskup@redhat.com>2015-03-17 13:52:47 +0100
commit81e523c0957a9bee557bf2cca25607989209ac2c (patch)
tree5789b6030b105b18b8820f07dae5b3947f7a7844
parentf7a46224bd60e177871a46ffd99f0492cb6e8b6f (diff)
downloadpostgresql-setup-81e523c0957a9bee557bf2cca25607989209ac2c.tar.gz
postgresql-setup-81e523c0957a9bee557bf2cca25607989209ac2c.tar.xz
postgresql-setup-81e523c0957a9bee557bf2cca25607989209ac2c.zip
sysvinit: transform for SCL & RHEL6 usage
This is mostly about simplification of intiscript and postgresql-setup script reuse. Still WIP, however (some options are not applicable for sysvinit system). * share/postgresql-setup/library.sh.in (parse_upgrade_setup): Parse new variable "pghost_override". (run_cmd_as_dbadmin): New wrapper function to run something under "postgres" user. Cut from postgresql-setup. * postgresql-setup.in: Optionally source software collections. Call new check_daemon_reload instead of in-place checking. (upgrade): Use new library function run_cmd_as_dbadmin. Add new hack for $PGHOST overriding via $pghost_override. Add more verbose output to make clear what tool failed. (check_daemon_reload): New function to make the systemd daemon reload conditionally on one dedicated place. (handle_envfile): New function to abstract environment file parsing, works both for systemd & sysvinit env-files. (handle_service_envfiles): Use handle_envfile instead of in-place parsing. (service_configuration): For sysvinit supported configuration, we support only one env file per service - take this into account and don't try to detect many from systemctl output. * postgresql.init.in: Switch to bash script. Parametrize metadata by configure results. Source software collections when needed. Source project library. Don't set PGDATA if admin uses non-default service naem. Do not define PGPORT as PGPORT should be defined in postgresql.conf. Allow passing parameters into initdb/upgrade targets. ($PREVMAJORVERSION, $PREVPGENGINE): Remove unused variables. ($PGENGINE): This is @bindir@ equivalent. ($PGLOG): Spell it so it will not clash with system default PostgreSQL installation. ($pidfile): Use $NAME instead of $PID to distinguish multiple postmaster scenarios (mainly because we do not know the PGPORT yet). (start): Drop the checking logic and use postgresql-check-db-dir instead. Reuse run_cmd_as_dbadmin instead of $SU_POSTGRES. (stop): Use run_cmd_as_dbadmin instead of $SU_POSTGRES. (reload): Just coding style fix. (perform_initdb): Drop unused function, done in postgresql-setup. (__single_comand): Mostly postgresql-setup wrapper with success/failure sugar around the call itself. (initdb): Substitute the function body with postgresql-setup call. (upgrade): Likewise. * postgresql-check-db-dir.in: Fix the hint for admin about where the service should be initialized. Do not warn about systemd-only unsupported configuration. Add @bindir@ into "you need --initdb" hint. Re-style the warning a bit. * etc/postgresql-setup/upgrade/postgresql.conf: Mention new configuration option pghost_override. * configure.ac (initscriptsconfdir): New configuration directory. (PGMAJORVERSION): Use sed and regexp for MAJOR version mining. It failed for micro versions of length >= 2. (SCL_SOURCE): Define to be empty by default. * README: Document what's needed to bootstrap from git source. WIP. * Makefile.am (initscripts_DATA): Use noinst_DATA. Firstly, having initscripts_DATA installs only non-executable files. So we needed something with _SCRIPTS primary. But initscripts_SCRIPTS would face the automake's limitation that it must be "static" list of files. So make the installation of initscript (of dynamically generated name) via automake's install/uninstall. ($(initscript)): Use $(INSTANTIATE_SCRIPT) instead of $(INSTANTIATE). (install-data-hook): Call 'systemctl daemon-reload' only for non-sysvinit systems. * .gitignore: Ignore 'initscript' filename.
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am24
-rw-r--r--README3
-rw-r--r--configure.ac4
-rw-r--r--etc/postgresql-setup/upgrade/postgresql.conf5
-rw-r--r--postgresql-check-db-dir.in18
-rw-r--r--postgresql-setup.in95
-rw-r--r--postgresql.init.in260
-rw-r--r--share/postgresql-setup/library.sh.in20
9 files changed, 211 insertions, 219 deletions
diff --git a/.gitignore b/.gitignore
index a72b090..da14563 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,4 +26,5 @@ README.rpm-dist*
testsuite
testsuite.dir/
initdb
+initscript
upgrade
diff --git a/Makefile.am b/Makefile.am
index bad62ec..123f2b4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -12,7 +12,7 @@ service_in = postgresql.service.in
serviceat = $(NAME_SERVICE)@.service
serviceat_in = postgresql.service.in
-initscript = $(NAME_SERVICE)
+initscript = initscript
initscript_in = postgresql.init.in
readme_dist = README.rpm-dist
@@ -35,9 +35,12 @@ libexec_SCRIPTS = $(ctl) $(checkdb)
legacyscriptsdir = $(systemdlegacyscriptsdir)/$(NAME_SERVICE)
+noinst_DATA =
+
# TODO: Ideally, 'make distcheck' should check every file we generate.
if WANT_SYSVINIT
-initscripts_DATA = $(initscript)
+noinst_DATA += $(initscript)
+GENERATED_FILES += $(initscript)
else
systemdunits_DATA = $(service) $(serviceat)
legacyscripts_SCRIPTS = initdb upgrade
@@ -45,7 +48,7 @@ endif
pgdoc_DATA = $(readme_dist)
-noinst_DATA = $(TEST_GEN_FILES_LIST)
+noinst_DATA += $(TEST_GEN_FILES_LIST)
$(ctl): $(ctl_in) $(c_s)
$(INSTANTIATE_SCRIPT)
@@ -57,7 +60,7 @@ $(checkdb): $(check) $(checkdb_in) $(c_s)
$(INSTANTIATE_SCRIPT)
$(initscript): $(initscript_in) $(c_s)
- $(INSTANTIATE)
+ $(INSTANTIATE_SCRIPT)
initdb upgrade: legacy-sysv-script.in $(c_s)
$(INSTANTIATE_SCRIPT)
@@ -105,7 +108,6 @@ GENERATED_FILES += \
$(pgdoc_DATA) \
$(pkgsysconf_DATA) \
$(systemdunits_DATA) \
- $(initscripts_DATA) \
$(legacyscripts_SCRIPTS)
INTERMEDIATE_FILES += \
@@ -122,9 +124,21 @@ $(TEST_GEN_FILES_LIST): $(GENERATED_FILES)
done
install-data-hook:
+if WANT_SYSVINIT
+else
test `id -u` -ne 0 || systemctl daemon-reload
+endif
$(MKDIR_P) $(DESTDIR)/$(sysconfdir)/$(NAME_BINARYBASE)-setup/upgrade
+install-exec-hook:
+if WANT_SYSVINIT
+ $(MKDIR_P) $(DESTDIR)/$(initscriptsdir)
+ $(install_sh_SCRIPT) $(initscript) $(DESTDIR)/$(initscriptsdir)/$(NAME_SERVICE)
+endif
+
+uninstall-hook:
+ rm -rf $(DESTDIR)/$(initscriptsdir)/$(NAME_SERVICE)
+
GL_GEN_BIN = gitlog-to-changelog
GL_GEN = if test -d .git; then \
$(SHELL) $(top_srcdir)/$(config_aux_dir)/missing --run \
diff --git a/README b/README
index 49ace1a..95147d6 100644
--- a/README
+++ b/README
@@ -4,3 +4,6 @@ postgresql-setup
BuildRequires:
- m4, docbook-utils, help2man
+
+Maintainer's BuildRequires
+ - autoconf, automake, autoconf-archive
diff --git a/configure.ac b/configure.ac
index 51f20d3..c222d1c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -80,6 +80,7 @@ conf_dir([systemdunitsdir], ['${prefix}/lib/systemd/system'])
conf_dir([systemduserunitsdir], ['${prefix}/etc/systemd/system'])
conf_dir([systemdlegacyscriptsdir], ['${libexecdir}/initscripts/legacy-actions'])
conf_dir([initscriptsdir], ['${sysconfdir}/rc.d/init.d'])
+conf_dir([initscriptsconfdir], ['/etc/sysconfig/pgsql'])
conf_dir([rawpkgdatadir], ['${datadir}/postgresql-setup'])
conf_dir([pgdocdir], ['${datarootdir}/doc/${NAME_PACKAGE}'])
conf_dir([pgcontribdocdir], ['${datarootdir}/doc/${NAME_PACKAGE}-contrib'])
@@ -126,7 +127,7 @@ if test -z "$PGVERSION" -a -z "$PGMAJORVERSION"; then
PGVERSION=${raw_version##* }
AC_MSG_RESULT($PGVERSION)
- PGMAJORVERSION=${PGVERSION%%.[[0-9]]}
+ PGMAJORVERSION=`echo $PGVERSION | sed 's/\.[0-9]\+$//'`
fi
AX_COMPARE_VERSION([9.4], [le], [$PGVERSION],
@@ -183,6 +184,7 @@ PGSETUP_SUBST_OPT([SCLS], [],
SCL_BINDIR=/usr/bin
SCL_BINARY=scl
+SCL_SOURCE=
SCL_SHELL_WRAPPER=
if test -n "$SCLS"
then
diff --git a/etc/postgresql-setup/upgrade/postgresql.conf b/etc/postgresql-setup/upgrade/postgresql.conf
index 49d58b5..850aa12 100644
--- a/etc/postgresql-setup/upgrade/postgresql.conf
+++ b/etc/postgresql-setup/upgrade/postgresql.conf
@@ -24,3 +24,8 @@ scls "postgresql92"
# is different from upstream (9.3+). You may need to let pg_upgrade know that
# it works against server 9.2 built by Red Hat.
redhat_sockets_hack yes
+
+# 8.4 servers did not provide info about socket directory in pidfile (and newer
+# clients look only into /var/run/postgresql on Red Hat). Use pghost_override
+# for specifying where to look by default (optional).
+pghost_override /tmp
diff --git a/postgresql-check-db-dir.in b/postgresql-check-db-dir.in
index 0d9854d..496732e 100644
--- a/postgresql-check-db-dir.in
+++ b/postgresql-check-db-dir.in
@@ -17,18 +17,25 @@ service_name="$1"
if [ -z "$PGDATA" ]; then
echo $"You try to start '$service_name' service"
echo $"but the required \$PGDATA environment variable is not set."
- echo $"You should use the @systemduserunitsdir@/$service_name.service.d/ANYTHING.conf"
+
+ if test @WANT_SYSVINIT@ -eq 1; then
+ echo $"You should use the @initscriptsconfdir@/$service_name"
+ else
+ echo $"You should use the @systemduserunitsdir@/$service_name.service.d/ANYTHING.conf"
+ fi
+
echo $"configuration file to set \$PGDATA. For more info see"
echo $"the @README_DIST@ file."
exit 1
fi
+
# Warn the user that the configuration should be adjusted via drop-in, in case
# the $PGDATA variable is set different way (and non-default service name is
-# used).
+# used, systemd systems only).
conf_dir="@systemduserunitsdir@/$service_name.service.d"
-if [[ "$service_name" == *@* ]] && test ! -d "$conf_dir"; then
+if test @WANT_SYSVINIT@ = 0 && [[ "$service_name" == *@* ]] && test ! -d "$conf_dir"; then
echo >&2 $"WARNING: Note that the '$conf_dir'"
echo >&2 $"directory does not exist while you are using non-default service"
echo >&2 $"name '$service_name'. You are encouraged to use the"
@@ -87,8 +94,9 @@ then
fi
else
# No existing PGDATA! Warn the user to initdb it.
- echo $"\"$PGDATA\" is missing or empty."
- echo $"Use \"@NAME_BINARYBASE@-setup --initdb\" to initialize the database cluster."
+ echo $"Directory \"$PGDATA\" is missing or empty."
+ echo $"Use \"@bindir@/@NAME_BINARYBASE@-setup --initdb\""
+ echo $"to initialize the database cluster."
echo $"See $README_DIST for more information."
exit 1
fi
diff --git a/postgresql-setup.in b/postgresql-setup.in
index af248a4..2c21349 100644
--- a/postgresql-setup.in
+++ b/postgresql-setup.in
@@ -23,6 +23,8 @@ PGPORT_DEF=5432
. "@rawpkgdatadir@/library.sh"
+@SCL_SOURCE@
+
# We upgrade by default from system's default PostgreSQL installation
option_upgradefrom="@NAME_DEFAULT_PREV_SERVICE@"
@@ -231,12 +233,17 @@ upgrade()
socket_hacks="export REDHAT_PGUPGRADE_FROM_RHEL=yes ;"
}
+ test -n "$upgradefrom_pghost_override" && {
+ pghost_override="PGHOST='$upgradefrom_pghost_override'"
+ }
+
# Create empty new-format database
if perform_initdb; then
# Do the upgrade
- $SU_POSTGRES -c "\
- @SCL_SOURCE@ $scls_upgrade_hacks \
+ run_cmd_as_dbadmin "\
+ $scls_upgrade_hacks \
$socket_hacks \
+ $pghost_override \
$PGENGINE/pg_upgrade \
'--old-bindir=$upgradefrom_engine' \
'--new-bindir=$PGENGINE' \
@@ -246,13 +253,15 @@ upgrade()
'--old-port=$PGPORT' '--new-port=$PGPORT' \
@PG_UPGRADE_BIN_USER_OPT@=postgres \
$PGSETUP_PGUPGRADE_OPTIONS" \
- >> "$upgrade_log" 2>&1 < /dev/null
+ "$upgrade_log" "$upgrade_log"
if [ $? -ne 0 ]; then
# pg_upgrade failed
+ error $"pg_upgrade tool failed"
script_result=1
fi
else
# initdb failed
+ error $"initdb failed"
script_result=1
fi
@@ -270,7 +279,7 @@ upgrade()
# Clean up after failure.
rm -rf "$pgdata"
$inplace && mv "$pgdataold" "$pgdata"
- error $"failed"
+ error $"Upgrade failed."
fi
info $"See $upgrade_log for details."
}
@@ -300,6 +309,22 @@ EOF
}
+check_daemon_reload()
+{
+ local nr_option=NeedDaemonReload
+
+ test @WANT_SYSVINIT@ = 1 && return 0
+
+ local nr_out="`systemctl show -p $nr_option $option_service.service 2>/dev/null`"
+ if [[ "$nr_out" != "$nr_option=no" ]]; then
+ error $"Note that systemd configuration for '$option_service' changed."
+ error_q $"You need to perform 'systemctl daemon-reload' otherwise the"
+ error_q $"results of this script can be inadequate."
+ exit 1
+ fi
+}
+
+
handle_service_env()
{
local service="$1"
@@ -323,6 +348,29 @@ handle_service_env()
}
+handle_envfile()
+{
+ local file="$1"
+
+ debug "trying to read '$file' env file"
+ if test ! -r "$file"; then
+ if test @WANT_SYSVINIT@ = 1; then
+ return
+ fi
+ error "Can not read EnvironmentFile '$file' specified"
+ error_q "in ${service}.service"
+ fi
+
+ # Note that the env file parser in systemd does not perform exactly the
+ # same job.
+ unset PGPORT PGDATA
+ . "$file"
+ envfile_pgdata="$PGDATA"
+ envfile_pgport="$PGPORT"
+ unset PGPORT PGDATA
+}
+
+
handle_service_envfiles()
{
local mode="$1"
@@ -338,24 +386,9 @@ handle_service_envfiles()
-e 's| ([^)]*)$||'
)
-
# Read the file names line-by-line (spaces may be inside)
while read line; do
- debug "trying to read '$line' env file"
-
- if test ! -r "$line"; then
- error "Can not read EnvironmentFile '$line' specified"
- error_q "in ${service}.service"
- fi
-
- # Note that the env file parser in systemd does not perform exactly the
- # same job.
- unset PGPORT PGDATA
- . "$line"
- envfile_pgdata="$PGDATA"
- envfile_pgport="$PGPORT"
- unset PGPORT PGDATA
-
+ handle_envfile "$line"
done <<<"$envfiles"
}
@@ -399,8 +432,17 @@ service_configuration()
local service="$service"
test upgrade = "$mode" && service="$option_upgradefrom"
- handle_service_env "$service"
- handle_service_envfiles "$option_mode" "$service"
+ # Sysvinit has the default PGDATA configured inside the script, no
+ # additional configuration must exist. Don't set the default for upgrade,
+ # however.
+ test initdb = "$mode" && set_var "$datavar" "@PGDATADIR@"
+
+ if test "@WANT_SYSVINIT@" = 1; then
+ handle_envfile "@initscriptsconfdir@/$service"
+ else
+ handle_service_env "$service"
+ handle_service_envfiles "$option_mode" "$service"
+ fi
test -n "$unit_pgdata" && set_var "$datavar" "$unit_pgdata"
test -n "$envfile_pgdata" && set_var "$datavar" "$envfile_pgdata"
@@ -647,14 +689,7 @@ Port is not set by postgresql.conf nor by --port."
## LAST CHECK THE SETUP ##
-nr_option=NeedDaemonReload
-nr_out="`systemctl show -p $nr_option $option_service.service 2>/dev/null`"
-if [[ "$nr_out" != "$nr_option=no" ]]; then
- error $"Note that systemd configuration for '$option_service' changed."
- error_q $"You need to perform 'systemctl daemon-reload' otherwise the"
- error_q $"results of this script can be inadequate."
- exit 1
-fi
+check_daemon_reload
# These variables are read by underlying utilites, rather export them.
export PGDATA=$pgdata
diff --git a/postgresql.init.in b/postgresql.init.in
index f75f4e5..b3bd560 100644
--- a/postgresql.init.in
+++ b/postgresql.init.in
@@ -1,7 +1,8 @@
-#!/bin/sh
+#!/bin/bash
#
-# postgresql This is the init script for starting up the PostgreSQL
-# server.
+# @NAME_SERVICE@
+#
+# This is the init script for starting up the PostgreSQL server.
#
# This script is slightly unusual in that the name of the daemon (postmaster)
# is not the same as the name of the subsystem (postgresql)
@@ -10,25 +11,22 @@
# description: PostgreSQL database server.
# processname: postmaster
# pidfile: /var/run/postmaster.PORT.pid
+
### BEGIN INIT INFO
-# Provides: postgresql
+# Provides: @NAME_SERVICE@
# Required-Start: $local_fs $remote_fs $network $named $syslog $time
# Required-Stop: $local_fs $remote_fs $network $named $syslog $time
-# Short-Description: start and stop PostgreSQL server
+# Short-Description: start and stop PostgreSQL @PGVERSION@ server
# Description: PostgreSQL database server
### END INIT INFO
# PGVERSION is the full package version, e.g., 9.0.2
# Note: the specfile inserts the correct value during package build
PGVERSION=@PGVERSION@
+
# PGMAJORVERSION is major version, e.g., 9.0 (this should match PG_VERSION)
PGMAJORVERSION=@PGMAJORVERSION@
-# PREVMAJORVERSION is the previous major version, e.g., 8.4, for upgrades
-# Note: the specfile inserts the correct value during package build
-PREVMAJORVERSION=@PREVMAJORVERSION@
-# PREVPGENGINE is the directory containing the previous postmaster executable
-# Note: the specfile inserts the correct value during package build
-PREVPGENGINE=@PREVPGENGINE@
+
# PGDOCDIR is the directory containing the package's documentation
# Note: the specfile inserts the correct value during package build
@@ -41,6 +39,11 @@ README_DIST=@README_DIST@
# Get network config.
. /etc/sysconfig/network
+# postgresql-setup library
+. "@rawpkgdatadir@/library.sh"
+
+@SCL_SOURCE@
+
# Find the name of the script
NAME=`basename $0`
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
@@ -51,10 +54,19 @@ fi
SU_POSTGRES="@SU_POSTGRES@"
# Set defaults for configuration variables
-PGENGINE=/usr/bin
-PGPORT=5432
-PGDATA=/var/lib/pgsql/data
-PGLOG=/var/lib/pgsql/pgstartup.log
+PGENGINE=@bindir@
+
+# Only default system service has default PGDATA set. This allows us to catch
+# admin's mistake of not creating /etc/sysconfig/psql/$NAME configuration file
+# and just hard/symlinking default init script. That way we can avoid (inside
+# postgresql-check-db-dir) runnnig "secondary" server against "default" data
+# directory.
+if test "@NAME_SERVICE@" = "$NAME"; then
+ PGDATA=@PGDATADIR@
+fi
+
+PGLOG=@POSTGRES_HOMEDIR@/pgstartup-@NAME_SERVICE@.log
+
# Value to set as postmaster process's oom_adj
PG_OOM_ADJ=-17
@@ -65,11 +77,16 @@ export PGDATA
export PGPORT
lockfile="/var/lock/subsys/${NAME}"
-pidfile="/var/run/postmaster.${PGPORT}.pid"
+
+# Ideally, we should use $PGDATA/postmaster.pid. It apparently works, but to
+# be honest I'm not sure about consequences. Most probably 'service status'
+# would not work for non-root/non-postgres users. TODO?
+pidfile="/var/run/${NAME}.pid"
script_result=0
-start(){
+start()
+{
[ -x "$PGENGINE/postmaster" ] || exit 5
PSQL_START=$"Starting ${NAME} service: "
@@ -83,40 +100,27 @@ start(){
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
fi
- # Check for the PGDATA structure
- if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]
- then
- # Check version of existing PGDATA
- if [ x`cat "$PGDATA/PG_VERSION"` = x"$PGMAJORVERSION" ]
- then
- : A-OK
- elif [ x`cat "$PGDATA/PG_VERSION"` = x"$PREVMAJORVERSION" ]
- then
- echo
- echo $"An old version of the database format was found."
- echo $"Use \"service postgresql upgrade\" to upgrade to version $PGMAJORVERSION."
- echo $"See $README_DIST for more information."
- exit 1
- else
- echo
- echo $"An old version of the database format was found."
- echo $"You need to dump and reload before using PostgreSQL $PGMAJORVERSION."
- echo $"See $README_DIST for more information."
- exit 1
- fi
- else
- # No existing PGDATA! Warn the user to initdb it.
- echo
- echo $"$PGDATA is missing. Use \"service postgresql initdb\" to initialize the cluster first."
- echo_failure
- echo
- exit 1
- fi
+ @libexecdir@/postgresql-check-db-dir "$NAME" || {
+ echo_failure
+ echo
+ exit 1
+ }
echo -n "$PSQL_START"
test x"$PG_OOM_ADJ" != x && echo "$PG_OOM_ADJ" > /proc/self/oom_adj
- $SU_POSTGRES -c "$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null
+
+ # Note that this does not fail/exit the 'service start' if the postmaster
+ # is already running. We should probably 'status' first and start only if
+ # postmaster is down. This just unnecessarily wastes time and generates
+ # too much (false) rush in $PGLOG.
+ run_cmd_as_dbadmin \
+ "$PGENGINE/postmaster ${PGPORT+-o "-p $PGPORT"} \
+ -D '$PGDATA' ${PGOPTS} &" \
+ "$PGLOG" "$PGLOG"
+
+ # TODO: parametrize
sleep 2
+
pid=`head -n 1 "$PGDATA/postmaster.pid" 2>/dev/null`
if [ "x$pid" != x ]
then
@@ -131,12 +135,15 @@ start(){
fi
}
-stop(){
+stop()
+{
echo -n $"Stopping ${NAME} service: "
if [ -e "$lockfile" ]
then
- $SU_POSTGRES -c "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" > /dev/null 2>&1 < /dev/null
- ret=$?
+ run_cmd_as_dbadmin \
+ "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" \
+ /dev/null /dev/null
+ ret=$?
if [ $ret -eq 0 ]
then
echo_success
@@ -162,138 +169,35 @@ condrestart(){
[ -e "$lockfile" ] && restart || :
}
-reload(){
- $SU_POSTGRES -c "$PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
-}
-
-# code shared between initdb and upgrade actions
-perform_initdb(){
- if [ ! -e "$PGDATA" -a ! -h "$PGDATA" ]
- then
- mkdir -p "$PGDATA" || return 1
- chown postgres:postgres "$PGDATA"
- chmod go-rwx "$PGDATA"
- fi
- # Clean up SELinux tagging for PGDATA
- [ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
-
- # Make sure the startup-time log file is OK, too
- if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
- then
- touch "$PGLOG" || return 1
- chown postgres:postgres "$PGLOG"
- chmod go-rwx "$PGLOG"
- [ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
- fi
-
- # Initialize the database
- $SU_POSTGRES -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'" >> "$PGLOG" 2>&1 < /dev/null
-
- # Create directory for postmaster log
- mkdir "$PGDATA/pg_log"
- chown postgres:postgres "$PGDATA/pg_log"
- chmod go-rwx "$PGDATA/pg_log"
-
- if [ -f "$PGDATA/PG_VERSION" ]
- then
- return 0
- fi
- return 1
+reload()
+{
+ $SU_POSTGRES -c "@SCL_SOURCE@ $PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
}
-initdb(){
- if [ -f "$PGDATA/PG_VERSION" ]
- then
- echo -n $"Data directory is not empty!"
- echo_failure
- echo
- script_result=1
- else
- echo -n $"Initializing database: "
- if perform_initdb
- then
- echo_success
- else
- echo_failure
- script_result=1
- fi
- echo
- fi
+__single_comand()
+{
+ local msg="$1"
+ shift
+ echo $"$msg ($@)"
+ "$@" && success || failure || script_result=1
+ echo
}
-upgrade(){
- # must see previous version in PG_VERSION
- if [ ! -f "$PGDATA/PG_VERSION" -o \
- x`cat "$PGDATA/PG_VERSION"` != x"$PREVMAJORVERSION" ]
- then
- echo
- echo $"Cannot upgrade because database is not of version $PREVMAJORVERSION."
- echo_failure
- echo
- exit 1
- fi
- if [ ! -x "$PGENGINE/pg_upgrade" ]
- then
- echo
- echo $"Please install the postgresql-upgrade RPM."
- echo_failure
- echo
- exit 5
- fi
- # Make sure service is stopped
- stop
- echo -n $"Upgrading database: "
-
- # Set up log file for pg_upgrade
- PGUPLOG=/var/lib/pgsql/pgupgrade.log
- rm -f "$PGUPLOG"
- touch "$PGUPLOG" || exit 1
- chown postgres:postgres "$PGUPLOG"
- chmod go-rwx "$PGUPLOG"
- [ -x /sbin/restorecon ] && /sbin/restorecon "$PGUPLOG"
-
- # Move old DB to PGDATAOLD
- PGDATAOLD="${PGDATA}-old"
- rm -rf "$PGDATAOLD"
- mv "$PGDATA" "$PGDATAOLD" || exit 1
-
- # Create empty new-format database
- if perform_initdb
- then
- # Do the upgrade
- $SU_POSTGRES -c "$PGENGINE/pg_upgrade \
- '--old-bindir=$PREVPGENGINE' \
- '--new-bindir=$PGENGINE' \
- '--old-datadir=$PGDATAOLD' \
- '--new-datadir=$PGDATA' \
- --link \
- '--old-port=$PGPORT' '--new-port=$PGPORT' \
- --user=postgres" >> "$PGUPLOG" 2>&1 < /dev/null
- if [ $? -ne 0 ]
- then
- # pg_upgrade failed
- script_result=1
- fi
- else
- # initdb failed
- script_result=1
- fi
+initdb()
+{
+ __single_comand $"Initializing database" \
+ @NAME_BINARYBASE@-setup --initdb "$NAME" "$@"
+}
- if [ $script_result -eq 0 ]
- then
- echo_success
- else
- # Clean up after failure
- rm -rf "$PGDATA"
- mv "$PGDATAOLD" "$PGDATA"
- echo_failure
- fi
- echo
- echo $"See $PGUPLOG for details."
+upgrade()
+{
+ __single_comand $"Upgrading database" \
+ @NAME_BINARYBASE@-setup --upgrade "$NAME" "$@"
}
+
# See how we were called.
case "$1" in
start)
@@ -319,11 +223,13 @@ case "$1" in
restart
;;
initdb)
- initdb
- ;;
+ shift
+ initdb "$@"
+ ;;
upgrade)
- upgrade
- ;;
+ shift
+ upgrade "$@"
+ ;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|initdb|upgrade}"
exit 2
diff --git a/share/postgresql-setup/library.sh.in b/share/postgresql-setup/library.sh.in
index fe38f6c..cfbf213 100644
--- a/share/postgresql-setup/library.sh.in
+++ b/share/postgresql-setup/library.sh.in
@@ -71,7 +71,7 @@ parse_upgrade_setup()
test -d "$upgrade_confdir" || die "can't read confdir $upgrade_confdir"
local my_vars="id comment data_default engine description major scls \
- redhat_sockets_hack"
+ redhat_sockets_hack pghost_override"
while read conffile
do
read_config_file "$conffile" "$my_vars"
@@ -119,3 +119,21 @@ parse_upgrade_setup()
}
+# run_cmd_as_dbadmin COMMAND STDOUT STDERR
+run_cmd_as_dbadmin()
+{
+ # Convenient tool-aliases
+ local SU_POSTGRES="@SU_POSTGRES@"
+
+ local cmd="@SCL_SOURCE@$1"
+ local stdout="$2" stderr="$3"
+
+ # don't print additional spaces
+ set -- $cmd
+ debug "running command under postgres user: $@"
+ (
+ test -n "$stdout" && exec >>"$stdout"
+ test -n "$stderr" && exec 2>>"$stderr"
+ $SU_POSTGRES -c "$cmd" < /dev/null
+ )
+}