-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathbase.sh
More file actions
328 lines (286 loc) · 12.4 KB
/
base.sh
File metadata and controls
328 lines (286 loc) · 12.4 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/bash
## -------------------------------------------
## Install PostgreSQL, extensions and contribs
## -------------------------------------------
export DEBIAN_FRONTEND=noninteractive
MAKEFLAGS="-j $(grep -c ^processor /proc/cpuinfo)"
export MAKEFLAGS
set -ex
sed -i 's/^#\s*\(deb.*universe\)$/\1/g' /etc/apt/sources.list
apt-get update
BUILD_PACKAGES=(devscripts equivs build-essential fakeroot debhelper git gcc libc6-dev make cmake libevent-dev libbrotli-dev libssl-dev libkrb5-dev)
if [ "$DEMO" = "true" ]; then
export DEB_PG_SUPPORTED_VERSIONS="$PGVERSION"
WITH_PERL=false
rm -f ./*.deb
apt-get install -y "${BUILD_PACKAGES[@]}"
else
BUILD_PACKAGES+=(zlib1g-dev
libprotobuf-c-dev
libpam0g-dev
libcurl4-openssl-dev
libicu-dev
libc-ares-dev
pandoc
pkg-config)
apt-get install -y "${BUILD_PACKAGES[@]}" libcurl4
# install pam_oauth2.so
git clone -b "$PAM_OAUTH2" --recurse-submodules https://github.com/zalando-pg/pam-oauth2.git
make -C pam-oauth2 install
# prepare 3rd sources
git clone -b "$PLPROFILER" https://github.com/bigsql/plprofiler.git
curl -sL "https://github.com/zalando-pg/pg_mon/archive/$PG_MON_COMMIT.tar.gz" | tar xz
for p in python3-keyring python3-docutils ieee-data; do
version=$(apt-cache show $p | sed -n 's/^Version: //p' | sort -rV | head -n 1)
printf "Section: misc\nPriority: optional\nStandards-Version: 3.9.8\nPackage: %s\nVersion: %s\nDescription: %s" "$p" "$version" "$p" > "$p"
equivs-build "$p"
done
fi
if [ "$WITH_PERL" != "true" ]; then
version=$(apt-cache show perl | sed -n 's/^Version: //p' | sort -rV | head -n 1)
printf "Priority: standard\nStandards-Version: 3.9.8\nPackage: perl\nMulti-Arch: allowed\nReplaces: perl-base, perl-modules\nVersion: %s\nDescription: perl" "$version" > perl
equivs-build perl
fi
curl -sL "https://github.com/zalando-pg/bg_mon/archive/$BG_MON_COMMIT.tar.gz" | tar xz
curl -sL "https://github.com/zalando-pg/pg_auth_mon/archive/$PG_AUTH_MON_COMMIT.tar.gz" | tar xz
curl -sL "https://github.com/zubkov-andrei/pg_profile/archive/$PG_PROFILE.tar.gz" | tar xz
apt-get install -y \
postgresql-common \
libevent-2.1 \
libevent-pthreads-2.1 \
brotli \
libbrotli1 \
python3.10 \
python3-psycopg2
# forbid creation of a main cluster when package is installed
sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf
for version in $DEB_PG_SUPPORTED_VERSIONS; do
sed -i "s/ main.*$/ main $version/g" /etc/apt/sources.list.d/pgdg.list
apt-get update
if [ "$DEMO" != "true" ]; then
EXTRAS=("postgresql-pltcl-${version}"
"postgresql-${version}-dirtyread"
"postgresql-${version}-extra-window-functions"
"postgresql-${version}-first-last-agg"
"postgresql-${version}-hll"
"postgresql-${version}-hypopg"
"postgresql-${version}-partman"
"postgresql-${version}-plproxy"
"postgresql-${version}-pgaudit"
"postgresql-${version}-pldebugger"
"postgresql-${version}-pglogical"
"postgresql-${version}-pglogical-ticker"
"postgresql-${version}-plpgsql-check"
"postgresql-${version}-pg-checksums"
"postgresql-${version}-pgl-ddl-deploy"
"postgresql-${version}-pgq-node"
"postgresql-${version}-postgis-${POSTGIS_VERSION%.*}"
"postgresql-${version}-postgis-${POSTGIS_VERSION%.*}-scripts"
"postgresql-${version}-repack"
"postgresql-${version}-wal2json"
"postgresql-${version}-decoderbufs"
"postgresql-${version}-pllua"
"postgresql-${version}-pgvector"
"postgresql-${version}-roaringbitmap")
if [ "$version" -ge 14 ]; then
EXTRAS+=("postgresql-${version}-pgfaceting")
fi
if [ "$WITH_PERL" = "true" ]; then
EXTRAS+=("postgresql-plperl-${version}")
fi
fi
if [ "${TIMESCALEDB_APACHE_ONLY}" = "true" ]; then
EXTRAS+=("timescaledb-2-oss-postgresql-${version}")
else
EXTRAS+=("timescaledb-2-postgresql-${version}")
fi
# Install PostgreSQL binaries, contrib, plproxy and multiple pl's
apt-get install --allow-downgrades -y \
"postgresql-${version}-cron" \
"postgresql-contrib-${version}" \
"postgresql-${version}-pgextwlist" \
"postgresql-plpython3-${version}" \
"postgresql-server-dev-${version}" \
"postgresql-${version}-pgq3" \
"postgresql-${version}-pg-stat-kcache" \
"postgresql-${version}-pg-permissions" \
"postgresql-${version}-set-user" \
"${EXTRAS[@]}"
# Clean up timescaledb versions - keep at least 5 minor versions, but ensure compatibility with the lowest/oldest PG version (where possible)
exclude_patterns=()
versions=$(find "/usr/lib/postgresql/$version/lib/" -name 'timescaledb-2.*.so' | sed -rn 's/.*timescaledb-([1-9]+\.[0-9]+\.[0-9]+)\.so$/\1/p' | sort -rV)
# Calculate the number of versions dynamically based on the lowest PG version's latest minor
num_versions=5
if [ -n "$first_latest_minor" ]; then
minor_versions=$(echo "$versions" | awk -F. '{print $1"."$2}' | uniq)
position=0
found=0
while IFS= read -r minor; do
position=$((position + 1))
if [ "$minor" = "$first_latest_minor" ]; then
found=1
break
fi
done <<< "$minor_versions"
# if found, keep max(5, position) versions (so all versions have at least 1 version in common with lowest PG version)
if [ $found -eq 1 ] && [ $position -gt $num_versions ]; then
num_versions=$position
fi
fi
latest_minor_versions=$(echo "$versions" | awk -F. '{print $1"."$2}' | uniq | head -n "$num_versions")
for minor in $latest_minor_versions; do
for full_version in $(echo "$versions" | grep "^$minor"); do
exclude_patterns+=(! -name timescaledb-"${full_version}".so)
exclude_patterns+=(! -name timescaledb-tsl-"${full_version}".so)
done
done
find "/usr/lib/postgresql/$version/lib/" \( -name 'timescaledb-2.*.so' -o -name 'timescaledb-tsl-2.*.so' \) "${exclude_patterns[@]}" -delete
# Save the latest minor version from the first PG version
if [ -z "$first_latest_minor" ]; then
first_latest_minor=$(echo "$latest_minor_versions" | head -n 1)
fi
# Install 3rd party stuff
if [ "${TIMESCALEDB_APACHE_ONLY}" != "true" ] && [ "${TIMESCALEDB_TOOLKIT}" = "true" ]; then
apt-get update
if [ "$(apt-cache search --names-only "^timescaledb-toolkit-postgresql-${version}$" | wc -l)" -eq 1 ]; then
apt-get install "timescaledb-toolkit-postgresql-$version"
else
echo "Skipping timescaledb-toolkit-postgresql-$version as it's not found in the repository"
fi
fi
EXTRA_EXTENSIONS=()
if [ "$DEMO" != "true" ]; then
EXTRA_EXTENSIONS+=("plprofiler" "pg_mon-${PG_MON_COMMIT}")
fi
for n in bg_mon-${BG_MON_COMMIT} \
pg_auth_mon-${PG_AUTH_MON_COMMIT} \
pg_profile-${PG_PROFILE} \
"${EXTRA_EXTENSIONS[@]}"; do
PATH="/usr/lib/postgresql/$version/bin:$PATH" make -C "$n" USE_PGXS=1 clean
PATH="/usr/lib/postgresql/$version/bin:$PATH" make -C "$n" USE_PGXS=1 install-strip
done
done
apt-get install -y skytools3-ticker pgbouncer
sed -i "s/ main.*$/ main/g" /etc/apt/sources.list.d/pgdg.list
apt-get update
apt-get install -y postgresql postgresql-server-dev-all postgresql-all libpq-dev
for version in $DEB_PG_SUPPORTED_VERSIONS; do
apt-get install -y "postgresql-server-dev-${version}"
done
if [ "$DEMO" != "true" ]; then
for version in $DEB_PG_SUPPORTED_VERSIONS; do
# create postgis symlinks to make it possible to perform update
ln -s "postgis-${POSTGIS_VERSION%.*}.so" "/usr/lib/postgresql/${version}/lib/postgis-2.5.so"
done
fi
# make it possible for cron to work without root
gcc -s -shared -fPIC -o /usr/local/lib/cron_unprivileged.so cron_unprivileged.c
apt-get purge -y "${BUILD_PACKAGES[@]}"
apt-get autoremove -y
if [ "$WITH_PERL" != "true" ] || [ "$DEMO" != "true" ]; then
dpkg -i ./*.deb || apt-get -y -f install
fi
# Remove unnecessary packages
apt-get purge -y \
libdpkg-perl \
libperl5.* \
perl-modules-5.* \
postgresql \
postgresql-all \
postgresql-server-dev-* \
libpq-dev=* \
libmagic1 \
bsdmainutils
apt-get autoremove -y
apt-get clean
dpkg -l | grep '^rc' | awk '{print $2}' | xargs apt-get purge -y
# Try to minimize size by creating symlinks instead of duplicate files
if [ "$DEMO" != "true" ]; then
cd "/usr/lib/postgresql/$PGVERSION/bin"
for u in clusterdb \
pg_archivecleanup \
pg_basebackup \
pg_isready \
pg_recvlogical \
pg_test_fsync \
pg_test_timing \
pgbench \
reindexdb \
vacuumlo *.py; do
for v in /usr/lib/postgresql/*; do
if [ "$v" != "/usr/lib/postgresql/$PGVERSION" ] && [ -f "$v/bin/$u" ]; then
rm "$v/bin/$u"
ln -s "../../$PGVERSION/bin/$u" "$v/bin/$u"
fi
done
done
set +x
for v1 in $(find /usr/share/postgresql -type d -mindepth 1 -maxdepth 1 | sort -Vr); do
# relink files with the same content
cd "$v1/extension"
while IFS= read -r -d '' orig
do
for f in "${orig%.sql}"--*.sql; do
if [ ! -L "$f" ] && diff "$orig" "$f" > /dev/null; then
echo "creating symlink $f -> $orig"
rm "$f" && ln -s "$orig" "$f"
fi
done
done < <(find . -type f -maxdepth 1 -name '*.sql' -not -name '*--*')
for e in pgq pgq_node plproxy address_standardizer address_standardizer_data_us; do
orig=$(basename "$(find . -maxdepth 1 -type f -name "$e--*--*.sql" | head -n1)")
if [ "x$orig" != "x" ]; then
for f in "$e"--*--*.sql; do
if [ "$f" != "$orig" ] && [ ! -L "$f" ] && diff "$f" "$orig" > /dev/null; then
echo "creating symlink $f -> $orig"
rm "$f" && ln -s "$orig" "$f"
fi
done
fi
done
# relink files with the same name and content across different major versions
started=0
for v2 in $(find /usr/share/postgresql -type d -mindepth 1 -maxdepth 1 | sort -Vr); do
if [ "$v1" = "$v2" ]; then
started=1
elif [ $started = 1 ]; then
for d1 in extension contrib contrib/postgis-$POSTGIS_VERSION; do
cd "$v1/$d1"
d2="$d1"
d1="../../${v1##*/}/$d1"
if [ "${d2%-*}" = "contrib/postgis" ]; then
d1="../$d1"
fi
d2="$v2/$d2"
for f in *.html *.sql *.control *.pl; do
if [ -f "$d2/$f" ] && [ ! -L "$d2/$f" ] && diff "$d2/$f" "$f" > /dev/null; then
echo "creating symlink $d2/$f -> $d1/$f"
rm "$d2/$f" && ln -s "$d1/$f" "$d2/$f"
fi
done
done
fi
done
done
set -x
fi
# Clean up
rm -rf /var/lib/apt/lists/* \
/var/cache/debconf/* \
/builddeps \
/usr/share/doc \
/usr/share/man \
/usr/share/info \
/usr/share/locale/?? \
/usr/share/locale/??_?? \
/usr/share/postgresql/*/man \
/etc/pgbouncer/* \
/usr/lib/postgresql/*/bin/createdb \
/usr/lib/postgresql/*/bin/createlang \
/usr/lib/postgresql/*/bin/createuser \
/usr/lib/postgresql/*/bin/dropdb \
/usr/lib/postgresql/*/bin/droplang \
/usr/lib/postgresql/*/bin/dropuser \
/usr/lib/postgresql/*/bin/pg_standby \
/usr/lib/postgresql/*/bin/pltcl_*
find /var/log -type f -exec truncate --size 0 {} \;