Commit 701bbcab authored by Boxiang Sun's avatar Boxiang Sun

Postgres improvement

See merge request nexedi/slapos!1402
parents 30ab77b9 939281b4
...@@ -194,6 +194,28 @@ class Recipe(GenericBaseRecipe): ...@@ -194,6 +194,28 @@ class Recipe(GenericBaseRecipe):
def createDatabase(self): def createDatabase(self):
self.runPostgresCommand(cmd='CREATE DATABASE "%s"' % self.options['dbname']) self.runPostgresCommand(cmd='CREATE DATABASE "%s"' % self.options['dbname'])
def isPosgresServerRunning(self):
pgdata = self.options['pgdata-directory']
postmaster_pid_file = os.path.join(pgdata, 'postmaster.pid')
if os.path.exists(postmaster_pid_file):
pg_ctl_binary = os.path.join(self.options['bin'], 'pg_ctl')
self.check_exists(pg_ctl_binary)
# Check the postgres is running or not
# if not, delete the ppostmaster.pid and run it again
try:
output1 = subprocess.check_output([pg_ctl_binary, 'status', '-D', pgdata], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
if e.returncode == 3:
# If the server is not running, pg_ctl returns an exit status of 3
# see https://www.postgresql.org/docs/current/app-pg-ctl.html
os.remove(postmaster_pid_file)
return False
else:
raise
return True
else:
return False
def updateSuperuser(self): def updateSuperuser(self):
"""\ """\
...@@ -211,7 +233,7 @@ class Recipe(GenericBaseRecipe): ...@@ -211,7 +233,7 @@ class Recipe(GenericBaseRecipe):
change_password_query = """ALTER USER "%s" ENCRYPTED PASSWORD '%s'""" % (user, enc_password) change_password_query = """ALTER USER "%s" ENCRYPTED PASSWORD '%s'""" % (user, enc_password)
pgdata = self.options['pgdata-directory'] pgdata = self.options['pgdata-directory']
if os.path.exists(os.path.join(pgdata, 'postmaster.pid')): if self.isPosgresServerRunning():
psql_binary = os.path.join(self.options['bin'], 'psql') psql_binary = os.path.join(self.options['bin'], 'psql')
# connect to a running postgres deamon # connect to a running postgres deamon
p = subprocess.Popen([ p = subprocess.Popen([
......
...@@ -90,6 +90,26 @@ class PostgresTest(unittest.TestCase): ...@@ -90,6 +90,26 @@ class PostgresTest(unittest.TestCase):
self.assertEqual(cursor.fetchone(), (2,)) self.assertEqual(cursor.fetchone(), (2,))
cnx.close() cnx.close()
def test_stale_pid_file_does_not_prevent_install(self):
self.recipe.install()
# Malformed postmaster.pid file should not prevent the service running
pgdata_directory = os.path.join(self.pgdata_directory, 'pgdata')
postmaster_pid_file =os.path.join(pgdata_directory, 'postmaster.pid')
content = '''\
1074626
/srv/slapgrid/slappart33/srv/runner/instance/slappart0/srv/postgresql
1686241354
5432
/srv/slapgrid/slappart33/srv/runner/instance/slappart0/srv/postgresql
10.0.156.45
5432001 1179658
ready'''
with open(postmaster_pid_file, 'w') as file:
file.write(content)
self.recipe.install()
def test_update_password(self): def test_update_password(self):
self.recipe.install() self.recipe.install()
self.start_postgres_server() self.start_postgres_server()
......
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