From 620e910477b122d89a146ae79b877e2fc0c8859b Mon Sep 17 00:00:00 2001 From: Graham Dumpleton Date: Thu, 16 Mar 2017 17:12:13 +1100 Subject: [PATCH] Add readiness and liveness probe scripts. --- .s2i/environment | 1 + requirements.txt | 1 + scripts/alive | 30 ++++++++++++++++++++++++++++++ scripts/check-database.py | 37 +++++++++++++++++++++++++++++++++++++ scripts/check-server.py | 7 +++++++ scripts/ready | 30 ++++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+) create mode 100755 scripts/alive create mode 100644 scripts/check-database.py create mode 100644 scripts/check-server.py create mode 100755 scripts/ready diff --git a/.s2i/environment b/.s2i/environment index 76be426..125c848 100644 --- a/.s2i/environment +++ b/.s2i/environment @@ -2,3 +2,4 @@ S2I_SCRIPTS_PATH=/usr/libexec/s2i S2I_BASH_ENV=/opt/app-root/etc/scl_enable DISABLE_COLLECTSTATIC=1 DISABLE_MIGRATE=1 +PYTHONUNBUFFERED=1 diff --git a/requirements.txt b/requirements.txt index f836030..8c13019 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ dj_database_url mod_wsgi Pillow psycopg2 +requests diff --git a/scripts/alive b/scripts/alive new file mode 100755 index 0000000..bdb0400 --- /dev/null +++ b/scripts/alive @@ -0,0 +1,30 @@ +#!/bin/bash + +set -eo pipefail + +HERE=`dirname $0` + +ready() { + echo " -----> Starting liveness script." + + # Check web server. + + echo " -----> Checking server is working." + + python $HERE/check-server.py + + # Check database. + + echo " -----> Checking database is working." + + if [ x"$DATABASE_URL" != x"" ]; then + (cat - | python manage.py shell --plain) << ! +import runpy +_ = runpy.run_path('$HERE/check-database.py') +! + fi + + echo " -----> Stopping liveness script." +} + +ready 2>&1 | tee -a /proc/1/fd/1 diff --git a/scripts/check-database.py b/scripts/check-database.py new file mode 100644 index 0000000..9424c48 --- /dev/null +++ b/scripts/check-database.py @@ -0,0 +1,37 @@ +from __future__ import print_function + +import sys +import time + +import django + +django.setup() + +from django.db import connection + +remaining = 600.0 +delay = 2.0 + +success = False + +while remaining > 0.0: + try: + print('Check whether database is ready...') + cursor = connection.cursor() + with cursor: + cursor.execute('SELECT 1') + cursor.fetchall() + + success = True + + break + + except Exception: + pass + + time.sleep(delay) + +if not success: + print('Failed to connect to database') + + sys.exit(1) diff --git a/scripts/check-server.py b/scripts/check-server.py new file mode 100644 index 0000000..471c085 --- /dev/null +++ b/scripts/check-server.py @@ -0,0 +1,7 @@ +import os +import requests + +hostname = os.environ['HOSTNAME'] + +response = requests.get('http://%s:8080/' % hostname) +response.raise_for_status() diff --git a/scripts/ready b/scripts/ready new file mode 100755 index 0000000..ffe9efa --- /dev/null +++ b/scripts/ready @@ -0,0 +1,30 @@ +#!/bin/bash + +set -eo pipefail + +HERE=`dirname $0` + +ready() { + echo " -----> Starting readiness script." + + # Check web server. + + echo " -----> Checking server is working." + + python $HERE/check-server.py + + # Check database. + + echo " -----> Checking database is working." + + if [ x"$DATABASE_URL" != x"" ]; then + (cat - | python manage.py shell --plain) << ! +import runpy +_ = runpy.run_path('$HERE/check-database.py') +! + fi + + echo " -----> Stopping readiness script." +} + +ready 2>&1 | tee -a /proc/1/fd/1