The Blue Pill - Automation Service v1.0 - Deployment Manual
Version: 1.0 (Production Hardened Release) Objective: To deploy a robust, secure, and production-ready automated attendance service. This guide incorporates critical fixes for concurrency, session management, timezone consistency, and security hardening.
v1.0 Key Features
- Database Concurrency: SQLite is now configured with WAL (Write-Ahead Logging) mode and a busy timeout, making it resilient to concurrent access from the API and Worker processes.
- Correct Session Management: SQLAlchemy session handling is corrected using a
scoped_session
to prevent thread-related errors. - Timezone-Aware Idempotency: Success flags are now set using the correct timezone-aware date, ensuring logic is sound regardless of the server’s local time.
- Hardened CORS Policy: Cross-Origin Resource Sharing is restricted to your specific domain, preventing unauthorized cross-site API calls.
- Smarter Worker Logic: The worker now correctly treats
NO_CLASS
andNO_ACTION_NEEDED
as successful outcomes for the day, preventing unnecessary retries within a time window. - Enhanced Security:
systemd
services are hardened with sandboxing options to limit their capabilities and potential attack surface.
Part 1: Initial Server Setup
Connect to your Ubuntu 24.04 LTS VPS via SSH with sudo
privileges.
1.1. Update System
|
|
1.2. Install Core Dependencies
|
|
1.3. Configure Firewall (UFW)
|
|
When prompted, press y
and Enter
.
Part 2: Backend Application Deployment
2.1. Create Project Directory Structure
|
|
2.2. Create requirements.txt
|
|
Copy and paste the following content:
|
|
2.3. Create .env
Configuration File
|
|
Copy and paste the following. You must generate and replace the APP_SECRET_KEY
value.
# Generate a new key with: python3 -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
APP_SECRET_KEY=REPLACE_THIS_WITH_YOUR_GENERATED_KEY
# Application Configuration
APP_DB_PATH=/var/www/seiue-app/backend/automation.db
APP_TIMEZONE=Asia/Shanghai
# Attendance Time Windows
AM_WINDOW_START=08:00
AM_WINDOW_END=12:30
PM_WINDOW_START=13:00
PM_WINDOW_END=20:30
Set secure permissions for this file:
|
|
2.4. Create Backend Python Modules (with All Patches Applied)
A. seiue_engine.py
(The Core Engine)
This is your original, unmodified core logic file, renamed for use as a library.
|
|
Paste the complete, unmodified code below:
|
|
B. crypto.py
(Encryption Utility)
This file is new to the architecture but essential for security.
|
|
Paste the complete code below:
|
|
C. models.py
(Database Models - Hardened)
This version includes fixes for SQLite concurrency (WAL) and correct session management (scoped_session).
|
|
Paste the complete code below:
|
|
D. app.py
(Flask API - Hardened)
This version includes the CORS hardening fix and now works correctly with scoped_session
.
|
|
Paste the complete code below:
|
|
E. worker.py
(Background Worker - Hardened)
This version includes fixes for timezone correctness and smarter success semantics.
|
|
Paste the complete code below:
|
|
2.5. Install Dependencies & Set Permissions
|
|
Part 3: Systemd Service Persistence (Hardened)
3.1. API Service (Hardened)
|
|
Paste the complete configuration below:
|
|
3.2. Worker Service (Hardened)
|
|
Paste the complete configuration below:
|
|
3.3. Enable and Start Services
|
|
Part 4: Frontend & Nginx Configuration
4.1. Create Frontend Enrollment Page
|
|
Paste the complete HTML code below:
|
|
4.2. Configure Nginx (with Security Headers)
|
|
Paste the complete configuration below:
|
|
4.3. Enable Nginx Site
|
|
Part 5: DNS & SSL/TLS (Cloudflare)
- DNS: Create an
A
record forseiue.bdfz.net
pointing to your VPS IP, with proxy status Proxied (Orange Cloud). - SSL/TLS: Set encryption mode to Full (Strict). This is the most secure option and requires a valid certificate on your server (as noted in step 4.2). A free Cloudflare Origin CA certificate is the easiest way to satisfy this.
Part 6: Final Testing and Sanity Checks
- Visit
https://seiue.bdfz.net
to enroll a user. - Tail Worker Logs:
sudo journalctl -u seiue-worker -f --no-pager
- Verify Database:
1 2 3 4 5
# Check users table sudo sqlite3 /var/www/seiue-app/backend/automation.db "SELECT id,username,is_active,last_am_success_date,last_pm_success_date FROM users;" # Check latest run logs sudo sqlite3 /var/www/seiue-app/backend/automation.db "SELECT user_id,period,run_at,status FROM run_logs ORDER BY id DESC LIMIT 10;"
- Smoke Test API:
curl -I -X POST https://seiue.bdfz.net/api/enroll
(This should be blocked by CORS unless run from your domain, which is correct behavior).
This v1.0 manual provides the complete steps to deploy a production-ready, secure, and resilient automation service.