Setting tag plcapi-5.4-2
[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     sshkeyfp = file(keyfile,"r")
51     sshkey = sshkeyfp.read()
52     sshkeyfp.close()
53
54     found=False
55     for key in keys:
56         if key['key_type']=='ssh':
57             if key['key'] == sshkey:
58                 found=True
59             else:
60                 # should we delete other keys?
61                 pass
62     if not found:
63         key_id = AddPersonKey(the_admin_id,{'key_type':'ssh','key':sshkey})
64 else:
65     if len(keys)==0:
66         print "WARNING: default administrator does not have an ssh key"
67         print "and the default ssh root pub key (%s) file does not exist." % keyfile