VArious admin fixes

This commit is contained in:
Jonny Ervine 2025-10-04 21:13:25 +08:00
parent 83fe4b9013
commit 85858d33bd
5 changed files with 371 additions and 3 deletions

80
motm_app/.dockerignore Normal file
View File

@ -0,0 +1,80 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# Virtual environments
venv/
env/
ENV/
env.bak/
venv.bak/
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# Database files
*.db
*.sqlite
*.sqlite3
# Logs
*.log
logs/
# Temporary files
*.tmp
*.temp
# Git
.git/
.gitignore
# Documentation
*.md
docs/
# Scripts and configs not needed in container
activate_motm.bat
activate_motm.sh
run_motm.bat
run_motm.sh
setup_venv_windows.bat
setup_venv.ps1
setup_venv.py
deploy.py
test_app.py
# Development files
app.yaml
database_config.ini
POSTGRESQL_SETUP.md
SETUP_GUIDE.md
VIRTUAL_ENV_GUIDE.md
README.md

View File

@ -0,0 +1,188 @@
# MOTM Application - Container Deployment
This document provides instructions for deploying the MOTM (Man of the Match) application using Docker containers.
## Quick Start
### Using Docker Compose (Recommended)
1. **Clone the repository and navigate to the project directory**
```bash
cd motm_app
```
2. **Start the application with PostgreSQL**
```bash
docker-compose up -d
```
3. **Access the application**
- Main page: http://localhost:5000
- Admin dashboard: http://localhost:5000/admin (username: `admin`, password: `letmein`)
### Using Docker Build
1. **Build the container**
```bash
docker build -f Containerfile -t motm-app .
```
2. **Run with external PostgreSQL**
```bash
docker run -d \
--name motm-app \
-p 5000:5000 \
-e DATABASE_TYPE=postgresql \
-e DB_HOST=your-postgres-host \
-e DB_PORT=5432 \
-e DB_NAME=motm_db \
-e DB_USER=motm_user \
-e DB_PASSWORD=motm_password \
motm-app
```
## Configuration
### Environment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| `DATABASE_TYPE` | `postgresql` | Database type (postgresql/sqlite) |
| `DB_HOST` | `postgres` | Database host (use `postgres` for docker-compose) |
| `DB_PORT` | `5432` | Database port |
| `DB_NAME` | `motm_db` | Database name |
| `DB_USER` | `motm_user` | Database username |
| `DB_PASSWORD` | `motm_password` | Database password |
| `FLASK_ENV` | `production` | Flask environment |
| `FLASK_RUN_HOST` | `0.0.0.0` | Flask host |
| `FLASK_RUN_PORT` | `5000` | Flask port |
| `SECRET_KEY` | `your-secret-key-change-this-in-production` | Flask secret key |
### Production Security
**Important**: Before deploying to production, change the following:
1. **Database credentials** in `docker-compose.yml`
2. **Secret key** in environment variables
3. **Admin password** (use the admin profile page after first login)
## Container Features
### Multi-stage Build
- **Builder stage**: Installs dependencies and builds Python packages
- **Runtime stage**: Minimal image with only runtime dependencies
### Security
- Runs as non-root user (`appuser`)
- Minimal attack surface with slim base image
- Health checks for container monitoring
### Database Integration
- Automatic database connection waiting
- Database initialization support
- PostgreSQL optimized configuration
## Monitoring
### Health Checks
- Application health check: `curl -f http://localhost:5000/`
- Database health check: `pg_isready`
### Logs
```bash
# View application logs
docker-compose logs motm-app
# View database logs
docker-compose logs postgres
# Follow logs in real-time
docker-compose logs -f motm-app
```
## Maintenance
### Backup Database
```bash
# Create backup
docker exec motm-postgres pg_dump -U motm_user motm_db > backup.sql
# Restore backup
docker exec -i motm-postgres psql -U motm_user motm_db < backup.sql
```
### Update Application
```bash
# Pull latest changes
git pull
# Rebuild and restart
docker-compose down
docker-compose up -d --build
```
### Reset Database
```bash
# Stop services
docker-compose down
# Remove database volume
docker volume rm motm_app_postgres_data
# Start fresh
docker-compose up -d
```
## Troubleshooting
### Common Issues
1. **Database connection errors**
- Check if PostgreSQL container is running: `docker-compose ps`
- Verify database credentials in environment variables
- Check database logs: `docker-compose logs postgres`
2. **Application won't start**
- Check application logs: `docker-compose logs motm-app`
- Verify all environment variables are set correctly
- Ensure database is healthy before application starts
3. **Port conflicts**
- Change port mapping in `docker-compose.yml`
- Example: `"8080:5000"` to use port 8080 instead of 5000
### Debug Mode
```bash
# Run container in interactive mode for debugging
docker run -it --rm \
-p 5000:5000 \
-e DATABASE_TYPE=postgresql \
-e DB_HOST=your-postgres-host \
-e DB_PORT=5432 \
-e DB_NAME=motm_db \
-e DB_USER=motm_user \
-e DB_PASSWORD=motm_password \
motm-app /bin/bash
```
## File Structure
```
motm_app/
├── Containerfile # Docker container definition
├── docker-compose.yml # Multi-service orchestration
├── .dockerignore # Files to exclude from build
├── init.sql # Database initialization script
├── requirements.txt # Python dependencies
├── main.py # Main application file
├── static/ # Static assets (CSS, JS, images)
├── templates/ # HTML templates
└── data/ # Persistent data directory
```
## Support
For issues or questions about container deployment, please check:
1. Application logs: `docker-compose logs motm-app`
2. Database logs: `docker-compose logs postgres`
3. Container status: `docker-compose ps`

View File

@ -34,9 +34,11 @@ FROM python:3.11-slim
ENV PYTHONDONTWRITEBYTECODE=1 \ ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \ PYTHONUNBUFFERED=1 \
PATH="/opt/venv/bin:$PATH" \ PATH="/opt/venv/bin:$PATH" \
DATABASE_TYPE=sqlite \ DATABASE_TYPE=postgresql \
FLASK_ENV=production \ FLASK_ENV=production \
FLASK_APP=main.py FLASK_APP=main.py \
FLASK_RUN_HOST=0.0.0.0 \
FLASK_RUN_PORT=5000
# Install runtime dependencies # Install runtime dependencies
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y \
@ -61,6 +63,24 @@ COPY --chown=appuser:appuser . .
RUN mkdir -p /app/data /app/logs && \ RUN mkdir -p /app/data /app/logs && \
chown -R appuser:appuser /app/data /app/logs chown -R appuser:appuser /app/data /app/logs
# Create a startup script for better initialization
RUN echo '#!/bin/bash\n\
# Wait for database to be ready\n\
echo "Waiting for database connection..."\n\
while ! python -c "import psycopg2; psycopg2.connect(host=\"$DB_HOST\", port=\"$DB_PORT\", user=\"$DB_USER\", password=\"$DB_PASSWORD\", dbname=\"$DB_NAME\")" 2>/dev/null; do\n\
echo "Database not ready, waiting..."\n\
sleep 2\n\
done\n\
echo "Database connection established!"\n\
\n\
# Initialize database if needed\n\
python -c "from db_setup import db_config_manager; db_config_manager.load_config(); db_config_manager._update_environment_variables()"\n\
\n\
# Start the application\n\
exec python main.py' > /app/start.sh && \
chmod +x /app/start.sh && \
chown appuser:appuser /app/start.sh
# Switch to non-root user # Switch to non-root user
USER appuser USER appuser
@ -72,4 +92,4 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:5000/ || exit 1 CMD curl -f http://localhost:5000/ || exit 1
# Default command # Default command
CMD ["python", "main.py"] CMD ["/app/start.sh"]

View File

@ -0,0 +1,69 @@
version: '3.8'
services:
# PostgreSQL Database
postgres:
image: postgres:15-alpine
container_name: motm-postgres
environment:
POSTGRES_DB: motm_db
POSTGRES_USER: motm_user
POSTGRES_PASSWORD: motm_password
volumes:
- postgres_data:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U motm_user -d motm_db"]
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
# MOTM Application
motm-app:
build:
context: .
dockerfile: Containerfile
container_name: motm-app
environment:
# Database configuration
DATABASE_TYPE: postgresql
DB_HOST: postgres
DB_PORT: 5432
DB_NAME: motm_db
DB_USER: motm_user
DB_PASSWORD: motm_password
# Flask configuration
FLASK_ENV: production
FLASK_APP: main.py
FLASK_RUN_HOST: 0.0.0.0
FLASK_RUN_PORT: 5000
# Security
SECRET_KEY: your-secret-key-change-this-in-production
ports:
- "5000:5000"
depends_on:
postgres:
condition: service_healthy
volumes:
- ./data:/app/data
- ./logs:/app/logs
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:5000/"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
volumes:
postgres_data:
driver: local
networks:
default:
name: motm-network

11
motm_app/init.sql Normal file
View File

@ -0,0 +1,11 @@
-- Database initialization script for MOTM application
-- This script runs when the PostgreSQL container starts for the first time
-- Create the database if it doesn't exist (this is handled by POSTGRES_DB env var)
-- But we can add any additional setup here
-- Create extensions if needed
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- The application will handle table creation through SQLAlchemy
-- This file is here for any additional database setup that might be needed