various updates
authorLorin Hochstein <lhochstein@netflix.com>
Fri, 27 Jan 2017 04:53:41 +0000 (20:53 -0800)
committerLorin Hochstein <lhochstein@netflix.com>
Fri, 27 Jan 2017 04:53:41 +0000 (20:53 -0800)
ch13/ansible/roles/mezzanine/files/entrypoint.sh
ch13/ansible/roles/mezzanine/files/wait_for_db.py [new file with mode: 0644]
ch13/ansible/roles/mezzanine/files/wait_on_postgres.py [deleted file]
ch13/ansible/roles/mezzanine/tasks/main.yml
ch13/mezzanine_example/mezzanine_example/local_settings.py

index 8e1b735..96fec6c 100644 (file)
@@ -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 (file)
index 0000000..272df6a
--- /dev/null
@@ -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 (file)
index 4c7b953..0000000
+++ /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)
index be79a75..9f0b80a 100644 (file)
@@ -33,4 +33,4 @@
     - entrypoint.sh
     - setsite.py
     - setadmin.py
-    - wait_on_postgres.py
+    - wait_for_db.py
index de131a1..43b64a2 100644 (file)
@@ -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")
     }
 }