This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlinkr_setup.py
110 lines (93 loc) · 3.5 KB
/
linkr_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
import getpass
import sys
import traceback
import linkr
import database.common
import database.user
def main():
"""
Main procedure; prompts the user interactively for input.
"""
print '==========LINKR SETUP=========='
print ''
print 'This script is used for initializing Linkr for the first time on a new deployment.'
print 'This will create the Linkr MySQL tables and create an admin user account.'
print 'Please ensure you have created the MySQL user and database BEFORE running this script.'
print 'Please ensure that you have copied and modified the templates from config/*/*.json. ' \
'and set all config options to your liking.'
print 'It is especially important that all database configuration constants are set properly ' \
'(host, name, username, password).'
print 'Press any key to continue or ^C to quit.'
wait_for_enter()
verify_config()
print ''
print 'Press any key to create the Linkr database and tables.'
wait_for_enter()
setup_db()
print 'Enter the username and password for the admin user.'
admin_username = raw_input('Admin username: ')
admin_password = getpass.getpass('Admin password: ')
admin_password_verify = getpass.getpass('Verify admin password: ')
if admin_password != admin_password_verify:
print 'The admin password does not match the verification password! Please try again.'
sys.exit(1)
setup_admin(admin_username, admin_password)
print 'Setup complete!'
def verify_config():
"""
Verify that the configuration options are set properly.
"""
try:
config = __import__('config')
print 'Configuration read successfully!'
print 'Linkr URL: {url}'.format(url=config.options.server('linkr_url'))
print 'Database host: {db_host}'.format(db_host=config.secrets.server('database.host'))
print 'Database name: {db_name}'.format(db_name=config.secrets.server('database.name'))
print 'Database username: {db_user}'.format(
db_user=config.secrets.server('database.user'),
)
print 'Database password: {db_pass}'.format(
db_pass=config.secrets.server('database.password'),
)
except:
print 'There was an error reading the config files!'
print 'Ensure that you have copied and modified the templates from config/*/*.json.'
sys.exit(1)
def setup_db():
"""
Create the database tables.
"""
try:
database.common.create_tables()
except:
print 'There was an error creating the tables. Are you sure your database credentials ' \
'are correct?'
print 'The full stack trace is as follows:'
traceback.print_exc()
sys.exit(1)
def setup_admin(username, password):
"""
Create the admin user account.
:param username: The desired admin username.
:param password: The desired admin password.
"""
try:
database.user.add_user(
username=username,
password=password,
signup_ip='127.0.0.1',
is_admin=True,
)
except:
print 'There was an error creating the admin user. Are you sure the database user ' \
'specified by your config has write permissions to the database?'
print 'The full stack trace is as follows:'
traceback.print_exc()
sys.exit(1)
def wait_for_enter():
"""
Block execution until the user presses enter.
"""
raw_input()
if __name__ == '__main__':
main()