|
| 1 | +# This file contains common functionality for all postgresql server |
| 2 | +# package init.d scripts. It is usually included by |
| 3 | +# /etc/init.d/postgresql |
| 4 | + |
| 5 | +init_functions=/lib/lsb/init-functions |
| 6 | +alias pg_ctlcluster="pg_ctlcluster --skip-systemctl-redirect" |
| 7 | + |
| 8 | +#redhat# init_functions=/usr/share/postgresql-common/init-functions-compat |
| 9 | +. $init_functions |
| 10 | + |
| 11 | +PGBINROOT="/usr/lib/postgresql/" |
| 12 | +#redhat# PGBINROOT="/usr/pgsql-" |
| 13 | + |
| 14 | +# do pg_ctlcluster action $1 to all clusters of version $2 with command |
| 15 | +# description $3; output according to Debian Policy for init scripts |
| 16 | +do_ctl_all() { |
| 17 | + [ "$1" ] || { echo "Error: invalid command '$1'" >&2; exit 1; } |
| 18 | + [ "$2" ] || { echo "Error: invalid version '$2'" >&2; exit 1; } |
| 19 | + [ -d "/etc/postgresql/$2" ] || return 0 |
| 20 | + [ "$(ls /etc/postgresql/$2)" ] || return 0 |
| 21 | + [ -x "$PGBINROOT$2/bin/postgres" ] || return 0 |
| 22 | + |
| 23 | + status=0 |
| 24 | + log_daemon_msg "$3" |
| 25 | + for c in /etc/postgresql/"$2"/*; do |
| 26 | + [ -e "$c/postgresql.conf" ] || continue |
| 27 | + name=$(basename "$c") |
| 28 | + |
| 29 | + # evaluate start.conf |
| 30 | + if [ -e "$c/start.conf" ]; then |
| 31 | + start=$(sed 's/#.*$//; /^[[:space:]]*$/d; s/^\s*//; s/\s*$//' "$c/start.conf") |
| 32 | + else |
| 33 | + start=auto |
| 34 | + fi |
| 35 | + [ "$start" = "auto" ] || continue |
| 36 | + |
| 37 | + log_progress_msg "$name" |
| 38 | + set +e |
| 39 | + if [ "$1" = "stop" ] || [ "$1" = "restart" ]; then |
| 40 | + ERRMSG=$(pg_ctlcluster --force "$2" "$name" $1 2>&1) |
| 41 | + else |
| 42 | + ERRMSG=$(pg_ctlcluster "$2" "$name" $1 2>&1) |
| 43 | + fi |
| 44 | + res=$? |
| 45 | + set -e |
| 46 | + # Do not fail on success or if cluster is already/not running |
| 47 | + [ $res -eq 0 ] || [ $res -eq 2 ] || status=$(($status || $res)) |
| 48 | + done |
| 49 | + if [ $status -ne 0 -a -n "$ERRMSG" ]; then |
| 50 | + log_failure_msg "$ERRMSG" |
| 51 | + fi |
| 52 | + log_end_msg $status |
| 53 | + return $status |
| 54 | +} |
| 55 | + |
| 56 | +# create /var/run/postgresql |
| 57 | +create_socket_directory() { |
| 58 | + if [ -d /var/run/postgresql ]; then |
| 59 | + chmod 2775 /var/run/postgresql |
| 60 | + else |
| 61 | + install -d -m 2775 -o postgres -g postgres /var/run/postgresql |
| 62 | + [ -x /sbin/restorecon ] && restorecon -R /var/run/postgresql || true |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +# start all clusters of version $1 |
| 67 | +# output according to Debian Policy for init scripts |
| 68 | +start() { |
| 69 | + do_ctl_all start "$1" "Starting PostgreSQL $1 database server" |
| 70 | +} |
| 71 | + |
| 72 | +# stop all clusters of version $1 |
| 73 | +# output according to Debian Policy for init scripts |
| 74 | +stop() { |
| 75 | + do_ctl_all stop "$1" "Stopping PostgreSQL $1 database server" |
| 76 | +} |
| 77 | + |
| 78 | +# restart all clusters of version $1 |
| 79 | +# output according to Debian Policy for init scripts |
| 80 | +restart() { |
| 81 | + do_ctl_all restart "$1" "Restarting PostgreSQL $1 database server" |
| 82 | +} |
| 83 | + |
| 84 | +# reload all clusters of version $1 |
| 85 | +# output according to Debian Policy for init scripts |
| 86 | +reload() { |
| 87 | + do_ctl_all reload "$1" "Reloading PostgreSQL $1 database server" |
| 88 | +} |
| 89 | + |
| 90 | +status() { |
| 91 | + CLUSTERS=`pg_lsclusters -h | grep "^$1[[:space:]]"` |
| 92 | + # no clusters -> unknown status |
| 93 | + [ -n "$CLUSTERS" ] || exit 4 |
| 94 | + echo "$CLUSTERS" | awk 'BEGIN {rc=0; printf("Running clusters: ")} {if (match($4, "online")) printf ("%s/%s ", $1, $2); else rc=3} END { printf("\n"); exit rc }' |
| 95 | +} |
| 96 | + |
| 97 | +# return all installed versions which do not have their own init script |
| 98 | +get_versions() { |
| 99 | + versions='' |
| 100 | + local v dir skipinit |
| 101 | + |
| 102 | + skipinit=continue |
| 103 | + #redhat# skipinit=true # RedHat systems will have /etc/init.d/postgresql-* provided by the yum.pg.o package |
| 104 | + dir=$PGBINROOT |
| 105 | + #redhat# dir="-d /usr/pgsql-*" |
| 106 | + |
| 107 | + for v in `ls $dir 2>/dev/null`; do |
| 108 | + #redhat# v=${v#*-} |
| 109 | + [ -x /etc/init.d/postgresql-$v ] && $skipinit |
| 110 | + if [ -x $PGBINROOT$v/bin/pg_ctl ]; then |
| 111 | + versions="$versions $v" |
| 112 | + fi |
| 113 | + done |
| 114 | +} |
0 commit comments