Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use django to check db connection #538

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions images/assets/wait_on_postgres.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
#!/usr/bin/env python3

import os
import socket
import sys
import time
from django.db import connection, utils
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely good idea!


if __name__ == "__main__":

postgres_is_alive = False
s4 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s6 = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
tries = 0
print("Waiting on postgresql to start...")
while not postgres_is_alive and tries < 100:
tries += 1
pg_port = 5432
for dummy in range(100):
try:
env_port = os.environ.get("POSTGRES_SERVICE_PORT", "5432")
pg_port = int(env_port)
except ValueError:
pass
try:
print("Checking postgres host %s" % os.environ["POSTGRES_SERVICE_HOST"])
print("Checking postgres port %s" % os.environ["POSTGRES_SERVICE_PORT"])
if tries % 2:
s4.connect((os.environ["POSTGRES_SERVICE_HOST"], pg_port))
else:
s6.connect((os.environ["POSTGRES_SERVICE_HOST"], pg_port))
except socket.error:
connection.ensure_connection()
break
except utils.OperationalError:
time.sleep(3)
else:
postgres_is_alive = True

if postgres_is_alive:
print("Postgres started!")
sys.exit(0)
else:
print("Unable to reach postgres on port %s" % os.environ["POSTGRES_SERVICE_PORT"])
print("Unable to reach postgres.")
sys.exit(1)

print("Postgres started.")
sys.exit(0)
8 changes: 0 additions & 8 deletions images/compose/compose.folders.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ services:
- "./assets/certs:/etc/pulp/certs:z"
- "../../pulp_storage:/var/lib/pulp:z"
environment:
POSTGRES_SERVICE_PORT: 5432
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see these are not necessary because they're in the settings.py file. 👍🏻

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it was only used by the old wait_on_postgres.

POSTGRES_SERVICE_HOST: postgres
PULP_ADMIN_PASSWORD: password

pulp_content:
Expand All @@ -70,9 +68,6 @@ services:
- "./assets/settings.py:/etc/pulp/settings.py:z"
- "./assets/certs:/etc/pulp/certs:z"
- "../../pulp_storage:/var/lib/pulp:z"
environment:
POSTGRES_SERVICE_PORT: 5432
POSTGRES_SERVICE_HOST: postgres

pulp_worker:
image: "pulp/pulp-minimal:latest"
Expand All @@ -88,9 +83,6 @@ services:
- "./assets/settings.py:/etc/pulp/settings.py:z"
- "./assets/certs:/etc/pulp/certs:z"
- "../../pulp_storage:/var/lib/pulp:z"
environment:
POSTGRES_SERVICE_PORT: 5432
POSTGRES_SERVICE_HOST: postgres

volumes:
redis_data:
Expand Down
8 changes: 0 additions & 8 deletions images/compose/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ services:
- "./assets/certs:/etc/pulp/certs:z"
- "pulp:/var/lib/pulp"
environment:
POSTGRES_SERVICE_PORT: 5432
POSTGRES_SERVICE_HOST: postgres
PULP_ADMIN_PASSWORD: password
restart: always

Expand All @@ -79,9 +77,6 @@ services:
- "./assets/settings.py:/etc/pulp/settings.py:z"
- "./assets/certs:/etc/pulp/certs:z"
- "pulp:/var/lib/pulp"
environment:
POSTGRES_SERVICE_PORT: 5432
POSTGRES_SERVICE_HOST: postgres
restart: always

pulp_worker:
Expand All @@ -98,9 +93,6 @@ services:
- "./assets/settings.py:/etc/pulp/settings.py:z"
- "./assets/certs:/etc/pulp/certs:z"
- "pulp:/var/lib/pulp"
environment:
POSTGRES_SERVICE_PORT: 5432
POSTGRES_SERVICE_HOST: postgres
restart: always

volumes:
Expand Down
Loading