Debian Codegraph @

pg_virtualenv

raw
1 #!/bin/bash
2
3 # Create a throw-away PostgreSQL environment for running regression tests.
4 # This does not interfere with existing clusters.
5 #
6 # (C) 2005-2012 Martin Pitt <[email protected]>
7 # (C) 2012-2020 Christoph Berg <[email protected]>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18
19 set -e # no -u here as that breaks PGCONF_OPTS[@]
20
21 # wrap ourselves in newpid if requested
22 if [ "$PG_VIRTUALENV_NEWPID" ]; then
23 unset PG_VIRTUALENV_NEWPID
24 exec newpid $0 "$@"
25 fi
26
27 # wrap ourselves in unshare if requested
28 if [ "$PG_VIRTUALENV_UNSHARE" ]; then
29 export _PG_VIRTUALENV_UNSHARE="$PG_VIRTUALENV_UNSHARE"
30 unset PG_VIRTUALENV_UNSHARE
31 exec unshare $_PG_VIRTUALENV_UNSHARE -- $0 "$@"
32 fi
33 if [ "$_PG_VIRTUALENV_UNSHARE" ]; then
34 unset _PG_VIRTUALENV_UNSHARE
35 # start localhost interface
36 if [ -x /bin/ip ]; then
37 ip link set dev lo up || true
38 else
39 ifconfig lo up || true
40 fi
41 fi
42
43 disable_fakeroot ()
44 {
45 case ${LD_PRELOAD:-} in
46 *fakeroot*) LD_PRELOAD=$(echo "$LD_PRELOAD" | sed -e 's/[^ ]*fakeroot[^ ]*//g') ;;
47 esac
48 }
49
50 help ()
51 {
52 echo "pg_virtualenv: Create throw-away PostgreSQL environment for regression tests"
53 echo "Syntax: $0 [options] [command]"
54 echo " -a use all installed server versions"
55 echo " -v 'version ...' list of PostgreSQL versions to run [default: latest]"
56 echo " -c 'options' extra options to pass to pg_createcluster"
57 echo " -i 'initdb opts' extra initdb options to pass to pg_createcluster"
58 echo " -o 'guc=value' postgresql.conf options to pass to pg_createcluster"
59 echo " -p 'package' set options to find extension files in debian/package/"
60 echo " -s open a shell when command fails"
61 echo " -t use a temporary cluster directory even as root"
62 exit ${1:-0}
63 }
64
65 # option parsing
66 PGBINROOT="/usr/lib/postgresql/"
67 #redhat# PGBINROOT="/usr/pgsql-"
68 PG_VERSIONS=""
69 PGCONF_OPTS=()
70 while getopts "ac:i:ho:p:stv:" opt ; do
71 case $opt in
72 a) for d in $PGBINROOT*/bin/pg_ctl; do
73 # prepend version so latest ends up first (i.e. on port 5432)
74 dir=${d%%/bin/pg_ctl}
75 PG_VERSIONS="${dir#$PGBINROOT} ${PG_VERSIONS:-}"
76 done ;;
77 c) CREATE_OPTS="$OPTARG" ;;
78 i) INITDB_OPTS="$OPTARG" ;;
79 h) help ;;
80 o) PGCONF_OPTS+=("--pgoption" "$OPTARG") ;;
81 p) PACKAGE="$OPTARG" ;;
82 s) run_shell=1 ;;
83 t) NONROOT=1 ;;
84 v) PG_VERSIONS="$OPTARG" ;;
85 *) help 1 ;;
86 esac
87 done
88 if [ -z "$PG_VERSIONS" ]; then
89 # use latest version
90 d=$(ls -v $PGBINROOT*/bin/pg_ctl 2> /dev/null | tail -1)
91 if [ -z "$d" ]; then
92 echo "Could not determine PostgreSQL version, are any PostgreSQL server packages installed?" >&2
93 exit 2
94 fi
95 dir=${d%%/bin/pg_ctl}
96 PG_VERSIONS="${dir#$PGBINROOT}"
97 fi
98 # shift away args
99 shift $(($OPTIND - 1))
100 # if no command is given, open a shell
101 [ "${1:-}" ] || set -- ${SHELL:-/bin/sh}
102
103 # generate a password
104 if [ -x /usr/bin/pwgen ]; then
105 export PGPASSWORD=$(pwgen 20 1)
106 else
107 export PGPASSWORD=$(dd if=/dev/urandom bs=1k count=1 2>/dev/null | md5sum - | awk '{ print $1 }')
108 fi
109
110 # we are not root
111 if [ "$(id -u)" != 0 ]; then
112 NONROOT=1
113 fi
114 # we aren't really root in fakeroot (but leave it enabled for commands spawned)
115 case ${LD_PRELOAD:-} in
116 *fakeroot*) NONROOT=1 ;;
117 esac
118
119 # non-root operation: create a temp dir where we store everything
120 if [ "${NONROOT:-}" ]; then
121 WORKDIR=$(mktemp -d -t pg_virtualenv.XXXXXX)
122 if [ $(id -u) = 0 ]; then
123 chown postgres:postgres $WORKDIR
124 umask 022
125 fi
126 export PG_CLUSTER_CONF_ROOT="$WORKDIR/postgresql"
127 export PGUSER="${USER:-${LOGNAME:-$(id -un)}}"
128 [ "$PGUSER" = "root" ] && PGUSER="postgres"
129 PGSYSCONFDIR="$WORKDIR/postgresql-common" # no export yet so pg_createcluster uses the original createcluster.conf
130 mkdir "$PGSYSCONFDIR" "$WORKDIR/log"
131 PWFILE="$PGSYSCONFDIR/pwfile"
132 LOGDIR="$WORKDIR/log"
133 echo "$PGPASSWORD" > "$PWFILE"
134
135 cleanup () {
136 set +e
137 disable_fakeroot
138 for v in $PG_VERSIONS; do
139 # don't drop existing clusters named "regress"
140 [ -f $PG_CLUSTER_CONF_ROOT/$v/regress/.by_pg_virtualenv ] || continue
141 echo "Dropping cluster $v/regress ..."
142 pg_ctlcluster --mode immediate $v regress stop
143 pg_dropcluster $v regress
144 done
145 rm -rf $WORKDIR
146 }
147 trap cleanup 0 HUP INT QUIT ILL ABRT PIPE TERM
148
149 # root: keep everything in the standard locations
150 else
151 for v in $PG_VERSIONS; do
152 if [ -d /etc/postgresql/$v/regress ]; then
153 echo "Cluster $v/regress exists, refusing to overwrite" >&2
154 exit 2
155 fi
156 done
157
158 : ${PGSYSCONFDIR:=/etc/postgresql-common}
159 pg_service="$PGSYSCONFDIR/pg_service.conf"
160
161 export PGUSER="postgres"
162 PWFILE=$(mktemp -t pgpassword.XXXXXX)
163 echo "$PGPASSWORD" > "$PWFILE" # write password before chowning the file
164 chown postgres:postgres "$PWFILE"
165
166 cleanup () {
167 set +e
168 rm -f $PWFILE $pg_service
169 if [ -f $pg_service.pg_virtualenv-save.$$ ]; then
170 mv -f $pg_service.pg_virtualenv-save.$$ $pg_service
171 fi
172 for v in $PG_VERSIONS; do
173 # don't drop existing clusters named "regress"
174 [ -f /etc/postgresql/$v/regress/.by_pg_virtualenv ] || continue
175 echo "Dropping cluster $v/regress ..."
176 rm -f /etc/postgresql/$v/regress/.by_pg_virtualenv
177 pg_ctlcluster --mode immediate $v regress stop
178 pg_dropcluster $v regress
179 done
180 }
181 trap cleanup 0 HUP INT QUIT ILL ABRT PIPE TERM
182
183 if [ -f $pg_service ]; then
184 mv --no-clobber $pg_service $pg_service.pg_virtualenv-save.$$
185 fi
186 fi
187
188 # create postgres environments
189 for v in $PG_VERSIONS; do
190 # create temporary cluster
191 if [ "${PACKAGE:-}" ]; then
192 if [ ${NONROOT:-} ]; then
193 extra_config="$WORKDIR/regress.conf"
194 else
195 extra_config="/etc/postgresql/$v/regress/regress.conf"
196 fi
197 case $v in
198 18|19|[2-7]*) # find extension using extension_control_path
199 PKGARGS="--pgoption include=$extra_config"
200 cat >$extra_config <<-EOF
201 extension_control_path='$PWD/debian/$PACKAGE/usr/share/postgresql/$v:\$system'
202 dynamic_library_path='$PWD/debian/$PACKAGE/usr/lib/postgresql/$v/lib:/usr/lib/postgresql/$v/lib'
203 EOF
204 ;;
205 *) # older versions have a Debian-specific patch
206 if grep -q 'extension_destdir' /usr/share/postgresql/$v/postgresql.conf.sample; then
207 PKGARGS="--pgoption include=$extra_config"
208 cat >$extra_config <<-EOF
209 extension_destdir='$PWD/debian/$PACKAGE'
210 dynamic_library_path='$PWD/debian/$PACKAGE/usr/lib/postgresql/$v/lib:/usr/lib/postgresql/$v/lib'
211 EOF
212 else
213 echo "$0: This PostgreSQL $v installation does not support 'extension_destdir', skipping this version"
214 [ "$PG_VERSIONS" = "$v" ] && exit 0 # only one version requested
215 continue
216 fi
217 ;;
218 esac
219 # tell regress and tap tests that recreate new temp instances to use the new config
220 export EXTRA_REGRESS_OPTS="--temp-config=$extra_config"
221 export PG_TEST_INITDB_EXTRA_OPTS="-c include=$extra_config"
222 fi
223 # we chdir to / so programs don't throw "could not change directory to ..."
224 (
225 cd /
226 case $v in
227 8*|9.0|9.1|9.2) : ;;
228 *) NOSYNC="--nosync" ;;
229 esac
230 if [ "${NONROOT:-}" ]; then
231 # stats_temp_directory in /var/run/postgresql needs root (or postgres), disable it
232 case $v in
233 8.4|9.*|1[01234]) PGCONF_OPTS+=("--pgoption" "stats_temp_directory=") ;;
234 esac
235 fi
236 # disable fakeroot here because we really can't run as root
237 # (and still switch to postgres when using fakeroot as root user)
238 disable_fakeroot
239 pg_createcluster --quiet \
240 ${PGPORT:+-p "$PGPORT"} \
241 ${NONROOT:+-d "$WORKDIR/data/$v/regress"} \
242 ${NONROOT:+-l "$WORKDIR/log/postgresql-$v-regress.log"} \
243 ${CREATE_OPTS:-} --pgoption fsync=off ${PKGARGS:-} "${PGCONF_OPTS[@]}" \
244 $v regress -- \
245 --username="$PGUSER" --pwfile="$PWFILE" $NOSYNC ${INITDB_OPTS:-}
246 # in fakeroot, the username will likely default to "postgres" otherwise
247 echo "This is a temporary throw-away cluster" > ${PG_CLUSTER_CONF_ROOT:-/etc/postgresql}/$v/regress/.by_pg_virtualenv
248 # start cluster with coredumps enabled
249 [ $v != 8.2 ] && ENABLE_COREDUMPS="-c"
250 pg_ctlcluster $v regress start -- ${ENABLE_COREDUMPS:-}
251 )
252 port=$(pg_conftool -s $v regress show port)
253
254 # record cluster information in service file
255 cat >> $PGSYSCONFDIR/pg_service.conf <<EOF
256 [$v]
257 host=localhost
258 port=$port
259 dbname=postgres
260 user=$PGUSER
261 password=$PGPASSWORD
262
263 EOF
264 done
265
266 export PGSYSCONFDIR
267 export PGHOST="localhost"
268 export PGDATABASE="postgres"
269 unset PGHOSTADDR PGPORT PGSERVICE # unset variables that might interfere
270 case $PG_VERSIONS in
271 *\ *) ;; # multiple versions: do not set PGPORT because that breaks --cluster
272 *)
273 export PGPORT="$port"
274 export PG_CONFIG="$PGBINROOT$PG_VERSIONS/bin/pg_config"
275 export PGVERSION="$PG_VERSIONS"
276 ;;
277 esac
278
279 # run program
280 "$@" || EXIT="$?"
281 if [ ${EXIT:-0} -gt 0 ]; then
282 for log in ${LOGDIR:-/var/log/postgresql}/*.log; do
283 echo "*** $log (last 100 lines) ***"
284 tail -100 $log
285 done
286
287 if command -v gdb > /dev/null; then
288 for v in $PG_VERSIONS; do
289 PGDATA=$(pg_conftool -s $v regress show data_directory)
290 for core in $PGDATA/core*; do
291 [ -f "$core" ] || continue
292 echo "*** $core ***"
293 gdb -batch -ex "bt full" /usr/lib/postgresql/$v/bin/postgres "$core"
294 done
295 done
296 fi
297
298 if [ "${run_shell:-}" ]; then
299 echo "pg_virtualenv: command exited with status $EXIT, dropping you into a shell"
300 ${SHELL:-/bin/sh}
301 fi
302 fi
303
304 exit ${EXIT:-0}