add automated postgres upgrade migrations
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
de8344f420
commit
d6662fd38e
@ -18,6 +18,7 @@ steps:
|
||||
STACK_NAME: pretix
|
||||
LETS_ENCRYPT_ENV: production
|
||||
CRON_ENTRYPOINT_VERSION: v1
|
||||
DB_ENTRYPOINT_VERSION: v1
|
||||
SECRET_DB_PASSWORD_VERSION: v1
|
||||
SECRET_DJANGO_SECRET_KEY_VERSION: v1
|
||||
SECRET_SMTP_PASSWORD_VERSION: v1
|
||||
|
1
abra.sh
1
abra.sh
@ -1,5 +1,6 @@
|
||||
export PRETIX_CONFIG_VERSION=v1
|
||||
export CRON_ENTRYPOINT_VERSION=v1
|
||||
export DB_ENTRYPOINT_VERSION=v1
|
||||
export PG_BACKUP_VERSION=v1
|
||||
|
||||
change_adminpass(){
|
||||
|
13
compose.yml
13
compose.yml
@ -49,8 +49,13 @@ services:
|
||||
- source: pg_backup
|
||||
target: /pg_backup.sh
|
||||
mode: 0555
|
||||
- source: db_entrypoint
|
||||
target: /docker-entrypoint.sh
|
||||
mode: 0555
|
||||
entrypoint:
|
||||
/docker-entrypoint.sh
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
|
||||
test: ["CMD-SHELL", "[ -f $${HEALTHCHECK_MARKER} ] || pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
@ -58,7 +63,7 @@ services:
|
||||
labels:
|
||||
backupbot.backup: "${ENABLE_BACKUPS:-true}"
|
||||
backupbot.backup.pre-hook: "/pg_backup.sh backup"
|
||||
backupbot.backup.volumes.database.path: "backup.sql"
|
||||
backupbot.backup.volumes.postgres.path: "backup.sql"
|
||||
backupbot.restore.post-hook: '/pg_backup.sh restore'
|
||||
|
||||
redis:
|
||||
@ -99,6 +104,10 @@ configs:
|
||||
cron_entrypoint:
|
||||
name: ${STACK_NAME}_cron_entrypoint_${CRON_ENTRYPOINT_VERSION}
|
||||
file: entrypoint.cron.sh
|
||||
db_entrypoint:
|
||||
name: ${STACK_NAME}_db_entrypoint_${DB_ENTRYPOINT_VERSION}
|
||||
file: entrypoint.postgres.sh.tmpl
|
||||
template_driver: golang
|
||||
pg_backup:
|
||||
name: ${STACK_NAME}_pg_backup_${PG_BACKUP_VERSION}
|
||||
file: pg_backup.sh
|
||||
|
77
entrypoint.postgres.sh.tmpl
Normal file
77
entrypoint.postgres.sh.tmpl
Normal file
@ -0,0 +1,77 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
HEALTHCHECK_MARKER=/tmp/skip_healthcheck
|
||||
touch $HEALTHCHECK_MARKER
|
||||
MIGRATION_MARKER=$PGDATA/migration_in_progress
|
||||
OLDDATA=$PGDATA/old_data
|
||||
NEWDATA=$PGDATA/new_data
|
||||
|
||||
install_old_postgres_debian() {
|
||||
## TODO: Replace with script from outline entrypoint
|
||||
apt-get update
|
||||
apt-get install -y git
|
||||
git clone https://github.com/theory/pgenv.git /tmp/pgenv
|
||||
cd /tmp/pgenv
|
||||
export PATH="/tmp/pgenv/bin:/tmp/pgenv/pgsql/bin:$PATH"
|
||||
|
||||
# Install missing packages
|
||||
apt-get install -y make curl patch gcc sudo libreadline-dev zlib1g-dev build-essential
|
||||
pgenv check
|
||||
LATEST_OLD_VERSION=$(pgenv available $DATA_VERSION | grep -oE "$DATA_VERSION\.[0-9]+" | tail -n 1)
|
||||
pgenv build $LATEST_OLD_VERSION
|
||||
}
|
||||
|
||||
install_old_postgres_alpine() {
|
||||
apk add git
|
||||
git clone https://github.com/theory/pgenv.git /tmp/pgenv
|
||||
cd /tmp/pgenv
|
||||
export PATH="/tmp/pgenv/bin:/tmp/pgenv/pgsql/bin:$PATH"
|
||||
|
||||
#Install Missing pagckages:
|
||||
apk add make curl make patch gcc alpine-sdk sudo readline-dev build-base zlib-dev linux-headers
|
||||
pgenv check
|
||||
LATEST_OLD_VERSION=$(pgenv available $DATA_VERSION | grep -oE "$DATA_VERSION\.[0-9]+" | tail -n 1)
|
||||
pgenv build $LATEST_OLD_VERSION
|
||||
}
|
||||
|
||||
if [ -e $MIGRATION_MARKER ]; then
|
||||
echo "FATAL: migration was started but did not complete in a previous run. manual recovery necessary"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -f $PGDATA/PG_VERSION ]; then
|
||||
DATA_VERSION=$(cat $PGDATA/PG_VERSION)
|
||||
|
||||
if [ -n "$DATA_VERSION" -a "$PG_MAJOR" != "$DATA_VERSION" ]; then
|
||||
echo "postgres data version $DATA_VERSION found, but need $PG_MAJOR. Starting migration"
|
||||
echo "Installing postgres $DATA_VERSION"
|
||||
if [ -f /etc/alpine-release ]; then
|
||||
install_old_postgres_alpine
|
||||
else
|
||||
install_old_postgres_debian
|
||||
fi
|
||||
echo "shuffling around"
|
||||
gosu postgres mkdir $OLDDATA $NEWDATA
|
||||
chmod 700 $OLDDATA $NEWDATA
|
||||
mv $PGDATA/* $OLDDATA/ || true
|
||||
touch $MIGRATION_MARKER
|
||||
echo "running initdb"
|
||||
# abuse entrypoint script for initdb by making server error out
|
||||
gosu postgres bash -c "export PGDATA=$NEWDATA ; /usr/local/bin/docker-entrypoint.sh --invalid-arg || true"
|
||||
echo "running pg_upgrade"
|
||||
cd /tmp
|
||||
gosu postgres pg_upgrade --link -b /tmp/pgenv/pgsql-$LATEST_OLD_VERSION/bin -d $OLDDATA -D $NEWDATA -U $POSTGRES_USER
|
||||
cp $OLDDATA/pg_hba.conf $NEWDATA/
|
||||
mv $NEWDATA/* $PGDATA
|
||||
rm -rf $OLDDATA
|
||||
rmdir $NEWDATA
|
||||
rm $MIGRATION_MARKER
|
||||
echo "migration complete"
|
||||
fi
|
||||
fi
|
||||
|
||||
rm $HEALTHCHECK_MARKER
|
||||
|
||||
/usr/local/bin/docker-entrypoint.sh postgres
|
Loading…
x
Reference in New Issue
Block a user