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