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 | |
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')
-rw-r--r-- | build/dbinit/Dockerfile | 5 | ||||
-rwxr-xr-x | build/dbinit/dbinit | 151 | ||||
-rw-r--r-- | build/php/Dockerfile | 4 | ||||
-rw-r--r-- | build/postgres/Dockerfile | 6 | ||||
-rw-r--r-- | build/postgrest/Dockerfile | 9 | ||||
-rwxr-xr-x | build/postgrest/entrypoint.sh | 20 | ||||
-rw-r--r-- | build/postgrest/postgrest.tar.xz | bin | 0 -> 3648348 bytes |
7 files changed, 195 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 diff --git a/build/php/Dockerfile b/build/php/Dockerfile new file mode 100644 index 0000000..280ca35 --- /dev/null +++ b/build/php/Dockerfile @@ -0,0 +1,4 @@ +FROM php:fpm-alpine +RUN apk add --no-cache postgresql-dev +RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql +RUN docker-php-ext-install pdo pdo_pgsql diff --git a/build/postgres/Dockerfile b/build/postgres/Dockerfile new file mode 100644 index 0000000..834fa89 --- /dev/null +++ b/build/postgres/Dockerfile @@ -0,0 +1,6 @@ +FROM postgres:16-alpine +RUN apk add --no-cache make git +RUN git clone https://github.com/michelp/pgjwt.git /tmp/pgjwt +WORKDIR /tmp/pgjwt +RUN make install +WORKDIR / diff --git a/build/postgrest/Dockerfile b/build/postgrest/Dockerfile new file mode 100644 index 0000000..62b8a2e --- /dev/null +++ b/build/postgrest/Dockerfile @@ -0,0 +1,9 @@ +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 rm /tmp/postgrest.tar.xz +COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh +CMD ["/usr/local/bin/entrypoint.sh"] + + diff --git a/build/postgrest/entrypoint.sh b/build/postgrest/entrypoint.sh new file mode 100755 index 0000000..d375769 --- /dev/null +++ b/build/postgrest/entrypoint.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +mkdir /etc/postgrest.d +config=/etc/postgrest.d/postgrest.conf + +PGRST_DB_URI="postgres://authenticator:postgrest@db:5432/$POSTGRES_DB" +PGRST_ROLE="rest_anon" +PGRST_SCHEMA="api" + +rm -fr "$config" +touch "$config" +printf 'db-uri = "%s"\n' "$PGRST_DB_URI" >> $config +printf 'db-anon-role = "%s"\n' "$PGRST_ROLE" >> $config +printf 'db-schemas = "%s"\n' "$PGRST_SCHEMA" >> $config +printf 'jwt-secret = "%s"\n' "$JWT_SECRET" >> $config +printf 'jwt-secret-is-base64 = false\n' >> $config +printf 'server-host = "*"\n' >> $config +printf 'server-port = 3000\n' >> $config + +exec /usr/local/bin/postgrest $config diff --git a/build/postgrest/postgrest.tar.xz b/build/postgrest/postgrest.tar.xz Binary files differnew file mode 100644 index 0000000..33c2b2d --- /dev/null +++ b/build/postgrest/postgrest.tar.xz |