mirror of
https://github.com/Mailu/Mailu.git
synced 2024-12-14 10:53:30 +02:00
56 lines
1.8 KiB
Python
Executable File
56 lines
1.8 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
import anosql
|
|
import psycopg2
|
|
import jinja2
|
|
import glob
|
|
import os
|
|
import subprocess
|
|
|
|
def setup():
|
|
conn = psycopg2.connect(user = 'postgres')
|
|
queries = anosql.load_queries('postgres', '/conf/queries.sql')
|
|
# Mailu user
|
|
queries.create_mailu_user(conn)
|
|
queries.update_pw(conn, pw=os.environ.get("SECRET_KEY"))
|
|
# Healthcheck user
|
|
queries.create_health_user(conn)
|
|
queries.grant_health(conn)
|
|
conn.commit()
|
|
# create db cannot be atomic. But this script is the only active connection, this is kinda safe.
|
|
if not queries.check_db(conn):
|
|
conn.set_isolation_level(0)
|
|
queries.create_db(conn)
|
|
conn.set_isolation_level(1)
|
|
conn.close()
|
|
conn = psycopg2.connect(user = 'postgres', database= 'mailu')
|
|
queries.create_citext(conn)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
# Bootstrap the database if postgresql is running for the first time
|
|
if not os.path.exists('/data/pg_wal'):
|
|
os.system("chown -R postgres:postgres /data")
|
|
os.system("sudo -u postgres initdb -D /data")
|
|
|
|
# Create backup directory structure, if it does not yet exist
|
|
os.system("mkdir -p /backup/wal_archive")
|
|
os.system("chown -R postgres:postgres /backup")
|
|
|
|
# Render config files
|
|
convert = lambda src, dst: open(dst, "w").write(jinja2.Template(open(src).read()).render(**os.environ))
|
|
for pg_file in glob.glob("/conf/*.conf"):
|
|
convert(pg_file, os.path.join("/data", os.path.basename(pg_file)))
|
|
|
|
# Run postgresql locally for DB and user creation
|
|
os.system("sudo -u postgres pg_ctl start -D /data -o '-h \"''\" '")
|
|
setup()
|
|
os.system("sudo -u postgres pg_ctl stop -m smart -w -D /data")
|
|
|
|
out=open("/proc/1/fd/1", "w")
|
|
err=open("/proc/1/fd/2", "w")
|
|
# Run the cron deamon
|
|
subprocess.Popen(["crond", "-f"], stdout=out, stderr=err)
|
|
# Run postgresql service
|
|
os.system("sudo -u postgres postgres -D /data -h \*")
|