From d7c5d3f33933cf2e422aac95d379cc394d1138da Mon Sep 17 00:00:00 2001
From: Marco Mariani <marco.mariani@nexedi.com>
Date: Tue, 25 Sep 2012 18:26:31 +0200
Subject: [PATCH] load initial database

---
 slapos/recipe/maarch/configuration.py | 58 ++++++++++++++++++++++++---
 1 file changed, 53 insertions(+), 5 deletions(-)

diff --git a/slapos/recipe/maarch/configuration.py b/slapos/recipe/maarch/configuration.py
index 2c076f5d4..54de8f316 100644
--- a/slapos/recipe/maarch/configuration.py
+++ b/slapos/recipe/maarch/configuration.py
@@ -27,8 +27,21 @@
 
 import ConfigParser
 import lxml.etree
+import md5
 import os
 
+import lxml
+
+def temporary_hack():
+    import sys
+    develop_eggs = '/'.join(lxml.__file__.split('/')[:lxml.__file__.split('/').index('develop-eggs')+1])
+    for egg_folder in os.listdir(develop_eggs):
+        if egg_folder.startswith('psycopg2'):
+            sys.path.append(os.path.join(develop_eggs, egg_folder))
+temporary_hack() # XXX TODO provide psycopg to sys.path by other means
+
+import psycopg2
+
 from slapos.recipe.librecipe import GenericBaseRecipe
 
 def xpath_set(xml, settings):
@@ -45,17 +58,18 @@ class Recipe(GenericBaseRecipe):
 
         self.update_phpini(php_ini_path=os.path.join(self.options['php_ini_dir'], 'php.ini'))
 
-        # XXX TODO: database schema and initial data
-        # XXX TODO: database schema patch for ipv6
+        self.load_initial_db()
+
         # XXX TODO: document folder
-        # XXX TODO: admin password
+        # XXX TODO: test admin password
 
         # confirm that everything is done, the app will run without further setup
-        self.installed_lock()
+        lck_path = self.installed_lock()
 
         return [
                 apps_config_xml,
                 core_config_xml,
+                lck_path,
                 ]
 
 
@@ -64,10 +78,13 @@ class Recipe(GenericBaseRecipe):
         Create an empty file to mean the setup is completed
         """
         htdocs = self.options['htdocs']
+        lck_path = os.path.join(htdocs, 'installed.lck')
 
-        with open(os.path.join(htdocs, 'installed.lck'), 'w'):
+        with open(lck_path, 'w'):
             pass
 
+        return lck_path
+
 
     def create_apps_config_xml(self):
         options = self.options
@@ -127,3 +144,34 @@ class Recipe(GenericBaseRecipe):
             php_ini.write(fout)
 
 
+    def load_initial_db(self):
+        options = self.options
+
+        conn = psycopg2.connect(host = options['db_host'],
+                                port = options['db_port'],
+                                database = options['db_dbname'],
+                                user = options['db_username'],
+                                password = options['db_password'])
+
+        cur = conn.cursor()
+
+        htdocs = options['htdocs']
+
+        # load the schema
+        with open(os.path.join(htdocs, 'structure.sql')) as fin:
+            cur.execute(fin.read())
+
+        # patch the schema to store long addresses (ipv6)
+        cur.execute('ALTER TABLE HISTORY ALTER COLUMN remote_ip TYPE CHAR(255);')
+
+        with open(os.path.join(htdocs, 'data_mini.sql')) as fin:
+            cur.execute(fin.read())
+
+        # initial admin password
+        enc_password = md5.md5(options['db_password']).hexdigest()
+        cur.execute("UPDATE users SET password=%s WHERE user_id='superadmin';", (enc_password, ))
+
+        conn.commit()
+        cur.close()
+        conn.close()
+
-- 
2.30.9