diff --git a/Dockerfile b/Dockerfile index 6515f8b3..02094289 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3-slim-bullseye as base +FROM python:3.9-slim-bullseye as base FROM base as builder @@ -8,7 +8,7 @@ RUN apt-get update && apt-get -y install build-essential libssl-dev libffi-dev p mkdir /install WORKDIR /install COPY requirements.txt . -RUN python -m pip install --prefix=/install --no-cache-dir --upgrade pip && \ +RUN python -m pip install --no-cache-dir --upgrade pip && \ pip install --prefix=/install --no-cache-dir -r requirements.txt && \ rm -rf /var/lib/apt/lists/* && \ apt-get clean @@ -19,7 +19,7 @@ COPY --from=builder /install /usr/local COPY app /app WORKDIR /app RUN apt-get update && \ - apt-get -y install iputils-ping nmap && \ + apt-get -y install iputils-ping nmap curl && \ rm -rf /var/lib/apt/lists/* CMD ["./run.sh"] diff --git a/app/wol/urls.py b/app/wol/urls.py index 3413ddcb..08ea8937 100644 --- a/app/wol/urls.py +++ b/app/wol/urls.py @@ -9,5 +9,6 @@ urlpatterns = [ path("settings/scan/", views.settings_scan, name="settings_scan"), path("settings/scan_add/", views.settings_scan_add, name="settings_scan_add"), path("settings/custom_add/", views.settings_custom_add, name="settings_custom_add"), - path("settings/del/", views.settings_del, name="settings_del") + path("settings/del/", views.settings_del, name="settings_del"), + path("health/", views.health, name="health") ] diff --git a/app/wol/views.py b/app/wol/views.py index 4df19eaf..02225d30 100644 --- a/app/wol/views.py +++ b/app/wol/views.py @@ -6,7 +6,7 @@ import subprocess from asgiref.sync import async_to_sync from channels.layers import get_channel_layer from django.http import HttpResponseRedirect -from django.http.response import JsonResponse +from django.http.response import HttpResponse, HttpResponseServerError, JsonResponse from django.shortcuts import render from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt @@ -164,3 +164,10 @@ def settings_del(request): })}) return JsonResponse(data=data) + +def health(request): + try: + Settings.objects.all() + return HttpResponse("Ok") + except Exception: + return HttpResponseServerError("db: cannot connect to database.") diff --git a/docker-compose.yml b/docker-compose.yml index 154840f9..eec805a2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,9 +4,6 @@ services: container_name: upsnap_django image: seriousm4x/upsnap:latest network_mode: host - depends_on: - - upsnap_redis - - upsnap_postgres restart: always environment: - DJANGO_SUPERUSER_USER=admin @@ -21,20 +18,32 @@ services: - POSTGRES_PASSWORD=upsnap - PING_INTERVAL=5 - ENABLE_NOTIFICATIONS=True + healthcheck: + test: curl --fail -s http://localhost:$DJANGO_PORT/health/ || exit 1 + interval: 10s + depends_on: + - upsnap_redis + - upsnap_postgres upsnap_redis: container_name: upsnap_redis image: redis:6 network_mode: host restart: always + healthcheck: + test: redis-cli ping + interval: 10s upsnap_postgres: container_name: upsnap_postgres image: postgres:13-alpine - restart: always network_mode: host + restart: always environment: - "POSTGRES_USER=upsnap" - "POSTGRES_PASSWORD=upsnap" - "POSTGRES_DB=upsnap" + healthcheck: + test: pg_isready + interval: 10s volumes: - upsnap_db:/var/lib/postgresql/data volumes: