Skip to content

Commit ae89bf4

Browse files
committed
feat(postgres): adding upgrade scripts to the postgres
1 parent d3a7f62 commit ae89bf4

File tree

6 files changed

+243
-32
lines changed

6 files changed

+243
-32
lines changed

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
FROM postgres:9.4-alpine
1+
FROM postgres:11-alpine
22

33
ENV WALE_LOG_DESTINATION stderr
44
ENV WALE_ENVDIR /etc/wal-e.d/env
5+
ENV PGDATA_RECOVERY /var/lib/postgresql/recovery
56

6-
RUN mkdir -p $WALE_ENVDIR \
7+
RUN mkdir -p $WALE_ENVDIR $PGDATA_RECOVERY \
78
&& echo 'http://dl-cdn.alpinelinux.org/alpine/v3.5/main' >> /etc/apk/repositories \
89
&& apk add --update --virtual .build-deps \
910
git \
@@ -31,6 +32,8 @@ RUN mkdir -p $WALE_ENVDIR \
3132

3233
COPY rootfs /
3334

35+
VOLUME /var/lib/postgresql/recovery
36+
3437
ARG PATCH_CMD="python3 /patcher-script.py"
3538
RUN $PATCH_CMD file /bin/create_bucket /patcher-script.d/patch_boto_s3.py
3639
RUN $PATCH_CMD module wal_e.cmd /patcher-script.d/patch_boto_s3.py

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ IMAGE_PREFIX ?= deis
88

99
include versioning.mk
1010

11-
SHELL_SCRIPTS = $(wildcard _scripts/*.sh contrib/ci/*.sh rootfs/bin/*backup) rootfs/bin/is_running
11+
SHELL_SCRIPTS = $(wildcard _scripts/*.sh contrib/ci/*.sh rootfs/bin/*backup rootfs/bin/do_upgrade) rootfs/bin/is_running
1212

1313
# The following variables describe the containerized development environment
1414
# and other build options
@@ -30,7 +30,10 @@ test: test-style test-functional
3030
test-style:
3131
${DEV_ENV_CMD} shellcheck $(SHELL_SCRIPTS)
3232

33-
test-functional: test-functional-swift test-functional-minio
33+
test-functional: test-functional-upgrade test-functional-swift test-functional-minio
34+
35+
test-functional-upgrade:
36+
contrib/ci/test-upgrade.sh ${IMAGE}
3437

3538
test-functional-minio:
3639
contrib/ci/test-minio.sh ${IMAGE}

contrib/ci/test-upgrade.sh

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env bash
2+
3+
#set -eof pipefail
4+
5+
cleanup() {
6+
kill-containers "${SWIFT_DATA}" "${SWIFT_JOB}" "${PG_JOB}"
7+
}
8+
trap cleanup EXIT
9+
10+
waitting-install-apk() {
11+
sleep 30s
12+
apk_running="apk"
13+
puts-step "Installing apk, coffee"
14+
while true
15+
do
16+
apk_running=$(docker exec "${PG_JOB}" "ps -ef|grep apk|grep -v grep")
17+
if [ -z "$apk_running" ] ; then
18+
break
19+
fi
20+
puts-step "..."
21+
sleep 10s
22+
done
23+
echo "Install apk process is complete"
24+
}
25+
26+
TEST_ROOT=$(dirname "${BASH_SOURCE[0]}")/
27+
# shellcheck source=/dev/null
28+
source "${TEST_ROOT}/test.sh"
29+
30+
# make sure we are in this dir
31+
CURRENT_DIR=$(cd "$(dirname "$0")"|| exit; pwd)
32+
33+
create-postgres-creds
34+
35+
puts-step "fetching openstack credentials"
36+
37+
# turn creds into something that we can use.
38+
mkdir -p "${CURRENT_DIR}"/tmp/swift
39+
40+
# guess which value to use for tenant:
41+
TENANT=""
42+
43+
echo "test:tester" > "${CURRENT_DIR}"/tmp/swift/username
44+
echo "testing" > "${CURRENT_DIR}"/tmp/swift/password
45+
echo "${TENANT}" > "${CURRENT_DIR}"/tmp/swift/tenant
46+
echo "http://swift:8080/auth/v1.0" > "${CURRENT_DIR}"/tmp/swift/authurl
47+
echo "1" > "${CURRENT_DIR}"/tmp/swift/authversion
48+
echo "deis-swift-test" > "${CURRENT_DIR}"/tmp/swift/database-container
49+
50+
# boot swift
51+
SWIFT_DATA=$(docker run -d -v /srv --name SWIFT_DATA busybox)
52+
53+
SWIFT_JOB=$(docker run -d --name onlyone --hostname onlyone --volumes-from SWIFT_DATA -t deis/swift-onlyone:git-8516d23)
54+
55+
test-upgrade-from(){
56+
PGDATA_DIR=${CURRENT_DIR}/tmp/postgres/tmp_$(date +%s)
57+
mkdir -p "$PGDATA_DIR"
58+
59+
docker run --rm \
60+
-v "${PGDATA_DIR}:/var/lib/postgres/pg_data" \
61+
-e PGDATA=/var/lib/postgres/pg_data \
62+
"$1" \
63+
bash -c "chown -R postgres /var/lib/postgres/pg_data && su-exec postgres initdb"
64+
65+
PG_CMD="docker run -d --link ${SWIFT_JOB}:swift -e BACKUP_FREQUENCY=3s \
66+
-e DATABASE_STORAGE=swift \
67+
-e PGCTLTIMEOUT=1200 \
68+
-e PGDATA=/var/lib/postgres/pg_data \
69+
-v ${PGDATA_DIR}:/var/lib/postgres/pg_data \
70+
-v ${CURRENT_DIR}/tmp/creds:/var/run/secrets/deis/database/creds \
71+
-v ${CURRENT_DIR}/tmp/swift:/var/run/secrets/deis/objectstore/creds \
72+
$IMAGE"
73+
PG_JOB=$($PG_CMD)
74+
sleep 90s
75+
puts-step "sleeping for 90s while postgres is restore..."
76+
77+
check-postgres "${PG_JOB}"
78+
puts-step "postgres upgrade from $1"
79+
}
80+
81+
test-upgrade-from-wal() {
82+
PG_CMD="docker run -d --link ${SWIFT_JOB}:swift -e BACKUP_FREQUENCY=3s \
83+
-e DATABASE_STORAGE=swift \
84+
-e PGCTLTIMEOUT=1200 \
85+
-v ${CURRENT_DIR}/tmp/creds:/var/run/secrets/deis/database/creds \
86+
-v ${CURRENT_DIR}/tmp/swift:/var/run/secrets/deis/objectstore/creds \
87+
$1"
88+
89+
start-postgres "$PG_CMD"
90+
# display logs for debugging purposes
91+
puts-step "displaying swift logs"
92+
docker logs "${SWIFT_JOB}"
93+
check-postgres "${PG_JOB}"
94+
puts-step "shutting off postgres, then rebooting to test data recovery"
95+
kill-containers "${PG_JOB}"
96+
97+
PG_CMD="docker run -d --link ${SWIFT_JOB}:swift -e BACKUP_FREQUENCY=3s \
98+
-e DATABASE_STORAGE=swift \
99+
-e PGCTLTIMEOUT=1200 \
100+
-v ${CURRENT_DIR}/tmp/creds:/var/run/secrets/deis/database/creds \
101+
-v ${CURRENT_DIR}/tmp/swift:/var/run/secrets/deis/objectstore/creds \
102+
$IMAGE"
103+
104+
start-postgres "${PG_CMD}"
105+
106+
check-postgres "${PG_JOB}"
107+
}
108+
109+
IMAGE="$1"
110+
111+
test-upgrade-from postgres:9.4-alpine
112+
kill-containers "${PG_JOB}"
113+
114+
test-upgrade-from postgres:9.5-alpine
115+
kill-containers "${PG_JOB}"
116+
117+
test-upgrade-from postgres:9.6-alpine
118+
kill-containers "${PG_JOB}"
119+
120+
test-upgrade-from postgres:10-alpine
121+
kill-containers "${PG_JOB}"
122+
123+
test-upgrade-from postgres:11-alpine
124+
kill-containers "${PG_JOB}"
125+
126+
test-upgrade-from-wal hephy/postgres:v2.7.1
127+
128+
puts-step "tests PASSED!"
129+
exit 0

rootfs/bin/do_upgrade

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
3+
POSTGRES_OLD_VERSION=$(cat "$1"/PG_VERSION)
4+
5+
install-old-postgres() {
6+
if [ "$POSTGRES_OLD_VERSION" == "9.4" ];then
7+
echo 'http://dl-cdn.alpinelinux.org/alpine/v3.3/main' >> /etc/apk/repositories
8+
apk add --update --virtual .upgrade-deps postgresql=9.4.15-r0
9+
elif [ "$POSTGRES_OLD_VERSION" == "9.5" ];then
10+
echo 'http://dl-cdn.alpinelinux.org/alpine/v3.4/main' >> /etc/apk/repositories
11+
apk add --update --virtual .upgrade-deps postgresql=9.5.13-r0
12+
elif [ "$POSTGRES_OLD_VERSION" == "9.6" ];then
13+
echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/main' >> /etc/apk/repositories
14+
apk add --update --virtual .upgrade-deps postgresql=9.6.10-r0
15+
elif [ "$POSTGRES_OLD_VERSION" == "10" ];then
16+
echo 'http://dl-cdn.alpinelinux.org/alpine/v3.8/main' >> /etc/apk/repositories
17+
apk add --update --virtual .upgrade-deps postgresql=10.5-r0
18+
else
19+
echo "Upgrading from ${POSTGRES_OLD_VERSION} is not supported"
20+
exit 1
21+
fi
22+
}
23+
24+
remove-old-postgres() {
25+
apk del .upgrade-deps
26+
}
27+
28+
if [ "$POSTGRES_OLD_VERSION" != "$PG_MAJOR" ] ; then
29+
echo
30+
echo 'PostgreSQL executing the pg_upgrade script.'
31+
echo
32+
33+
cd /tmp||exit
34+
install-old-postgres
35+
36+
su-exec postgres /usr/bin/pg_ctl \
37+
-D "$1" \
38+
-o "-c listen_addresses=''" \
39+
-w start
40+
41+
su-exec postgres /usr/bin/pg_ctl \
42+
-D "$1" \
43+
-w stop
44+
45+
su-exec postgres pg_upgrade \
46+
--old-datadir="$1" \
47+
--new-datadir="$2" \
48+
--old-bindir=/usr/bin \
49+
--new-bindir=/usr/local/bin
50+
51+
remove-old-postgres
52+
rm -rf /var/cache/apk/*
53+
54+
echo
55+
echo 'PostgreSQL upgrade process complete; ready for start up.'
56+
echo
57+
fi

rootfs/docker-entrypoint-initdb.d/003_restore_from_backup.sh

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ su-exec postgres pg_ctl -D "$PGDATA" -w restart
2020
if [[ $(envdir "$WALE_ENVDIR" wal-e --terse backup-list | wc -l) -gt "1" ]]; then
2121
echo "Found backups. Restoring from backup..."
2222
su-exec postgres pg_ctl -D "$PGDATA" -w stop
23-
rm -rf "$PGDATA/*"
24-
envdir "$WALE_ENVDIR" wal-e backup-fetch "$PGDATA" LATEST
25-
cat << EOF > "$PGDATA/postgresql.conf"
23+
envdir "$WALE_ENVDIR" wal-e backup-fetch "$PGDATA_RECOVERY" LATEST
24+
cat << EOF > "$PGDATA_RECOVERY/postgresql.conf"
2625
# These settings are initialized by initdb, but they can be changed.
2726
log_timezone = 'UTC'
2827
lc_messages = 'C' # locale for system error message
@@ -35,8 +34,9 @@ archive_mode = on
3534
archive_command = 'envdir "${WALE_ENVDIR}" wal-e wal-push %p'
3635
archive_timeout = 60
3736
listen_addresses = '*'
37+
unix_socket_directories = '/var/run/postgresql'
3838
EOF
39-
cat << EOF > "$PGDATA/pg_hba.conf"
39+
cat << EOF > "$PGDATA_RECOVERY/pg_hba.conf"
4040
# "local" is for Unix domain socket connections only
4141
local all all trust
4242
# IPv4 local connections:
@@ -46,13 +46,22 @@ host all all ::1/128 trust
4646
# IPv4 global connections
4747
host all all 0.0.0.0/0 md5
4848
EOF
49-
touch "$PGDATA/pg_ident.conf"
50-
echo "restore_command = 'envdir /etc/wal-e.d/env wal-e wal-fetch \"%f\" \"%p\"'" >> "$PGDATA/recovery.conf"
51-
chown -R postgres:postgres "$PGDATA"
52-
chmod 0700 "$PGDATA"
53-
su-exec postgres pg_ctl -D "$PGDATA" \
54-
-o "-c listen_addresses=''" \
55-
-w start
49+
touch "$PGDATA_RECOVERY/pg_ident.conf"
50+
echo "restore_command = 'envdir /etc/wal-e.d/env wal-e wal-fetch \"%f\" \"%p\"'" >> "$PGDATA_RECOVERY/recovery.conf"
51+
chown -R postgres:postgres "$PGDATA_RECOVERY"
52+
chmod 0700 "$PGDATA_RECOVERY"
53+
54+
POSTGRES_OLD_VERSION=$(cat "$PGDATA_RECOVERY"/PG_VERSION)
55+
if [ "$POSTGRES_OLD_VERSION" = "$PG_MAJOR" ] ; then
56+
su-exec postgres rm -rf "$PGDATA"/*
57+
su-exec postgres cp -rf "$PGDATA_RECOVERY"/* "$PGDATA"
58+
else
59+
do_upgrade "$PGDATA_RECOVERY" "$PGDATA"
60+
fi
61+
su-exec postgres pg_ctl \
62+
-D "$PGDATA" \
63+
-o "-c listen_addresses=''" \
64+
-w start
5665
fi
5766

5867
# ensure $PGDATA has the right permissions

rootfs/docker-entrypoint.sh

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ POSTGRES_USER="$(cat /var/run/secrets/deis/database/creds/user)"
1414
POSTGRES_PASSWORD="$(cat /var/run/secrets/deis/database/creds/password)"
1515

1616
if [ "$1" = 'postgres' ]; then
17-
mkdir -p "$PGDATA"
18-
chmod 700 "$PGDATA"
19-
chown -R postgres "$PGDATA"
17+
mkdir -p "$PGDATA" "$PGDATA_RECOVERY"
18+
chmod 700 "$PGDATA" "$PGDATA_RECOVERY"
19+
chown -R postgres "$PGDATA" "$PGDATA_RECOVERY"
2020

2121
chmod g+s /run/postgresql
2222
chown -R postgres /run/postgresql
@@ -57,7 +57,23 @@ if [ "$1" = 'postgres' ]; then
5757
su-exec postgres pg_ctl -D "$PGDATA" \
5858
-o "-c listen_addresses=''" \
5959
-w start
60+
echo
61+
62+
echo
63+
for f in /docker-entrypoint-initdb.d/*; do
64+
case "$f" in
65+
*.sh) echo "$0: running $f"; . "$f" ;;
66+
*.sql)
67+
echo "$0: running $f";
68+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f"
69+
echo
70+
;;
71+
*) echo "$0: ignoring $f" ;;
72+
esac
73+
echo
74+
done
6075

76+
echo
6177
: ${POSTGRES_USER:=postgres}
6278
: ${POSTGRES_DB:=$POSTGRES_USER}
6379
export POSTGRES_USER POSTGRES_DB
@@ -80,26 +96,20 @@ if [ "$1" = 'postgres' ]; then
8096
EOSQL
8197
echo
8298

83-
echo
84-
for f in /docker-entrypoint-initdb.d/*; do
85-
case "$f" in
86-
*.sh) echo "$0: running $f"; . "$f" ;;
87-
*.sql)
88-
echo "$0: running $f";
89-
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f"
90-
echo
91-
;;
92-
*) echo "$0: ignoring $f" ;;
93-
esac
94-
echo
95-
done
96-
9799
su-exec postgres pg_ctl -D "$PGDATA" -m fast -w stop
98100
set_listen_addresses '*'
99101

100102
echo
101103
echo 'PostgreSQL init process complete; ready for start up.'
102104
echo
105+
else
106+
POSTGRES_OLD_VERSION=$(cat $PGDATA/PG_VERSION)
107+
if [ "$POSTGRES_OLD_VERSION" != "$PG_MAJOR" ] ; then
108+
su-exec postgres cp -rf "$PGDATA"/* "$PGDATA_RECOVERY"
109+
su-exec postgres rm -rf "$PGDATA"/*
110+
su-exec postgres initdb
111+
do_upgrade "$PGDATA_RECOVERY" "$PGDATA"
112+
fi
103113
fi
104114

105115
exec su-exec postgres "$@"

0 commit comments

Comments
 (0)