Ruhi_hosting / core /config.py
Ruhivig65's picture
Upload 11 files
9684770 verified
"""
============================================
RUHI-CORE - Global Configuration
============================================
"""
import os
import secrets
import aiosqlite
from pathlib import Path
from pydantic_settings import BaseSettings
from loguru import logger
import sys
# Configure loguru
logger.remove()
logger.add(
sys.stdout,
format="<green>{time:HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan> - <level>{message}</level>",
level="INFO",
colorize=True
)
logger.add(
"/data/logs/ruhi_core.log",
rotation="10 MB",
retention="7 days",
compression="zip",
level="DEBUG"
)
class Settings(BaseSettings):
"""Global settings for RUHI-CORE"""
# App Settings
APP_NAME: str = "RUHI-CORE"
APP_VERSION: str = "1.0.0"
APP_DESCRIPTION: str = "Premium PaaS Platform - Better than Render.com"
DEBUG: bool = False
# Server
HOST: str = "0.0.0.0"
PORT: int = int(os.getenv("RUHI_PORT", "7860"))
# Authentication
ADMIN_USERNAME: str = os.getenv("ADMIN_USERNAME", "RUHIVIGQNR")
ADMIN_PASSWORD: str = os.getenv("ADMIN_PASSWORD", "RUHIVIGQNR")
SECRET_KEY: str = os.getenv("SECRET_KEY", secrets.token_hex(32))
ACCESS_TOKEN_EXPIRE_MINUTES: int = 1440 # 24 hours
# Paths
DATA_DIR: Path = Path("/data")
APPS_DIR: Path = Path("/data/apps")
LOGS_DIR: Path = Path("/data/logs")
CONFIG_DIR: Path = Path("/data/config")
UPLOADS_DIR: Path = Path("/data/uploads")
BACKUPS_DIR: Path = Path("/data/backups")
TEMP_DIR: Path = Path("/data/temp")
DB_PATH: Path = Path("/data/db/ruhi_core.db")
# Process Management
MAX_SERVICES: int = 25
AUTO_RESTART: bool = True
AUTO_RESTART_DELAY: int = 5 # seconds
MAX_RESTART_ATTEMPTS: int = 10
HEALTH_CHECK_INTERVAL: int = 30 # seconds
# Port Management
PORT_RANGE_START: int = 8001
PORT_RANGE_END: int = 8050
# File Upload
MAX_UPLOAD_SIZE: int = 500 * 1024 * 1024 # 500MB
ALLOWED_EXTENSIONS: list = [".zip", ".tar.gz", ".tar", ".7z", ".rar"]
# Security
IP_WHITELIST_ENABLED: bool = False
WHITELISTED_IPS: list = []
# Metrics
METRICS_RETENTION_HOURS: int = 72
METRICS_INTERVAL: int = 5 # seconds
class Config:
env_file = ".env"
case_sensitive = True
# Global settings instance
settings = Settings()
async def init_db():
"""Initialize SQLite database with all required tables"""
logger.info("Initializing RUHI-CORE database...")
settings.DB_PATH.parent.mkdir(parents=True, exist_ok=True)
async with aiosqlite.connect(str(settings.DB_PATH)) as db:
# Services table
await db.execute("""
CREATE TABLE IF NOT EXISTS services (
id TEXT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
type TEXT NOT NULL DEFAULT 'web',
language TEXT DEFAULT 'python',
entry_file TEXT DEFAULT 'main.py',
port INTEGER,
status TEXT DEFAULT 'stopped',
auto_restart INTEGER DEFAULT 1,
env_vars TEXT DEFAULT '{}',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
app_dir TEXT,
command TEXT,
description TEXT DEFAULT '',
max_memory_mb INTEGER DEFAULT 512,
restart_count INTEGER DEFAULT 0,
last_started TIMESTAMP,
last_stopped TIMESTAMP,
pid INTEGER DEFAULT 0
)
""")
# Metrics table
await db.execute("""
CREATE TABLE IF NOT EXISTS metrics (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
cpu_percent REAL,
memory_percent REAL,
memory_used_mb REAL,
memory_total_mb REAL,
disk_used_gb REAL,
disk_total_gb REAL,
disk_percent REAL,
network_sent_mb REAL,
network_recv_mb REAL,
active_services INTEGER,
total_processes INTEGER,
load_avg_1 REAL,
load_avg_5 REAL,
load_avg_15 REAL
)
""")
# Logs table
await db.execute("""
CREATE TABLE IF NOT EXISTS service_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
service_id TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
level TEXT DEFAULT 'INFO',
message TEXT,
source TEXT DEFAULT 'stdout',
FOREIGN KEY (service_id) REFERENCES services(id)
)
""")
# Deployments table
await db.execute("""
CREATE TABLE IF NOT EXISTS deployments (
id TEXT PRIMARY KEY,
service_id TEXT,
status TEXT DEFAULT 'pending',
started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMP,
log TEXT DEFAULT '',
zip_file TEXT,
FOREIGN KEY (service_id) REFERENCES services(id)
)
""")
# Sessions table
await db.execute("""
CREATE TABLE IF NOT EXISTS sessions (
token TEXT PRIMARY KEY,
username TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP,
ip_address TEXT
)
""")
# Audit log
await db.execute("""
CREATE TABLE IF NOT EXISTS audit_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
action TEXT NOT NULL,
details TEXT DEFAULT '',
ip_address TEXT,
username TEXT DEFAULT 'admin'
)
""")
await db.commit()
logger.info("✅ Database initialized successfully!")