Skip to content

Commit

Permalink
Add testing MySQL connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
nutjob4life committed Jan 16, 2025
1 parent 3de4497 commit 980fb4f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion drupal/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ENV PYTHONUNBUFFERED 1

WORKDIR /app

RUN pip install flask requests
RUN pip install flask requests mysql-connector-python
COPY drupal/app.py /app/
EXPOSE 8080

Expand Down
32 changes: 25 additions & 7 deletions drupal/app.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
# encoding: utf-8

from flask import Flask, Response
import requests, os
import requests, os, mysql.connector
from mysql.connector import Error

app = Flask(__name__)


@app.route('/')
def index():
# Gather
solr_dns = os.getenv('SOLR_SERVICE_DNS', 'solr.drupalservices.local')
solr_url = f'http://{solr_dns}:8983/solr/'
rds_endpoint, rds_port = os.getenv('RDS_ENDPOINT', None), int(os.getenv('RDS_PORT', '3306'))
db_username, db_password = os.getenv('DB_USERNAME', 'drupal'), os.getenv('DB_PASSWORD', None)

rds_endpoint, rds_port = os.getenv('RDS_ENDPOINT', '«unknown»'), os.getenv('RDS_PORT', '«unknown»')

# Solr
try:
response = requests.get(solr_url)
if response.status_code == 200:
solr_status = '<pre>✅ Solr OK</pre>'
solr_status = f'<pre>✅ Solr at {solr_url} OK</pre>'
else:
solr_status = f'<pre>❌ Solr NOT OK: {response.status_code}</pre>'
solr_status = f'<pre>❌ Solr at {solr_url} NOT OK: {response.status_code}</pre>'
except Exception as e:
solr_status = f'<pre>Error: {e}</pre>'
solr_status = f'<pre>Cannot access Solr at {solr_url}: {e}</pre>'

if rds_endpoint is not None and db_password is not None:
try:
connection = mysql.connector.connect(
host=rds_endpoint, port=rds_port, username=db_username, password=db_password
)
if connection.is_connected(): mysql_status = f'✅ Connection to RDS at {rds_endpoint} ok!'
else: mysql_status = f'❌ Cannot connect to RDS at {rds_endpoint}'
except Error as ex:
mysql_status = f'❌ Error connecting to RDS at {rds_endpoint}: {ex}'
else:
mysql_status = '🤷 Need both RDS_ENDPOINT and DB_PASSWORD at a minimum to test RDS'

return Response(
f'''
<p>Okay, this from Flask, from a Docker container allegedly from ECR (allegedly) 🌍
</p>
<p>RDS is at {rds_endpoint}:{rds_port}.</p>
<p>The SOLR_SERVICE_DNS is: <code>{solr_dns}</code>, which makes the URL {solr_url}.</p>
<p>Solr status is {solr_status}.</p>
<p>RDS is at {rds_endpoint}:{rds_port} with username {db_username}.</p>
<p>MySQL status is {mysql_status}.</p>
<p>Hope this helps! 🙏</p>
''',
mimetype='text/html'
Expand Down

0 comments on commit 980fb4f

Please sign in to comment.