MyPLC: portable self-contained PLC installation
[myplc.git] / api-config
1 #!/usr/bin/python
2 #
3 # Bootstraps the PLC database with a default administrator account and
4 # a default site.
5 #
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2006 The Trustees of Princeton University
8 #
9 # $Id$
10 #
11
12 import plcapilib
13 (plcapi, moreopts, argv) = plcapilib.plcapi(globals())
14 from plc_config import PLCConfiguration
15
16
17 def main():
18     cfg = PLCConfiguration()
19     cfg.load()
20     variables = cfg.variables()
21
22     # Load variables into dictionaries
23     (category, variablelist) = variables['plc']
24     plc = dict(zip(variablelist.keys(),
25                    [variable['value'] for variable in variablelist.values()]))
26
27     (category, variablelist) = variables['plc_www']
28     plc_www = dict(zip(variablelist.keys(),
29                        [variable['value'] for variable in variablelist.values()]))
30
31     (category, variablelist) = variables['plc_api']
32     plc_api = dict(zip(variablelist.keys(),
33                        [variable['value'] for variable in variablelist.values()]))
34
35     # Create/update the default administrator account (should be
36     # person_id 2).
37     admin = { 'person_id': 2,
38               'first_name': "Default",
39               'last_name': "Administrator",
40               'email': plc['root_user'],
41               'password': plc['root_password'] }
42     persons = AdmGetPersons([admin['person_id']])
43     if not persons:
44         person_id = AdmAddPerson(admin['first_name'], admin['last_name'], admin)
45         if person_id != admin['person_id']:
46             # Huh? Someone deleted the account manually from the database.
47             AdmDeletePerson(person_id)
48             raise Exception, "Someone deleted the \"%s %s\" account from the database!" % \
49                   (admin['first_name'], admin['last_name'])
50         AdmSetPersonEnabled(person_id, True)
51     else:
52         person_id = persons[0]['person_id']
53         AdmUpdatePerson(person_id, admin)
54
55     # Create/update the default site (should be site_id 0)
56     if plc_www['port'] == '80':
57         url = "http://" + plc_www['host'] + "/"
58     elif plc_www['port'] == '443':
59         url = "https://" + plc_www['host'] + "/"
60     else:
61         url = "http://" + plc_www['host'] + ":" + plc_www['port'] + "/"
62     site = { 'site_id': 1,
63              'name': plc['name'] + " Central",
64              'abbreviated_name': plc['name'],
65              'login_base': plc['slice_prefix'],
66              'is_public': False,
67              'url': url,
68              'max_slices': 100 }
69
70     sites = AdmGetSites([site['site_id']])
71     if not sites:
72         site_id = AdmAddSite(site['name'], site['abbreviated_name'], site['login_base'], site)
73         if site_id != site['site_id']:
74             AdmDeleteSite(site_id)
75             raise Exception, "Someone deleted the \"%s\" site from the database!" % \
76                   site['name']
77     else:
78         site_id = sites[0]['site_id']
79         # XXX login_base cannot be updated
80         del site['login_base']
81         AdmUpdateSite(site_id, site)
82
83     # The default administrator account must be associated with a site
84     # in order to login.
85     AdmAddPersonToSite(admin['person_id'], site['site_id'])
86     AdmSetPersonPrimarySite(admin['person_id'], site['site_id'])
87
88     # Grant admin and PI roles to the default administrator account
89     AdmGrantRoleToPerson(admin['person_id'], 10)
90     AdmGrantRoleToPerson(admin['person_id'], 20)
91
92     # XXX Setup default slice attributes and initscripts (copy from PLC)
93
94     # XXX Setup PlanetLabConf entries (copy from PLC)
95
96 if __name__ == '__main__':
97     main()