From 034b5ec7e4b50af498aea0166ade7667fef85eb3 Mon Sep 17 00:00:00 2001
From: Marco Mariani <marco.mariani@nexedi.com>
Date: Thu, 20 Sep 2012 14:51:01 +0200
Subject: [PATCH] don't do anything if data directory exists; fix ipv6 url

---
 slapos/recipe/postgres/__init__.py | 31 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 16 deletions(-)

diff --git a/slapos/recipe/postgres/__init__.py b/slapos/recipe/postgres/__init__.py
index 004981600..40b44d36c 100644
--- a/slapos/recipe/postgres/__init__.py
+++ b/slapos/recipe/postgres/__init__.py
@@ -38,17 +38,18 @@ class Recipe(GenericBaseRecipe):
 
     def _options(self, options):
         options['password'] = self.generatePassword()
-        options['url'] = 'postgresql://%(user)s:%(password)s/%(host)s:%(port)s/%(dbname)s' % dict(options, host=options['ipv6_host'].pop())
+        options['url'] = 'postgresql://%(user)s:%(password)s/[%(host)s]:%(port)s/%(dbname)s' % dict(options, host=options['ipv6_host'].pop())
 
 
     def install(self):
-        self.createCluster()
-        self.createConfig()
-        self.createDatabase()
-        self.createRunScript()
-
         pgdata = self.options['pgdata-directory']
 
+        if not os.path.exists(pgdata):
+            self.createCluster()
+            self.createConfig()
+            self.createDatabase()
+            self.createRunScript()
+
         return [
                 os.path.join(pgdata, 'postgresql.conf')
                 ]
@@ -57,18 +58,16 @@ class Recipe(GenericBaseRecipe):
     def createCluster(self):
         initdb_binary = os.path.join(self.options['bin'], 'initdb')
 
-
         pgdata = self.options['pgdata-directory']
 
-        if not os.path.exists(pgdata):
-            try:
-                subprocess.check_call([initdb_binary,
-                                       '-D', pgdata,
-                                       '-A', 'ident',
-                                       '-E', 'UTF8',
-                                       ])
-            except subprocess.CalledProcessError:
-                raise UserError('Could not create cluster directory in %s' % pgdata)
+        try:
+            subprocess.check_call([initdb_binary,
+                                   '-D', pgdata,
+                                   '-A', 'ident',
+                                   '-E', 'UTF8',
+                                   ])
+        except subprocess.CalledProcessError:
+            raise UserError('Could not create cluster directory in %s' % pgdata)
 
 
     def createConfig(self):
-- 
2.30.9