From 708594d32ffe779cf547c816fa7cdd19d095be2e Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Mon, 20 May 2024 17:11:38 -0400 Subject: v2 done --- build/dbinit/Dockerfile | 5 -- build/dbinit/dbinit | 151 --------------------------------------------- build/init/Dockerfile | 5 ++ build/init/init | 151 +++++++++++++++++++++++++++++++++++++++++++++ build/postgrest/Dockerfile | 2 +- build/shim/Dockerfile | 9 +++ 6 files changed, 166 insertions(+), 157 deletions(-) delete mode 100644 build/dbinit/Dockerfile delete mode 100755 build/dbinit/dbinit create mode 100644 build/init/Dockerfile create mode 100755 build/init/init create mode 100644 build/shim/Dockerfile (limited to 'build') diff --git a/build/dbinit/Dockerfile b/build/dbinit/Dockerfile deleted file mode 100644 index 81c66ea..0000000 --- a/build/dbinit/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100755 index c64f139..0000000 --- a/build/dbinit/dbinit +++ /dev/null @@ -1,151 +0,0 @@ -#!/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 diff --git a/build/init/Dockerfile b/build/init/Dockerfile new file mode 100644 index 0000000..2b3d770 --- /dev/null +++ b/build/init/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine:3.19 +RUN apk add --no-cache postgresql16-client tini +COPY ./init /usr/local/bin/init +ENTRYPOINT ["/sbin/tini", "--"] +CMD ["/usr/local/bin/init"] diff --git a/build/init/init b/build/init/init new file mode 100755 index 0000000..c64f139 --- /dev/null +++ b/build/init/init @@ -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 diff --git a/build/postgrest/Dockerfile b/build/postgrest/Dockerfile index 62b8a2e..d7720aa 100644 --- a/build/postgrest/Dockerfile +++ b/build/postgrest/Dockerfile @@ -1,7 +1,7 @@ FROM alpine:3.19 COPY ./postgrest.tar.xz /tmp/postgrest.tar.xz RUN tar xJf /tmp/postgrest.tar.xz -C /tmp -RUN mv /tmp/postgrest /usr/local/bin/postgrest +RUN cp /tmp/postgrest /usr/local/bin/postgrest RUN rm /tmp/postgrest.tar.xz COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh CMD ["/usr/local/bin/entrypoint.sh"] diff --git a/build/shim/Dockerfile b/build/shim/Dockerfile new file mode 100644 index 0000000..82cc9a7 --- /dev/null +++ b/build/shim/Dockerfile @@ -0,0 +1,9 @@ +FROM alpine:3.19 +RUN apk add --no-cache \ + php83 \ + php83-pdo \ + php83-pdo_sqlite \ + php83-pdo_pgsql \ + tini +ENTRYPOINT ["/sbin/tini", "--"] +CMD ["/usr/bin/php83", "/opt/shim/shim.php"] -- cgit v1.2.3-freya