1 #! /bin/bash
2 #
3 # PostRR test setup helper script
4 #
5 # Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 if test -z "$PG_CONFIG"; then
30 PG_CONFIG=`which pg_config`
31 fi
32 if test -z "$PG_CONFIG"; then
33 echo "pg_config not found!" >&2
34 exit 1
35 fi
37 BIN_DIR=`$PG_CONFIG --bindir`
38 if test -z "$TARGET"; then
39 TARGET=`pwd`/target
40 fi
42 PWD_esc="$( echo "$PWD" | sed -e 's/\//\\\//g' )"
43 TARGET_esc="$( echo "$TARGET" | sed -e 's/\//\\\//g' )"
45 set -e
47 case "$1" in
48 setup)
49 if test $# -ne 1; then
50 echo "Too many arguments!" >&2
51 echo "Usage: $0 setup" >&2
52 exit 1
53 fi
54 mkdir -p $TARGET/var/lib/postgresql/main
55 mkdir -p $TARGET/var/run/postgresql
56 mkdir -p $TARGET/var/log/postgresql/main
57 $BIN_DIR/initdb -D $TARGET/var/lib/postgresql/main
58 sed -r -i -e 's/^#port = 5432/port = 2345/' \
59 $TARGET/var/lib/postgresql/main/postgresql.conf
60 sed -r -i -e "s/^#dynamic_library_path = '\\\$libdir'/dynamic_library_path = '\$libdir:$PWD_esc\/src'/" $TARGET/var/lib/postgresql/main/postgresql.conf
61 sed -r -i -e "s/^#unix_socket_directory = ''/unix_socket_directory = '$TARGET_esc\/var\/run\/postgresql'/" $TARGET/var/lib/postgresql/main/postgresql.conf
62 $0 start -B
63 $BIN_DIR/createdb -e -h $TARGET/var/run/postgresql/ -p 2345 "$( id -un )"
64 $0 stop
65 ;;
66 client)
67 libedit=$( ldd $BIN_DIR/psql 2> /dev/null \
68 | grep -Eo '=> [^ ]+\/libedit\.so\..? ' | cut -d' ' -f2 )
69 if test -n "$libedit"; then
70 libdir=${libedit/libedit.*/}
71 libreadline=$( ls $libdir/libreadline.so* 2> /dev/null | head -n1 )
72 if test -n "$libreadline"; then
73 export LD_PRELOAD="$LD_PRELOAD:$libreadline"
74 fi
75 fi
76 shift
77 $BIN_DIR/psql -h $TARGET/var/run/postgresql/ -p 2345 "$@"
78 ;;
79 stop)
80 if test $# -ne 1; then
81 echo "Too many arguments!" >&2
82 echo "Usage: $0 stop" >&2
83 exit 1
84 fi
85 $BIN_DIR/pg_ctl -D $TARGET/var/lib/postgresql/main stop
86 ;;
87 start)
88 shift
89 if test "$1" = "-B"; then
90 shift
91 $BIN_DIR/pg_ctl -D $TARGET/var/lib/postgresql/main -l logfile -w start "$@"
92 else
93 $BIN_DIR/postgres -D $TARGET/var/lib/postgresql/main "$@"
94 fi
95 ;;
96 restart)
97 $0 stop && $0 start -B
98 ;;
99 dump)
100 shift
101 $BIN_DIR/pg_dump -h $TARGET/var/run/postgresql/ -p 2345 "$@"
102 ;;
103 restore)
104 shift
105 $BIN_DIR/pg_restore -h $TARGET/var/run/postgresql/ -p 2345 "$@"
106 ;;
107 *)
108 echo "Usage: $0 setup|client|stop|start" >&2
109 echo ""
110 echo " - setup"
111 echo " Set up a new PostgreSQL server listening on port 2345."
112 echo " - client [<psql args>]"
113 echo " Start a PostgreSQL interactive terminal connected to the"
114 echo " PostgreSQL server on port 2345."
115 echo " - start [-B [<postgres args>]]"
116 echo " Start the PostgreSQL server."
117 echo " - stop"
118 echo " Stop the PostgreSQL server."
119 echo " - restart"
120 echo " Restart a background PostgreSQL server process."
121 echo ""
122 echo "Environment variables:"
123 echo " - TARGET"
124 echo " Target directory of the PostgreSQL test setup."
125 if test "$1" = "help"; then
126 exit 0
127 fi
128 exit 1
129 ;;
130 esac
132 # vim: set tw=0 :