#!/usr/bin/env python3 """ Debian Forge Documentation Generator This module generates comprehensive documentation for the Debian Forge project including technical documentation, user guides, deployment guides, and maintenance documentation. """ import os import json import sqlite3 from pathlib import Path from typing import Dict, List, Any, Optional from datetime import datetime import re class DocumentationGenerator: """Generates comprehensive documentation for Debian Forge""" def __init__(self, output_dir: str = "generated_docs"): self.output_dir = Path(output_dir) self.output_dir.mkdir(exist_ok=True) self.project_root = Path(__file__).parent def generate_technical_documentation(self) -> str: """Generate technical documentation""" print("šŸ“š Generating Technical Documentation...") content = [] content.append("# Debian Forge Technical Documentation") content.append("") content.append(f"*Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*") content.append("") # Architecture Overview content.append("## Architecture Overview") content.append("") content.append("Debian Forge is a fork of OSBuild, adapted for Debian with 1:1 compatibility goals.") content.append("") content.append("### Core Components") content.append("- **debian-forge**: Core OSBuild fork with Debian-specific modifications") content.append("- **debian-forge-cli**: CLI tools for image building (fork of osbuild/image-builder-cli)") content.append("- **debian-forge-composer**: Web service and orchestration (fork of osbuild/osbuild-composer)") content.append("") # Technical Specifications content.append("## Technical Specifications") content.append("") content.append("### System Requirements") content.append("- **Operating System**: Debian 12+ or compatible") content.append("- **Python**: 3.8+") content.append("- **Database**: SQLite (default), PostgreSQL (optional)") content.append("- **Memory**: 4GB minimum, 8GB recommended") content.append("- **Storage**: 20GB minimum for base system") content.append("") content.append("### Dependencies") content.append("- **Core**: Python standard library") content.append("- **Database**: sqlite3 (built-in)") content.append("- **Security**: OWASP Top 10 compliance") content.append("- **Monitoring**: Performance metrics collection") content.append("") # API Documentation content.append("## API Documentation") content.append("") content.append("### Core Modules") content.append("") # List Python modules python_files = list(self.project_root.glob("*.py")) for py_file in python_files: if py_file.name != "__init__.py" and py_file.name != "documentation_generator.py": module_name = py_file.stem content.append(f"#### {module_name}") content.append(f"- **File**: `{py_file.name}`") content.append(f"- **Purpose**: {self._get_module_purpose(module_name)}") content.append("") # Database Schema content.append("## Database Schema") content.append("") content.append("### SQLite Databases") content.append("- **users.db**: User management and authentication") content.append("- **production_metrics.db**: Performance monitoring and load testing") content.append("- **security_vulnerabilities.db**: Security audit results") content.append("") # Security Architecture content.append("## Security Architecture") content.append("") content.append("### Security Features") content.append("- **Authentication**: User management with role-based access control") content.append("- **Input Validation**: Comprehensive input sanitization") content.append("- **Data Protection**: Secure data handling and storage") content.append("- **File Permissions**: Secure file access controls") content.append("- **SQL Injection Protection**: Parameterized queries") content.append("- **XSS Protection**: Output sanitization") content.append("") content.append("### Compliance") content.append("- **OWASP Top 10**: Web application security compliance") content.append("- **CIS Benchmarks**: Security configuration guidelines") content.append("- **Risk Assessment**: Automated vulnerability detection") content.append("") # Performance Architecture content.append("## Performance Architecture") content.append("") content.append("### Monitoring") content.append("- **Real-time Metrics**: CPU, memory, disk I/O, network I/O") content.append("- **Build Metrics**: Active builds, queue length, response times") content.append("- **Load Testing**: Multi-scenario performance testing") content.append("") content.append("### Optimization") content.append("- **Bottleneck Detection**: Automated performance analysis") content.append("- **Recommendations**: Prioritized optimization suggestions") content.append("- **Historical Data**: Performance trend analysis") content.append("") # Integration Architecture content.append("## Integration Architecture") content.append("") content.append("### CLI Integration") content.append("- **debian-forge-cli**: Direct CLI command execution") content.append("- **Blueprint Management**: Debian-specific blueprint creation") content.append("- **Image Building**: CLI-based image generation") content.append("") content.append("### Composer Integration") content.append("- **debian-forge-composer**: Web service integration") content.append("- **API Communication**: RESTful API interactions") content.append("- **Build Orchestration**: Centralized build management") content.append("") # Write to file output_file = self.output_dir / "TECHNICAL_DOCUMENTATION.md" with open(output_file, 'w') as f: f.write('\n'.join(content)) print(f" āœ… Technical documentation generated: {output_file}") return str(output_file) def generate_user_documentation(self) -> str: """Generate user documentation""" print("šŸ“– Generating User Documentation...") content = [] content.append("# Debian Forge User Guide") content.append("") content.append(f"*Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*") content.append("") # Getting Started content.append("## Getting Started") content.append("") content.append("### Installation") content.append("1. Clone the repository: `git clone `") content.append("2. Navigate to the project directory: `cd debian-forge`") content.append("3. Install dependencies: `pip install -r requirements.txt`") content.append("4. Initialize the system: `python3 -m debian_forge.init`") content.append("") content.append("### Quick Start") content.append("1. **Start the system**: `python3 main.py`") content.append("2. **Access web interface**: Open browser to `http://localhost:8080`") content.append("3. **Create your first blueprint**: Use the web interface or CLI") content.append("4. **Build your first image**: Submit a build request") content.append("") # User Interface content.append("## User Interface") content.append("") content.append("### Web Interface") content.append("- **Dashboard**: System overview and status") content.append("- **Blueprint Management**: Create and manage image blueprints") content.append("- **Build Management**: Monitor and control build processes") content.append("- **User Management**: Manage user accounts and permissions") content.append("") content.append("### Command Line Interface") content.append("- **Image Building**: `debian-forge-cli build-image `") content.append("- **Blueprint Management**: `debian-forge-cli blueprint `") content.append("- **System Status**: `debian-forge-cli status`") content.append("") # Blueprint Creation content.append("## Blueprint Creation") content.append("") content.append("### Basic Blueprint Structure") content.append("```json") content.append("{") content.append(' "name": "debian-server",') content.append(' "description": "Debian server image",') content.append(' "version": "1.0.0",') content.append(' "packages": [') content.append(' "openssh-server",') content.append(' "nginx",') content.append(' "postgresql"') content.append(' ],') content.append(' "customizations": {') content.append(' "user": {') content.append(' "name": "admin",') content.append(' "password": "secure_password"') content.append(' }') content.append(' }') content.append("}") content.append("```") content.append("") content.append("### Debian-Specific Features") content.append("- **Package Management**: APT-based package installation") content.append("- **Repository Configuration**: Debian repository management") content.append("- **Debian Variants**: Support for different Debian flavors") content.append("") # Image Building content.append("## Image Building") content.append("") content.append("### Build Process") content.append("1. **Blueprint Submission**: Submit blueprint to the system") content.append("2. **Build Queuing**: Build request enters the queue") content.append("3. **Build Execution**: System processes the build request") content.append("4. **Image Generation**: OSBuild stages create the final image") content.append("5. **Result Delivery**: Download or access the generated image") content.append("") content.append("### Build Types") content.append("- **Raw Images**: Direct disk images for virtualization") content.append("- **Container Images**: Docker/OCI compatible images") content.append("- **Cloud Images**: Cloud provider specific formats") content.append("- **Live Images**: Bootable ISO images") content.append("") # User Management content.append("## User Management") content.append("") content.append("### User Roles") content.append("- **Administrator**: Full system access and control") content.append("- **Builder**: Can create and manage blueprints and builds") content.append("- **Viewer**: Read-only access to system information") content.append("") content.append("### Authentication") content.append("- **User Registration**: Self-service user creation") content.append("- **Password Management**: Secure password policies") content.append("- **Session Management**: Secure session handling") content.append("") # Troubleshooting content.append("## Troubleshooting") content.append("") content.append("### Common Issues") content.append("- **Build Failures**: Check blueprint syntax and dependencies") content.append("- **Authentication Issues**: Verify user credentials and permissions") content.append("- **Performance Issues**: Monitor system resources and queue length") content.append("") content.append("### Getting Help") content.append("- **System Logs**: Check application logs for errors") content.append("- **Documentation**: Refer to technical documentation") content.append("- **Community**: Join Debian Forge community forums") content.append("") # Write to file output_file = self.output_dir / "USER_GUIDE.md" with open(output_file, 'w') as f: f.write('\n'.join(content)) print(f" āœ… User documentation generated: {output_file}") return str(output_file) def generate_deployment_documentation(self) -> str: """Generate deployment documentation""" print("šŸš€ Generating Deployment Documentation...") content = [] content.append("# Debian Forge Deployment Guide") content.append("") content.append(f"*Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*") content.append("") # System Requirements content.append("## System Requirements") content.append("") content.append("### Hardware Requirements") content.append("- **CPU**: 4 cores minimum, 8+ cores recommended") content.append("- **Memory**: 8GB minimum, 16GB+ recommended") content.append("- **Storage**: 50GB minimum, SSD recommended") content.append("- **Network**: 1Gbps minimum, 10Gbps recommended") content.append("") content.append("### Software Requirements") content.append("- **Operating System**: Debian 12+ (Bookworm)") content.append("- **Kernel**: Linux 5.15+") content.append("- **Python**: 3.8+") content.append("- **Database**: SQLite (default) or PostgreSQL") content.append("") # Installation content.append("## Installation") content.append("") content.append("### Prerequisites") content.append("```bash") content.append("# Update system") content.append("sudo apt update && sudo apt upgrade -y") content.append("") content.append("# Install required packages") content.append("sudo apt install -y python3 python3-pip python3-venv git") content.append("sudo apt install -y build-essential libssl-dev libffi-dev") content.append("") content.append("# Install Go (for CLI and Composer)") content.append("sudo apt install -y golang-go") content.append("```") content.append("") content.append("### Source Installation") content.append("```bash") content.append("# Clone repositories") content.append("git clone ") content.append("git clone ") content.append("git clone ") content.append("") content.append("# Set up Python environment") content.append("cd debian-forge") content.append("python3 -m venv venv") content.append("source venv/bin/activate") content.append("pip install -r requirements.txt") content.append("```") content.append("") # Configuration content.append("## Configuration") content.append("") content.append("### Environment Configuration") content.append("```bash") content.append("# Create configuration file") content.append("cp config.example.yaml config.yaml") content.append("") content.append("# Edit configuration") content.append("nano config.yaml") content.append("```") content.append("") content.append("### Database Configuration") content.append("- **SQLite**: Default, no additional configuration needed") content.append("- **PostgreSQL**: Configure connection parameters") content.append("- **Database Initialization**: Run setup scripts") content.append("") content.append("### Security Configuration") content.append("- **SSL/TLS**: Configure HTTPS certificates") content.append("- **Firewall**: Configure network security") content.append("- **User Authentication**: Set up initial admin user") content.append("") # Service Configuration content.append("## Service Configuration") content.append("") content.append("### Systemd Service") content.append("```ini") content.append("[Unit]") content.append("Description=Debian Forge Service") content.append("After=network.target") content.append("") content.append("[Service]") content.append("Type=simple") content.append("User=debian-forge") content.append("WorkingDirectory=/opt/debian-forge") content.append("ExecStart=/opt/debian-forge/venv/bin/python main.py") content.append("Restart=always") content.append("") content.append("[Install]") content.append("WantedBy=multi-user.target") content.append("```") content.append("") content.append("### Nginx Configuration") content.append("```nginx") content.append("server {") content.append(" listen 80;") content.append(" server_name debian-forge.example.com;") content.append(" return 301 https://$server_name$request_uri;") content.append("}") content.append("") content.append("server {") content.append(" listen 443 ssl;") content.append(" server_name debian-forge.example.com;") content.append(" ssl_certificate /path/to/cert.pem;") content.append(" ssl_certificate_key /path/to/key.pem;") content.append(" location / {") content.append(" proxy_pass http://127.0.0.1:8080;") content.append(" proxy_set_header Host $host;") content.append(" proxy_set_header X-Real-IP $remote_addr;") content.append(" }") content.append("}") content.append("```") content.append("") # Deployment Steps content.append("## Deployment Steps") content.append("") content.append("### 1. System Preparation") content.append("- Verify system requirements") content.append("- Install prerequisites") content.append("- Configure system settings") content.append("") content.append("### 2. Application Installation") content.append("- Clone source repositories") content.append("- Install dependencies") content.append("- Configure application") content.append("") content.append("### 3. Service Setup") content.append("- Create system user") content.append("- Configure systemd service") content.append("- Set up reverse proxy") content.append("") content.append("### 4. Initial Configuration") content.append("- Initialize database") content.append("- Create admin user") content.append("- Configure security settings") content.append("") content.append("### 5. Testing and Validation") content.append("- Test service startup") content.append("- Verify web interface") content.append("- Test basic functionality") content.append("") # Monitoring and Maintenance content.append("## Monitoring and Maintenance") content.append("") content.append("### Health Checks") content.append("- **Service Status**: Check systemd service status") content.append("- **Web Interface**: Verify web interface accessibility") content.append("- **Database Health**: Check database connectivity") content.append("- **Performance Metrics**: Monitor system performance") content.append("") content.append("### Backup Procedures") content.append("- **Configuration Files**: Backup configuration directory") content.append("- **Database**: Regular database backups") content.append("- **User Data**: Backup user uploads and generated images") content.append("") # Write to file output_file = self.output_dir / "DEPLOYMENT_GUIDE.md" with open(output_file, 'w') as f: f.write('\n'.join(content)) print(f" āœ… Deployment documentation generated: {output_file}") return str(output_file) def generate_maintenance_documentation(self) -> str: """Generate maintenance documentation""" print("šŸ”§ Generating Maintenance Documentation...") content = [] content.append("# Debian Forge Maintenance Guide") content.append("") content.append(f"*Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*") content.append("") # Regular Maintenance Tasks content.append("## Regular Maintenance Tasks") content.append("") content.append("### Daily Tasks") content.append("- **System Health Check**: Verify all services are running") content.append("- **Performance Monitoring**: Review performance metrics") content.append("- **Error Log Review**: Check for new error messages") content.append("- **Backup Verification**: Ensure backups completed successfully") content.append("") content.append("### Weekly Tasks") content.append("- **Performance Analysis**: Review weekly performance trends") content.append("- **Security Audit**: Run security vulnerability scans") content.append("- **Database Maintenance**: Clean up old data and optimize") content.append("- **Log Rotation**: Rotate and compress log files") content.append("") content.append("### Monthly Tasks") content.append("- **System Updates**: Apply security and feature updates") content.append("- **Capacity Planning**: Review resource usage trends") content.append("- **Security Review**: Update security configurations") content.append("- **Documentation Review**: Update operational procedures") content.append("") # Troubleshooting content.append("## Troubleshooting") content.append("") content.append("### Common Issues and Solutions") content.append("") content.append("#### Service Won't Start") content.append("1. Check systemd service status: `systemctl status debian-forge`") content.append("2. Review service logs: `journalctl -u debian-forge`") content.append("3. Verify configuration files") content.append("4. Check file permissions and ownership") content.append("") content.append("#### Performance Issues") content.append("1. Monitor system resources: `htop`, `iotop`") content.append("2. Check database performance") content.append("3. Review build queue length") content.append("4. Analyze performance metrics") content.append("") content.append("#### Authentication Problems") content.append("1. Verify user database integrity") content.append("2. Check password policies") content.append("3. Review authentication logs") content.append("4. Test user login process") content.append("") # Backup and Recovery content.append("## Backup and Recovery") content.append("") content.append("### Backup Procedures") content.append("") content.append("#### Configuration Backup") content.append("```bash") content.append("# Backup configuration directory") content.append("tar -czf config-backup-$(date +%Y%m%d).tar.gz config/") content.append("") content.append("# Backup database files") content.append("cp *.db backup/") content.append("```") content.append("") content.append("#### Database Backup") content.append("```bash") content.append("# SQLite backup") content.append("sqlite3 users.db .dump > backup/users-$(date +%Y%m%d).sql") content.append("") content.append("# PostgreSQL backup") content.append("pg_dump debian_forge > backup/postgres-$(date +%Y%m%d).sql") content.append("```") content.append("") content.append("### Recovery Procedures") content.append("") content.append("#### Configuration Recovery") content.append("1. Stop the service: `systemctl stop debian-forge`") content.append("2. Restore configuration files") content.append("3. Verify file permissions") content.append("4. Start the service: `systemctl start debian-forge`") content.append("") content.append("#### Database Recovery") content.append("1. Stop the service") content.append("2. Restore database from backup") content.append("3. Verify database integrity") content.append("4. Start the service") content.append("") # Performance Optimization content.append("## Performance Optimization") content.append("") content.append("### System Tuning") content.append("- **CPU Optimization**: Adjust process priorities") content.append("- **Memory Management**: Configure swap and memory limits") content.append("- **Disk I/O**: Optimize storage configuration") content.append("- **Network Tuning**: Optimize network parameters") content.append("") content.append("### Application Tuning") content.append("- **Database Optimization**: Index optimization and query tuning") content.append("- **Build Optimization**: Parallel build processing") content.append("- **Cache Management**: Implement and tune caching") content.append("- **Resource Pooling**: Optimize resource allocation") content.append("") # Security Maintenance content.append("## Security Maintenance") content.append("") content.append("### Regular Security Tasks") content.append("- **Vulnerability Scanning**: Run security audits") content.append("- **Access Review**: Review user access and permissions") content.append("- **Security Updates**: Apply security patches") content.append("- **Configuration Review**: Review security settings") content.append("") content.append("### Incident Response") content.append("1. **Detection**: Identify security incidents") content.append("2. **Assessment**: Evaluate incident severity") content.append("3. **Containment**: Limit incident impact") content.append("4. **Eradication**: Remove security threats") content.append("5. **Recovery**: Restore normal operations") content.append("6. **Lessons Learned**: Document and improve procedures") content.append("") # Monitoring and Alerting content.append("## Monitoring and Alerting") content.append("") content.append("### Key Metrics to Monitor") content.append("- **System Resources**: CPU, memory, disk, network") content.append("- **Application Performance**: Response times, throughput") content.append("- **Build Queue**: Queue length, processing times") content.append("- **Security Events**: Authentication failures, access attempts") content.append("") content.append("### Alerting Configuration") content.append("- **Threshold Alerts**: Resource usage alerts") content.append("- **Performance Alerts**: Response time and error rate alerts") content.append("- **Security Alerts**: Security incident notifications") content.append("- **Service Alerts**: Service availability notifications") content.append("") # Write to file output_file = self.output_dir / "MAINTENANCE_GUIDE.md" with open(output_file, 'w') as f: f.write('\n'.join(content)) print(f" āœ… Maintenance documentation generated: {output_file}") return str(output_file) def _get_module_purpose(self, module_name: str) -> str: """Get the purpose of a module based on its name""" purposes = { "user_manager": "User authentication, authorization, and role management", "admin_interface_simple": "System administration and configuration interface", "security_hardening": "Security testing, vulnerability assessment, and compliance", "production_optimization": "Performance monitoring, load testing, and optimization", "cli_integration": "Integration with debian-forge-cli for command-line operations", "composer_integration_simple": "Integration with debian-forge-composer web service", "unified_integration": "Unified interface for CLI and Composer integration", "test_integration_simple": "Testing framework for integration modules" } return purposes.get(module_name, "Core functionality module") def generate_all_documentation(self) -> Dict[str, str]: """Generate all documentation types""" print("šŸ“š Generating Complete Documentation Suite...") print("=" * 60) results = {} # Generate all documentation types results["technical"] = self.generate_technical_documentation() results["user"] = self.generate_user_documentation() results["deployment"] = self.generate_deployment_documentation() results["maintenance"] = self.generate_maintenance_documentation() # Generate index file results["index"] = self._generate_documentation_index() print("\n" + "=" * 60) print("šŸŽ‰ All documentation generated successfully!") print(f"šŸ“ Output directory: {self.output_dir}") return results def _generate_documentation_index(self) -> str: """Generate documentation index file""" content = [] content.append("# Debian Forge Documentation Index") content.append("") content.append(f"*Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*") content.append("") content.append("## Documentation Overview") content.append("") content.append("This directory contains comprehensive documentation for the Debian Forge project.") content.append("") content.append("## Available Documentation") content.append("") content.append("### šŸ“š [Technical Documentation](TECHNICAL_DOCUMENTATION.md)") content.append("Comprehensive technical reference including architecture, API documentation, and system specifications.") content.append("") content.append("### šŸ“– [User Guide](USER_GUIDE.md)") content.append("User-friendly guide for using Debian Forge, including getting started, blueprint creation, and troubleshooting.") content.append("") content.append("### šŸš€ [Deployment Guide](DEPLOYMENT_GUIDE.md)") content.append("Step-by-step deployment instructions, system requirements, and configuration details.") content.append("") content.append("### šŸ”§ [Maintenance Guide](MAINTENANCE_GUIDE.md)") content.append("Operational procedures, troubleshooting guides, and maintenance best practices.") content.append("") content.append("## Quick Start") content.append("") content.append("1. **New Users**: Start with the [User Guide](USER_GUIDE.md)") content.append("2. **System Administrators**: Review the [Deployment Guide](DEPLOYMENT_GUIDE.md)") content.append("3. **Developers**: Reference the [Technical Documentation](TECHNICAL_DOCUMENTATION.md)") content.append("4. **Operations**: Use the [Maintenance Guide](MAINTENANCE_GUIDE.md)") content.append("") content.append("## Documentation Maintenance") content.append("") content.append("This documentation is automatically generated and should be updated when:") content.append("- New features are added to the system") content.append("- Configuration options change") content.append("- Security procedures are updated") content.append("- Deployment processes are modified") content.append("") # Write to file output_file = self.output_dir / "README.md" with open(output_file, 'w') as f: f.write('\n'.join(content)) return str(output_file) def main(): """Generate all documentation""" try: generator = DocumentationGenerator() results = generator.generate_all_documentation() print("\nšŸ“‹ Documentation Generation Summary:") for doc_type, file_path in results.items(): print(f" āœ… {doc_type.title()}: {file_path}") return True except Exception as e: print(f"šŸ’„ Documentation generation failed: {e}") import traceback traceback.print_exc() return False if __name__ == "__main__": try: success = main() exit(0 if success else 1) except Exception as e: print(f"šŸ’„ Documentation generation failed: {e}") exit(1)