summaryrefslogtreecommitdiff
path: root/build/init
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-05-24 09:05:42 -0400
committerFreya Murphy <freya@freyacat.org>2024-05-24 09:05:42 -0400
commitc5f39ea2cd7cf02246705ea8872d3b350526165c (patch)
tree2694f9fdc5d83b529a01f2997c1d89c271c86592 /build/init
downloadwebsite-c5f39ea2cd7cf02246705ea8872d3b350526165c.tar.gz
website-c5f39ea2cd7cf02246705ea8872d3b350526165c.tar.bz2
website-c5f39ea2cd7cf02246705ea8872d3b350526165c.zip
initial
Diffstat (limited to 'build/init')
-rw-r--r--build/init/Dockerfile21
-rwxr-xr-xbuild/init/init108
2 files changed, 129 insertions, 0 deletions
diff --git a/build/init/Dockerfile b/build/init/Dockerfile
new file mode 100644
index 0000000..98eb285
--- /dev/null
+++ b/build/init/Dockerfile
@@ -0,0 +1,21 @@
+FROM alpine:3.19
+
+# install packages
+RUN apk add --no-cache postgresql16-client tini shadow
+RUN rm -fr /var/cache/apk/*
+
+# setup main user
+RUN adduser -D init
+RUN groupmod --gid 1000 init
+RUN usermod --uid 1000 init
+
+# copy scripts
+COPY ./init /usr/local/bin/init
+
+# remove build packages
+RUN apk del shadow
+
+# do the
+USER 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..a889a8b
--- /dev/null
+++ b/build/init/init
@@ -0,0 +1,108 @@
+#!/bin/sh
+
+errors=$(mktemp)
+
+step() {
+ printf '\x1b[34;1m>> %s\x1b[0m\n' "$*"
+}
+
+error() {
+ {
+ printf '\x1b[31;1merror: \x1b[0m%s\n' "$*";
+ grep -v 'current transaction is aborted' < "$errors";
+ printf "\x1b[31m;1error: \x1b[0mAborting migrations, fix file(s) then restart process.";
+ } 1>&2;
+}
+
+try() {
+ "$@" 2> "$errors";
+ count=$(grep -c 'ERROR' < "$errors")
+ if [ "$count" -eq 0 ]; then
+ return 0;
+ else
+ return 1;
+ fi
+}
+
+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
+ if try psql -f "$file"; then
+ i=$((i+1));
+ continue;
+ else
+ error "An error occoured during a migration (rev $name)"
+ return 1;
+ fi
+ else
+ return 0;
+ fi
+ done
+}
+
+init () {
+ # reomve ready status
+ # so php ignores requests
+ rm -f /status/ready
+
+ step 'Waiting for database';
+ # make sure the database is running
+ # before we run any requests
+ wait_until_ready;
+ step 'Database ready';
+
+ 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
+ if ! run_migrations "$REV"; then
+ return 1;
+ fi
+
+ step 'Database is initialized'
+ # database is ready
+ touch /status/ready
+}
+
+init
+rm "$errors"