1
0
mirror of https://github.com/daniviga/bite.git synced 2024-11-22 21:16:12 +01:00

Add static support and health-check (#17)

This commit is contained in:
Daniele Viganò 2020-06-18 19:09:46 +02:00 committed by GitHub
parent 454c4b43cf
commit b18030f5e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 109 additions and 37 deletions

View File

@ -1,3 +1,3 @@
# BITE - BasicIoTExample
# BITE - Basic/IoT/Example
Playing with IoT

View File

@ -37,6 +37,9 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'health_check',
'health_check.db',
# 'health_check.storage',
'rest_framework',
'api',
'telemetry',
@ -126,6 +129,8 @@ USE_TZ = True
STATIC_URL = '/static/'
STATIC_ROOT = '/srv/appdata/bite/static'
SKIP_WHITELIST = True
MQTT_BROKER = {

View File

@ -22,6 +22,7 @@ from telemetry import urls as telemetry_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('ht/', include('health_check.urls')),
path('api/', include(api_urls)),
path('telemetry/', include(telemetry_urls)),
]

View File

@ -0,0 +1 @@
default_app_config = 'telemetry.apps.TelemetryConfig'

View File

@ -1,5 +1,10 @@
from django.apps import AppConfig
from health_check.plugins import plugin_dir
class TelemetryConfig(AppConfig):
name = 'telemetry'
def ready(self):
from telemetry.health import MQTTHealthCheck
plugin_dir.register(MQTTHealthCheck)

24
bite/telemetry/health.py Normal file
View File

@ -0,0 +1,24 @@
import socket
import paho.mqtt.client as mqtt
from health_check.backends import BaseHealthCheckBackend
from health_check.exceptions import ServiceUnavailable
from django.conf import settings
MQTT_HOST = settings.MQTT_BROKER['HOST']
MQTT_PORT = int(settings.MQTT_BROKER['PORT'])
class MQTTHealthCheck(BaseHealthCheckBackend):
critical_service = True
def check_status(self):
client = mqtt.Client(client_id="django-hc")
try:
client.connect(MQTT_HOST, port=MQTT_PORT)
client.disconnect()
except (socket.gaierror, ConnectionRefusedError):
self.add_error(ServiceUnavailable("Connection refused"))
def identifier(self):
return self.__class__.__name__

View File

@ -1,3 +1,4 @@
import socket
import asyncio
import json
import time
@ -46,8 +47,11 @@ class Command(BaseCommand):
try:
client.connect(MQTT_HOST, MQTT_PORT)
break
except ConnectionRefusedError:
self.stdout.write('WARNING: Broker not available')
except (socket.gaierror, ConnectionRefusedError):
self.stdout.write(
self.style.WARNING('WARNING: Broker not available'))
time.sleep(5)
self.stdout.write(self.style.SUCCESS('INFO: Broker subscribed'))
client.disconnect()
asyncio.run(self.mqtt_broker())

View File

@ -15,7 +15,10 @@ COPY --from=builder /usr/local/lib/python3.8/site-packages/ /usr/local/lib/pytho
COPY --chown=1000:1000 bite /srv/app/bite
COPY --chown=1000:1000 requirements.txt /tmp/requirements.txt
RUN pip3 install -r /tmp/requirements.txt && rm /tmp/requirements.txt
RUN mkdir -p /srv/appdata/bite/static \
&& chown -R 1000:1000 /srv/appdata/bite \
&& pip3 install -r /tmp/requirements.txt \
&& rm /tmp/requirements.txt
USER 1000:1000
WORKDIR /srv/app/bite

View File

@ -0,0 +1,21 @@
version: "3.7"
services:
timescale:
ports:
- "5432:5432"
bite:
volumes:
- ../bite:/srv/app/bite
command: ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
ports:
- "8000:8000"
data-migration:
volumes:
- ../bite:/srv/app/bite
mqtt-to-db:
volumes:
- ../bite:/srv/app/bite

View File

@ -1,20 +1,15 @@
version: "3.7"
services:
ingress:
# command: --providers.docker # For traefik only
ports:
- "80:80"
bite:
volumes:
- "./django/production.py.sample:/srv/app/bite/bite/production.py"
- ./django/production.py.sample:/srv/app/bite/bite/production.py
command: ["gunicorn", "-b", "0.0.0.0:8000", "bite.wsgi:application"]
data-migration:
volumes:
- "./django/production.py.sample:/srv/app/bite/bite/production.py"
- ./django/production.py.sample:/srv/app/bite/bite/production.py
mqtt-to-db:
volumes:
- "./django/production.py.sample:/srv/app/bite/bite/production.py"
- ./django/production.py.sample:/srv/app/bite/bite/production.py

View File

@ -5,6 +5,7 @@ networks:
volumes:
pgdata:
staticdata:
x-op-service-default: &service_default
restart: always # unless-stopped
@ -44,25 +45,13 @@ services:
<<: *service_default
image: nginx:stable-alpine
ports:
- "8000:80"
- "80:80"
networks:
- net
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- staticdata:/srv/appdata/bite/static
- ./ingress/nginx.conf:/etc/nginx/nginx.conf
# User traefix instead of nginx
# ingress:
# <<: *service_default
# image: traefik:v2.2
# command: --api.insecure=true --providers.docker
# ports:
# - "8000:80"
# - "8080:8080"
# networks:
# - net
# volumes:
# # So that Traefik can listen to the Docker events
# - /var/run/docker.sock:/var/run/docker.sock
bite:
<<: *service_default
@ -70,33 +59,31 @@ services:
context: ..
dockerfile: ./docker/django/Dockerfile
image: daniviga/bite
volumes:
- "../bite:/srv/app/bite"
command: ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
command: ["gunicorn", "-b", "0.0.0.0:8000", "bite.wsgi:application"]
networks:
- net
depends_on:
- ingress
- data-migration
- timescale
labels:
- "traefik.http.routers.bite.rule=PathPrefix(`/`)"
data-migration:
image: daniviga/bite
volumes:
- "../bite:/srv/app/bite"
command: ["dockerize", "-wait", "tcp://timescale:5432", "python3", "manage.py", "migrate", "--noinput"]
networks:
- net
depends_on:
- timescale
static-files:
image: daniviga/bite
volumes:
- staticdata:/srv/appdata/bite/static
command: ["python3", "manage.py", "collectstatic", "--noinput"]
mqtt-to-db:
<<: *service_default
image: daniviga/bite
volumes:
- "../bite:/srv/app/bite"
command: ["python3", "manage.py", "mqtt-to-db"]
networks:
- net

View File

@ -2,5 +2,5 @@
```bash
export DOCKER_HOST='127.0.0.1:22375'
docker-compose -f docker-compose.edge.yml up -d --scale device-http=4
docker-compose -f docker-compose.modules.yml up -d --scale device-http=4
```

View File

@ -0,0 +1,21 @@
version: "3.7"
services:
ingress:
<<: *service_default
image: traefik:v2.2
command: --api.insecure=true --providers.docker
ports:
- "8000:80"
- "8080:8080"
networks:
- net
volumes:
# So that Traefik can listen to the Docker events
- /var/run/docker.sock:/var/run/docker.sock
restart: always
init: true
bite:
labels:
- "traefik.http.routers.bite.rule=PathPrefix(`/`)"

View File

@ -51,6 +51,10 @@ http {
proxy_connect_timeout 300;
}
location /static/ {
root /srv/appdata/bite;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;

View File

@ -1,5 +1,6 @@
Django
djangorestframework
django-health-check
psycopg2-binary
paho-mqtt==1.5.0
asyncio-mqtt==0.5.0