split plc.d/ and db-config.d between myplc and plcapi modules as a first step
[plcapi.git] / db-config.d / 002-system_site
1 # -*-python-*-
2 # $Id$
3 # $URL$
4 #################### 
5 # Create/update and populate the default site (should be site_id 1)
6
7 ### plc_www holds the contents of the PLC_WWW configuration category
8 if plc_www['port'] == '80':
9     url = "http://" + plc_www['host'] + "/"
10 elif plc_www['port'] == '443':
11     url = "https://" + plc_www['host'] + "/"
12 else:
13     url = "http://" + plc_www['host'] + ":" + plc_www['port'] + "/"
14
15 SetMyPLCURL(url)
16
17 site = { 'site_id': 1,
18          'name': plc['name'] + " Central",
19          'abbreviated_name': plc['name'],
20          'login_base': plc['slice_prefix'],
21          'is_public': False,
22          'url': url,
23          'max_slices': 100 }
24
25 sites = GetSites([site['site_id']])
26 if not sites:
27     site_id = AddSite(site['name'], site['abbreviated_name'], site['login_base'], site)
28     if site_id != site['site_id']:
29         DeleteSite(site_id)
30         raise Exception, "Someone deleted the \"%s\" site from the database!" % \
31               site['name']
32     sites = [site]
33
34 # Must call UpdateSite() even after AddSite() to update max_slices
35 site_id = sites[0]['site_id']
36 UpdateSite(site_id, site)
37
38 # The default administrator account must be associated with a site
39 # in order to login.
40 AddPersonToSite(admin['person_id'], site['site_id'])
41 SetPersonPrimarySite(admin['person_id'], site['site_id'])
42
43 # Grant admin and PI roles to the default administrator account
44 AddRoleToPerson(10, admin['person_id'])
45 AddRoleToPerson(20, admin['person_id'])
46
47 # Associate root ssh key with the default administrator
48 keyfile=plc['root_ssh_key_pub']
49 person = GetPersons(admin['person_id'])[0]
50 keys = GetKeys(person['key_ids'])
51 if os.path.exists(keyfile):
52     sshkeyfp = file(keyfile,"r")
53     sshkey = sshkeyfp.read()
54     sshkeyfp.close()
55
56     found=False
57     for key in keys:
58         if key['key_type']=='ssh':
59             if key['key'] == sshkey:
60                 found=True
61             else:
62                 # should we delete other keys?
63                 pass
64     if not found:
65         key_id = AddPersonKey(admin['person_id'],{'key_type':'ssh','key':sshkey})
66 else:
67     if len(keys)==0:
68         print "WARNING: default administrator does not have an ssh key"
69         print "and the default ssh root pub key (%s) file does not exist." % keyfile