From 6974d9d6e13a433123d40b49a7c75d807a9f3aaf Mon Sep 17 00:00:00 2001
From: Marco Mariani <marco.mariani@nexedi.com>
Date: Fri, 21 Sep 2012 18:13:05 +0200
Subject: [PATCH] create the superuser, set his password, provide a php.ini

---
 slapos/recipe/postgres/__init__.py  | 14 +++++++++++++-
 software/maarch/php.ini.in          | 18 ++++++++++++++++++
 software/maarch/software.cfg        |  4 ++++
 stack/lapp/instance-postgres.cfg.in |  2 +-
 4 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 software/maarch/php.ini.in

diff --git a/slapos/recipe/postgres/__init__.py b/slapos/recipe/postgres/__init__.py
index 5a3a19407..005616717 100644
--- a/slapos/recipe/postgres/__init__.py
+++ b/slapos/recipe/postgres/__init__.py
@@ -25,6 +25,7 @@
 #
 ##############################################################################
 
+import md5
 import os
 import sys
 import subprocess
@@ -74,6 +75,7 @@ class Recipe(GenericBaseRecipe):
         pgdata = self.options['pgdata-directory']
 
         with open(os.path.join(pgdata, 'postgresql.conf'), 'wb') as cfg:
+            # XXX TODO listen_addresses
             cfg.write(textwrap.dedent("""\
                     logging_collector = on
                     log_rotation_size = 50MB
@@ -118,7 +120,17 @@ class Recipe(GenericBaseRecipe):
                                   '-D', pgdata,
                                   'postgres',
                                   ], stdin=subprocess.PIPE)
-            p.communicate('CREATE DATABASE %s\n' % self.options['dbname'])
+            password = 'insecure'
+            enc_password = md5.md5(password).hexdigest()
+
+            # to execute multiple commands, all newlines (but the last) must be preceded by backslash.
+            # see http://www.postgresql.org/docs/9.1/static/app-postgres.html
+
+            sql = '\n'.join([
+                    'CREATE DATABASE %s\\\n' % self.options['dbname'],
+                    "CREATE USER '%s' PASSWORD '%s' SUPERUSER'\n" % (self.options['user'], enc_password),
+                ])
+            p.communicate(sql)
         except subprocess.CalledProcessError:
             raise UserError('Could not create database %s' % pgdata)
 
diff --git a/software/maarch/php.ini.in b/software/maarch/php.ini.in
new file mode 100644
index 000000000..de62bb83d
--- /dev/null
+++ b/software/maarch/php.ini.in
@@ -0,0 +1,18 @@
+[PHP]
+engine = On
+safe_mode = Off
+expose_php = Off
+error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED
+display_errors = On
+display_startup_errors = Off
+log_errors = On
+log_errors_max_len = 1024
+ignore_repeated_errors = Off
+ignore_repeated_source = Off
+session.save_path = "%(tmp_directory)s"
+session.auto_start = 0
+date.timezone = Europe/Paris
+file_uploads = On
+upload_max_filesize = 8M
+post_max_size = 8M
+magic_quotes_gpc=Off
diff --git a/software/maarch/software.cfg b/software/maarch/software.cfg
index bf9733046..46f0d0061 100644
--- a/software/maarch/software.cfg
+++ b/software/maarch/software.cfg
@@ -41,3 +41,7 @@ install_cmd =
     ${apache-php-postgres:location}/bin/pear install maarch/CLITools-0.3.1
     ${apache-php-postgres:location}/bin/pear install MIME_Type
 
+
+[apache-php-postgres]
+php-ini-dir = ${buildout:directory}
+
diff --git a/stack/lapp/instance-postgres.cfg.in b/stack/lapp/instance-postgres.cfg.in
index b9b3429ef..402f6acbf 100644
--- a/stack/lapp/instance-postgres.cfg.in
+++ b/stack/lapp/instance-postgres.cfg.in
@@ -43,7 +43,7 @@ recipe = slapos.cookbook:postgres
 
 # Options
 ipv6_host = $${instance-parameters:ipv6}
-user = user
+user = postgres
 port = 5432
 dbname = db
 # pgdata_directory is created by initdb, and should not exist beforehand.
-- 
2.30.9