diff options
author | Pavel Raiskup <praiskup@redhat.com> | 2014-10-02 18:06:17 +0200 |
---|---|---|
committer | Pavel Raiskup <praiskup@redhat.com> | 2014-10-02 18:16:44 +0200 |
commit | aa49c2f1f1f89bca0ca6c6ed5c93c49f6e5c95e5 (patch) | |
tree | a32837f9918dd54956a70992506a71cb2fd1bee7 | |
parent | 33481db83bb5f6f7d1017e17a00cd0012a06f117 (diff) | |
download | postgresql-setup-aa49c2f1f1f89bca0ca6c6ed5c93c49f6e5c95e5.tar.gz postgresql-setup-aa49c2f1f1f89bca0ca6c6ed5c93c49f6e5c95e5.tar.xz postgresql-setup-aa49c2f1f1f89bca0ca6c6ed5c93c49f6e5c95e5.zip |
sysvinit: generate init file when needed
From now, if ./configure --with-sysvinit is done, we install init
script and do not install systemd files. Original patch written
by Jozef MlĂch (jmlich@redhat.com).
* Makefile.am (initscriptsdir, nodist_initscripts_DATA): New
variables. New postgresql.init target.
* configure.ac: Add --with-sysvinit option.
* postgresql.init.in: New sysvinit template.
* .gitignore: Mention postgresql.init.
* NEWS: Document.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 11 | ||||
-rw-r--r-- | NEWS | 9 | ||||
-rw-r--r-- | configure.ac | 10 | ||||
-rw-r--r-- | postgresql.init.in | 337 |
5 files changed, 366 insertions, 2 deletions
@@ -21,6 +21,7 @@ postgresql*-ctl postgresql*.service postgresql*-setup postgresql*-setup.1 +postgresql*.init README.rpm-dist testsuite testsuite.dir/ diff --git a/Makefile.am b/Makefile.am index 8aeac27..9301d56 100644 --- a/Makefile.am +++ b/Makefile.am @@ -9,8 +9,13 @@ libexec_SCRIPTS = postgresql$(DISTSUFF)-ctl \ userunitsdir = /etc/systemd/system +if WANT_SYSVINIT +initscriptsdir = $(prefix)/etc/init.d +nodist_initscripts_DATA = postgresql$(DISTSUFF).init +else systemdunitsdir = $(prefix)/lib/systemd/system nodist_systemdunits_DATA = postgresql$(DISTSUFF).service postgresql$(DISTSUFF)@.service +endif nodist_doc_DATA = $(README_DIST_BASENAME) @@ -25,6 +30,9 @@ postgresql$(DISTSUFF)-setup: postgresql-setup.in $(c_s) postgresql$(DISTSUFF)-check-db-dir: postgresql-check-db-dir.in $(c_s) $(INSTANTIATE_SCRIPT) +postgresql$(DISTSUFF).init: postgresql.init.in $(c_s) + $(INSTANTIATE) + # In ideal world, postgresql@.service would be hardlink to postgresql.service. # That would require, however, the rhbz#1141824 resolved so we could install # system-default drop-in file. Make a "almost" duplicate files now. @@ -48,7 +56,8 @@ GENERATED_FILES = $(bin_SCRIPTS) \ $(libexec_SCRIPTS) \ $(nodist_doc_DATA) \ $(pkgsysconf_DATA) \ - $(nodist_systemdunits_DATA) + $(nodist_systemdunits_DATA) \ + $(nodist_initscripts_DATA) CLEANFILES = $(GENERATED_FILES) $(TEST_GEN_FILES_LIST) @@ -1,6 +1,13 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -New in 1.0.0 version +New in 2.0 version + +* With configre --with-sysvinit we are able to install sysvinit + configuration instead of systemd. + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +New in 1.0 version * The postgresql-check-db-dir is now more strict to admin's configuration and tries to avoid future incompatibilities by warning about the diff --git a/configure.ac b/configure.ac index a4d0c0f..7cc23df 100644 --- a/configure.ac +++ b/configure.ac @@ -17,6 +17,16 @@ AC_CONFIG_FILES([tests/Makefile]) AM_MISSING_PROG([AUTOM4TE], [autom4te]) AC_SUBST([TEST_GEN_FILES_LIST], [.generated_files]) +# TODO: detect systemd/sysvinit. Also, we should probably be able to install +# both configuration at once when needed. +AC_ARG_WITH([sysvinit], + AS_HELP_STRING([--with-sysvinit], + [prepares sys v init script]), + [WANT_SYSVINIT=1], + [WANT_SYSVINIT=0] +) + +AM_CONDITIONAL([WANT_SYSVINIT], [test "$WANT_SYSVINIT" -eq 1]) # Instatiating generated files by sed instead of config.status. # This is encouraged by autoconf documentation itself because if we used diff --git a/postgresql.init.in b/postgresql.init.in new file mode 100644 index 0000000..f3e21c8 --- /dev/null +++ b/postgresql.init.in @@ -0,0 +1,337 @@ +#!/bin/sh +# +# postgresql 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) +# +# chkconfig: - 64 36 +# description: PostgreSQL database server. +# processname: postmaster +# pidfile: /var/run/postmaster.PORT.pid +### BEGIN INIT INFO +# Provides: postgresql +# 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 +# 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 +PGDOCDIR=@PGDOCDIR@ + +# Source function library. +. /etc/rc.d/init.d/functions + +# Get network config. +. /etc/sysconfig/network + +# Find the name of the script +NAME=`basename $0` +if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ] +then + NAME=${NAME:3} +fi + +# For SELinux we need to use 'runuser' not 'su' +if [ -x /sbin/runuser ] +then + SU=runuser +else + SU=su +fi + + +# Set defaults for configuration variables +PGENGINE=/usr/bin +PGPORT=5432 +PGDATA=/var/lib/pgsql/data +PGLOG=/var/lib/pgsql/pgstartup.log +# Value to set as postmaster process's oom_adj +PG_OOM_ADJ=-17 + +# Override defaults from /etc/sysconfig/pgsql if file is present +[ -f /etc/sysconfig/pgsql/${NAME} ] && . /etc/sysconfig/pgsql/${NAME} + +export PGDATA +export PGPORT + +lockfile="/var/lock/subsys/${NAME}" +pidfile="/var/run/postmaster.${PGPORT}.pid" + +script_result=0 + +start(){ + [ -x "$PGENGINE/postmaster" ] || exit 5 + + PSQL_START=$"Starting ${NAME} service: " + + # Make sure startup-time log file is valid + if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ] + then + touch "$PGLOG" || exit 4 + chown postgres:postgres "$PGLOG" + chmod go-rwx "$PGLOG" + [ -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 $PGDOCDIR/README.rpm-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 $PGDOCDIR/README.rpm-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 + + echo -n "$PSQL_START" + test x"$PG_OOM_ADJ" != x && echo "$PG_OOM_ADJ" > /proc/self/oom_adj + $SU -l postgres -c "$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null + sleep 2 + pid=`head -n 1 "$PGDATA/postmaster.pid" 2>/dev/null` + if [ "x$pid" != x ] + then + success "$PSQL_START" + touch "$lockfile" + echo $pid > "$pidfile" + echo + else + failure "$PSQL_START" + echo + script_result=1 + fi +} + +stop(){ + echo -n $"Stopping ${NAME} service: " + if [ -e "$lockfile" ] + then + $SU -l postgres -c "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" > /dev/null 2>&1 < /dev/null + ret=$? + if [ $ret -eq 0 ] + then + echo_success + rm -f "$pidfile" + rm -f "$lockfile" + else + echo_failure + script_result=1 + fi + else + # not running; per LSB standards this is "ok" + echo_success + fi + echo +} + +restart(){ + stop + start +} + +condrestart(){ + [ -e "$lockfile" ] && restart || : +} + +reload(){ + $SU -l 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 -l 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 +} + +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 +} + +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 -l 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 + + 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." +} + +# See how we were called. +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status -p "$pidfile" postmaster + script_result=$? + ;; + restart) + restart + ;; + condrestart|try-restart) + condrestart + ;; + reload) + reload + ;; + force-reload) + restart + ;; + initdb) + initdb + ;; + upgrade) + upgrade + ;; + *) + echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|initdb|upgrade}" + exit 2 +esac + +exit $script_result |