replace old-school formatting in Filter.py with f-strings
[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(
26         site['name'], site['abbreviated_name'], site['login_base'], site)
27     if site_id != site['site_id']:
28         DeleteSite(site_id)
29         raise Exception("Someone deleted the \"%s\" site from the database!" %
30                         site['name'])
31     sites = [site]
32
33 # Must call UpdateSite() even after AddSite() to update max_slices
34 site_id = sites[0]['site_id']
35 UpdateSite(site_id, site)
36
37 # The default administrator account must be associated with a site
38 # in order to login - see 001-admin_user
39 AddPersonToSite(the_admin_id, site['site_id'])
40 SetPersonPrimarySite(the_admin_id, site['site_id'])
41
42 # Grant admin and PI roles to the default administrator account
43 AddRoleToPerson(10, the_admin_id)
44 AddRoleToPerson(20, the_admin_id)
45
46 # Associate root ssh key with the default administrator
47 keyfile = plc['root_ssh_key_pub']
48 person = GetPersons(the_admin_id)[0]
49 keys = GetKeys(person['key_ids'])
50 if os.path.exists(keyfile):
51     with open(keyfile) as feed:
52         sshkey = feed.read()
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 not keys:
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)