7fd6bdf065bc16bab1c2c147ed5d3c00b5e4141d
[myplc.git] / db-config.d / 01-init
1 # Create/update the default administrator account (should be person_id 2).
2
3 admin = { 'person_id': 2,
4           'first_name': "Default",
5           'last_name': "Administrator",
6           'email': plc['root_user'],
7           'password': plc['root_password'] }
8 persons = GetPersons([admin['person_id']])
9 if not persons:
10     person_id = AddPerson(admin)
11     if person_id != admin['person_id']:
12         # Huh? Someone deleted the account manually from the database.
13         DeletePerson(person_id)
14         raise Exception, "Someone deleted the \"%s %s\" account from the database!" % \
15               (admin['first_name'], admin['last_name'])
16     UpdatePerson(person_id, { 'enabled': True })
17 else:
18     person_id = persons[0]['person_id']
19     UpdatePerson(person_id, admin)
20
21 # Create/update the default site (should be site_id 1)
22 if plc_www['port'] == '80':
23     url = "http://" + plc_www['host'] + "/"
24 elif plc_www['port'] == '443':
25     url = "https://" + plc_www['host'] + "/"
26 else:
27     url = "http://" + plc_www['host'] + ":" + plc_www['port'] + "/"
28
29 SetMyPLCURL(url)
30
31 site = { 'site_id': 1,
32          'name': plc['name'] + " Central",
33          'abbreviated_name': plc['name'],
34          'login_base': plc['slice_prefix'],
35          'is_public': False,
36          'url': url,
37          'max_slices': 100 }
38
39 sites = GetSites([site['site_id']])
40 if not sites:
41     site_id = AddSite(site['name'], site['abbreviated_name'], site['login_base'], site)
42     if site_id != site['site_id']:
43         DeleteSite(site_id)
44         raise Exception, "Someone deleted the \"%s\" site from the database!" % \
45               site['name']
46     sites = [site]
47
48 # Must call UpdateSite() even after AddSite() to update max_slices
49 site_id = sites[0]['site_id']
50 UpdateSite(site_id, site)
51
52 # The default administrator account must be associated with a site
53 # in order to login.
54 AddPersonToSite(admin['person_id'], site['site_id'])
55 SetPersonPrimarySite(admin['person_id'], site['site_id'])
56
57 # Grant admin and PI roles to the default administrator account
58 AddRoleToPerson(10, admin['person_id'])
59 AddRoleToPerson(20, admin['person_id'])
60