Docker compose and container stuff

This commit is contained in:
Eduardo Silva 2024-04-04 15:11:13 -03:00
parent 7332bca96e
commit 563ee7408e
12 changed files with 187 additions and 2 deletions

25
Dockerfile Normal file
View file

@ -0,0 +1,25 @@
# Usar uma imagem base do Python
FROM python:3.10
WORKDIR /app
RUN apt-get update && apt-get install -y \
net-tools \
inetutils-ping \
inetutils-traceroute \
nano \
vim-nox \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt
COPY . /app/
RUN chmod +x /app/init.sh
RUN chmod +x /app/entrypoint.sh
ARG SERVER_ADDRESS
ARG DEBUG_MODE
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["/app/init.sh"]

View file

@ -0,0 +1,16 @@
FROM ubuntu:latest
# Instalar cron
RUN apt-get update && apt-get install -y cron curl
# Adicionar seus scripts de cron
COPY cron_tasks /etc/cron.d/cron_tasks
# Dar permissões apropriadas
RUN chmod 0644 /etc/cron.d/cron_tasks
# Criar um arquivo de log para armazenar os resultados do cron
RUN touch /var/log/cron.log
# Executar o cron em primeiro plano
CMD cron -f

View file

@ -0,0 +1,5 @@
* * * * * root /usr/bin/curl -s http://routerfleet:8001/cron/check_updates/ >> /var/log/cron.log 2>&1
*/5 * * * * root /usr/bin/curl -s http://routerfleet:8001/cron/generate_backup_schedule/ >> /var/log/cron.log 2>&1
* * * * * root /usr/bin/curl -s http://routerfleet:8001/cron/create_backup_tasks/ >> /var/log/cron.log 2>&1
* * * * * root /usr/bin/curl -s http://routerfleet:8001/cron/perform_backup_tasks/ >> /var/log/cron.log 2>&1
*/10 * * * * root /usr/bin/curl -s http://routerfleet:8001/cron/housekeeping/ >> /var/log/cron.log 2>&1

View file

@ -0,0 +1,17 @@
FROM python:3.10
WORKDIR /app
RUN apt-get update && apt-get install -y \
net-tools \
inetutils-ping \
inetutils-traceroute \
nano \
vim-nox \
fping \
&& rm -rf /var/lib/apt/lists/* \
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY monitoring.py .
CMD ["python", "-u", "monitoring.py"]

View file

@ -7,8 +7,8 @@ from subprocess import Popen, PIPE
UPDATE_HOST_LIST_INTERVAL = 600 # How often to update the router list in seconds
MONITOR_INTERVAL = 60 # How often to monitor each router in seconds
MAX_NOTIFICATIONS_PER_MONITOR_INTERVAL = 50 # Throttle the number of notifications sent to the remote API
HOST_LIST_URL = "http://127.0.0.1:8000/monitoring/export_router_list/"
UPDATE_STATUS_URL = "http://127.0.0.1:8000/monitoring/update_router_status/"
HOST_LIST_URL = "http://routerfleet:8001/monitoring/export_router_list/"
UPDATE_STATUS_URL = "http://routerfleet:8001/monitoring/update_router_status/"
DEBUG = False
# Global variables

View file

@ -0,0 +1,6 @@
FROM nginx:alpine
RUN apk --no-cache add openssl
COPY nginx_entrypoint.sh /nginx_entrypoint.sh
RUN chmod +x /nginx_entrypoint.sh
ENTRYPOINT ["/nginx_entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]

View file

@ -0,0 +1,12 @@
#!/bin/sh
CERT_DIR="/certificate"
if [ ! -f "$CERT_DIR/nginx.key" ] || [ ! -f "$CERT_DIR/nginx.pem" ]; then
echo "Creating self signed certificate..."
openssl req -x509 -newkey rsa:4096 -nodes -keyout "$CERT_DIR/nginx.key" -out "$CERT_DIR/nginx.pem" -days 3650 -subj "/CN=localhost"
else
echo "Skipping self signed certificate creation, files already exist."
fi
exec "$@"

View file

@ -0,0 +1,25 @@
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /certificate/nginx.pem;
ssl_certificate_key /certificate/nginx.key;
# if you are using cloudflare, you can use this enable authenticated origin pull. Dont forget to activate it in cloudflare
#ssl_client_certificate /certificate/cloudflare_authenticated_origin_pull_ca.pem;
#ssl_verify_client on;
location /static/ {
alias /static/;
}
location / {
proxy_pass http://routerfleet:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

43
docker-compose.yml Normal file
View file

@ -0,0 +1,43 @@
version: '3'
services:
routerfleet:
container_name: routerfleet
restart: unless-stopped
build:
context: .
environment:
- SERVER_ADDRESS=${SERVER_ADDRESS}
- DEBUG_MODE=${DEBUG_MODE}
volumes:
- media_root:/var/lib/routerfleet/
- static_volume:/app_static_files/
command: /bin/bash /app/init.sh
routerfleet-cron:
container_name: routerfleet-cron
restart: unless-stopped
build:
context: ./containers/cron
dockerfile: Dockerfile-cron
depends_on:
- routerfleet
nginx:
container_name: routerfleet-nginx
restart: unless-stopped
image: nginx:alpine
build:
context: .
dockerfile: Dockerfile_nginx
volumes:
- ./containers/nginx/virtualhost.conf:/etc/nginx/conf.d/routerfleet.conf
- static_volume:/static
- https_cert:/certificate
ports:
- "80:80"
- "443:443"
volumes:
static_volume:
https_cert:
media_root:

24
entrypoint.sh Normal file
View file

@ -0,0 +1,24 @@
#!/bin/bash
set -e
if [ -z "$SERVER_ADDRESS" ]; then
echo "SERVER_ADDRESS environment variable is not set. Exiting."
exit 1
fi
DEBUG_VALUE="False"
if [[ "${DEBUG_MODE,,}" == "true" ]]; then
DEBUG_VALUE="True"
fi
cat > /app/routerfleet/production_settings.py <<EOL
DEBUG = $DEBUG_VALUE
ALLOWED_HOSTS = ['routerfleet', '$SERVER_ADDRESS']
CSRF_TRUSTED_ORIGINS = ['http://routerfleet', 'https://$SERVER_ADDRESS']
SECRET_KEY = '$(openssl rand -base64 32)'
EOL
#sed -i "/^ path('admin\/', admin.site.urls),/s/^ / # /" /app/routerfleet/urls.py
exec "$@"

10
init.sh Normal file
View file

@ -0,0 +1,10 @@
#!/bin/bash
set -e
# Set up the environment
shopt -s nullglob
# Django startup
python manage.py migrate --noinput
python manage.py collectstatic --noinput
exec python manage.py runserver 0.0.0.0:8001

View file

@ -141,3 +141,5 @@ STATICFILES_DIRS = [
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_ROOT = '/var/lib/routerfleet/'
ROUTERFLEET_VERSION = 7002
from routerfleet.production_settings import *