Configure the SAML2 session database during installation
[cascardo/ipsilon.git] / quickrun.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
4
5 import argparse
6 import os
7 import shutil
8 import subprocess
9 from string import Template
10
11
12 logger = None
13
14
15 def parse_args():
16     parser = argparse.ArgumentParser(description=\
17         'Run a test Ipsilon instance from the checkout directory')
18     parser.add_argument('--workdir', default=os.path.join(os.getcwd(), 'qrun'),
19                         help="Directory in which db/session files are stored")
20     parser.add_argument('--cleanup', '-c', action='store_true', default=False,
21                         help="Wipe workdir before starting")
22     return vars(parser.parse_args())
23
24
25 CONF_TEMPLATE="templates/install/ipsilon.conf"
26
27 ADMIN_TEMPLATE='''
28 CREATE TABLE login_config (name TEXT,option TEXT,value TEXT);
29 INSERT INTO login_config VALUES('global', 'enabled', 'testauth');
30 '''
31
32 USERS_TEMPLATE='''
33 CREATE TABLE users(name TEXT, option TEXT, value TEXT);
34 INSERT INTO users VALUES('admin', 'is_admin', '1');
35 '''
36
37 def config(workdir):
38     os.makedirs(workdir)
39     os.makedirs(os.path.join(workdir, 'sessions'))
40
41     admin_db = os.path.join(workdir, 'adminconfig.sqlite')
42     sql = os.path.join(workdir, 'admin.sql')
43     with open(sql, 'w+') as f:
44         f.write(ADMIN_TEMPLATE)
45     subprocess.call(['sqlite3', '-init', sql, admin_db, '.quit'])
46
47     users_db = os.path.join(workdir, 'userprefs.sqlite')
48     sql = os.path.join(workdir, 'users.sql')
49     with open(sql, 'w+') as f:
50         f.write(USERS_TEMPLATE)
51     subprocess.call(['sqlite3', '-init', sql, users_db, '.quit'])
52
53     trans_db = os.path.join(workdir, 'transactions.sqlite')
54
55     with open(CONF_TEMPLATE) as f:
56         conf_template = f.read()
57     t = Template(conf_template)
58     text = t.substitute({'debugging': 'True',
59                          'instance': 'idp',
60                          'staticdir': os.getcwd(),
61                          'datadir': workdir,
62                          'admindb': admin_db,
63                          'usersdb': users_db,
64                          'transdb': trans_db,
65                          'sesstype': 'file',
66                          'sessopt': 'path',
67                          'sessval': os.path.join(workdir, 'sessions'),
68                          'secure': 'False',
69                         })
70     conf = os.path.join(workdir, 'ipsilon.conf')
71     with open(conf, 'w+') as f:
72         f.write(text)
73     return conf
74
75 if __name__ == '__main__':
76
77     args = parse_args()
78
79     penv = dict()
80     penv.update(os.environ)
81     penv['PYTHONPATH'] = os.getcwd()
82
83     exe = os.path.join(os.getcwd(), 'ipsilon/ipsilon')
84
85     if args['cleanup']:
86         shutil.rmtree(args['workdir'])
87
88     if not os.path.exists(args['workdir']):
89         conf = config(args['workdir'])
90     else:
91         conf = os.path.join(args['workdir'], 'ipsilon.conf')
92
93     if not os.path.exists(os.path.join(args['workdir'], 'ui')):
94         os.symlink(os.path.join(os.getcwd(), 'ui'),
95                    os.path.join(args['workdir'], 'ui'))
96
97
98     os.chdir(args['workdir'])
99
100     p = subprocess.Popen([exe, conf], env=penv)
101     p.wait()