add wait_on_postgres.py
authorLorin Hochstein <lhochstein@netflix.com>
Tue, 24 Jan 2017 06:29:20 +0000 (22:29 -0800)
committerLorin Hochstein <lhochstein@netflix.com>
Tue, 24 Jan 2017 06:29:20 +0000 (22:29 -0800)
ch13/ansible/roles/mezzanine/files/wait_on_postgres.py [new file with mode: 0644]

diff --git a/ch13/ansible/roles/mezzanine/files/wait_on_postgres.py b/ch13/ansible/roles/mezzanine/files/wait_on_postgres.py
new file mode 100644 (file)
index 0000000..4c7b953
--- /dev/null
@@ -0,0 +1,29 @@
+#!/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)