-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild-postgres
More file actions
executable file
·267 lines (229 loc) · 6.82 KB
/
build-postgres
File metadata and controls
executable file
·267 lines (229 loc) · 6.82 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env bash
#=======================================================================
# build-postgres
# File ID: 86126f82-2e60-11e5-bf33-fefdb24f8e10
#
# Compile and install the Postgres version stored in $srcdir .
#
# Author: Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later.
#=======================================================================
progname=build-postgres
VERSION=0.3.0
ARGS="$(getopt -o "\
h\
q\
u\
v\
" -l "\
help,\
quiet,\
update,\
verbose,\
version,\
" -n "$progname" -- "$@")"
test "$?" = "0" || exit 1
eval set -- "$ARGS"
opt_help=0
opt_quiet=0
opt_update=0
opt_verbose=0
while :; do
case "$1" in
-h|--help) opt_help=1; shift ;;
-q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
-u|--update) opt_update=1; shift ;;
-v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
--version) echo $progname $VERSION; exit 0 ;;
--) shift; break ;;
*) echo $progname: Internal error >&2; exit 1 ;;
esac
done
opt_verbose=$(($opt_verbose - $opt_quiet))
if test "$opt_help" = "1"; then
test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
cat <<END
Usage: $progname [options]
Options:
-h, --help
Show this help.
-q, --quiet
Be more quiet. Can be repeated to increase silence.
-u, --update
Fetch new commits and update the source code.
-v, --verbose
Increase level of verbosity. Can be repeated.
--version
Print version information.
END
exit 0
fi
msg() {
unset no_lf
if test "$1" = "-n"; then
# If -n is first argument, don't terminate with \n
local no_lf="-n"
shift
fi
if test "$1" = "-s"; then
# If -s is specified, skip initial \n
shift
else
echo >&2
fi
echo $no_lf "$progname: $*" >&2
return
}
srcdir="$HOME/src/other/postgres"
cd "$srcdir" || exit 1
if test "$opt_update" = "1"; then
git fetch origin
git merge --ff-only
git allbr origin
shift
fi
user=sunny
pgdesc="$(git desc --long --tags)"
pgdir="postgres-$pgdesc"
prefix="/usr/local"
prgdir="$prefix/prg"
destdir="$prefix/varprg/$pgdir"
dbroot="/var/lib/postgresql"
dumpfile="$dbroot/$(date -u +"%Y%m%dT%H%M%SZ").all.pgdump.gz"
test -e "$destdir" && {
sudo rmdir "$destdir" || {
echo $progname: $destdir already exists >&2
exit 1
}
}
test -e "$dbroot/$pgdesc/main" && {
echo $progname: $dbroot/$pgdesc/main already exists >&2
exit 1
}
git wait-until-clean
msg Remove ignored files from $(pwd)/
git clean -fxd || {
msg \"git wait-until-clean\" or \"git clean\" failed
exit 1
}
old_version="$(
$prgdir/postgres/bin/postgres --version 2>/dev/null | fmt -1 | tail -1
)"
if [ -n "$old_version" ]; then
msg git status in $(pwd)
GIT_PAGER=cat git status
unset choice
until [ "$choice" = "y" ]; do
echo
unset choice
echo $(
git log --format=oneline $old_version..$pgdesc | wc -l
) new commits available in range $old_version..$pgdesc
echo Going to compile Postgres $pgdesc
echo
echo If that looks okay to you, press \'y\' to start the build, or:
echo \'d\' to diff
echo \'ds\' to diff --stat
echo \'dw\' to word-diff
echo \'l\' to list log
echo \'ld\' to list current databases
echo \'lp\' to list log with patch
echo \'lt\' to list log with commit tree
echo \'lw\' to list log with patch using word diff
echo \'n\' to abort
echo \'p\' to push new commits
echo \'t\' to show commit tree
read choice
[ "$choice" = "d" ] && git diff $old_version $pgdesc
[ "$choice" = "ds" ] && git diff --stat $old_version $pgdesc
[ "$choice" = "dw" ] && git diff --word-diff $old_version $pgdesc
[ "$choice" = "l" ] && git log --stat $old_version..$pgdesc
[ "$choice" = "ld" ] && psql -X -c '\l+'
[ "$choice" = "lp" ] && git log --patch $old_version..$pgdesc
[ "$choice" = "lt" ] && git log --graph --stat $old_version..$pgdesc
[ "$choice" = "lw" ] && git log --patch --word-diff $old_version..$pgdesc
[ "$choice" = "n" ] && exit 0
[ "$choice" = "p" ] && git pa
[ "$choice" = "t" ] && {
git log --abbrev-commit --date-order --decorate=short --graph \
--pretty=format:'%Cred%h %Cblue%p%Creset -%Creset %s %Cgreen(%cd %Cblue%an%Cgreen)%Creset%C(yellow)%d%Creset' \
$old_version..$pgdesc
}
echo
done
else
unset choice
until [ "$choice" = "y" ]; do
echo -n Press \'y\' to start the build, or \'n\' to abort...
read choice
[ "$choice" = "n" ] && exit 0
done
fi
msg mkdir $destdir &&
sudo mkdir -p "$destdir" &&
msg chown $user.$user $destdir &&
sudo chown $user.$user "$destdir" &&
msg Run ./configure &&
./configure --prefix="$destdir" --with-extra-version="+$pgdesc" \
--enable-debug --with-libxml --with-openssl --with-perl --with-uuid=ossp &&
msg make world &&
make world &&
msg make check &&
make check &&
msg make install-world &&
make install-world &&
until sudo -u postgres echo postgres pwd OK | grep -q "postgres pwd OK"; do
:
done &&
msg Dump all databases to $dumpfile &&
sudo -u postgres bash -c "pg_dumpall | gzip >\"$dumpfile\"" &&
msg pg_ctl stop &&
sudo -u postgres pg_ctl -D "$dbroot/current/main" stop &&
msg Update the $prgdir/postgres symlink &&
cd "$prgdir" &&
sudo ln -fnsv "../varprg/$pgdir" postgres &&
msg Create \'current\' symlink to $pgdesc &&
sudo -u postgres ln -fnsv "$pgdesc" "$dbroot/current" &&
msg initdb &&
sudo -u postgres initdb --lc-collate=C -E UTF-8 "$dbroot/$pgdesc/main" &&
msg pg_ctl start &&
sudo -u postgres pg_ctl -D "$dbroot/$pgdesc/main" \
-l "$dbroot/$pgdesc/logfile" start &&
sudo -u postgres bash -c "until pg_isready -q; do sleep 1; done" &&
msg build-postgis &&
build-postgis &&
msg Restore databases from $dumpfile &&
sudo -u postgres bash -c "zcat \"$dumpfile\" | psql postgres" &&
msg Create manpage symlinks &&
for f in 1 3 7; do
cd "$prefix/share/man/man$f" &&
sudo ln -nfsv ../../../prg/postgres/share/man/man$f/* . || true
done &&
echo &&
echo $progname: Old version: $old_version >&2 &&
echo $progname: New version: $(
$prgdir/postgres/bin/postgres --version | fmt -1 | tail -1
) >&2 &&
if test -d "$prefix/.git/."; then
msg Commit the symlink &&
commitmsg=$(
echo $progname installed $pgdir on $(hostname)
echo
echo -n "Commit ID: "
suuid -t commit,$progname
) &&
cd "$prgdir" &&
sudo git add postgres &&
echo Commit message: &&
echo $commitmsg &&
sudo git commit -m "$commitmsg" &&
msg \"git pa\" in $(pwd) &&
(git pa || true)
fi &&
echo &&
echo $progname finished successfully >&2 &&
exit 0 || {
msg Something went wrong, aborted
sudo rmdir "$destdir"
exit 1
}