Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 39595
1
#!/usr/bin/env python
2
3
"""
4
This is a script for starting PostgreSQL for SMC main site deployment, at
5
least until we change the database to use Kubernetes.
6
"""
7
8
import os, sys, time
9
10
path = os.path.split(os.path.realpath(__file__))[0]; os.chdir(path); sys.path.insert(0, path)
11
12
PG_DATA = os.path.join(path, "postgres_data")
13
14
def cmd(s):
15
print(s)
16
if os.system(s):
17
raise RuntimeError
18
19
def stop_server():
20
try:
21
cmd("kill %s"%(open(os.path.join(PG_DATA, 'postmaster.pid')).read().split()[0]))
22
time.sleep(3)
23
except Exception, err:
24
print "WARNING", err
25
26
27
if __name__ == '__main__':
28
if not os.path.exists(PG_DATA):
29
# Create the database directory file structure
30
cmd("pg_ctl init -D '%s'"%PG_DATA)
31
32
# Set database to only use local sockets for communication (with no password)
33
open(os.path.join(PG_DATA,'pg_hba.conf'), 'w').write('local all all trust\n')
34
cmd("chmod og-rwx '%s'"%PG_DATA) # just in case -- be paranoid...
35
36
# Start database running in background as daemon
37
cmd("postgres -D '%s' >%s/postgres.log 2>&1 &"%(PG_DATA, PG_DATA))
38
time.sleep(5)
39
40
# Create the smc user (with no password -- you better do that!!)
41
cmd("createuser -sE smc")
42
43
# Stop database daemon
44
stop_server()
45
46
# Set database so only way to connect is as 'smc' user via encrypted password.
47
# (TODO: Note -- connection (so data) isn't necessarily encrypted unless we build
48
# postgreSQL properly -- see https://www.postgresql.org/docs/9.6/static/auth-pg-hba-conf.html)
49
open(os.path.join(PG_DATA,'pg_hba.conf'), 'w').write('host all smc all md5\n')
50
51
# Start database daemon listening on all network interfaces.
52
cmd("postgres -h 0.0.0.0 -D '%s' >%s/postgres.log 2>&1 &"%(PG_DATA, PG_DATA))
53
54
55