modularize db-config
[myplc.git] / db-config.d / 01-init
diff --git a/db-config.d/01-init b/db-config.d/01-init
new file mode 100644 (file)
index 0000000..7fd6bdf
--- /dev/null
@@ -0,0 +1,60 @@
+# Create/update the default administrator account (should be person_id 2).
+
+admin = { 'person_id': 2,
+          'first_name': "Default",
+          'last_name': "Administrator",
+          'email': plc['root_user'],
+          'password': plc['root_password'] }
+persons = GetPersons([admin['person_id']])
+if not persons:
+    person_id = AddPerson(admin)
+    if person_id != admin['person_id']:
+        # Huh? Someone deleted the account manually from the database.
+        DeletePerson(person_id)
+        raise Exception, "Someone deleted the \"%s %s\" account from the database!" % \
+              (admin['first_name'], admin['last_name'])
+    UpdatePerson(person_id, { 'enabled': True })
+else:
+    person_id = persons[0]['person_id']
+    UpdatePerson(person_id, admin)
+
+# Create/update the default site (should be site_id 1)
+if plc_www['port'] == '80':
+    url = "http://" + plc_www['host'] + "/"
+elif plc_www['port'] == '443':
+    url = "https://" + plc_www['host'] + "/"
+else:
+    url = "http://" + plc_www['host'] + ":" + plc_www['port'] + "/"
+
+SetMyPLCURL(url)
+
+site = { 'site_id': 1,
+         'name': plc['name'] + " Central",
+         'abbreviated_name': plc['name'],
+         'login_base': plc['slice_prefix'],
+         'is_public': False,
+         'url': url,
+         'max_slices': 100 }
+
+sites = GetSites([site['site_id']])
+if not sites:
+    site_id = AddSite(site['name'], site['abbreviated_name'], site['login_base'], site)
+    if site_id != site['site_id']:
+        DeleteSite(site_id)
+        raise Exception, "Someone deleted the \"%s\" site from the database!" % \
+              site['name']
+    sites = [site]
+
+# Must call UpdateSite() even after AddSite() to update max_slices
+site_id = sites[0]['site_id']
+UpdateSite(site_id, site)
+
+# The default administrator account must be associated with a site
+# in order to login.
+AddPersonToSite(admin['person_id'], site['site_id'])
+SetPersonPrimarySite(admin['person_id'], site['site_id'])
+
+# Grant admin and PI roles to the default administrator account
+AddRoleToPerson(10, admin['person_id'])
+AddRoleToPerson(20, admin['person_id'])
+