This commit is contained in:
Holger Sielaff
2025-08-27 09:55:55 +02:00
commit 90c0ff61ed
107 changed files with 8535 additions and 0 deletions

View File

View File

@@ -0,0 +1,16 @@
"""
ASGI config for django_proxmox_mikrotik project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_proxmox_mikrotik.settings')
application = get_asgi_application()

View File

@@ -0,0 +1,79 @@
import json
import logging
import os
def env_true(value):
return value.lower() in ('yes', 'y', '1', 'on', 'true', 't')
def env_false(value):
return value.lower() in ('no', 'n', '0', 'off', 'false', 'f')
PROXMOX_READONLY = env_true(os.environ.get('PROXMOX_READONLY', '0'))
MIKROTIK_READONLY = env_true(os.environ.get('MIKROTIK_READONLY', '0'))
class ProxmoxConfig:
HOST = os.environ.get('PROXMOX_HOST')
USER = os.environ.get('PROXMOX_USER')
PASS = os.environ.get('PROXMOX_PASS')
NODE = os.environ.get('PROXMOX_NODE')
READONLY = PROXMOX_READONLY
MAX_MEM = int(os.environ.get('PROXMOX_MAX_MEM', 8192))
MAX_DISK = int(os.environ.get('PROXMOX_MAX_DISK', 100))
MAX_CORES = int(os.environ.get('PROXMOX_MAX_CORES', 8))
CREATE_LXC_TIMEOUT = int(os.environ.get('PROXMOX_CREATE_LXC_TIMEOUT', 600))
DEFAULT_STORAGE = os.environ.get('PROXMOX_DEFAULT_STORAGE', 'local')
class MikrotikConfig:
HOST = os.environ.get('MIKROTIK_HOST')
USER = os.environ.get('MIKROTIK_USER')
PASS = os.environ.get('MIKROTIK_PASS')
IP_8 = os.environ.get('MIKROTIK_IP_8', '192,172')
READONLY = MIKROTIK_READONLY
ROOT_NETWORKS = os.environ.get('MIKROTIK_ROOT_NETWORKS', '192.168.1.0,192.168.107.0').split(',')
class DatabaseConfig:
HOST = os.environ.get('DATABASE_HOST', 'localhost') or 'localhost'
ENGINE = os.environ.get('DATABASE_ENGINE', 'sqlite3') or 'sqlite3'
USER = os.environ.get('DATABASE_USER', '') or ''
PASSWORD = os.environ.get('DATABASE_PASSWORD', '') or ''
NAME = os.environ.get('DATABASE_NAME', '')
PORT = os.environ.get('DATABASE_PORT', 5432) or 5432
class AuthLDAPConfig:
HOST = os.environ.get('AUTH_LDAP_HOST')
BIND_DN = os.environ.get('AUTH_LDAP_BIND_DN')
BIND_PASSWORD = os.environ.get('AUTH_LDAP_BIND_PASSWORD')
USER_BASE = os.environ.get('AUTH_LDAP_USER_BASE')
USER_FILTER = os.environ.get('AUTH_LDAP_USER_FILTER')
GROUP_SEARCH_BASE = os.environ.get('AUTH_LDAP_GROUP_SEARCH_BASE')
GROUP_SEARCH_FILTER = os.environ.get('AUTH_LDAP_GROUP_SEARCH_FILTER')
USER_ATTR_MAP = json.loads(os.environ.get('AUTH_LDAP_USER_ATTR_MAP'))
USER_FLAGS_BY_GROUP = json.loads(os.environ.get('AUTH_LDAP_USER_FLAGS_BY_GROUP'))
FIND_GROUP_PERMS = os.environ.get('AUTH_LDAP_FIND_GROUP_PERMS')
CACHE_GROUPS = os.environ.get('AUTH_LDAP_CACHE_GROUPS')
GROUP_CACHE_TIMEOUT = os.environ.get('AUTH_LDAP_GROUP_CACHE_TIMEOUT')
_missing = []
for cfg in ['Proxmox', 'Mikrotik', 'Database', ('AuthLDAP', 'AUTH_LDAP')]:
if isinstance(cfg, tuple):
cfg, mapname = cfg
else:
mapname = cfg.upper()
cls = globals()[cfg + 'Config']
for k, v in cls.__dict__.items():
if k.startswith(mapname + '_'):
if v is None:
_missing.append(f'{cfg}.{k}')
if _missing:
raise Exception(f'Missing environment variables: \n{"\n".join(_missing)}\n')
logging.debug(f'ProxmoxConfig: {ProxmoxConfig.__dict__}')
logging.debug(f'MikrotikConfig: {MikrotikConfig.__dict__}')
logging.debug(f'DatabaseConfig: {DatabaseConfig.__dict__}')
logging.debug(f'AuthLDAPConfig: {AuthLDAPConfig.__dict__}')

View File

@@ -0,0 +1,181 @@
"""
For more information on this file, see
https://docs.djangoproject.com/en/5.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.2/ref/settings/
"""
import json
import os
from pathlib import Path
import ldap
from django_auth_ldap.config import GroupOfNamesType, LDAPSearch
from dotenv import dotenv_values, load_dotenv
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
load_dotenv(BASE_DIR / '.env', override=False)
import logging
LOGLEVEL = getattr(logging,os.environ.get('LOG_LEVEL', 'DEBUG').upper())
logging.basicConfig(level=LOGLEVEL)
from django_proxmox_mikrotik.configs import (
env_true,
env_false,
ProxmoxConfig,
DatabaseConfig,
AuthLDAPConfig,
MikrotikConfig,
)
LOGIN_URL = '/frontend/login/'
# fallback
DatabaseConfig.NAME = DatabaseConfig.NAME or BASE_DIR / 'db.sqlite3'
SECRET_KEY = 'django-insecure-o$tw_(450z^cl%mq(h1&=jltu51mfnmiown&l^dinx+z-!nzem'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '127.0.0.1,localhost').strip().replace(' ', '').split(',')
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django_middleware_global_request',
'proxmox',
'tasklogger',
'mikrotik',
'manager',
'frontend',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'frontend.middleware.FrontendSessionMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_middleware_global_request.middleware.GlobalRequestMiddleware',
]
ROOT_URLCONF = 'django_proxmox_mikrotik.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates']
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'django_proxmox_mikrotik.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.' + DatabaseConfig.ENGINE,
'NAME': DatabaseConfig.NAME,
'PASSWORD': DatabaseConfig.PASSWORD,
'USER': DatabaseConfig.USER,
'HOST': DatabaseConfig.HOST,
'PORT': DatabaseConfig.PORT,
}
}
# Password validation
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/5.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.2/howto/static-files/
STATIC_URL = 'static/'
STATIC_ROOT = str(BASE_DIR / 'static')
STATICFILES_DIRS = [
BASE_DIR / "frontend" / "static",
]
# Default primary key field type
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
AUTH_LDAP_SERVER_URI = AuthLDAPConfig.HOST
AUTH_LDAP_BIND_DN = AuthLDAPConfig.BIND_DN
AUTH_LDAP_BIND_PASSWORD = AuthLDAPConfig.BIND_PASSWORD
AUTH_LDAP_USER_SEARCH = LDAPSearch(
AuthLDAPConfig.USER_BASE,
ldap.SCOPE_SUBTREE,
AuthLDAPConfig.USER_FILTER,
)
AUTH_LDAP_GROUP_SEARCH = LDAPSearch(
AuthLDAPConfig.GROUP_SEARCH_BASE,
ldap.SCOPE_SUBTREE,
AuthLDAPConfig.GROUP_SEARCH_FILTER,
)
AUTH_LDAP_USER_ATTR_MAP = AuthLDAPConfig.USER_ATTR_MAP
AUTH_LDAP_USER_FLAGS_BY_GROUP = AuthLDAPConfig.USER_FLAGS_BY_GROUP
AUTH_LDAP_FIND_GROUP_PERMS = bool(AuthLDAPConfig.FIND_GROUP_PERMS)
AUTH_LDAP_CACHE_GROUPS = bool(AuthLDAPConfig.CACHE_GROUPS)
AUTH_LDAP_GROUP_CACHE_TIMEOUT = AuthLDAPConfig.GROUP_CACHE_TIMEOUT
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn")
AUTHENTICATION_BACKENDS = [
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
]
SESSION_COOKIE_NAME_FRONTEND = 'django_pm_mk_frontend_sessionid'

View File

@@ -0,0 +1,31 @@
from django.contrib import admin
"""
URL configuration for django_proxmox_mikrotik project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path, include
from django.shortcuts import redirect
urlpatterns = [
path('admin/', admin.site.urls),
]
# django_proxmox_mikrotik/urls.py
urlpatterns += [
path('', lambda request: redirect('frontend/'), name='home'),
path('frontend/', include('frontend.urls', namespace='frontend')),
path('manager/', include('manager.urls', namespace='manager')),
path('tasklogger/', include('tasklogger.urls', namespace='tasklogger')),
]

View File

@@ -0,0 +1,16 @@
"""
WSGI config for django_proxmox_mikrotik project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.2/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_proxmox_mikrotik.settings')
application = get_wsgi_application()