From 035226864a87317a1095acbf13165105894db6a9 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Thu, 26 Jan 2017 20:53:41 -0800 Subject: [PATCH] various updates --- .../roles/mezzanine/files/entrypoint.sh | 2 +- .../roles/mezzanine/files/wait_for_db.py | 31 +++++++++++++++++++ .../roles/mezzanine/files/wait_on_postgres.py | 29 ----------------- ch13/ansible/roles/mezzanine/tasks/main.yml | 2 +- .../mezzanine_example/local_settings.py | 6 ++-- 5 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 ch13/ansible/roles/mezzanine/files/wait_for_db.py delete mode 100644 ch13/ansible/roles/mezzanine/files/wait_on_postgres.py diff --git a/ch13/ansible/roles/mezzanine/files/entrypoint.sh b/ch13/ansible/roles/mezzanine/files/entrypoint.sh index 8e1b735..96fec6c 100644 --- a/ch13/ansible/roles/mezzanine/files/entrypoint.sh +++ b/ch13/ansible/roles/mezzanine/files/entrypoint.sh @@ -21,7 +21,7 @@ set -x if [[ $@ == *"gunicorn"* || $@ == *"runserver"* ]]; then if [[ -f $MANAGE ]]; then - $BINDIR/wait_on_postgres.py + $BINDIR/wait_for_db.py if [[ $? == 0 ]]; then $MANAGE migrate --noinput $MANAGE collectstatic --noinput diff --git a/ch13/ansible/roles/mezzanine/files/wait_for_db.py b/ch13/ansible/roles/mezzanine/files/wait_for_db.py new file mode 100644 index 0000000..272df6a --- /dev/null +++ b/ch13/ansible/roles/mezzanine/files/wait_for_db.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +import os +import psycopg2 +import time + +dbname = os.environ.get("DATABASE_NAME", ""), +user = os.environ.get("DATABASE_USER", ""), +password = os.environ.get("DATABASE_PASSWORD", ""), +host = os.environ.get("DATABASE_HOST", "postgres"), +port = int(os.environ.get("DATABASE_PORT", "5432")) + +attempts = 0 +max_attempts = 20 + +while True: + try: + conn = psycopg2.connect(dbname=dbname, user=user, password=password, host=host, port=port) + # If we reach here, we're done + print("Connected to database {}".format(host)) + sys.exit(0) + except Exception as e: + attempts += 1 + if attempts > max_attempts: + print("Unable to connect to database") + print(e) + sys.exit(1) + + time.sleep(3) + + + diff --git a/ch13/ansible/roles/mezzanine/files/wait_on_postgres.py b/ch13/ansible/roles/mezzanine/files/wait_on_postgres.py deleted file mode 100644 index 4c7b953..0000000 --- a/ch13/ansible/roles/mezzanine/files/wait_on_postgres.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python -# Code copied from: https://github.com/ansible/django-gulp-nginx/ -# - -import sys -import socket -import time - -if __name__ == '__main__': - - postgres_is_alive = False - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - tries = 0 - print ("Waiting on postgresql to start...") - while not postgres_is_alive and tries < 20: - tries += 1 - try: - s.connect(('postgresql', 5432)) - except socket.error: - time.sleep(3) - else: - postgres_is_alive = True - - if postgres_is_alive: - print ("Postgresql started!") - sys.exit(0) - else: - print ("Unable to reach postgresql on port 5432") - sys.exit(1) diff --git a/ch13/ansible/roles/mezzanine/tasks/main.yml b/ch13/ansible/roles/mezzanine/tasks/main.yml index be79a75..9f0b80a 100644 --- a/ch13/ansible/roles/mezzanine/tasks/main.yml +++ b/ch13/ansible/roles/mezzanine/tasks/main.yml @@ -33,4 +33,4 @@ - entrypoint.sh - setsite.py - setadmin.py - - wait_on_postgres.py + - wait_for_db.py diff --git a/ch13/mezzanine_example/mezzanine_example/local_settings.py b/ch13/mezzanine_example/mezzanine_example/local_settings.py index de131a1..43b64a2 100644 --- a/ch13/mezzanine_example/mezzanine_example/local_settings.py +++ b/ch13/mezzanine_example/mezzanine_example/local_settings.py @@ -3,7 +3,7 @@ import os SECRET_KEY = os.environ.get("SECRET_KEY", "") NEVERCACHE_KEY = os.environ.get("NEVERCACHE_KEY", "") -ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS", "") +ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS", "['*']") DATABASES = { "default": { @@ -16,9 +16,9 @@ DATABASES = { # Not used with sqlite3. "PASSWORD": os.environ.get("DATABASE_PASSWORD", ""), # Set to empty string for localhost. Not used with sqlite3. - "HOST": os.environ.get("DATABASE_HOST", ""), + "HOST": os.environ.get("DATABASE_HOST", "postgres"), # Set to empty string for default. Not used with sqlite3. - "PORT": os.environ.get("DATABASE_PORT", "") + "PORT": os.environ.get("DATABASE_PORT", "5432") } } -- 2.44.0