Commit 89f369e8 authored by Jérome Perrin's avatar Jérome Perrin

recipe/postgres: support regenerating config files

If for example the ip changed, this recipe needs to re-generate
configuration files.
parent 4cf4dfe6
...@@ -87,25 +87,25 @@ class Recipe(GenericBaseRecipe): ...@@ -87,25 +87,25 @@ class Recipe(GenericBaseRecipe):
def install(self): def install(self):
pgdata = self.options['pgdata-directory'] pgdata = self.options['pgdata-directory']
# if the pgdata already exists, skip all steps, we don't need to do anything. paths = []
# if the pgdata already exists, we don't need to recreate databases.
if not os.path.exists(pgdata): if not os.path.exists(pgdata):
try: try:
self.createCluster() self.createCluster()
self.createConfig() paths.extend(self.createConfig())
self.createDatabase() self.createDatabase()
self.updateSuperuser() self.updateSuperuser()
self.createRunScript() paths.extend(self.createRunScript())
except: except:
# do not leave half-installed postgresql - else next time we # do not leave half-installed postgresql - else next time we
# run we won't update it. # run we won't update it.
shutil.rmtree(pgdata) shutil.rmtree(pgdata)
raise raise
else: else:
self.createConfig() paths.extend(self.createConfig())
self.createRunScript() paths.extend(self.createRunScript())
return [] return paths
update = install update = install
...@@ -142,7 +142,8 @@ class Recipe(GenericBaseRecipe): ...@@ -142,7 +142,8 @@ class Recipe(GenericBaseRecipe):
ipv4 = self.options['ipv4'].splitlines() ipv4 = self.options['ipv4'].splitlines()
ipv6 = self.options['ipv6'].splitlines() ipv6 = self.options['ipv6'].splitlines()
with open(os.path.join(pgdata, 'postgresql.conf'), 'wb') as cfg: postgres_conf = os.path.join(pgdata, 'postgresql.conf')
with open(postgres_conf, 'wb') as cfg:
cfg.write(textwrap.dedent("""\ cfg.write(textwrap.dedent("""\
listen_addresses = '%s' listen_addresses = '%s'
logging_collector = on logging_collector = on
...@@ -163,7 +164,8 @@ class Recipe(GenericBaseRecipe): ...@@ -163,7 +164,8 @@ class Recipe(GenericBaseRecipe):
pgdata, pgdata,
))) )))
with open(os.path.join(pgdata, 'pg_hba.conf'), 'wb') as cfg: pg_hba_conf = os.path.join(pgdata, 'pg_hba.conf')
with open(pg_hba_conf, 'wb') as cfg:
# see http://www.postgresql.org/docs/9.2/static/auth-pg-hba-conf.html # see http://www.postgresql.org/docs/9.2/static/auth-pg-hba-conf.html
cfg_lines = [ cfg_lines = [
...@@ -184,7 +186,7 @@ class Recipe(GenericBaseRecipe): ...@@ -184,7 +186,7 @@ class Recipe(GenericBaseRecipe):
cfg_lines.append('host all all %s/%s md5' % (ip, ipv6_netmask_bits)) cfg_lines.append('host all all %s/%s md5' % (ip, ipv6_netmask_bits))
cfg.write('\n'.join(cfg_lines)) cfg.write('\n'.join(cfg_lines))
return postgres_conf, pg_hba_conf
def createDatabase(self): def createDatabase(self):
self.runPostgresCommand(cmd='CREATE DATABASE "%s"' % self.options['dbname']) self.runPostgresCommand(cmd='CREATE DATABASE "%s"' % self.options['dbname'])
...@@ -242,6 +244,6 @@ class Recipe(GenericBaseRecipe): ...@@ -242,6 +244,6 @@ class Recipe(GenericBaseRecipe):
-D %(pgdata-directory)s -D %(pgdata-directory)s
""" % self.options) """ % self.options)
name = os.path.join(self.options['services'], 'postgres-start') name = os.path.join(self.options['services'], 'postgres-start')
self.createExecutable(name, content=content) return [self.createExecutable(name, content=content)]
...@@ -37,8 +37,13 @@ class PostgresTest(unittest.TestCase): ...@@ -37,8 +37,13 @@ class PostgresTest(unittest.TestCase):
def test_install(self): def test_install(self):
installed = self.recipe.install() installed = self.recipe.install()
self.assertEqual(installed, [])
self.assertIn('postgresql.conf', os.listdir(self.pgdata_directory)) self.assertIn('postgresql.conf', os.listdir(self.pgdata_directory))
self.assertIn('pg_hba.conf', os.listdir(self.pgdata_directory)) self.assertIn('pg_hba.conf', os.listdir(self.pgdata_directory))
self.assertIn('postgres-start', os.listdir(self.services_directory)) self.assertIn('postgres-start', os.listdir(self.services_directory))
self.assertEqual(
sorted(installed),
sorted([
os.path.join(self.pgdata_directory, 'postgresql.conf'),
os.path.join(self.pgdata_directory, 'pg_hba.conf'),
os.path.join(self.services_directory, 'postgres-start')]))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment