Skip to content

Commit

Permalink
Merge pull request #538 from mdellweg/rework_wait_for_db
Browse files Browse the repository at this point in the history
Use django to check db connection
  • Loading branch information
mdellweg authored Sep 1, 2023
2 parents 65346a9 + 6b8d59d commit e8d49aa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 43 deletions.
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

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
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

0 comments on commit e8d49aa

Please sign in to comment.