From a2494d4275c6bbe578156601b0fda0cf203e7af7 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 9 Sep 2021 15:53:32 +0100 Subject: Init metricity using docker-compose init volume --- manage.py | 37 ------------------------------------- 1 file changed, 37 deletions(-) (limited to 'manage.py') diff --git a/manage.py b/manage.py index 648d6635..d8258281 100755 --- a/manage.py +++ b/manage.py @@ -141,44 +141,8 @@ class SiteManager: name="pythondiscord.local:8000" ) - @staticmethod - def run_metricity_init() -> None: - """ - Initialise metricity relations and populate with some testing data. - - This is done at run time since other projects, like Python bot, - rely on the site initialising it's own db, since they do not have - access to the init.sql file to mount a docker-compose volume. - """ - import psycopg2 - from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT - - print("Initialising metricity.") - - db_url_parts = SiteManager.parse_db_url(os.environ["DATABASE_URL"]) - conn = psycopg2.connect( - host=db_url_parts.hostname, - port=db_url_parts.port, - user=db_url_parts.username, - password=db_url_parts.password, - database=db_url_parts.path[1:] - ) - # Required to create a db from `cursor.execute()` - conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) - - with conn.cursor() as cursor, open("postgres/init.sql", encoding="utf-8") as f: - cursor.execute( - f.read(), - ("metricity", db_url_parts.username, db_url_parts.password) - ) - conn.close() - def prepare_server(self) -> None: """Perform preparation tasks before running the server.""" - self.wait_for_postgres() - if self.debug: - self.run_metricity_init() - django.setup() print("Applying migrations.") @@ -236,7 +200,6 @@ def main() -> None: # Always run metricity init when in CI, indicated by the CI env var if os.environ.get("CI", "false").lower() == "true": SiteManager.wait_for_postgres() - SiteManager.run_metricity_init() # Use the custom site manager for launching the server if len(sys.argv) > 1 and sys.argv[1] == "run": -- cgit v1.2.3 From c9aed176706852196fec0fc65ee7552d511316f2 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 9 Sep 2021 15:54:17 +0100 Subject: Move psql health check to docker compose file --- docker-compose.yml | 8 +++++++- manage.py | 52 ---------------------------------------------------- 2 files changed, 7 insertions(+), 53 deletions(-) (limited to 'manage.py') diff --git a/docker-compose.yml b/docker-compose.yml index 05867a46..eb987624 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,6 +18,11 @@ services: POSTGRES_DB: pysite POSTGRES_PASSWORD: pysite POSTGRES_USER: pysite + healthcheck: + test: ["CMD-SHELL", "pg_isready -U pysite"] + interval: 2s + timeout: 1s + retries: 5 volumes: - ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql @@ -35,7 +40,8 @@ services: ports: - "127.0.0.1:8000:8000" depends_on: - - postgres + postgres: + condition: service_healthy tty: true volumes: - .:/app:ro diff --git a/manage.py b/manage.py index d8258281..578f4748 100755 --- a/manage.py +++ b/manage.py @@ -1,9 +1,6 @@ #!/usr/bin/env python import os -import socket import sys -import time -from urllib.parse import SplitResult, urlsplit import django from django.contrib.auth import get_user_model @@ -54,21 +51,6 @@ class SiteManager: os.environ.setdefault("DEBUG", "true") print("Starting in debug mode.") - @staticmethod - def parse_db_url(db_url: str) -> SplitResult: - """Validate and split the given databse url.""" - db_url_parts = urlsplit(db_url) - if not all(( - db_url_parts.hostname, - db_url_parts.username, - db_url_parts.password, - db_url_parts.path - )): - raise ValueError( - "The DATABASE_URL environment variable is not a valid PostgreSQL database URL." - ) - return db_url_parts - @staticmethod def create_superuser() -> None: """Create a default django admin super user in development environments.""" @@ -98,36 +80,6 @@ class SiteManager: else: print(f"Existing bot token found: {token}") - @staticmethod - def wait_for_postgres() -> None: - """Wait for the PostgreSQL database specified in DATABASE_URL.""" - print("Waiting for PostgreSQL database.") - - # Get database URL based on environmental variable passed in compose - database_url_parts = SiteManager.parse_db_url(os.environ["DATABASE_URL"]) - domain = database_url_parts.hostname - # Port may be omitted, 5432 is the default psql port - port = database_url_parts.port or 5432 - - # Attempt to connect to the database socket - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - attempts_left = 10 - while attempts_left: - try: - # Ignore 'incomplete startup packet' - s.connect((domain, port)) - s.shutdown(socket.SHUT_RDWR) - print("Database is ready.") - break - except socket.error: - attempts_left -= 1 - print("Not ready yet, retrying.") - time.sleep(0.5) - else: - print("Database could not be found, exiting.") - sys.exit(1) - @staticmethod def set_dev_site_name() -> None: """Set the development site domain in admin from default example.""" @@ -197,10 +149,6 @@ class SiteManager: def main() -> None: """Entry point for Django management script.""" - # Always run metricity init when in CI, indicated by the CI env var - if os.environ.get("CI", "false").lower() == "true": - SiteManager.wait_for_postgres() - # Use the custom site manager for launching the server if len(sys.argv) > 1 and sys.argv[1] == "run": SiteManager(sys.argv).run_server() -- cgit v1.2.3