diff options
author | Freya Murphy <freya@freyacat.org> | 2024-03-29 22:29:56 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-03-29 22:29:56 -0400 |
commit | 944b6b0526032ad8c1b4a2612d6723bec75e0e4c (patch) | |
tree | d3da5584df33a7878c087622b4fc2ec2883cf880 /build/dbinit | |
download | xssbook2-944b6b0526032ad8c1b4a2612d6723bec75e0e4c.tar.gz xssbook2-944b6b0526032ad8c1b4a2612d6723bec75e0e4c.tar.bz2 xssbook2-944b6b0526032ad8c1b4a2612d6723bec75e0e4c.zip |
start database (user and post), and initial barebones home page
Diffstat (limited to 'build/dbinit')
-rw-r--r-- | build/dbinit/Dockerfile | 5 | ||||
-rwxr-xr-x | build/dbinit/dbinit | 151 |
2 files changed, 156 insertions, 0 deletions
diff --git a/build/dbinit/Dockerfile b/build/dbinit/Dockerfile new file mode 100644 index 0000000..81c66ea --- /dev/null +++ b/build/dbinit/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine:3.19 +RUN apk add --no-cache postgresql16-client tini +COPY ./dbinit /usr/local/bin/dbinit +ENTRYPOINT ["/sbin/tini", "--"] +CMD ["/usr/local/bin/dbinit"] diff --git a/build/dbinit/dbinit b/build/dbinit/dbinit new file mode 100755 index 0000000..c64f139 --- /dev/null +++ b/build/dbinit/dbinit @@ -0,0 +1,151 @@ +#!/bin/sh + +step() { + printf '\x1b[34;1m>> %s\x1b[0m\n' "$*" +} + +error() { + printf '\x1b[31;1merror: \x1b[0m%s\n' "$*" +} + +export PGPASSWORD=$POSTGRES_PASSWORD + +psql() { + /usr/bin/psql \ + -h db \ + -p 5432 \ + -d $POSTGRES_DB \ + -U $POSTGRES_USER \ + "$@" +} + +pg_isready() { + /usr/bin/pg_isready \ + -h db \ + -p 5432 \ + -d $POSTGRES_DB \ + -U $POSTGRES_USER \ + "$@" +} + +curr_revision() { + psql -qtAX -f /db/rev.sql; +} + +wait_until_ready() { + step 'Checking if the database is ready...'; + while true; do + pg_isready; + code=$?; + if [ $code -eq 0 ]; then + break; + fi + sleep 3; + done +} + +run_migrations() { + i="$1" + while true; do + name=$(printf "%04d" "$i"); + file="/db/migrations/$name.sql" + + if [ -f $file ]; then + psql -f $file 2> /errors + errors=$(cat /errors | grep 'ERROR' | wc -l) + if [ "$errors" -eq 0 ]; then + i=$((i+1)); + continue; + else + error "An error occoured during a migration (rev $name)" + cat /errors | grep -v 'current transaction is aborted'; + error "Aborting migrations, fix file(s) then restart process." + return 1; + fi + else + return 0; + fi + done +} + +init_api() { + psql -f /db/rest/rest.sql 2> /errors; + errors=$(cat /errors | grep 'ERROR' | wc -l) + if [ "$errors" -eq 0 ]; then + return 0; + else + error "An error occoured during api initialization" + cat /errors | grep -v 'current transaction is aborted'; + error "Aborting api initialization, fix file(s) then restart process." + return 1; + fi +} + +update_jwt() { + psql -c "UPDATE sys.database_info SET jwt_secret = '$JWT_SECRET' WHERE name = current_database();" + errors=$(cat /errors | grep 'ERROR' | wc -l) + if [ "$errors" -eq 0 ]; then + return 0; + else + return 1; + fi +} + +load_ext() { + psql -qtAX -f /db/ext.sql; +} + +init () { + # reomve ready status + # so php ignores requests + rm -fr /status/ready + + step 'Waiting for database'; + # make sure the database is running + # before we run any requests + wait_until_ready; + step 'Database ready'; + + step 'Loading extensions'; + # Make sure extensions are loaded + load_ext; + + step 'Peforming migrations'; + # get the current revision + REV=$(curr_revision); + step "Database at revision: $REV" + # run each migration that is + # higher than our current revision + run_migrations "$REV" + CODE=$?; + + if [ $CODE -ne 0 ]; then + return $CODE; + fi + + step 'Initalizing the api'; + # reinit the api schema for + # postgrest + init_api; + CODE=$?; + + if [ $CODE -ne 0 ]; then + return $CODE; + fi + + step 'Updating JWT secret'; + # make sure postgres has the corrent + # jwt secret + update_jwt; + CODE=$?; + + if [ $CODE -ne 0 ]; then + return $CODE; + fi + + step 'Database is initialized' + # database is ready + touch /status/ready +} + +init |