healthchecks/docker/fetchstatus.py
Pēteris Caune 320a7c7733
Fix the Docker healthcheck script to supply correct Host header
Commit 8fed685f12 added a HEALTHCHECK
instruction in the Dockerfile. The healthcheck script calls http://localhost:8000/api/v3/status/, which fails if localhost is not in ALLOWED_HOSTS.

With this change, the healthcheck script is now a Django management
command. It reads Django's ALLOWED_HOSTS setting, grabs the first
element, and uses it in the "Host:" HTTP header when making a HTTP
request.

cc: #1051
2024-08-21 15:52:19 +03:00

20 lines
556 B
Python

from __future__ import annotations
from typing import Any
from urllib.request import Request, urlopen
from django.conf import settings
from django.core.management.base import BaseCommand
class Command(BaseCommand):
def handle(self, **options: Any) -> str:
host = settings.ALLOWED_HOSTS[0]
if host == "*":
host = "localhost"
req = Request("http://localhost:8000/api/v3/status/", headers={"Host": host})
with urlopen(req) as response:
assert response.status == 200
return "Status OK"