Implement health checking

master
Tim Möhlmann 6 years ago
parent 92bdc6e0a3
commit de3f125ed3
No known key found for this signature in database
GPG Key ID: 8677988D8072E8DE

@ -7,7 +7,7 @@ RUN apk add --no-cache \
RUN pip3 install jinja2 RUN pip3 install jinja2
# Image specific layers under this line # Image specific layers under this line
RUN apk add --no-cache \ RUN apk add --no-cache \
postgresql postgresql-libs postgresql-contrib busybox-suid \ postgresql postgresql-libs postgresql-contrib busybox-suid sudo\
&& apk add --virtual .build-deps gcc musl-dev postgresql-dev python3-dev \ && apk add --virtual .build-deps gcc musl-dev postgresql-dev python3-dev \
&& pip3 install psycopg2 anosql \ && pip3 install psycopg2 anosql \
&& apk --purge del .build-deps && apk --purge del .build-deps
@ -30,3 +30,4 @@ VOLUME /backup
EXPOSE 5432 EXPOSE 5432
CMD /start.py CMD /start.py
HEALTHCHECK CMD psql -h 127.0.0.1 -d postgres -U health -c "select 1 as ok;" || exit 1

@ -80,7 +80,7 @@
local all all peer map=local local all all peer map=local
# IPv4 connections: # IPv4 connections:
host all mailu {{ SUBNET }} md5 host all mailu {{ SUBNET }} md5
host postgres health 127.0.0.1/32 md5 host postgres health 127.0.0.1/32 trust
# IPv6 local connections: # IPv6 local connections:
host all all ::1/128 reject host all all ::1/128 reject
# Allow replication connections from localhost, by a user with the # Allow replication connections from localhost, by a user with the

@ -42,4 +42,3 @@
# MAPNAME SYSTEM-USERNAME PG-USERNAME # MAPNAME SYSTEM-USERNAME PG-USERNAME
local postgres postgres local postgres postgres
local root postgres local root postgres
local root health

@ -18,6 +18,12 @@ begin
end end
$$; $$;
-- name: grant_health!
-- Grant connect permission for the health user
grant connect
on database postgres
to health;
-- name: update_pw! -- name: update_pw!
alter alter
user mailu user mailu

@ -15,6 +15,7 @@ def setup():
queries.update_pw(conn, pw=os.environ.get("SECRET_KEY")) queries.update_pw(conn, pw=os.environ.get("SECRET_KEY"))
# Healthcheck user # Healthcheck user
queries.create_health_user(conn) queries.create_health_user(conn)
queries.grant_health(conn)
conn.commit() conn.commit()
# create db cannot be atomic. But this script is the only active connection, this is kinda safe. # create db cannot be atomic. But this script is the only active connection, this is kinda safe.
if not queries.check_db(conn): if not queries.check_db(conn):
@ -30,7 +31,7 @@ def setup():
# Bootstrap the database if postgresql is running for the first time # Bootstrap the database if postgresql is running for the first time
if not os.path.exists('/data/pg_wal'): if not os.path.exists('/data/pg_wal'):
os.system("chown -R postgres:postgres /data") os.system("chown -R postgres:postgres /data")
os.system("su - postgres -c 'initdb -D /data'") os.system("sudo -u postgres initdb -D /data")
# Create backup directory structure, if it does not yet exist # Create backup directory structure, if it does not yet exist
os.system("mkdir -p /backup/wal_archive") os.system("mkdir -p /backup/wal_archive")
@ -42,13 +43,13 @@ for pg_file in glob.glob("/conf/*.conf"):
convert(pg_file, os.path.join("/data", os.path.basename(pg_file))) convert(pg_file, os.path.join("/data", os.path.basename(pg_file)))
# Run postgresql locally for DB and user creation # Run postgresql locally for DB and user creation
os.system("su - postgres -c 'pg_ctl start -D /data -o \"-h localhost\"'") os.system("sudo -u postgres pg_ctl start -D /data -o '-h \"''\" '")
setup() setup()
os.system("su - postgres -c 'pg_ctl stop -m smart -w -D /data'") os.system("sudo -u postgres pg_ctl stop -m smart -w -D /data")
out=open("/proc/1/fd/1", "w") out=open("/proc/1/fd/1", "w")
err=open("/proc/1/fd/2", "w") err=open("/proc/1/fd/2", "w")
# Run the cron deamon # Run the cron deamon
subprocess.Popen(["crond", "-f", "-d7"], stdout=out, stderr=err) subprocess.Popen(["crond", "-f"], stdout=out, stderr=err)
# Run postgresql service # Run postgresql service
os.system("su - postgres -c 'postgres -D /data -h \*'") os.system("sudo -u postgres postgres -D /data -h \*")

Loading…
Cancel
Save