From bcc03a397b13d63030aedcfd703410b25ebb4725 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 22 May 2017 14:43:04 +0200 Subject: [PATCH] saving uncommitted changes in /etc prior to emerge run --- conf.d/postgresql-9.5 | 7 +++ init.d/postgresql-9.5 | 133 +++++++++++++++++++++++------------------- 2 files changed, 81 insertions(+), 59 deletions(-) diff --git a/conf.d/postgresql-9.5 b/conf.d/postgresql-9.5 index f50cf5f..f76e13c 100644 --- a/conf.d/postgresql-9.5 +++ b/conf.d/postgresql-9.5 @@ -1,3 +1,10 @@ +# Comma-separated list of directories that contain a unix +# socket. Created and controlled by the related initscript. The +# directories created will be owned root:postgres with mode 1775. +# +# /run/postgresql is the default directory. +PG_SOCKET_DIRECTORIES="/run/postgresql" + # Which port and socket to bind PostgreSQL PGPORT="5432" diff --git a/init.d/postgresql-9.5 b/init.d/postgresql-9.5 index 6b35885..a9361f0 100755 --- a/init.d/postgresql-9.5 +++ b/init.d/postgresql-9.5 @@ -1,13 +1,19 @@ #!/sbin/openrc-run -# Copyright 1999-2015 Gentoo Foundation +# Copyright 1999-2017 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -extra_started_commands="reload" +extra_started_commands="reload promote" + +PG_CTL="/usr/lib64/postgresql-9.5/bin/pg_ctl" + +description="PostgreSQL 9.5 -- the world's most advanced open source database -- +${RC_SERVICE} is a wrapper around pg_ctl with additional administrative checks +and convenience" get_config() { - [ -f ${PGDATA%/}/postgresql.conf ] || return 1 + [ -f "${PGDATA%/}/postgresql.conf" ] || return 1 - eval echo $(sed -e 's:#.*::' ${PGDATA%/}/postgresql.conf \ + eval echo $(sed -e 's:#.*::' "${PGDATA%/}/postgresql.conf" \ | awk '$1 == "'$1'" { print ($2 == "=" ? $3 : $2) }') } @@ -22,19 +28,17 @@ depend() { configured_port=$(get_config port) : ${configured_port:=${PGPORT}} -socket_paths=$(get_config unix_socket_directories) -: ${socket_paths:=/run/postgresql} checkconfig() { # Check that DATA_DIR has been set - if [ -z ${DATA_DIR} ] ; then + if [ -z "${DATA_DIR}" ] ; then eerror "DATA_DIR not set" eerror "HINT: Perhaps you need to update /etc/conf.d/postgresql-9.5" return 1 fi # Check that DATA_DIR exists - if [ ! -d ${DATA_DIR} ] ; then + if [ ! -d "${DATA_DIR}" ] ; then eerror "Directory not found: ${DATA_DIR}" eerror "HINT: Ensure that DATA_DIR points to the right path." eerror "HINT: Or perhaps you need to create the database cluster:" @@ -49,8 +53,8 @@ checkconfig() { local file for file in postgresql pg_hba pg_ident ; do file="${PGDATA%/}/${file}.conf" - if [ -f ${file} ] ; then - checkpath -f -m 0600 -o postgres:postgres ${file} + if [ -f "${file}" ] ; then + checkpath -f -m 0600 -o postgres:postgres "${file}" else eerror "${file} not found" eerror "HINT: mv ${DATA_DIR%/}/*.conf ${PGDATA}" @@ -60,50 +64,43 @@ checkconfig() { # Set the proper permission for the socket paths and create it if # it doesn't exist. - set -f; IFS=',' - local s - for s in ${socket_paths}; do - checkpath -d -m 1775 -o postgres:postgres ${s} - if [ -e ${s%/}/.s.PGSQL.${configured_port} ] ; then - eerror "Socket conflict." - eerror "A server is already listening on:" - eerror " ${s%/}/.s.PGSQL.${configured_port}" - eerror "HINT: Change PGPORT to listen on a different socket." - return 1 - fi - done - set +f; unset IFS + set -f; IFS=',' + local s + for s in ${PG_SOCKET_DIRECTORIES}; do + checkpath -d -m 1775 -o root:postgres "${s}" + if [ -e "${s%/}/.s.PGSQL.${configured_port}" ] ; then + eerror "Socket conflict." + eerror "A server is already listening on:" + eerror " ${s%/}/.s.PGSQL.${configured_port}" + eerror "HINT: Change PGPORT to listen on a different socket." + return 1 + fi + done + set +f; unset IFS } start() { checkconfig || return 1 - ebegin "Starting PostgreSQL" + ebegin "Starting PostgreSQL 9.5" - rm -f ${DATA_DIR%/}/postmaster.pid + rm -f "${DATA_DIR%/}/postmaster.pid" - local extraenv - local x - for x in ${PG_EXTRA_ENV} ; do - extraenv="${extraenv} --env ${x}" - done + su - postgres -c \ + "PGPORT=${configured_port} ${PG_EXTRA_ENV} ${PG_CTL} start \ + -s -w -t ${START_TIMEOUT} -l ${DATA_DIR%/}/postmaster.log \ + -D ${PGDATA} \ + -o '--data-directory=${DATA_DIR} \ + --unix-socket-directories=${PG_SOCKET_DIRECTORIES} \ + ${PGOPTS}'" - start-stop-daemon --start \ - --user postgres \ - --env "PGPORT=${configured_port}" \ - ${extraenv} \ - --pidfile ${DATA_DIR%/}/postmaster.pid \ - --exec /usr/lib64/postgresql-9.5/bin/pg_ctl \ - -- start -s -w -t ${START_TIMEOUT} -l ${DATA_DIR%/}/postmaster.log \ - -D ${PGDATA} -o "--data-directory=${DATA_DIR} ${PGOPTS}" local retval=$? if [ $retval -ne 0 ] ; then - eerror "Check the log for a possible explanation of the above error. The log may be" + eerror "Check the log for a possible explanation of the above error." + eerror "The log may be located at:" eerror " ${DATA_DIR%/}/postmaster.log" - eerror "Or wherever you configured PostgreSQL 9.5 log messages to be sent." - eend $retval - return $retval + eerror "Or wherever you configured PostgreSQL 9.5 to log." fi eend $retval @@ -111,32 +108,50 @@ start() { stop() { local seconds=$(( ${NICE_TIMEOUT} + ${RUDE_TIMEOUT} + ${FORCE_TIMEOUT} )) - ebegin "Stopping PostgreSQL (this can take up to ${seconds} seconds)" + ebegin "Stopping PostgreSQL 9.5 (this can take up to ${seconds} seconds)" - local retval - local retries=SIGTERM/${NICE_TIMEOUT} + su - postgres -c \ + "${PG_CTL} stop -t ${NICE_TIMEOUT} -s -D ${DATA_DIR} -m smart" + local retval=$? - if [ "${RUDE_QUIT}" != "NO" ] ; then - einfo "RUDE_QUIT enabled." - retries="${retries}/SIGINT/${RUDE_TIMEOUT}" + if [ "${RUDE_QUIT}" != "NO" -a ${retval} -ne 0 ] ; then + einfo "Previous attempt failed. Trying RUDE_QUIT." + su - postgres -c \ + "${PG_CTL} stop -t ${RUDE_TIMEOUT} -s -D ${DATA_DIR} -m fast" + retval=$? fi - if [ "${FORCE_QUIT}" = "YES" ] ; then - einfo "FORCE_QUIT enabled." + + if [ "${FORCE_QUIT}" = "YES" -a ${retval} -ne 0 ] ; then + einfo "Previous step failed. Trying FORCE_QUIT." ewarn "A recover-run might be executed on next startup." - retries="${retries}/SIGQUIT/${FORCE_TIMEOUT}" + su - postgres -c \ + "${PG_CTL} stop -t ${FORCE_TIMEOUT} -s -D ${DATA_DIR} -m immediate" + retval=$? fi - # Loops through nice, rude, and force quit in one go. - start-stop-daemon --stop \ - --exec /usr/lib64/postgresql-9.5/bin/postgres \ - --retry ${retries} \ - --pidfile ${DATA_DIR%/}/postmaster.pid + eend ${retval} +} - eend +status() { + ebegin "Checking PostgreSQL 9.5 status" + su - postgres -c "${PG_CTL} status -D ${DATA_DIR}" + eend $? } +description_reload="Simply sends the postgres process a SIGHUP signal, causing + it to reread its configuration files (postgresql.conf, pg_hba.conf, + etc.). This allows changing of configuration-file options that do not + require a complete restart to take effect." reload() { - ebegin "Reloading PostgreSQL configuration" - kill -HUP $(head -n1 ${DATA_DIR%/}/postmaster.pid) + ebegin "Reloading PostgreSQL 9.5 configuration" + su - postgres -c "${PG_CTL} reload -s -D ${DATA_DIR}" + eend $? +} + +description_promote="If the server is in standby, it is commanded to exit + recovery and begin read-write operations." +promote() { + ebegin "Promoting PostgreSQL 9.5" + su - postgres -c "${PG_CTL} promote -s -D ${DATA_DIR}" eend $? } -- 2.39.5