-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
executable file
·96 lines (78 loc) · 2.96 KB
/
manage.py
File metadata and controls
executable file
·96 lines (78 loc) · 2.96 KB
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
#!/usr/bin/env python
import sys
import getopt
from rethinkdb import r
from web import app
from werkzeug.security import generate_password_hash
def usage():
print( "Usage: %s [options]\n" % sys.argv[0] )
print( """Options:
-b, --bind=ADDRESS bind to specific ip ADDRESS (default 0.0.0.0)
-d, --debug run in debug mode (default False)
-i, --init initialize database, tables and user admin
-h, --help display this help and exit
-p, --port=PORT listen to specific PORT (default 8000)
-t, --thread run in threaded mode (default False)
""")
def main(argv):
# variables
opt_host = '0.0.0.0'
opt_port = 8000
opt_debug = False
opt_init = False
opt_thread = False
# manage options
try:
opts, args = getopt.getopt(argv,'hditb:p:',['help','debug','init','thread','bind=','port='])
except getopt.GetoptError:
usage()
sys.exit(2)
# setup variables with options
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
if opt in ('-d', '--debug'):
opt_debug = True
if opt in ('-i', '--init'):
opt_init = True
if opt in ('-t', '--thread'):
opt_thread = True
if opt in ('-b', '--bind'):
opt_host = arg
if opt in ('-p', '--port'):
opt_port = int(arg)
# if initialization required
if( opt_init ):
# connection to RethinkDB
rdb = r.connect(
host=app.config['RETHINKDB_HOST'],
port=app.config['RETHINKDB_PORT'],
).repl()
# check if RETHINK_BASE already exists
if( not app.config['RETHINKDB_BASE'] in r.db_list().run() ):
# setup minimal configuration
res = r.db_create( app.config['RETHINKDB_BASE'] ).run()
rdb.use( app.config['RETHINKDB_BASE'] )
print('[1/4] Database "%s" created !' % app.config['RETHINKDB_BASE'])
r.table_create('contacts').run()
print('[2/4] Table "contacts" created !')
r.table_create('users').run()
print('[3/4] Table "users" created !')
r.table('users').insert({
'username': 'admin',
'email': '',
'password': generate_password_hash( app.config['TAGSCARDS_DATABASE'],'sha256' )
}).run();
print('[4/4] User "admin" created (password="%s") !' % app.config['TAGSCARDS_PASSWORD'] )
else:
print('[ERROR] Database "%s" already exists !' % app.config['RETHINKDB_BASE'])
# close connection
rdb.close()
else:
# verbose
print( "HOST=%s\nPORT=%d\nTHREAD=%r\nDEBUG=%r\n" % (opt_host,opt_port,opt_thread,opt_debug) )
# run application
app.run(host=opt_host,port=opt_port,threaded=opt_thread,debug=opt_debug)
if __name__ == "__main__":
main(sys.argv[1:])