#!/bin/bash
cd /root/firefrost-work/firefrost-operations-manual

# Document 1: Migration Master Plan
cat > docs/dedis-to-vps-migration-plan.md << 'PLAN'
# 🔥❄️ FIREFROST GAMING: DEDIS → VPS MIGRATION PLAN

**Created:** February 9, 2026  
**Status:** Planning Phase  
**Priority:** 🔴 Critical - Blocks Phase 0.5 Completion

---

## Executive Summary

**DECISION:** Move ALL management services from dedicated servers (TX1/NC1) back to VPS tier.

**REASON:** Dedicated servers have excessive networking complexity:
- Manual IP assignment required
- Routing issues from internet
- SSL certificate challenges
- VPS = plug-and-play, Dedis = DIY everything

**GOAL:** TX1 and NC1 become **game servers ONLY**. All management infrastructure on VPS tier where networking "just works."

---

## Current State (What We Built)

### On TX1 Dallas (Dedicated):
1. ✅ Gitea (git.firefrostgaming.com) - Port 3000, IP 74.63.218.202
2. ✅ Uptime Kuma (status.firefrostgaming.com) - Port 3001, IP 74.63.218.203
3. ✅ MkDocs (docs.firefrostgaming.com) - Port 8000, IP 74.63.218.204
4. ❌ Wiki.js (FAILED) - Port 3000 conflict, IP 74.63.218.205 not routable

### Issues Discovered:
- Command Center /29 block (74.63.218.201-206) not routed from internet
- Manual IP assignment required (doesn't survive reboot without netplan config)
- Port conflicts (Gitea on 3000, Wiki.js also wants 3000)
- Wiki.js crashing on startup (exit code 1)
- SSL certificates work but routing doesn't

---

## Target State (Where We're Going)

### VPS Allocation Strategy

**OPTION A: Spread Services Across Existing VPS (Recommended)**

| VPS | Current Use | Add Services | Reasoning |
|-----|-------------|--------------|-----------|
| **Panel** (45.94.168.138) | Pterodactyl Panel | - | Leave dedicated to Panel (performance) |
| **Command Center** (63.143.34.217) | Reserved for DDoS | Gitea + Uptime Kuma | Management hub (original purpose) |
| **Billing** (38.68.14.188) | Paymenter | - | Keep billing isolated |
| **Ghost** (64.50.188.14) | Ghost CMS | MkDocs + Wiki.js + NextCloud | Documentation cluster |

**OPTION B: Consolidate on Command Center (Alternative)**

| VPS | Services | Reasoning |
|-----|----------|-----------|
| **Command Center** (63.143.34.217) | Gitea + Uptime Kuma + MkDocs + Wiki.js + NextCloud | Single management hub |
| Others | Original purposes | Simpler, all management in one place |

**OPTION C: New VPS for Management (Cleanest)**

| Server | Purpose | Cost |
|--------|---------|------|
| **NEW: Management VPS** | All 5 Phase 0.5 services | ~$10-15/month |
| Existing VPS | Original purposes | No changes |
| TX1/NC1 | Game servers only | Simplified |

---

## Migration Phases

### Phase M1: Planning & Preparation (TODAY)
- ✅ Document current state
- ✅ Choose VPS allocation strategy
- ⏳ Create migration checklist
- ⏳ Plan DNS cutover strategy
- ⏳ Document rollback procedures

### Phase M2: Gitea Migration (Day 1)
**Target:** Command Center VPS (63.143.34.217)

**Steps:**
1. Backup Gitea data from TX1 (`/var/lib/gitea`)
2. Export Gitea database (SQLite dump)
3. Install Gitea on Command Center VPS
4. Restore data and database
5. Update DNS: git.firefrostgaming.com → 63.143.34.217
6. SSL certificate (easy on VPS - no routing issues)
7. Test and verify
8. Decommission TX1 Gitea

**Downtime:** ~10-15 minutes (DNS propagation)

### Phase M3: Uptime Kuma Migration (Day 1)
**Target:** Command Center VPS (63.143.34.217)

**Steps:**
1. Export Uptime Kuma config/monitors
2. Install on Command Center VPS
3. Import monitors
4. Update DNS: status.firefrostgaming.com → 63.143.34.217
5. SSL certificate
6. Reconfigure Discord webhook (if needed)
7. Test monitoring
8. Decommission TX1 Uptime Kuma

**Downtime:** ~5 minutes

### Phase M4: MkDocs Migration (Day 1-2)
**Target:** Ghost VPS (64.50.188.14) OR Command Center

**Steps:**
1. Copy MkDocs source files
2. Install MkDocs + Material theme
3. Configure build webhook
4. Update DNS: docs.firefrostgaming.com → [target VPS]
5. SSL certificate
6. Test site build
7. Decommission TX1 MkDocs

**Downtime:** ~5 minutes

### Phase M5: Wiki.js Fresh Deploy (Day 2)
**Target:** Ghost VPS (64.50.188.14) OR Command Center

**Steps:**
1. Fresh Wiki.js install (no migration needed - never worked)
2. Dual domain config: subscribers + staff
3. DNS: Both → [target VPS]
4. SSL certificates (single command, no routing issues)
5. Complete setup wizard
6. Configure Git sync with Gitea

**Downtime:** None (new service)

### Phase M6: NextCloud Deploy (Day 2-3)
**Target:** Ghost VPS (64.50.188.14) OR Command Center

**Steps:**
1. Install NextCloud
2. Configure storage for world downloads
3. DNS: downloads.firefrostgaming.com → [target VPS]
4. SSL certificate
5. Configure external storage (if needed)
6. Test upload/download

**Downtime:** None (new service)

---

## DNS Changes Required

**Before Migration:**
| Domain | Current IP | Type |
|--------|-----------|------|
| git.firefrostgaming.com | 38.68.14.26 (TX1) | A |
| status.firefrostgaming.com | 38.68.14.26 (TX1) | A |
| docs.firefrostgaming.com | 38.68.14.26 (TX1) | A |

**After Migration (Option A - Spread):**
| Domain | New IP | Type |
|--------|--------|------|
| git.firefrostgaming.com | 63.143.34.217 (Command Center) | A |
| status.firefrostgaming.com | 63.143.34.217 (Command Center) | A |
| docs.firefrostgaming.com | 64.50.188.14 (Ghost) | A |
| subscribers.firefrostgaming.com | 64.50.188.14 (Ghost) | A |
| staff.firefrostgaming.com | 64.50.188.14 (Ghost) | A |
| downloads.firefrostgaming.com | 64.50.188.14 (Ghost) | A |

**After Migration (Option B - Consolidate):**
| Domain | New IP | Type |
|--------|--------|------|
| All management services | 63.143.34.217 (Command Center) | A |

---

## Rollback Plan

**If migration fails:**
1. Services still running on TX1 (don't decommission until verified)
2. Revert DNS changes in Cloudflare (instant)
3. Old services continue working
4. Max downtime: DNS TTL (5 minutes)

**Safety net:**
- All TX1 services stay running during migration
- Only decommission after 24 hours of stable operation
- Keep backups of TX1 data for 7 days

---

## Benefits of Migration

### Technical Benefits:
- ✅ No manual IP assignment
- ✅ No routing issues
- ✅ SSL certificates "just work"
- ✅ Simpler networking (everything on standard ports)
- ✅ Faster deployments (no dedi complexity)

### Operational Benefits:
- ✅ TX1/NC1 dedicated to game servers (cleaner architecture)
- ✅ Management services isolated from game server load
- ✅ Easier troubleshooting (VPS networking predictable)
- ✅ Less hand strain for Michael (fewer manual operations)

### Psychological Benefits:
- ✅ No more fighting with dedi networking
- ✅ Services deploy in minutes, not hours
- ✅ Predictable, reliable infrastructure
- ✅ Focus on game servers, not infrastructure headaches

---

## Cost Analysis

**Current Setup:** $0 additional (using TX1 resources)

**Option A (Spread):** $0 additional (using existing VPS)

**Option B (Consolidate):** $0 additional (using Command Center)

**Option C (New VPS):** ~$10-15/month
- Pros: Cleanest separation, dedicated management server
- Cons: Additional cost

**Recommendation:** Option A or B (no additional cost)

---

## Timeline Estimate

**Conservative (Safe):**
- Day 1: Gitea + Uptime Kuma migration (3-4 hours)
- Day 2: MkDocs + Wiki.js deployment (2-3 hours)
- Day 3: NextCloud deployment (1-2 hours)
- **Total:** 3 days, 6-9 hours work

**Aggressive (If marathon session):**
- Day 1: All 5 services (6-8 hours)
- Day 2: Testing and verification
- **Total:** 1-2 days

---

## Risk Assessment

| Risk | Probability | Impact | Mitigation |
|------|------------|--------|------------|
| Data loss during migration | Low | High | Full backups before migration |
| DNS propagation delay | Medium | Low | Low TTL, staged cutover |
| SSL certificate issues | Low | Medium | VPS networking reliable |
| Port conflicts | Low | Low | Plan port allocation upfront |
| Service crashes | Low | Medium | Keep TX1 services running as backup |

---

## Decision Point

**MICHAEL: Which option do you prefer?**

**Option A:** Spread services (Command Center + Ghost)
- Gitea + Uptime Kuma on Command Center
- MkDocs + Wiki.js + NextCloud on Ghost

**Option B:** Consolidate all on Command Center
- All 5 services on one VPS
- Simpler management, single point

**Option C:** New dedicated management VPS
- Cleanest architecture
- ~$10-15/month additional cost

**My Recommendation:** Option A (spread)
- Balances load across existing VPS
- No additional cost
- Ghost VPS currently underutilized

---

**Fire + Frost = Where Passion Meets Precision** 🔥❄️

**Next Step:** Michael reviews and chooses option, then we execute migration plan.
PLAN

# Document 2: Migration Checklist
cat > docs/migration-checklist.md << 'CHECKLIST'
# Migration Execution Checklist

## Pre-Migration (Do Once)

- [ ] Choose VPS allocation option (A, B, or C)
- [ ] Verify all VPS accessible via SSH
- [ ] Create backup directory structure
- [ ] Set DNS TTL to 300 seconds (5 minutes) in Cloudflare
- [ ] Announce maintenance window in Discord

---

## Service 1: Gitea Migration

### Backup Phase
- [ ] Stop Gitea on TX1: `systemctl stop gitea`
- [ ] Backup Gitea data: `tar -czf gitea-backup.tar.gz /var/lib/gitea`
- [ ] Backup Gitea DB: `sqlite3 /var/lib/gitea/gitea.db .dump > gitea-dump.sql`
- [ ] Copy backups off TX1

### Deploy Phase
- [ ] Install Gitea on target VPS
- [ ] Restore data directory
- [ ] Restore database
- [ ] Update `app.ini` with new domain/IP
- [ ] Start Gitea service
- [ ] Test locally: `curl -I http://localhost:3000`

### DNS Cutover
- [ ] Update Cloudflare: git.firefrostgaming.com → [target VPS IP]
- [ ] Wait for propagation (5 min)
- [ ] Get SSL certificate: `certbot --nginx -d git.firefrostgaming.com`
- [ ] Test externally: Browse to https://git.firefrostgaming.com

### Verification
- [ ] Login works
- [ ] Repositories accessible
- [ ] Push/pull works
- [ ] Webhooks functional

### Cleanup
- [ ] Wait 24 hours
- [ ] Stop TX1 Gitea: `systemctl stop gitea`
- [ ] Disable TX1 Gitea: `systemctl disable gitea`
- [ ] Archive TX1 Gitea data

---

## Service 2: Uptime Kuma Migration

### Backup Phase
- [ ] Export monitors from Uptime Kuma web UI
- [ ] Backup data: `tar -czf kuma-backup.tar.gz /opt/uptime-kuma/data`

### Deploy Phase
- [ ] Install Uptime Kuma on target VPS
- [ ] Import monitors
- [ ] Configure Discord webhook
- [ ] Test monitoring

### DNS Cutover
- [ ] Update Cloudflare: status.firefrostgaming.com → [target VPS IP]
- [ ] Get SSL certificate
- [ ] Test externally

### Verification
- [ ] All monitors active
- [ ] Discord notifications working
- [ ] Status page accessible

---

## Service 3: MkDocs Migration

### Backup Phase
- [ ] Copy MkDocs source: `tar -czf mkdocs-source.tar.gz /var/www/docs`

### Deploy Phase
- [ ] Install MkDocs + Material theme
- [ ] Restore source files
- [ ] Configure nginx
- [ ] Build site: `mkdocs build`

### DNS Cutover
- [ ] Update Cloudflare: docs.firefrostgaming.com → [target VPS IP]
- [ ] Get SSL certificate
- [ ] Test externally

### Verification
- [ ] Site loads correctly
- [ ] Search works
- [ ] All pages accessible

---

## Service 4: Wiki.js Fresh Deploy

### Deploy Phase
- [ ] Install Node.js 18+
- [ ] Install Wiki.js 2.5.303
- [ ] Create dual Nginx configs (subscribers + staff)
- [ ] Configure Wiki.js

### DNS Cutover
- [ ] Update Cloudflare: subscribers.firefrostgaming.com → [target VPS IP]
- [ ] Update Cloudflare: staff.firefrostgaming.com → [target VPS IP]
- [ ] Get SSL certificates (single command for both domains)
- [ ] Test externally

### Setup
- [ ] Complete setup wizard
- [ ] Create admin account
- [ ] Configure groups (Subscribers, Staff, Admin)
- [ ] Set up Git sync with Gitea

---

## Service 5: NextCloud Deploy

### Deploy Phase
- [ ] Install NextCloud
- [ ] Configure database
- [ ] Set up storage directory
- [ ] Configure nginx

### DNS Cutover
- [ ] Update Cloudflare: downloads.firefrostgaming.com → [target VPS IP]
- [ ] Get SSL certificate
- [ ] Test externally

### Setup
- [ ] Create admin account
- [ ] Configure external storage (if needed)
- [ ] Test upload/download
- [ ] Set up retention policy (30-day for non-premium, 90-day for premium)

---

## Post-Migration

- [ ] All services responding on new IPs
- [ ] SSL certificates auto-renewing
- [ ] Monitoring confirms 100% uptime
- [ ] Discord announcement: Migration complete
- [ ] Wait 24-48 hours before TX1 cleanup
- [ ] Update all documentation with new IPs
- [ ] Remove Command Center IPs from TX1 interface
- [ ] Document lessons learned

---

**Total Estimated Time:** 6-9 hours (spread across 1-3 days)
CHECKLIST

# Document 3: Rollback Procedures
cat > docs/migration-rollback-plan.md << 'ROLLBACK'
# Migration Rollback Plan

## If Things Go Wrong

### Immediate Rollback (During Migration)

**If service fails on new VPS:**
1. DO NOT stop TX1 service
2. Revert DNS in Cloudflare (instant)
3. TX1 service continues working
4. Debug VPS issue offline

**Maximum Downtime:** DNS TTL (5 minutes)

---

## Service-Specific Rollback

### Gitea Rollback
```bash
# On Cloudflare
git.firefrostgaming.com A record → 38.68.14.26 (TX1)

# On TX1
systemctl start gitea
```

### Uptime Kuma Rollback
```bash
# On Cloudflare
status.firefrostgaming.com A record → 38.68.14.26 (TX1)

# On TX1
systemctl start uptime-kuma
```

### MkDocs Rollback
```bash
# On Cloudflare
docs.firefrostgaming.com A record → 38.68.14.26 (TX1)

# On TX1
systemctl start mkdocs
```

---

## Data Recovery

### If Backup Needed
All backups stored in `/root/migration-backups/` on TX1:
- `gitea-backup.tar.gz` + `gitea-dump.sql`
- `kuma-backup.tar.gz`
- `mkdocs-source.tar.gz`

**Retention:** 7 days after successful migration

---

## Lessons Learned Log

**What went wrong:**
- 

**Root cause:**
- 

**How we fixed it:**
- 

**How to prevent next time:**
- 

---

**Remember:** Don't panic. TX1 services are still running. DNS changes are instant.
ROLLBACK

# Commit all documents
git add docs/dedis-to-vps-migration-plan.md docs/migration-checklist.md docs/migration-rollback-plan.md
git commit -m "Migration Plan: Move management services from TX1/NC1 dedis to VPS tier"
git push

echo "Migration planning documents created successfully!"
