get root ssh pub key path from configuration
[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
61 # Associate root ssh key with the default administrator
62 keyfile=plc['root_ssh_key_pub']
63 person = GetPersons(admin['person_id'])[0]
64 keys = GetKeys(person['key_ids'])
65 if os.path.exists(keyfile):
66     sshkeyfp = file(keyfile,"r")
67     sshkey = sshkeyfp.read()
68     sshkeyfp.close()
69
70     found=False
71     for key in keys:
72         if key['key_type']=='ssh':
73             if key['key'] == sshkey:
74                 found=True
75             else:
76                 # should we delete other keys?
77                 pass
78     if not found:
79         key_id = AddPersonKey(admin['person_id'],{'key_type':'ssh','key':sshkey})
80 else:
81     if len(keys)==0:
82         print "WARNING: default administrator does not have an ssh key"
83         print "and the default ssh root pub key (%s) file does not exist."