-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Postgres support: Added simple PostgreSQL check if the DB is up
- Loading branch information
1 parent
6d8c8cd
commit f8aecbf
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import sys | ||
import subprocess | ||
|
||
""" | ||
<sphinx> | ||
postgres | ||
-------- | ||
Uses `pg_isready` tool to verify if PostgreSQL is up and ready to connect. | ||
Parameters: | ||
- pg_host (hostname or socket path, defaults to "localhost" which will use local unix socket, use IP address eg. 127.0.0.1 to connect via tcp) | ||
- pg_port (port, defaults to 5432) | ||
- pg_db_name (database name to connect to, defaults to "postgres") | ||
- pg_user (username, defaults to "postgres") | ||
- pg_conn_timeout (defaults to 15 which means 15 seconds) | ||
</sphinx> | ||
""" | ||
|
||
|
||
class PostgresCheck: | ||
def main(self, host: str, port: int, dbname: str, username: str, conn_timeout: int) -> tuple: | ||
command = ['pg_isready', '-h', host, '-p', str(port), '-U', username, '-t', str(conn_timeout)] | ||
|
||
if dbname: | ||
command += ['-d', dbname] | ||
|
||
try: | ||
out = subprocess.check_output(command).strip() | ||
return True, out.decode('utf-8') | ||
|
||
except subprocess.CalledProcessError as err: | ||
return False, err.output.decode('utf-8').strip() | ||
|
||
|
||
if __name__ == '__main__': | ||
app = PostgresCheck() | ||
|
||
status, message = app.main( | ||
host=os.getenv('PG_HOST', 'localhost'), | ||
port=int(os.getenv('PG_PORT', 5432)), | ||
dbname=os.getenv('PG_DB_NAME', 'postgres'), | ||
username=os.getenv('PG_USER', 'postgres'), | ||
conn_timeout=int(os.getenv('PG_CONN_TIMEOUT', 15)) | ||
) | ||
|
||
print(message) | ||
sys.exit(0 if status else 1) |