"""
This is a script for starting PostgreSQL for SMC main site deployment, at
least until we change the database to use Kubernetes.
"""
import os, sys, time
path = os.path.split(os.path.realpath(__file__))[0]; os.chdir(path); sys.path.insert(0, path)
PG_DATA = os.path.join(path, "postgres_data")
def cmd(s):
print(s)
if os.system(s):
raise RuntimeError
def stop_server():
try:
cmd("kill %s"%(open(os.path.join(PG_DATA, 'postmaster.pid')).read().split()[0]))
time.sleep(3)
except Exception as err:
print("WARNING ", err)
if __name__ == '__main__':
if not os.path.exists(PG_DATA):
cmd("pg_ctl init -D '%s'"%PG_DATA)
open(os.path.join(PG_DATA,'pg_hba.conf'), 'w').write('local all all trust\n')
cmd("chmod og-rwx '%s'"%PG_DATA)
cmd("postgres -D '%s' >%s/postgres.log 2>&1 &"%(PG_DATA, PG_DATA))
time.sleep(5)
cmd("createuser -sE smc")
stop_server()
open(os.path.join(PG_DATA,'pg_hba.conf'), 'w').write('host all smc all md5\n')
cmd("postgres -h 0.0.0.0 -D '%s' >%s/postgres.log 2>&1 &"%(PG_DATA, PG_DATA))