forked from cortex-lab/alyx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·144 lines (109 loc) · 4.67 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
from getpass import getpass
import os
import os.path as op
import shutil
from django.utils.crypto import get_random_string
import sys
def _secret_key():
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
return get_random_string(50, chars)
def _system(cmd):
res = os.popen(cmd).read().strip()
return res
def _psql(sql, **kwargs):
sql = sql.format(**kwargs)
cmd = 'sudo su - postgres -c "psql -tc \\"{}\\""'.format(sql)
return _system(cmd)
def _replace_in_file(source_file, target_file, replacements=None, target_mode='w', chmod=None):
target_file = op.expanduser(target_file)
#copy2(source_file, target_file)
with open(source_file, 'r') as f:
contents = f.read()
for key, value in replacements.items():
contents = contents.replace(key, value)
with open(target_file, target_mode) as f:
f.write(contents)
if chmod:
os.chmod(target_file, chmod)
# Check if we are inside a virtual environment
if not hasattr(sys, 'real_prefix'):
raise Warning('You are not currently in a virtual environment, would you like to proceed anyway? (y/n): ')
continue_anyway = input()
if continue_anyway not in ("y", 'yes'):
print('Create a virtual environment from the repository root with: "virtualenv alyxvenv --python=python3"')
print('Enter a virtual environment from the repository root with: "source alyxvenv/bin/activate"')
sys.exit()
# Prompt information.
DBNAME = input("Enter a database name [labdb]:") or 'labdb'
DBUSER = input("Enter a postgres username [labdbuser]:") or 'labdbuser'
DBHOST = input("Enter a postgres host [localhost]:") or 'localhost'
DBPASSWORD = getpass("Enter a postgres password:")
if not DBPASSWORD:
print("A password is mandatory.")
exit(1)
if getpass("Enter a postgres password (again):") != DBPASSWORD:
print("The passwords don't match.")
exit(1)
SECRET_KEY = _secret_key()
# Make sure the database exists.
out = _psql("SELECT 1 FROM pg_database WHERE datname = '{DBNAME}'", DBNAME=DBNAME)
if not out:
out = _psql("CREATE DATABASE {DBNAME}", DBNAME=DBNAME)
# Make sure the user exists.
out = _psql("SELECT 1 FROM pg_user WHERE usename = '{DBUSER}'", DBUSER=DBUSER)
if not out:
out = _psql("CREATE USER {DBUSER} WITH PASSWORD '{DBPASSWORD}'",
DBUSER=DBUSER, DBPASSWORD=DBPASSWORD)
# Set up the roles for the user.
try:
_psql("ALTER ROLE {DBUSER} SET client_encoding TO 'utf8';", DBUSER=DBUSER)
_psql("ALTER ROLE {DBUSER} SET default_transaction_isolation TO 'read committed';", DBUSER=DBUSER)
_psql("ALTER ROLE {DBUSER} SET timezone TO 'UTC';", DBUSER=DBUSER)
_psql("GRANT ALL PRIVILEGES ON DATABASE {DBNAME} TO {DBUSER};", DBNAME=DBNAME, DBUSER=DBUSER)
_psql("ALTER USER {DBUSER} WITH CREATEROLE;", DBUSER=DBUSER)
_psql("ALTER USER {DBUSER} WITH SUPERUSER;", DBUSER=DBUSER)
_psql("ALTER USER {DBUSER} WITH CREATEDB;", DBUSER=DBUSER)
print('-----------------------------')
print('Database successfully created')
except Exception as e:
print('Could not create database, error message:\n')
raise e
repl = {
'%SECRET_KEY%': SECRET_KEY,
'%DBNAME%': DBNAME,
'%DBUSER%': DBUSER,
'%DBHOST%': DBHOST,
'%DBPASSWORD%': DBPASSWORD,
}
try:
# Set up the settings file.
_replace_in_file('alyx/alyx/settings_secret_template.py',
'alyx/alyx/settings_secret.py',
replacements=repl,
)
# Set up the .pgpass file to avoid typing the postgres password.
_replace_in_file('scripts/templates/.pgpass_template', '~/.pgpass',
replacements=repl, target_mode='a', chmod=0o600)
# Set up the maintainance scripts.
_replace_in_file('scripts/templates/load_db.sh', 'scripts/load_db.sh',
replacements=repl, chmod=0o755)
_replace_in_file('scripts/templates/dump_db.sh', 'scripts/dump_db.sh',
replacements=repl, chmod=0o755)
print('Configuration files successfully created from templates')
except Exception as e:
print('Could not create configuration from templates, error message:\n')
raise e
# Set up the database.
try:
_system('python alyx/manage.py makemigrations')
_system('python alyx/manage.py migrate')
_system('''echo "from misc.models import LabMember;'''
'''LabMember.objects.create_superuser('admin', 'admin@example.com', 'admin')"'''
'''| python alyx/manage.py shell''')
print('Database successfully configured for Alyx')
except Exception as e:
print('Could not configure database for Alyx, error message:\n')
raise e
print('------------------------')
print('Alyx setup successful <3')