From 5b0a0292e10d0a35678131b6ee59c546e3e555f4 Mon Sep 17 00:00:00 2001 From: Aron Date: Thu, 20 Nov 2025 12:53:12 +0100 Subject: [PATCH] service change --- .env.example | 9 ++++ PRODUCTION.md | 115 +++++++++++++++++++++++++++++++++++++++++++++ app/main.py | 11 +++-- autokanban.service | 4 ++ 4 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 .env.example create mode 100644 PRODUCTION.md diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..869f3d7 --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +# AutoKanban Environment Configuration +# Copy this file to .env and update with your values + +# Admin password for task approval and management +ADMIN_PASSWORD=your_secure_password_here + +# Session secret key for secure cookie signing +# Generate a random string for production (e.g., openssl rand -hex 32) +SESSION_SECRET=your_random_secret_key_here_at_least_32_characters_long diff --git a/PRODUCTION.md b/PRODUCTION.md new file mode 100644 index 0000000..5231aa5 --- /dev/null +++ b/PRODUCTION.md @@ -0,0 +1,115 @@ +# Production Deployment Checklist + +## Before Going Live + +### 1. Environment Configuration + +- [ ] Copy `.env.example` to `.env` on the Pi +- [ ] Set a strong `ADMIN_PASSWORD` in `.env` +- [ ] Generate and set `SESSION_SECRET` in `.env`: + + ```bash + # On the Pi, generate a secure random key: + openssl rand -hex 32 + # Copy the output and add to .env as SESSION_SECRET + ``` + +### 2. Security + +- [ ] Ensure `.env` is in `.gitignore` (already done) +- [ ] Never commit `.env` file to git +- [ ] Change default admin password from example + +### 3. Hardware Setup + +- [ ] Thermal printer connected via GPIO (Yellow to Pin 8, GND to Pin 6) +- [ ] External 9V power supply for printer +- [ ] Verify printer works: `echo -e "\x1B\x40Test\n\n\n" > /dev/serial0` +- [ ] Serial port enabled: `ls -la /dev/serial0` should point to ttyAMA0 + +### 4. Service Configuration + +- [ ] Run `./setup_service.sh` to enable autostart +- [ ] Verify service is running: `sudo systemctl status autokanban` +- [ ] Test autostart by rebooting: `sudo reboot` + +### 5. Network Access + +- [ ] Set hostname to `autokanban`: `sudo hostnamectl set-hostname autokanban` +- [ ] Access app at `http://autokanban.local:8000` +- [ ] Verify 124 Hub link in header works + +### 6. Production Settings Verified + +- [ ] `ENABLE_PHYSICAL_PRINTER = True` in `app/main.py` +- [ ] Debug print statements removed +- [ ] Session secret loaded from environment variable +- [ ] Service runs without `--reload` flag + +### 7. Testing + +- [ ] Submit a test task +- [ ] Approve task as admin +- [ ] Print task and verify: + - [ ] Preview image shows on website + - [ ] Physical card prints with formatting + - [ ] Icon appears correctly + - [ ] Text is readable (larger fonts) +- [ ] Test "Erneut drucken" (reprint) function +- [ ] Test service restart: `sudo systemctl restart autokanban` + +### 8. Monitoring + +- [ ] Check logs: `sudo journalctl -u autokanban -f` +- [ ] Verify no errors in startup +- [ ] Test graceful shutdown: `sudo systemctl stop autokanban` + +## Post-Deployment + +### Regular Maintenance + +```bash +# Update code +cd ~/autokanban +git pull +sudo systemctl restart autokanban + +# View logs +sudo journalctl -u autokanban -n 100 + +# Check service status +sudo systemctl status autokanban + +# Backup tasks +cp data/tasks.json data/tasks.json.backup +``` + +### Troubleshooting + +```bash +# Service won't start +sudo journalctl -u autokanban -n 50 + +# Printer not working +ls -la /dev/serial0 +stty -F /dev/serial0 + +# Check if port is accessible +curl http://localhost:8000 + +# Restart service +sudo systemctl restart autokanban +``` + +## Current Configuration + +- **Port**: 8000 +- **Hostname**: autokanban.local +- **Access URL**: +- **Printer**: GPIO serial (/dev/serial0, 19200 baud) +- **Service**: Systemd with autostart enabled +- **Data**: JSON file at `data/tasks.json` +- **Previews**: PNG files in `out/` + +--- +Last updated: 2025-11-20 diff --git a/app/main.py b/app/main.py index 5f174a2..b161d2e 100644 --- a/app/main.py +++ b/app/main.py @@ -14,8 +14,13 @@ from app.models import Task from dotenv import load_dotenv from PIL import Image, ImageDraw, ImageFont +# Load environment variables +load_dotenv() + app = FastAPI() -app.add_middleware(SessionMiddleware, secret_key="CHANGE_THIS_SECRET") +# Use environment variable for session secret, fallback to random for dev +SESSION_SECRET = os.getenv("SESSION_SECRET", "CHANGE_THIS_SECRET") +app.add_middleware(SessionMiddleware, secret_key=SESSION_SECRET) templates = Jinja2Templates(directory="app/templates") app.mount("/static", StaticFiles(directory="app/static"), name="static") app.mount("/out", StaticFiles(directory="out"), name="out") @@ -240,7 +245,6 @@ def print_task(request: Request, task_id: str): # Print to physical printer if enabled if ENABLE_PHYSICAL_PRINTER: - print(f"[DEBUG] Attempting to print to {PRINTER_DEVICE} at {PRINTER_BAUDRATE} baud...") try: printer = escpos.printer.Serial(devfile=PRINTER_DEVICE, baudrate=PRINTER_BAUDRATE, timeout=1) @@ -249,13 +253,10 @@ def print_task(request: Request, task_id: str): printer.text("\n\n") # Add some spacing after image printer.cut() - print(f"[DEBUG] Print command sent successfully") except Exception as print_err: print(f"[ERROR] Printer failed: {print_err}") import traceback traceback.print_exc() - else: - print(f"[DEBUG] Physical printer disabled, skipping print") task.status = "printed" msg = "success" diff --git a/autokanban.service b/autokanban.service index 2ceda95..7f9c129 100644 --- a/autokanban.service +++ b/autokanban.service @@ -1,12 +1,16 @@ [Unit] Description=AutoKanban Thermal Printer Web App After=network.target +# Wait for system to fully boot before starting +After=multi-user.target [Service] Type=simple User=pi Group=pi WorkingDirectory=/home/pi/autokanban +# Add delay to avoid boot messages on serial port +ExecStartPre=/bin/sleep 10 Environment="PATH=/home/pi/.pyenv/versions/autokanban/bin:/usr/local/bin:/usr/bin:/bin" Environment="PYENV_ROOT=/home/pi/.pyenv" ExecStart=/home/pi/.pyenv/versions/autokanban/bin/python /home/pi/autokanban/start_app.py