/usr/bin/pg_virtualenv is in postgresql-common 154ubuntu1.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | #!/bin/bash
# Create a throw-away PostgreSQL environment for running regression tests. This
# happens in a temp dir/on unshared tmpfses, so does not interfere with
# existing clusters.
#
# (C) 2005-2012 Martin Pitt <mpitt@debian.org>
# (C) 2012-2014 Christoph Berg <myon@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
set -e # no -u here as that breaks PGCONF_OPTS[@]
case ${LD_PRELOAD:-} in
*fakeroot*) NONROOT=1 ;;
esac
if [ -z "${NONROOT:-}" ] && [ "$(id -u)" != 0 ]; then
NONROOT=1
fi
# root operation: re-exec ourselves through unshare
if [ -z "${NONROOT:-}" ] && [ -z "${UNSHARED:-}" ]; then
UNSHARED=1 exec unshare -uimn -- "$0" "$@"
fi
# unshared program starts here
help ()
{
echo "pg_virtualenv: Create throw-away PostgreSQL environment for regression tests"
echo "Syntax: $0 [options] [command]"
echo " -a use all installed server versions"
echo " -v 'version ...' list of PostgreSQL versions to run [default: latest]"
echo " -c 'options' extra options to pass to pg_createcluster"
echo " -i 'initdb opts' extra initdb options to pass to pg_createcluster"
echo " -o 'guc=value' postgresql.conf options to pass to pg_createcluster"
echo " -s open a shell when command fails"
exit ${1:-0}
}
# option parsing
PG_VERSIONS=""
PGCONF_OPTS=()
while getopts "ac:i:ho:sv:" opt ; do
case $opt in
a) for d in /usr/lib/postgresql/*/bin/pg_ctl; do
# prepend version so latest ends up first (i.e. on port 5432)
PG_VERSIONS="$(basename ${d%%/bin/pg_ctl}) ${PG_VERSIONS:-}"
done ;;
c) CREATE_OPTS="$OPTARG" ;;
i) INITDB_OPTS="$OPTARG" ;;
h) help ;;
o) PGCONF_OPTS+=("--pgoption" "$OPTARG") ;;
s) run_shell=1 ;;
v) PG_VERSIONS="$OPTARG" ;;
*) help 1 ;;
esac
done
if [ -z "$PG_VERSIONS" ]; then
# use latest version
d=$(ls /usr/lib/postgresql/*/bin/pg_ctl | tail -1)
PG_VERSIONS="$(basename ${d%%/bin/pg_ctl})"
fi
# shift away args
shift $(($OPTIND - 1))
# if no command is given, open a shell
[ "${1:-}" ] || set -- ${SHELL:-/bin/sh}
# non-root operation: create a temp dir where we store everything
if [ "${NONROOT:-}" ]; then
WORKDIR=$(mktemp -d -t pg_virtualenv.XXXXXX)
export PG_CLUSTER_CONF_ROOT="$WORKDIR/postgresql"
export PGUSER="${USER:-${LOGNAME:-$(id -un)}}"
PGSYSCONFDIR="$WORKDIR/postgresql-common" # no export yet so pg_createcluster uses the original createcluster.conf
mkdir "$PGSYSCONFDIR" "$WORKDIR/log"
PWFILE="$PGSYSCONFDIR/pwfile"
LOGDIR="$WORKDIR/log"
cleanup () {
set +e
for v in $PG_VERSIONS; do
echo "Stopping cluster $v/regress..."
pg_ctlcluster --mode immediate $v regress stop
done
rm -rf $WORKDIR
}
trap cleanup 0 HUP INT QUIT ILL ABRT PIPE TERM
# root operation: keep everything in the standard locations
else
# let everything happen in overlay tmpfses to avoid interfering with already
# existing clusters; this also speeds up testing
created_dirs=""
for d in /etc/postgresql /var/lib/postgresql /var/log/postgresql /var/run/postgresql; do
if ! [ -d $d ]; then
created_dirs="$created_dirs $d"
mkdir -p $d
fi
mount -n -t tmpfs -o mode=755 tmpfs $d
done
: ${PGSYSCONFDIR:=/etc/postgresql-common}
pg_service="$PGSYSCONFDIR/pg_service.conf"
export PGUSER="postgres"
PWFILE=$(mktemp -t pgpassword.XXXXXX)
chown postgres:postgres "$PWFILE"
# clean up created clusters and directories after us
cleanup () {
set +e
for v in $PG_VERSIONS; do
echo "Stopping cluster $v/regress..."
pg_ctlcluster --mode immediate $v regress stop
done
if [ "$created_dirs" ]; then
umount $created_dirs
rmdir --ignore-fail-on-non-empty -p $created_dirs
fi
rm -f $PWFILE $pg_service
if [ -f $pg_service.pg_virtualenv-save ]; then
mv -f $pg_service.pg_virtualenv-save $pg_service
fi
}
trap cleanup 0 HUP INT QUIT ILL ABRT PIPE TERM
chown root:postgres /var/log/postgresql
chmod 1775 /var/log/postgresql
chown postgres:postgres /var/run/postgresql
chmod 2775 /var/run/postgresql
if [ -f $pg_service ]; then
mv -f $pg_service $pg_service.pg_virtualenv-save
fi
# start localhost interface
ifconfig lo up || true
fi
# create postgres environments
if [ -x /usr/bin/pwgen ]; then
export PGPASSWORD=$(pwgen 20 1)
else
export PGPASSWORD=$(dd if=/dev/urandom bs=1k count=1 2>/dev/null | md5sum - | awk '{ print $1 }')
fi
echo "$PGPASSWORD" > "$PWFILE"
for v in $PG_VERSIONS; do
# create temporary cluster
# we chdir to / so programs don't throw "could not change directory to ..."
(
cd /
pg_createcluster \
${NONROOT:+-d "$WORKDIR/data/$v/regress"} \
${NONROOT:+-l "$WORKDIR/log/postgresql-$v-regress.log"} \
${CREATE_OPTS:-} "${PGCONF_OPTS[@]}" --start $v regress -- \
--username="$PGUSER" --pwfile="$PWFILE" ${INITDB_OPTS:-}
# in fakeroot, the username will likely default to "postgres" otherwise
)
port=$(perl -lne 'print $1 if /^port = (\d+)/' \
${PG_CLUSTER_CONF_ROOT:-/etc/postgresql}/$v/regress/postgresql.conf)
# record cluster information in service file
cat >> $PGSYSCONFDIR/pg_service.conf <<EOF
[$v]
host=localhost
port=$port
dbname=postgres
user=$PGUSER
password=$PGPASSWORD
EOF
done
export PGSYSCONFDIR
export PGHOST="localhost"
export PGDATABASE="postgres"
case $PG_VERSIONS in
*\ *) ;; # multiple versions: do not set PGPORT because that breaks --cluster
*)
export PGPORT="$port" # will always be 5432 in root mode, but could differ otherwise
export PG_CONFIG="/usr/lib/postgresql/$PG_VERSIONS/bin/pg_config"
;;
esac
# run program
"$@" || EXIT="$?"
if [ ${EXIT:-0} -gt 0 ]; then
for log in ${LOGDIR:-/var/log/postgresql}/*.log; do
echo "$log:"
tail -100 $log
done
if [ "${run_shell:-}" ]; then
echo "pg_virtualenv: command exited with status $EXIT, dropping you into a shell"
$SHELL
fi
fi
exit ${EXIT:-0}
|