From 0dbce8ec668bc8a82eff525283afca93574f64e4 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Mon, 23 Jan 2017 22:29:20 -0800 Subject: [PATCH] add wait_on_postgres.py --- .../roles/mezzanine/files/wait_on_postgres.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ch13/ansible/roles/mezzanine/files/wait_on_postgres.py 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 index 0000000..4c7b953 --- /dev/null +++ b/ch13/ansible/roles/mezzanine/files/wait_on_postgres.py @@ -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) -- 2.44.0