From ec715926bb827478f0433e716977af9b0ea835bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20N=C3=A4slund?= Date: Wed, 16 Nov 2022 23:43:43 +0100 Subject: fixed healthcheck path --- data/collector_container/Dockerfile | 4 +++- data/healthcheck.py | 40 +++++++++++++++++++++++++++++++++++++ data/healthcheck.sh | 11 ++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 data/healthcheck.py create mode 100755 data/healthcheck.sh (limited to 'data') diff --git a/data/collector_container/Dockerfile b/data/collector_container/Dockerfile index a35b2cb..eef532f 100644 --- a/data/collector_container/Dockerfile +++ b/data/collector_container/Dockerfile @@ -29,6 +29,8 @@ COPY ./data/collector-dev.soc.sunet.se.crt /app/collector-dev.soc.sunet.se.crt COPY ./data/collector-dev.soc.sunet.se.key /app/collector-dev.soc.sunet.se.key COPY ./data/collector_root_ca.crt /app/collector_root_ca.crt COPY ./data/api_keys.txt /app/api_keys.txt +COPY ./data/healthcheck.sh /app/healthcheck.sh +COPY ./data/healthcheck.py /app/healthcheck.py WORKDIR /app/ @@ -36,7 +38,7 @@ USER soc_collector # Add healthcheck HEALTHCHECK --interval=2m --timeout=15s --retries=1 --start-period=30s \ - CMD sh ./src/soc_collector/healthcheck.sh COLLECTOR || bash -c 'kill -s 15 1 && (sleep 7; kill -s 9 1)' + CMD sh ./healthcheck.sh COLLECTOR || bash -c 'kill -s 15 1 && (sleep 7; kill -s 9 1)' ENTRYPOINT ["uvicorn", "src.soc_collector.main:app", "--log-config", "./logging.json", "--host", "0.0.0.0", "--port", "8000", "--ssl-keyfile", "./collector-dev.soc.sunet.se.key", "--ssl-certfile", "./collector-dev.soc.sunet.se.crt", "--ssl-version", "2", "--workers", "1", "--header", "server:collector"] diff --git a/data/healthcheck.py b/data/healthcheck.py new file mode 100644 index 0000000..1ccd8f3 --- /dev/null +++ b/data/healthcheck.py @@ -0,0 +1,40 @@ +""" +Send a healthcheck request +""" +import sys +import time +import json + +import requests + +from src.soc_collector.auth import load_api_keys + +def check_collector() -> bool: + """Check our collector using /info + + :return: bool + """ + time.sleep(2) # Prevent race condition with mongodb container healthcheck + + req = requests.get( + "https://localhost:8000/info", + headers={"API-KEY": load_api_keys("./api_keys.txt")[-1]}, + timeout=3, + verify="./collector_root_ca.crt", + ) + + if req.status_code != 200: + return False + + data = json.loads(req.text) + if isinstance(data["Estimated document count"], int) and data["Estimated document count"] >= 0: + return req.status_code == 200 + + return False + + +if __name__ == "__main__": + if sys.argv[1] == "COLLECTOR": + if check_collector(): + sys.exit(0) + sys.exit(1) diff --git a/data/healthcheck.sh b/data/healthcheck.sh new file mode 100755 index 0000000..2e76684 --- /dev/null +++ b/data/healthcheck.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# If mongodb container +if [ "$1" = "MONGODB" ] +then + /usr/bin/mongosh --eval 'disableTelemetry()' -u "$MONGODB_USERNAME" -p "$MONGODB_PASSWORD" localhost:27017/production /healthcheck-mongodb.js + exit $? +fi + +# If collector +/usr/bin/python3 ./healthcheck.py "$1" || exit 1 -- cgit v1.1