Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
slapos
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
slapos
Commits
701bbcab
Commit
701bbcab
authored
Jun 15, 2023
by
Boxiang Sun
Browse files
Options
Browse Files
Download
Plain Diff
Postgres improvement
See merge request
nexedi/slapos!1402
parents
30ab77b9
939281b4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
1 deletion
+43
-1
slapos/recipe/postgres.py
slapos/recipe/postgres.py
+23
-1
slapos/test/recipe/test_postgres.py
slapos/test/recipe/test_postgres.py
+20
-0
No files found.
slapos/recipe/postgres.py
View file @
701bbcab
...
...
@@ -194,6 +194,28 @@ class Recipe(GenericBaseRecipe):
def
createDatabase
(
self
):
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
):
"""
\
...
...
@@ -211,7 +233,7 @@ class Recipe(GenericBaseRecipe):
change_password_query
=
"""ALTER USER "%s" ENCRYPTED PASSWORD '%s'"""
%
(
user
,
enc_password
)
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'
)
# connect to a running postgres deamon
p
=
subprocess
.
Popen
([
...
...
slapos/test/recipe/test_postgres.py
View file @
701bbcab
...
...
@@ -90,6 +90,26 @@ class PostgresTest(unittest.TestCase):
self
.
assertEqual
(
cursor
.
fetchone
(),
(
2
,))
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
):
self
.
recipe
.
install
()
self
.
start_postgres_server
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment