From 81e523c0957a9bee557bf2cca25607989209ac2c Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Tue, 17 Mar 2015 11:34:21 +0100 Subject: 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. --- postgresql-setup.in | 95 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 30 deletions(-) (limited to 'postgresql-setup.in') 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 -- cgit