diff --git a/V2/app/.dockerignore b/V2/app/.dockerignore index 8b0e4c6..c8e4b7e 100644 --- a/V2/app/.dockerignore +++ b/V2/app/.dockerignore @@ -1,6 +1,7 @@ node_modules .env *.sqlite +*.sqlite-* uploads/ tmp/ pino-*.log diff --git a/V2/app/Dockerfile b/V2/app/Dockerfile index d7ada03..5fba4f1 100644 --- a/V2/app/Dockerfile +++ b/V2/app/Dockerfile @@ -1,11 +1,12 @@ # ── Stage 1: install production dependencies ────────────────────────────────── -FROM node:20-alpine AS deps +ARG NODE_VERSION=20 +FROM node:${NODE_VERSION}-alpine AS deps WORKDIR /app COPY package*.json ./ RUN npm ci --omit=dev && npm cache clean --force # ── Stage 2: runtime image ──────────────────────────────────────────────────── -FROM node:20-alpine +FROM node:${NODE_VERSION}-alpine # tini: PID 1 signal forwarding + zombie reaping RUN apk add --no-cache tini diff --git a/V2/app/app.js b/V2/app/app.js index 2a95756..1574701 100644 --- a/V2/app/app.js +++ b/V2/app/app.js @@ -134,7 +134,7 @@ async function boot() { initSocket(server); const port = parseInt(process.env.PORT || '3000', 10); - server.listen(port, () => { + server.listen(port, process.env.HOST || undefined, () => { logger.info(`VitFit v${require('./package.json').version} listening on port ${port}`); if (process.send) process.send('ready'); // PM2 ready signal }); diff --git a/V2/app/modules/org/routes.js b/V2/app/modules/org/routes.js index 784196f..822a75b 100644 --- a/V2/app/modules/org/routes.js +++ b/V2/app/modules/org/routes.js @@ -2,9 +2,7 @@ const express = require('express'); const multer = require('multer'); -const path = require('path'); const os = require('os'); -const fs = require('fs'); const { models } = require('../../core/db'); const { authenticateToken, authenticateRole } = require('../../core/auth'); const { ok, noContent } = require('../../core/respond'); @@ -80,18 +78,24 @@ router.post('/logo', authenticateToken, authenticateRole(OWNERS), upload.single( } catch (err) { next(err); } }); -router.get('/logo', authenticateToken, async (req, res, next) => { +// Public: logos are rendered by (login page, banner), which cannot send +// an Authorization header. Org id comes from the path; legacy /logo falls back to +// the first org, matching GET /branding/public. +router.get('/logo/:orgId?', async (req, res, next) => { try { - const branding = await svc.getBranding(models, req.user.organizationId); - if (!branding.logoPath) return next(new AppError('NOT_FOUND', 'No custom logo set.', 404)); + let orgId = parseInt(req.params.orgId, 10); + if (isNaN(orgId)) { + const org = await models.Organization.findOne({ order: [['id', 'ASC']] }); + if (!org) return next(new AppError('NOT_FOUND', 'No custom logo set.', 404)); + orgId = org.id; + } - // Find logo file in uploads dir - const uploadsDir = process.env.UPLOADS_DIR || path.join(__dirname, '..', '..', 'uploads'); - const files = fs.readdirSync(uploadsDir).filter(f => f.startsWith(`logo_${req.user.organizationId}_`)); - if (!files.length) return next(new AppError('NOT_FOUND', 'Logo file not found.', 404)); + const filePath = svc.findLogoFile(orgId); + if (!filePath) return next(new AppError('NOT_FOUND', 'No custom logo set.', 404)); - const logoFile = files.sort().pop(); // latest - res.sendFile(path.join(uploadsDir, logoFile)); + // Path is versioned (?v=), so the file at a given URL never changes + res.set('Cache-Control', 'public, max-age=31536000, immutable'); + res.sendFile(filePath); } catch (err) { next(err); } }); diff --git a/V2/app/modules/org/service.js b/V2/app/modules/org/service.js index 7ba6aa0..9e6018a 100644 --- a/V2/app/modules/org/service.js +++ b/V2/app/modules/org/service.js @@ -5,7 +5,12 @@ const path = require('path'); const { AppError } = require('../../core/errors'); const { log: auditLog } = require('../../core/audit'); -const UPLOADS_DIR = () => process.env.UPLOADS_DIR || path.join(__dirname, '..', '..', 'uploads'); +// Resolved to an absolute path: res.sendFile() rejects relative paths, and +// UPLOADS_DIR is commonly configured as './uploads'. +// file-type's documented minimum sniff size +const SNIFF_BYTES = 4100; + +const UPLOADS_DIR = () => path.resolve(process.env.UPLOADS_DIR || path.join(__dirname, '..', '..', 'uploads')); async function getOrg(models, orgId) { @@ -42,6 +47,17 @@ async function updateBranding(models, req, orgId, data) { return branding; } +async function moveFile(src, dest) { + try { + await fs.promises.rename(src, dest); + } catch (err) { + // Cross-device rename (systemd PrivateTmp, docker tmpfs, separate volumes) + if (err.code !== 'EXDEV') throw err; + await fs.promises.copyFile(src, dest); + await fs.promises.unlink(src).catch(() => {}); + } +} + async function uploadLogo(models, req, orgId, file) { const { fileTypeFromBuffer } = await import('file-type'); const ALLOWED = ['image/png', 'image/jpeg', 'image/gif', 'image/webp']; @@ -49,11 +65,13 @@ async function uploadLogo(models, req, orgId, file) { let detectedMime; try { + // file-type needs 4100 bytes: detectors read past the magic number (a PNG + // header alone makes it run off the end and throw EndOfStreamError). const fd = fs.openSync(file.path, 'r'); - const buf = Buffer.alloc(12); - fs.readSync(fd, buf, 0, 12, 0); + const buf = Buffer.alloc(SNIFF_BYTES); + const read = fs.readSync(fd, buf, 0, SNIFF_BYTES, 0); fs.closeSync(fd); - const result = await fileTypeFromBuffer(buf); + const result = await fileTypeFromBuffer(buf.subarray(0, read)); detectedMime = result?.mime; } catch { await fs.promises.unlink(file.path).catch(() => {}); @@ -65,23 +83,65 @@ async function uploadLogo(models, req, orgId, file) { throw new AppError('UPLOAD_TYPE_INVALID', 'Only PNG, JPG, GIF, and WebP images are supported.', 400); } + const uploadsDir = UPLOADS_DIR(); + const version = Date.now(); const ext = EXT_MAP[detectedMime]; - const safeName = `logo_${orgId}_${Date.now()}${ext}`; - const destPath = path.join(UPLOADS_DIR(), safeName); + const safeName = `logo_${orgId}_${version}${ext}`; + + try { + await fs.promises.mkdir(uploadsDir, { recursive: true }); + } catch (err) { + await fs.promises.unlink(file.path).catch(() => {}); + throw new AppError('UPLOAD_FAILED', `Uploads directory is not writable (${uploadsDir}): ${err.code}.`, 500); + } const branding = await getBranding(models, orgId); - // Delete old logo - if (branding.logoPath) { - const oldFile = path.join(UPLOADS_DIR(), path.basename(branding.logoPath)); - await fs.promises.unlink(oldFile).catch(() => {}); + try { + await moveFile(file.path, path.join(uploadsDir, safeName)); + } catch (err) { + await fs.promises.unlink(file.path).catch(() => {}); + throw new AppError('UPLOAD_FAILED', `Could not store the logo (${err.code}).`, 500); } - await fs.promises.rename(file.path, destPath); - await branding.update({ logoPath: `/api/v1/org/logo` }); + // Remove previous logos for this org (filenames, not the API path, are the source of truth) + await removeOldLogos(uploadsDir, orgId, safeName); + + await branding.update({ logoPath: `/api/v1/org/logo/${orgId}?v=${version}` }); await auditLog(req, 'logo.upload', 'branding', branding.id, { filename: safeName }); return branding; } -module.exports = { getOrg, updateOrg, getBranding, updateBranding, uploadLogo }; +async function removeOldLogos(uploadsDir, orgId, keepName) { + try { + const files = await fs.promises.readdir(uploadsDir); + await Promise.all( + files + .filter(f => f.startsWith(`logo_${orgId}_`) && f !== keepName) + .map(f => fs.promises.unlink(path.join(uploadsDir, f)).catch(() => {})) + ); + } catch { /* non-fatal */ } +} + +function findLogoFile(orgId) { + const uploadsDir = UPLOADS_DIR(); + let files; + try { + files = fs.readdirSync(uploadsDir); + } catch { + return null; + } + const matches = files + .filter(f => f.startsWith(`logo_${orgId}_`)) + .sort((a, b) => logoTimestamp(a) - logoTimestamp(b)); + if (!matches.length) return null; + return path.join(uploadsDir, matches[matches.length - 1]); +} + +function logoTimestamp(filename) { + const m = filename.match(/^logo_\d+_(\d+)\./); + return m ? parseInt(m[1], 10) : 0; +} + +module.exports = { getOrg, updateOrg, getBranding, updateBranding, uploadLogo, findLogoFile }; diff --git a/V2/app/public/profile.html b/V2/app/public/profile.html index cee9b96..d6a19a1 100644 --- a/V2/app/public/profile.html +++ b/V2/app/public/profile.html @@ -108,7 +108,7 @@ const btn = document.getElementById('password-btn'); btn.disabled = true; btn.textContent = 'Changing…'; try { - const res = await Api.post('/api/v1/my/change-password', { + const res = await Api.put('/api/v1/my/password', { currentPassword: document.getElementById('current-password').value, newPassword: newPw, }); diff --git a/V2/app/public/settings.html b/V2/app/public/settings.html index 91a2f91..7cdccae 100644 --- a/V2/app/public/settings.html +++ b/V2/app/public/settings.html @@ -58,7 +58,7 @@
- +
@@ -271,8 +271,8 @@ const { data } = await res.json(); el.innerHTML = ` ${data.map(u => ` - - + +
NameEmailRoleStatus
${u.name}${u.email}${[u.firstName, u.lastName].filter(Boolean).join(' ') || u.username}${u.email || '—'} ${u.role} ${u.isActive ? 'Active' : 'Inactive'} @@ -392,6 +392,12 @@ } } catch {} + // Server errors are shaped { error: { code, message } } + async function errMessage(res) { + try { const b = await res.json(); return b.error?.message || b.message || 'Failed.'; } + catch { return `Failed (HTTP ${res.status}).`; } + } + function hexOrUndefined(id) { const v = document.getElementById(id).value.trim(); return /^#[0-9A-Fa-f]{6}$/.test(v) ? v : undefined; } document.getElementById('branding-form').addEventListener('submit', async (e) => { @@ -426,10 +432,14 @@ const fd = new FormData(); fd.append('logo', logoFile); const logoRes = await Api.upload('/api/v1/org/logo', fd); - if (!logoRes.ok) { const b = await logoRes.json(); errEl.textContent = b.message || 'Failed.'; errEl.style.display = 'block'; return; } + if (!logoRes.ok) { errEl.textContent = await errMessage(logoRes); errEl.style.display = 'block'; return; } + const { data } = await logoRes.json(); + const img = document.getElementById('logo-preview'); + img.src = data.logoPath; img.style.display = ''; + document.getElementById('logo-file').value = ''; } const res = await Api.patch('/api/v1/org/branding', brandFields); - if (!res.ok) { const b = await res.json(); errEl.textContent = b.message || 'Failed.'; errEl.style.display = 'block'; return; } + if (!res.ok) { errEl.textContent = await errMessage(res); errEl.style.display = 'block'; return; } toast('Branding updated.', 'success'); Branding.load(); } catch { errEl.textContent = 'Network error.'; errEl.style.display = 'block'; } diff --git a/V2/app/scripts/migrate-v1-to-v2.js b/V2/app/scripts/migrate-v1-to-v2.js index cde2e34..49e1af5 100644 --- a/V2/app/scripts/migrate-v1-to-v2.js +++ b/V2/app/scripts/migrate-v1-to-v2.js @@ -32,15 +32,17 @@ async function main() { console.log('║ VitFit V1 → V2 Data Migration ║'); console.log('╚══════════════════════════════════════════╝\n'); - const { initDb, runMigrations, models } = require('../core/db'); - const { sequelize } = require('../core/db'); + const db = require('../core/db'); // ── Step 1: schema migrations ──────────────────────────────────────────── console.log('Step 1/5 Running schema migrations...'); - await initDb(); - await runMigrations(); + await db.initDb(); + await db.runMigrations(); console.log(' Schema migrations complete.\n'); + const { models } = db; + const sequelize = db.getSequelize(); + // ── Idempotency guard ──────────────────────────────────────────────────── const orgCount = await models.Organization.count(); if (orgCount > 0) { diff --git a/V2/app/vitfit.sqlite-shm b/V2/app/vitfit.sqlite-shm index 9244051..e064d18 100644 Binary files a/V2/app/vitfit.sqlite-shm and b/V2/app/vitfit.sqlite-shm differ diff --git a/V2/app/vitfit.sqlite-wal b/V2/app/vitfit.sqlite-wal index b8510d5..9b496c3 100644 Binary files a/V2/app/vitfit.sqlite-wal and b/V2/app/vitfit.sqlite-wal differ diff --git a/V2/deploy/coexist/.env.example b/V2/deploy/coexist/.env.example new file mode 100644 index 0000000..80891de --- /dev/null +++ b/V2/deploy/coexist/.env.example @@ -0,0 +1,4 @@ +# Copy to .env in this directory, then set a new random secret. +# Do not reuse the current app's JWT secret. +V2_JWT_SECRET= +V2_DATA_DIR=/var/lib/vitfit-v2 diff --git a/V2/deploy/coexist/.gitattributes b/V2/deploy/coexist/.gitattributes new file mode 100644 index 0000000..fcadb2c --- /dev/null +++ b/V2/deploy/coexist/.gitattributes @@ -0,0 +1 @@ +* text eol=lf diff --git a/V2/deploy/coexist/.gitignore b/V2/deploy/coexist/.gitignore new file mode 100644 index 0000000..d50a09f --- /dev/null +++ b/V2/deploy/coexist/.gitignore @@ -0,0 +1,2 @@ +.env +__pycache__/ diff --git a/V2/deploy/coexist/AWS-DEPLOYED.md b/V2/deploy/coexist/AWS-DEPLOYED.md new file mode 100644 index 0000000..0938f67 --- /dev/null +++ b/V2/deploy/coexist/AWS-DEPLOYED.md @@ -0,0 +1,143 @@ +# Live AWS deployment + +Deployed September 13, 2026 to `ubuntu@34.228.59.242` using the supplied PPK key. +The server identifies itself as `ip-172-31-20-183`. Its timezone remains UTC. + +| | Current version | V2 | +| --- | --- | --- | +| URL | https://workouttv.vitalityfitnessva.com | https://workouttv2.vitalityfitnessva.com | +| Service | `vitfit.service` | `vitfit-v2.service` | +| App | `/home/ubuntu/site` | `/home/ubuntu/vitfit-v2/app` | +| Node | Existing `/usr/bin/node` (18.19.1), unchanged | Separate `/home/ubuntu/vitfit-v2/runtime/bin/node` (24.21.0) | +| Port | Existing 3000 | `127.0.0.1:3001` | +| Database | `/home/ubuntu/site/database.sqlite` | `/home/ubuntu/vitfit-v2/data/vitfit.sqlite` | +| Uploads | `/home/ubuntu/site/public/uploads`, unchanged | `/home/ubuntu/vitfit-v2/data/uploads`, separate copies | + +No Docker installation or server backup was made. V1 was never restarted during +deployment. Its Node installation, app code, database and original uploads were +left intact. Its Nginx configuration now uses Let's Encrypt and permits ACME +validation requests; its old certificate and private-key files remain in place. + +## Login and data + +Use existing V1 usernames/passwords on V2. The initial sync imported all 5 V1 +accounts, 27 standard workouts, 192 video records and their referenced files, +plus 6 supporting categories. V1 has no Crossfit workouts currently; future +Crossfit workouts will sync. The existing V1 timer was not imported. + +V1 `Admin` maps to V2 `Owner`; V1 `Coach` maps to V2 `Coach`. Imported usernames, +passwords, roles, active status, workout contents and video metadata are +authoritative in V1 and overwrite edits to those fields in V2 on the next sync. +Other V2 account profile fields, settings, branding, client data and V2-only +records remain local to V2. No data goes back to V1. + +Exception: `jalleman` (V1 user ID 1, V2 user ID 2) is a V2 `SystemAdmin` at the +owner's request. `user_role_overrides` in `aws-sync.json` preserves this role +across daily syncs. The account retains its home organization and existing +password; password changes continue to sync from V1. V1's role is unchanged. + +A separate `v2-system-admin` account exists for V2 system administration. Its +generated password is stored only on the server in +`/home/ubuntu/vitfit-v2/system-admin-credentials.json` (mode 0600, owned by ubuntu). +No generated password or JWT secret is included in this repository or deployment +output. The imported Admin accounts can administer their V2 organization without +using this separate account. + +## Daily sync + +`/etc/cron.d/vitfit-v2-sync` checks hourly. The script runs the actual sync once +at **05:00 America/New_York**, automatically following daylight saving time. +The server stays on UTC. A successful manual sync also marks that local day as +complete; a failed run does not mark it complete. A failed scheduled attempt +requires manual retry or waits until the next day's scheduled hour. + +- Script: `/home/ubuntu/vitfit-v2/deploy/coexist/sync.py` +- Configuration: `/home/ubuntu/vitfit-v2/deploy/coexist/aws-sync.json` +- Log: `/var/log/vitfit-v2-sync.log`, weekly rotation, 8 rotations retained +- Last success: `/home/ubuntu/vitfit-v2/sync-state/last-success.json` +- Lock: `/home/ubuntu/vitfit-v2/sync-state/sync.lock` + +`retain_snapshots` is **false**. SQLite's online backup API creates temporary +database working copies for a consistent source read and merge validation; +normal completion or an exception removes those copies. There is no retained +database backup or full-server backup. The last-success file contains counts +and timestamps only. As with other temporary files, a forced process kill or +power loss can leave interrupted working files; check `sync-state` after one. + +Original videos never move, rename or change. Referenced videos are copied into +V2 under separate content-based filenames. Only 192 of the original directory's +263 entries are referenced by V1's video table, so the initial V2 copy is about +1.7 GiB. Old copied versions are retained; V2 never deletes original V1 files. +Monitor storage growth. The sync stages one source video library while running. + +Deleted V1 accounts are disabled in V2. Deleted imported workouts are removed. +Deleted video metadata is retained if a V2 workout/client assignment still uses +it; the sync report names this count `Videos.retained_referenced`. Categories are +added without deleting categories needed by V2-only content. + +Manual dry run and manual sync (run as ubuntu, never root): + +```bash +cd /home/ubuntu/vitfit-v2/deploy/coexist +python3 sync.py --config aws-sync.json +python3 sync.py --config aws-sync.json --apply +``` + +Neither command moves original uploads or retains database working copies under +the installed configuration. The operation only updates mapped V2 records and +copies referenced media. Avoid editing imported videos in V2 while sync runs. + +## HTTPS and renewal + +Separate Let's Encrypt certificates are installed for both domain names under +`/etc/letsencrypt/live//`. Their initial expiry is December 12, 2026. +Certbot is installed through snap. `snap.certbot.renew.timer` is enabled, and +`/etc/letsencrypt/renewal-hooks/deploy/20-vitfit-nginx` tests and reloads Nginx after +renewal. The certificates use webroot validation at `/var/www/letsencrypt`. +Keep inbound port 80 reachable for renewal, along with port 443 for the sites. + +Certbot was registered without an email address because none was supplied. +An operational email can be added later with +`sudo /snap/bin/certbot update_account --email YOUR_EMAIL`. + +Renewal test: + +```bash +sudo /snap/bin/certbot renew --dry-run --run-deploy-hooks --no-random-sleep-on-renew +``` + +Nginx uses the existing `/etc/nginx/sites-enabled/default` for V1 and +`/etc/nginx/sites-available/workouttv2.conf` for V2. The latter matches +`workouttv2-ssl.conf` in this folder. Do not replace it with the HTTP bootstrap +template after certificates have been installed. + +## Verification completed + +- All 14 backup/sync integration tests passed on both Windows and this Ubuntu + host against disposable databases built using the real V2 migrations. +- Both HTTPS sites returned successfully, and V2's database/disk health was OK. +- V2 SystemAdmin login, all 192 video file-existence checks, listing all 27 + workouts, authenticated byte-range video streaming, and an authenticated + WebSocket connection through Nginx passed. +- Imported usernames/password hashes matched all 5 original accounts. +- Both certificate renewal simulations and Nginx deploy hooks succeeded. +- V1's database hash was unchanged. All 263 original upload entries retained + the same filenames, inodes, sizes and modification times. +- V1 kept PID 3428973 and its original service start time throughout deployment. +- No database working-copy directories remained after the first sync. + +## Operations + +```bash +systemctl status vitfit vitfit-v2 +journalctl -u vitfit-v2 --since today +cat /home/ubuntu/vitfit-v2/sync-state/last-success.json +tail -n 50 /var/log/vitfit-v2-sync.log +systemctl list-timers --all | grep certbot +df -h / +``` + +To stop V2 temporarily, stop `vitfit-v2.service`. Disable its cron entry too if +V2 must receive no further data updates. V1 continues independently. There is no +automatic rollback backup, per the server constraint. Preserve the original V1 +database and uploads as the authoritative source throughout the transition. diff --git a/V2/deploy/coexist/README.md b/V2/deploy/coexist/README.md new file mode 100644 index 0000000..e899d37 --- /dev/null +++ b/V2/deploy/coexist/README.md @@ -0,0 +1,246 @@ +# V1 and V2 on the same Ubuntu server + +**The AWS deployment is live. See [AWS-DEPLOYED.md](AWS-DEPLOYED.md) for its actual +systemd services, paths, schedule and verification. It creates no server backup, +retains no database snapshots, and leaves original uploads in place.** + +The instructions below describe the alternative Docker deployment template. +**V1 is authoritative for imported accounts, workouts +and videos.** The job snapshots both databases, then merges selected data into +V2. It never replaces V2's database or writes to V1. + +| Hostname | Upstream | Database | Media | +| --- | --- | --- | --- | +| workouttv.vitalityfitnessva.com | Existing app, port 3000 | Existing `site/database.sqlite` | Existing `site/public/uploads` | +| workouttv2.vitalityfitnessva.com | New container, localhost port 3001 | `/var/lib/vitfit-v2/vitfit.sqlite` | `/var/lib/vitfit-v2/uploads` | + +V2 has a separate Compose project, image, secret, storage directory and port. +V1 keeps its existing process manager. Never mount the V1 database or uploads +directory as V2's writable storage; deleting a video in V2 deletes its file. + +## 1. Confirm the host layout + +The server is reachable as `ubuntu@34.228.59.242`, runs Ubuntu 24.04, and runs V1 +under `vitfit.service` from `/home/ubuntu/site`. The Docker example below assumes +the repository has +been transferred to `/opt/VitFit` separately from the running V1 installation. +Transfer application source, excluding `.env`, `node_modules`, databases, +database sidecars, uploads and temporary files. Set production secrets on the host. + +Inspect existing services before installing or changing the proxy: + +```bash +lsb_release -ds +sudo ss -ltnp +sudo nginx -t +sudo nginx -T +docker compose version +df -h +timedatectl +``` + +If Apache or another proxy owns ports 80/443, adapt that setup before installing +these Nginx files. Ensure port 3001 is free. Use the existing Docker installation; +if absent, follow [Docker's Ubuntu installation instructions](https://docs.docker.com/engine/install/ubuntu/). +Do not replace the current app's Node installation. The V2 container uses Node 24 +from the [Node.js LTS release line](https://nodejs.org/en/about/previous-releases). + +## 2. Prepare and start an empty V2 + +These commands initialize a fresh V2 environment for testing. They do **not** +import V1 records. Existing accounts will work there after the selective sync +in step 5 is run. + +```bash +cd /opt/VitFit/V2/deploy/coexist +sudo install -d -m 0750 -o 1000 -g 1000 /var/lib/vitfit-v2 +sudo install -d -m 0750 -o 1000 -g 1000 /var/lib/vitfit-v2/uploads /var/lib/vitfit-v2/tmp +umask 077 +cp -n .env.example .env +openssl rand -hex 32 +nano .env +# Set V2_JWT_SECRET to the generated value; use a different secret from V1. +docker compose config --quiet +docker compose build --pull +docker compose run --rm app node scripts/setup.js +docker compose up -d +docker compose ps +curl --fail http://127.0.0.1:3001/health +``` + +Setup prompts for the organization, timezone, Owner and SystemAdmin. Use distinct +V2 usernames that do not overlap V1 usernames. The importer refuses to merge an +unmapped account by username. Set the organization timezone to `America/New_York`. +Setup runs once. Imported V1 Admin accounts become additional V2 Owners. +The database health value must be `ok`; investigate a disk warning before copying +videos. Plan capacity for both media libraries and retained backups. + +The existing `scripts/migrate-v1-to-v2.js` is a one-time full upgrade, includes +timers and other tables, and exits once an organization exists. It is **not** the +selective daily sync. Do not point it at the live V1 database or use it for cron. + +## 3. Route both names and enable Let's Encrypt + +Both DNS A records must point to the server's public IPv4 address. Any AAAA +records must point to a working IPv6 address on this server. Allow inbound TCP +80 and 443 in both the server firewall and provider firewall; retain SSH access. +Keep port 80 reachable for renewal. Do not expose port 3001 publicly. + +Back up the existing Nginx configuration, then install the V2 server block: + +```bash +sudo tar -czf "/root/nginx-before-vitfit-v2-$(date +%Y%m%d-%H%M%S).tgz" -C /etc nginx +sudo install -m 0644 workouttv2.conf /etc/nginx/sites-available/workouttv2.conf +sudo ln -s /etc/nginx/sites-available/workouttv2.conf /etc/nginx/sites-enabled/workouttv2.conf +sudo nginx -t +sudo systemctl reload nginx +``` + +Keep the current domain's existing server block. Only if it has none, install +`workouttv.conf` the same way. Never enable duplicate blocks for a hostname. +The supplied files bootstrap HTTP without referencing nonexistent certificates. +After Certbot modifies them, preserve the installed files on later deployments. + +Use one Certbot installation. If it is already installed, reuse it. For a new +installation, Ubuntu documents the snap distribution: + +```bash +sudo snap install --classic certbot +sudo /snap/bin/certbot --nginx --redirect -d workouttv.vitalityfitnessva.com +sudo /snap/bin/certbot --nginx --redirect -d workouttv2.vitalityfitnessva.com +sudo /snap/bin/certbot renew --dry-run +sudo systemctl list-timers --all | grep -i certbot +sudo nginx -t +curl -I https://workouttv.vitalityfitnessva.com +curl --fail https://workouttv2.vitalityfitnessva.com/health +``` + +For an existing non-snap installation, use its `certbot` executable instead. +Supply the operational email when prompted. These commands request a separate +certificate for each name; Certbot's scheduled renewal handles both. Verify its +timer (or existing cron installation) is enabled, and that the dry run succeeds. +The Nginx plugin reloads Nginx after renewal. Do not add a second renewal scheduler. +See [Ubuntu's TLS guide](https://ubuntu.com/server/docs/how-to/security/obtain-tls-certificates/) +and [Certbot's Nginx instructions](https://certbot.eff.org/instructions?os=snap&ws=nginx). + +Validate login, video upload/playback, workout creation and a Socket.IO display +connection on V2. Check the current site still logs in and plays videos. + +## 4. Database snapshot utility + +`snapshot.py` uses SQLite's online backup API, includes committed WAL data, +validates the resulting database and refuses to overwrite an existing file. +This is a backup primitive, **not a selective importer or synchronizer**. +The full snapshot contains sensitive account hashes and all tables; only the +agreed tables should be imported from it. Keep backups outside web directories. + +```bash +sudo install -d -m 0700 /var/backups/vitfit +sudo python3 snapshot.py /ACTUAL/V1/site/database.sqlite "/var/backups/vitfit/v1-$(date +%Y%m%d-%H%M%S).sqlite" +sudo python3 snapshot.py /var/lib/vitfit-v2/vitfit.sqlite "/var/backups/vitfit/v2-$(date +%Y%m%d-%H%M%S).sqlite" +``` + +Use the real V1 path in place of the placeholder. Never `cp` an active SQLite +database: recent transactions can reside in its WAL. See the +[SQLite backup API documentation](https://www.sqlite.org/backup.html). +Media files require their own backup; a database snapshot does not contain them. + +## 5. Selective morning sync: V1 wins + +`sync.py` requires Python 3.9+ on Ubuntu. It runs with a non-overlapping lock and +takes SQLite snapshots of both databases before each attempt. It reads V1's +snapshot, stages and hashes video files, validates the merge on the V2 backup, +then commits selected changes and ID mappings together in live V2. No restart +is required; V2 writes may briefly wait for the SQLite transaction to finish. +V2's normal startup migrations create/upgrade its schema; daily sync transforms +the selected V1 records into that schema without running V1's full upgrade. + +| Data | Daily behavior | +| --- | --- | +| Accounts | Sync username, bcrypt password and role; `Admin` maps to `Owner`, `Coach` to `Coach`. Existing V1 accounts are active in V2. Password, role or activation changes invalidate V2 sessions. | +| Deleted V1 accounts | Disable the mapped V2 account and revoke device tokens; keep references/history. | +| Standard and Crossfit workouts | Sync contents, dates and repetition counts; remap video IDs. Delete mapped workouts removed from V1. | +| Videos | Copy actual files using immutable content-based names; sync metadata and remap IDs. Delete mapped metadata removed from V1 unless V2 workouts/client assignments still reference it. | +| Categories | Add V1 categories and category names used by videos. Retain existing categories to preserve V2-only usage. | +| V2-only data | Preserve organization, branding, profiles, timers, clients, assignments, calendars, audit history and accounts created only in V2. | + +Edits to **imported** account credentials/roles/activation, workouts and video +metadata in V2 are overwritten at the next sync. Records created only in V2 +remain there and never flow back to V1. V2-only profile fields on imported +accounts remain unchanged. Keep routine editing of shared content in V1. +Changes can take about 24 hours to appear in V2. + +The `V1SyncMap` and `V1SyncConfig` tables belong to the importer. Retain them in +backups. Do not point this importer at a previously fully migrated V1 copy: it +does not guess account mappings and will stop on username collisions. Use a +fresh V2 setup above, or prepare an explicit mapping for an existing V2 database. +Source identity is pinned to its absolute path and selected organization. + +Missing/changed video files, broken source workout references, account name +collisions or organization mismatches abort the database merge. Original source +media never changes. Old imported file versions are retained, including files +published before a failed commit; retrying is safe. This makes rollback possible +but requires disk monitoring. A V2 deletion concurrent with file publication can +still remove its file; avoid editing imported videos while the morning job runs. + +Configure actual paths and IDs on the host: + +```bash +cd /opt/VitFit/V2/deploy/coexist +id ubuntu +# The cron user must match the container's UID 1000 and have read access to V1. +sudo install -d -m 0700 -o ubuntu -g ubuntu /var/backups/vitfit-sync +sudo install -m 0600 -o ubuntu -g ubuntu sync.example.json /etc/vitfit-v2-sync.json +sudo -u ubuntu nano /etc/vitfit-v2-sync.json +# Set actual v1_db/v1_uploads, organization_id and an active V2 creator_id. +sudo install -m 0600 -o ubuntu -g ubuntu /dev/null /var/log/vitfit-v2-sync.log +sudo -u ubuntu python3 sync.py --config /etc/vitfit-v2-sync.json +# Review the dry-run counts, then apply the same configuration: +sudo -u ubuntu python3 sync.py --config /etc/vitfit-v2-sync.json --apply +``` + +If Ubuntu's UID is not 1000, adapt the container user and data ownership before +running sync. Do not run live sync as root: root-owned SQLite sidecars can prevent +the container from writing. Each run creates a mode-0700 backup directory with +`v1.sqlite`, `v2-before.sqlite` and, on success, `result.json`. Dry-runs leave the +live database unchanged and keep their snapshots for review. Video staging needs +temporary free space for one complete V1 video library in addition to both apps' +stored media. Retain at least 30 days of successful backups and arrange pruning +and off-server copies; this package deliberately does not delete backups or media. + +Run the script as the same user for manual and cron runs; the lock is in +`backup_dir/sync.lock`. Lock contention, validation failures, missing files and +database failures return a nonzero exit status. Successful reports contain +counts, never usernames or password hashes. A `Videos.retained_referenced` count +means a deleted V1 video is still needed by V2; resolve those references if you +want its metadata removed on the next run. + +After testing both apps, install the job and log rotation: + +```bash +timedatectl +sudo install -m 0644 vitfit-v2-sync.cron /etc/cron.d/vitfit-v2-sync +sudo install -m 0644 vitfit-v2-sync.logrotate /etc/logrotate.d/vitfit-v2-sync +sudo systemctl enable --now cron +``` + +The supplied cron entry runs at **05:00 in the server's timezone**. Adjust before +installing if the AWS server uses UTC and a different local morning is wanted; +the organization timezone does not control cron. Files must use Unix LF line +endings. Monitor `/var/log/vitfit-v2-sync.log` and alert if no successful applied +`result.json` appears within 26 hours. Actual alert delivery depends on the +server's monitoring/mail setup; redirecting logs alone does not send alerts. + +## Rollback and updates + +Disable `/etc/cron.d/vitfit-v2-sync` before restoring or retiring V2; wait for any +active sync to finish. To stop V2, run `docker compose down` here. Its bind-mounted data remains and V1 +continues running. Disable only the V2 Nginx symlink if retiring the V2 hostname, +then test and reload Nginx. Keep the V1 server block and its certificate intact. +For a V2 update, snapshot its DB and back up uploads, retain the previous image, +then rebuild and run `docker compose up -d`. Schema rollback may require restoring +the matching backup with V2 stopped, so retain both image and data together. + +Local tests: `python -m unittest discover -s V2/deploy/coexist -p 'test_*.py'`. +Sync integration tests use the real V2 migrations and require Node plus installed +V2 dependencies. All test data is disposable; tests never open the app databases. diff --git a/V2/deploy/coexist/aws-sync.cron b/V2/deploy/coexist/aws-sync.cron new file mode 100644 index 0000000..e17dbad --- /dev/null +++ b/V2/deploy/coexist/aws-sync.cron @@ -0,0 +1,4 @@ +# Check hourly; sync.py runs once at 05:00 America/New_York, including DST. +SHELL=/bin/sh +PATH=/usr/local/bin:/usr/bin:/bin +0 * * * * ubuntu /usr/bin/timeout 3h /usr/bin/python3 /home/ubuntu/vitfit-v2/deploy/coexist/sync.py --config /home/ubuntu/vitfit-v2/deploy/coexist/aws-sync.json --scheduled --apply >> /var/log/vitfit-v2-sync.log 2>&1 diff --git a/V2/deploy/coexist/aws-sync.json b/V2/deploy/coexist/aws-sync.json new file mode 100644 index 0000000..02e2574 --- /dev/null +++ b/V2/deploy/coexist/aws-sync.json @@ -0,0 +1,13 @@ +{ + "v1_db": "/home/ubuntu/site/database.sqlite", + "v1_uploads": "/home/ubuntu/site/public/uploads", + "v2_db": "/home/ubuntu/vitfit-v2/data/vitfit.sqlite", + "v2_uploads": "/home/ubuntu/vitfit-v2/data/uploads", + "backup_dir": "/home/ubuntu/vitfit-v2/sync-state", + "organization_id": 1, + "creator_id": null, + "retain_snapshots": false, + "timezone": "America/New_York", + "sync_hour": 5, + "user_role_overrides": {"1": "SystemAdmin"} +} diff --git a/V2/deploy/coexist/bootstrap-server.js b/V2/deploy/coexist/bootstrap-server.js new file mode 100644 index 0000000..25e836e --- /dev/null +++ b/V2/deploy/coexist/bootstrap-server.js @@ -0,0 +1,40 @@ +'use strict'; + +// For a new V2 database only. V1 accounts are imported separately by sync.py. +const fs = require('fs'); +const path = require('path'); +const crypto = require('crypto'); +const bcrypt = require('../../app/node_modules/bcryptjs'); +const db = require('../../app/core/db'); + +async function main() { + await db.initDb(); + await db.runMigrations(); + if (await db.models.Organization.count()) { + console.log('V2 organization already exists; bootstrap skipped.'); + await db.getSequelize().close(); + return; + } + const credentials = { + username: 'v2-system-admin', + password: crypto.randomBytes(24).toString('base64url'), + }; + const credentialPath = path.resolve(__dirname, '../../system-admin-credentials.json'); + // Never print generated credentials or overwrite credentials from a prior run. + fs.writeFileSync(credentialPath, JSON.stringify(credentials, null, 2) + '\n', { flag: 'wx', mode: 0o600 }); + await db.getSequelize().transaction(async transaction => { + const org = await db.models.Organization.create({ + name: 'Vitality Fitness', timezone: 'America/New_York', isActive: true, + }, { transaction }); + await db.models.Branding.create({ organizationId: org.id }, { transaction }); + await db.models.User.create({ + username: credentials.username, + password: await bcrypt.hash(credentials.password, 12), + role: 'SystemAdmin', organizationId: null, isActive: true, + }, { transaction }); + }); + console.log('V2 initialized; SystemAdmin credentials saved to ' + credentialPath); + await db.getSequelize().close(); +} + +main().catch(error => { console.error(error.message); process.exit(1); }); diff --git a/V2/deploy/coexist/compose.yml b/V2/deploy/coexist/compose.yml new file mode 100644 index 0000000..242df5a --- /dev/null +++ b/V2/deploy/coexist/compose.yml @@ -0,0 +1,45 @@ +name: vitfit-v2 + +services: + app: + build: + context: ../../app + dockerfile: Dockerfile + args: + NODE_VERSION: "24" + image: vitfit-v2:local + restart: unless-stopped + environment: + NODE_ENV: production + PORT: "3000" + JWT_SECRET: ${V2_JWT_SECRET:?Set a separate V2_JWT_SECRET in .env} + CORS_ORIGIN: https://workouttv2.vitalityfitnessva.com + DB_PATH: /data/vitfit.sqlite + UPLOADS_DIR: /data/uploads + UPLOAD_TEMP_DIR: /data/tmp + UPLOAD_MAX_BYTES: "524288000" + STORAGE_WARN_BYTES: "5368709120" + LOG_LEVEL: info + volumes: + - type: bind + source: ${V2_DATA_DIR:-/var/lib/vitfit-v2} + target: /data + bind: + create_host_path: false + ports: + - "127.0.0.1:3001:3000" + healthcheck: + test: + - CMD + - node + - -e + - "fetch('http://127.0.0.1:3000/health').then(r=>r.json()).then(h=>process.exit(h.db==='ok'?0:1)).catch(()=>process.exit(1))" + interval: 30s + timeout: 5s + retries: 3 + start_period: 30s + logging: + driver: json-file + options: + max-size: "20m" + max-file: "5" diff --git a/V2/deploy/coexist/renew-nginx.sh b/V2/deploy/coexist/renew-nginx.sh new file mode 100644 index 0000000..d99deb6 --- /dev/null +++ b/V2/deploy/coexist/renew-nginx.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +/usr/sbin/nginx -t +/bin/systemctl reload nginx diff --git a/V2/deploy/coexist/snapshot.py b/V2/deploy/coexist/snapshot.py new file mode 100644 index 0000000..b5fcc44 --- /dev/null +++ b/V2/deploy/coexist/snapshot.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +"""Create a consistent SQLite backup without replacing an existing destination.""" + +import argparse +import os +from pathlib import Path +import sqlite3 +import tempfile +import time + + +def snapshot(source, destination, timeout=120): + source = Path(source).resolve(strict=True) + destination = Path(destination).absolute() + if not source.is_file(): + raise ValueError("Source must be a database file") + if destination.exists(): + raise FileExistsError("Destination already exists; use a new backup filename") + # mkstemp creates mode 0600 on Ubuntu. Place alongside destination so publishing + # can use an atomic, exclusive hard link, including against concurrent runs. + fd, temporary = tempfile.mkstemp(prefix=".snapshot-", dir=destination.parent) + os.close(fd) + deadline = time.monotonic() + timeout + + def progress(status, remaining, total): + if time.monotonic() > deadline: + raise TimeoutError("SQLite backup timed out") + + try: + src = sqlite3.connect(source.as_uri() + "?mode=ro", uri=True, timeout=5) + try: + dst = sqlite3.connect(temporary) + try: + src.backup(dst, pages=256, progress=progress, sleep=0.1) + if dst.execute("PRAGMA quick_check").fetchall() != [("ok",)]: + raise ValueError("Snapshot failed SQLite integrity check") + # A standalone snapshot must not depend on a WAL sidecar. + dst.execute("PRAGMA journal_mode=DELETE") + finally: + dst.close() + finally: + src.close() + with open(temporary, "rb+") as backup: + os.fsync(backup.fileno()) + os.link(temporary, destination) + finally: + Path(temporary).unlink(missing_ok=True) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("source") + parser.add_argument("destination", help="New filename in an existing directory") + args = parser.parse_args() + snapshot(args.source, args.destination) + print("SQLite snapshot created successfully") diff --git a/V2/deploy/coexist/sync.example.json b/V2/deploy/coexist/sync.example.json new file mode 100644 index 0000000..cf784ec --- /dev/null +++ b/V2/deploy/coexist/sync.example.json @@ -0,0 +1,9 @@ +{ + "v1_db": "/REPLACE/WITH/ACTUAL/site/database.sqlite", + "v1_uploads": "/REPLACE/WITH/ACTUAL/site/public/uploads", + "v2_db": "/var/lib/vitfit-v2/vitfit.sqlite", + "v2_uploads": "/var/lib/vitfit-v2/uploads", + "backup_dir": "/var/backups/vitfit-sync", + "organization_id": 1, + "creator_id": 1 +} diff --git a/V2/deploy/coexist/sync.py b/V2/deploy/coexist/sync.py new file mode 100644 index 0000000..67aa3b7 --- /dev/null +++ b/V2/deploy/coexist/sync.py @@ -0,0 +1,336 @@ +#!/usr/bin/env python3 +"""One-way V1 to V2 merge. Dry-run unless --apply is supplied.""" + +import argparse +from collections import Counter +from contextlib import contextmanager, nullcontext +from datetime import datetime, timezone +import hashlib +import json +import os +from pathlib import Path +import shutil +import sqlite3 +import tempfile +from zoneinfo import ZoneInfo + +from snapshot import snapshot + + +TABLES = ("Users", "Categories", "Videos", "Workouts", "CrossfitWorkouts") + + +@contextmanager +def exclusive_lock(filename): + with Path(filename).open("a+b") as lock: + if os.name == "nt": + import msvcrt + if lock.seek(0, os.SEEK_END) == 0: + lock.write(b"0") + lock.flush() + lock.seek(0) + msvcrt.locking(lock.fileno(), msvcrt.LK_NBLCK, 1) + try: + yield + finally: + lock.seek(0) + msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1) + else: + import fcntl + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + try: + yield + finally: + fcntl.flock(lock, fcntl.LOCK_UN) + + +def connect(filename): + db = sqlite3.connect(Path(filename).resolve().as_uri() + "?mode=rw", uri=True, timeout=30) + db.row_factory = sqlite3.Row + db.execute("PRAGMA foreign_keys=ON") + return db + + +def now(): + return datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S.%f +00:00") + + +def digest_file(filename): + digest = hashlib.sha256() + with open(filename, "rb") as stream: + for block in iter(lambda: stream.read(1024 * 1024), b""): + digest.update(block) + return digest.hexdigest() + + +def stage_videos(rows, source_dir, stage_dir): + """Copy while hashing; changes during a copy abort the whole database merge.""" + source_dir = Path(source_dir).resolve(strict=True) + staged = {} + for row in rows: + name = row["filename"] + if not name or Path(name).name != name or "/" in name or "\\" in name: + raise ValueError("Invalid V1 video filename") + source = (source_dir / name).resolve(strict=True) + if source.parent != source_dir or not source.is_file(): + raise ValueError("V1 video must be a regular file inside its uploads directory") + temporary = Path(stage_dir) / str(row["id"]) + with source.open("rb") as src, temporary.open("wb") as dst: + before = os.fstat(src.fileno()) + shutil.copyfileobj(src, dst, 1024 * 1024) + after = os.fstat(src.fileno()) + current = source.stat() + if (before.st_size, before.st_mtime_ns, before.st_ino) != ( + after.st_size, after.st_mtime_ns, after.st_ino) or ( + before.st_size, before.st_mtime_ns, before.st_ino) != ( + current.st_size, current.st_mtime_ns, current.st_ino): + raise ValueError("V1 video changed during copy; retry the sync") + digest = digest_file(temporary) + suffix = source.suffix.lower() + if suffix not in (".mp4", ".mov", ".m4v"): + raise ValueError("Unsupported V1 video extension") + filename = f"v1-{row['id']}-{digest}{suffix}" + staged[row["id"]] = { + "filename": filename, "temporary": temporary, "digest": digest, + "fileSizeBytes": before.st_size, + "mimeType": {".mp4": "video/mp4", ".mov": "video/quicktime", ".m4v": "video/x-m4v"}[suffix], + } + return staged + + +def publish_videos(staged, uploads): + """Publish immutable filenames before DB commit; never replace a V2 file.""" + for entry in staged.values(): + target = Path(uploads) / entry["filename"] + try: + os.link(entry["temporary"], target) + except FileExistsError: + if target.is_symlink() or digest_file(target) != entry["digest"]: + raise ValueError("Imported media filename collision or damaged file") + # Files unreferenced after a rollback are harmless and reusable on retry. + + +def merge(db, source, media, organization_id, creator_id, source_identity, apply=False, user_role_overrides=None): + """All row changes and ID mappings commit together. Never write source DB.""" + user_role_overrides = user_role_overrides or {} + if any(not str(key).isdigit() or value != 'SystemAdmin' for key, value in user_role_overrides.items()): + raise ValueError('Role overrides must map explicit V1 user IDs to SystemAdmin') + counts = Counter() + timestamp = now() + db.execute("BEGIN IMMEDIATE") + try: + org = db.execute('SELECT id FROM Organizations WHERE id=?', (organization_id,)).fetchone() + creator = db.execute('SELECT * FROM Users WHERE id=?', (creator_id,)).fetchone() if creator_id is not None else None + if not org or (creator_id is not None and (not creator or creator["organizationId"] != organization_id or not creator["isActive"])): + raise ValueError("Select an existing organization and active creator in that organization") + db.execute('''CREATE TABLE IF NOT EXISTS V1SyncConfig ( + id INTEGER PRIMARY KEY CHECK(id=1), source TEXT NOT NULL, organizationId INTEGER NOT NULL)''') + settings = db.execute('SELECT * FROM V1SyncConfig WHERE id=1').fetchone() + if settings and (settings["source"] != source_identity or settings["organizationId"] != organization_id): + raise ValueError("This database is already mapped to a different V1 source or organization") + db.execute('INSERT OR IGNORE INTO V1SyncConfig VALUES (1, ?, ?)', (source_identity, organization_id)) + db.execute('''CREATE TABLE IF NOT EXISTS V1SyncMap ( + entity TEXT NOT NULL, sourceId INTEGER NOT NULL, targetId INTEGER NOT NULL, + PRIMARY KEY(entity, sourceId), UNIQUE(entity, targetId))''') + + def mapped(table, sid): + row = db.execute('SELECT targetId FROM V1SyncMap WHERE entity=? AND sourceId=?', (table, sid)).fetchone() + return row[0] if row else None + + def upsert(table, row, values, extra=None): + tid = mapped(table, row["id"]) + target = db.execute(f'SELECT * FROM "{table}" WHERE id=?', (tid,)).fetchone() if tid else None + if target and (target["organizationId"] != organization_id or + (table == "Users" and target["role"] == "SystemAdmin" and + user_role_overrides.get(str(row["id"])) != "SystemAdmin")): + raise ValueError("Mapped record changed organization or became SystemAdmin; review mapping") + if target is None: + # Do not guess that a matching username or numeric ID is the same account. + if table == "Users" and db.execute('SELECT 1 FROM Users WHERE username=?', (values["username"],)).fetchone(): + raise ValueError(f"Unmapped username collision for V1 user ID {row['id']}; resolve before import") + record = {"organizationId": organization_id, **(extra or {}), **values, + "createdAt": row.get("createdAt") or timestamp, "updatedAt": timestamp} + # Recreate the mapped ID if V2 deleted an imported object. Keep references stable. + if tid is not None: + record["id"] = tid + columns = ', '.join(f'"{key}"' for key in record) + cursor = db.execute(f'INSERT INTO "{table}" ({columns}) VALUES ({",".join("?" for _ in record)})', tuple(record.values())) + tid = cursor.lastrowid + db.execute('INSERT OR REPLACE INTO V1SyncMap VALUES (?, ?, ?)', (table, row["id"], tid)) + counts[f"{table}.inserted"] += 1 + else: + changed = {key: value for key, value in values.items() if target[key] != value} + if table == "Users" and ("password" in changed or "role" in changed or "isActive" in changed): + changed["passwordChangedAt"] = timestamp + db.execute('UPDATE DeviceTokens SET isRevoked=1, updatedAt=? WHERE userId=? AND isRevoked=0', (timestamp, tid)) + if changed: + changed["updatedAt"] = timestamp + columns = ', '.join(f'"{key}"=?' for key in changed) + db.execute(f'UPDATE "{table}" SET {columns} WHERE id=?', (*changed.values(), tid)) + counts[f"{table}.updated"] += 1 + return tid + + for row in source["Users"]: + role = {"Admin": "Owner", "Coach": "Coach"}.get(row["role"]) + if role is None: + raise ValueError("Unexpected V1 role; define its V2 mapping before import") + role = user_role_overrides.get(str(row["id"]), role) + upsert("Users", row, {"username": row["username"], "password": row["password"], + "role": role, "isActive": 1}) + + if creator_id is None: + candidate = next((row for row in source["Users"] if row["role"] == "Admin"), + next(iter(source["Users"]), None)) + if candidate is None: + raise ValueError("V1 needs at least one account to own imported workouts") + creator_id = mapped("Users", candidate["id"]) + + # Categories are supporting video metadata, identified by name within this org. + categories = {r["name"] for r in source["Categories"]} + categories.update(r["category"] for r in source["Videos"] if r["category"]) + for name in sorted(categories): + if not db.execute('SELECT 1 FROM Categories WHERE organizationId=? AND name=?', (organization_id, name)).fetchone(): + db.execute('INSERT INTO Categories (organizationId,name,createdAt,updatedAt) VALUES (?,?,?,?)', + (organization_id, name, timestamp, timestamp)) + counts["Categories.inserted"] += 1 + + for row in source["Videos"]: + entry = media[row["id"]] + upsert("Videos", row, {"description": row["description"], "category": row["category"], + **{key: entry[key] for key in ("filename", "mimeType", "fileSizeBytes")}}) + + video_ids = {r["id"] for r in source["Videos"]} + for row in source["Workouts"]: + values = {key: row[key] for key in ("name", "workoutDate", "number1", "number2", "number3", "number4")} + for key in ("video1", "video2", "video3", "video4"): + sid = row[key] + if sid is not None and sid not in video_ids: + raise ValueError("V1 workout references a missing video; fix source data before syncing") + values[key] = mapped("Videos", sid) if sid is not None else None + upsert("Workouts", row, values, {"createdByUserId": creator_id}) + + for row in source["CrossfitWorkouts"]: + items = json.loads(row["workoutItems"]) + if not isinstance(items, list) or any(not isinstance(item, str) for item in items): + raise ValueError("Expected V1 Crossfit workoutItems to be a list of text lines") + upsert("CrossfitWorkouts", row, {"name": row["name"], "workoutDate": row["workoutDate"], + "workoutItems": json.dumps(items, ensure_ascii=False)}, + {"createdByUserId": creator_id}) + + # Apply deletions only to mapped data, children first. Media files are retained. + for table in ("Workouts", "CrossfitWorkouts", "Videos", "Users"): + live = {r["id"] for r in source[table]} + for mapping in db.execute('SELECT * FROM V1SyncMap WHERE entity=?', (table,)).fetchall(): + if mapping["sourceId"] in live: + continue + tid = mapping["targetId"] + target = db.execute(f'SELECT * FROM "{table}" WHERE id=?', (tid,)).fetchone() + if target is None: + continue + if target["organizationId"] != organization_id or (table == "Users" and target["role"] == "SystemAdmin" and + user_role_overrides.get(str(mapping["sourceId"])) != "SystemAdmin"): + raise ValueError("Cannot retire a mapped record whose ownership changed") + if table == "Users": + if target["isActive"]: + db.execute('UPDATE Users SET isActive=0,passwordChangedAt=?,updatedAt=? WHERE id=?', (timestamp, timestamp, tid)) + db.execute('UPDATE DeviceTokens SET isRevoked=1,updatedAt=? WHERE userId=?', (timestamp, tid)) + counts["Users.deactivated"] += 1 + continue + if table == "Videos": + referenced = db.execute('SELECT 1 FROM Workouts WHERE video1=? OR video2=? OR video3=? OR video4=? LIMIT 1', (tid,) * 4).fetchone() + for assignment in db.execute('SELECT items FROM ClientWorkouts'): + items = json.loads(assignment[0] or '[]') + if any(isinstance(item, dict) and str(item.get("videoId")) == str(tid) for item in items): + referenced = True + if referenced: + counts["Videos.retained_referenced"] += 1 + continue + db.execute(f'DELETE FROM "{table}" WHERE id=?', (tid,)) + counts[f"{table}.deleted"] += 1 + + if db.execute('PRAGMA foreign_key_check').fetchone(): + raise ValueError("Foreign-key check failed; transaction rolled back") + if apply: + db.commit() + else: + db.rollback() + return dict(counts) + except BaseException: + db.rollback() + raise + + +def run(config, apply=False, scheduled=False): + paths = {key: Path(config[key]).resolve(strict=True) for key in + ("v1_db", "v1_uploads", "v2_db", "v2_uploads", "backup_dir")} + if os.path.samefile(paths["v1_db"], paths["v2_db"]): + raise ValueError("V1 and V2 must use separate databases") + for a, b in (("v1_uploads", "v2_uploads"), ("backup_dir", "v1_uploads"), ("backup_dir", "v2_uploads")): + if paths[a] == paths[b] or paths[a] in paths[b].parents or paths[b] in paths[a].parents: + raise ValueError("Uploads and backup directories must be separate and non-nested") + # Cron and manual runs use the same advisory lock. + with exclusive_lock(paths["backup_dir"] / "sync.lock"): + zone_name = config.get("timezone", "America/New_York") + local_time = datetime.now(timezone.utc if zone_name == "UTC" else ZoneInfo(zone_name)) + last_success = paths["backup_dir"] / "last-success.json" + if scheduled: + if local_time.hour != config.get("sync_hour", 5): + return {"skipped": "outside scheduled hour"} + if last_success.exists() and json.loads(last_success.read_text()).get("local_day") == local_time.date().isoformat(): + return {"skipped": "already synced today"} + prefix = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ-") + context = (nullcontext(tempfile.mkdtemp(prefix=prefix, dir=paths["backup_dir"])) + if config.get("retain_snapshots", True) else + tempfile.TemporaryDirectory(prefix=prefix, dir=paths["backup_dir"])) + with context as directory: + report = run_batch(config, paths, Path(directory), apply) + if apply: + report["local_day"] = local_time.date().isoformat() + temporary = last_success.with_suffix('.tmp') + temporary.write_text(json.dumps(report) + "\n") + os.replace(temporary, last_success) + print(json.dumps(report)) + return report + + +def run_batch(config, paths, batch, apply): + snapshot(paths["v1_db"], batch / "v1.sqlite") + snapshot(paths["v2_db"], batch / "v2-before.sqlite") + source_db = connect(batch / "v1.sqlite") + try: + source = {table: [dict(row) for row in source_db.execute(f'SELECT * FROM "{table}" ORDER BY id')] for table in TABLES} + finally: + source_db.close() + # Dry-runs merge into a disposable backup, never acquire a write lock on live V2. + target = paths["v2_db"] if apply else batch / "v2-before.sqlite" + with tempfile.TemporaryDirectory(prefix=".v1-stage-", dir=paths["v2_uploads"]) as stage: + media = stage_videos(source["Videos"], paths["v1_uploads"], stage) + # Validate the whole merge against the backup before publishing any files. + preview = connect(batch / "v2-before.sqlite") + try: + counts = merge(preview, source, media, config["organization_id"], config["creator_id"], str(paths["v1_db"]), + user_role_overrides=config.get("user_role_overrides")) + finally: + preview.close() + if apply: + publish_videos(media, paths["v2_uploads"]) + db = connect(target) + try: + counts = merge(db, source, media, config["organization_id"], config["creator_id"], str(paths["v1_db"]), apply=True, + user_role_overrides=config.get("user_role_overrides")) + finally: + db.close() + report = {"applied": apply, "finished_at": now(), "counts": counts} + (batch / "result.json").write_text(json.dumps(report, indent=2) + "\n") + return report + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--config", required=True) + parser.add_argument("--apply", action="store_true") + parser.add_argument("--scheduled", action="store_true", help="Run only once daily during configured local hour") + args = parser.parse_args() + os.umask(0o077) + with open(args.config) as stream: + run(json.load(stream), apply=args.apply, scheduled=args.scheduled) diff --git a/V2/deploy/coexist/test_snapshot.py b/V2/deploy/coexist/test_snapshot.py new file mode 100644 index 0000000..e1616f2 --- /dev/null +++ b/V2/deploy/coexist/test_snapshot.py @@ -0,0 +1,63 @@ +import sqlite3 +import tempfile +import unittest +from pathlib import Path + +from snapshot import snapshot + + +class SnapshotTests(unittest.TestCase): + def test_copies_committed_wal_and_excludes_uncommitted_changes(self): + with tempfile.TemporaryDirectory() as directory: + source = Path(directory) / "live.sqlite" + target = Path(directory) / "backup.sqlite" + writer = sqlite3.connect(source) + try: + writer.execute("PRAGMA journal_mode=WAL") + writer.execute("CREATE TABLE records (value TEXT)") + writer.execute("INSERT INTO records VALUES ('committed')") + writer.commit() + writer.execute("INSERT INTO records VALUES ('uncommitted')") + snapshot(source, target) + reader = sqlite3.connect(target) + try: + self.assertEqual(reader.execute("SELECT * FROM records").fetchall(), + [("committed",)]) + self.assertEqual(reader.execute("PRAGMA journal_mode").fetchone(), + ("delete",)) + finally: + reader.close() + finally: + writer.close() + + def test_refuses_to_replace_source_or_existing_backup(self): + with tempfile.TemporaryDirectory() as directory: + source = Path(directory) / "live.sqlite" + db = sqlite3.connect(source) + db.execute("CREATE TABLE records (value TEXT)") + db.close() + original = source.read_bytes() + with self.assertRaises(FileExistsError): + snapshot(source, source) + self.assertEqual(source.read_bytes(), original) + target = Path(directory) / "backup.sqlite" + target.write_bytes(b"keep this") + with self.assertRaises(FileExistsError): + snapshot(source, target) + self.assertEqual(target.read_bytes(), b"keep this") + + def test_missing_or_invalid_database_leaves_no_snapshot(self): + with tempfile.TemporaryDirectory() as directory: + source = Path(directory) / "missing.sqlite" + target = Path(directory) / "backup.sqlite" + with self.assertRaises(FileNotFoundError): + snapshot(source, target) + source.write_text("not a database") + with self.assertRaises(sqlite3.DatabaseError): + snapshot(source, target) + self.assertFalse(target.exists()) + self.assertEqual(list(Path(directory).glob(".snapshot-*")), []) + + +if __name__ == "__main__": + unittest.main() diff --git a/V2/deploy/coexist/test_sync.py b/V2/deploy/coexist/test_sync.py new file mode 100644 index 0000000..b6aaa70 --- /dev/null +++ b/V2/deploy/coexist/test_sync.py @@ -0,0 +1,249 @@ +"""Integration tests against the real V2 migrations, using disposable databases.""" +import json +import io +from contextlib import redirect_stdout +import os +from pathlib import Path +import shutil +import sqlite3 +import subprocess +import tempfile +import unittest +from datetime import datetime, timezone + +from sync import connect, merge, publish_videos, stage_videos, run, exclusive_lock + + +class SyncTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.fixture = tempfile.TemporaryDirectory() + cls.schema = Path(cls.fixture.name) / "schema.sqlite" + app = Path(__file__).resolve().parents[2] / "app" + code = """ + const db = require('./core/db'); + (async () => { + await db.initDb(); await db.runMigrations(); + const org = await db.models.Organization.create({name:'Test org'}); + await db.models.Branding.create({organizationId:org.id}); + await db.models.User.create({organizationId:org.id,username:'v2-owner',password:'v2-password',role:'Owner'}); + await db.models.User.create({username:'system-admin',password:'private',role:'SystemAdmin'}); + await db.getSequelize().close(); + })().catch(e=>{console.error(e); process.exit(1)}); + """ + result = subprocess.run(["node", "-e", code], cwd=app, + env={**os.environ, "DB_PATH": str(cls.schema), "LOG_LEVEL": "silent"}, + capture_output=True, text=True, timeout=60) + if result.returncode: + raise RuntimeError(result.stderr) + + @classmethod + def tearDownClass(cls): + cls.fixture.cleanup() + + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + root = Path(self.directory.name) + self.target = root / "v2.sqlite" + shutil.copyfile(self.schema, self.target) + self.db = connect(self.target) + self.source = { + "Users": [{"id": 1, "username": "v1-admin", "password": "original-hash", "role": "Admin"}], + "Categories": [{"id": 1, "name": "Legs"}], + "Videos": [{"id": 1, "filename": "video.mp4", "description": "Squat", "category": "Legs"}], + "Workouts": [{"id": 1, "name": "Day one", "workoutDate": "2026-09-13", "video1": 1, + "video2": None, "video3": None, "video4": None, + "number1": 10, "number2": None, "number3": None, "number4": None}], + "CrossfitWorkouts": [{"id": 1, "name": "Circuit", "workoutDate": "2026-09-13", + "workoutItems": '["10 squats"]'}], + } + self.media = {1: {"filename": "imported.mp4", "mimeType": "video/mp4", "fileSizeBytes": 10}} + + def tearDown(self): + self.db.close() + self.directory.cleanup() + + def sync(self, apply=True): + return merge(self.db, self.source, self.media, 1, 1, "v1-source", apply=apply) + + def mapped_id(self, table): + return self.db.execute('SELECT targetId FROM V1SyncMap WHERE entity=? AND sourceId=1', (table,)).fetchone()[0] + + def test_id_collisions_reruns_and_v2_only_data(self): + self.db.execute("INSERT INTO Videos (organizationId,filename,createdAt,updatedAt) VALUES (1,'v2.mp4','2026','2026')") + self.db.execute("UPDATE Brandings SET appName='Keep my branding'") + self.db.commit() + counts = self.sync() + self.assertEqual(counts["Users.inserted"], 1) + self.assertNotEqual(self.mapped_id("Users"), 1) + self.assertNotEqual(self.mapped_id("Videos"), 1) + workout = self.db.execute('SELECT * FROM Workouts').fetchone() + self.assertEqual(workout["video1"], self.mapped_id("Videos")) + self.assertEqual(self.sync(), {}) + self.assertEqual(self.db.execute('SELECT appName FROM Brandings').fetchone()[0], 'Keep my branding') + self.assertEqual(self.db.execute('SELECT password FROM Users WHERE id=1').fetchone()[0], 'v2-password') + self.assertEqual(self.db.execute('SELECT COUNT(*) FROM Timers').fetchone()[0], 0) + + def test_password_change_revokes_sessions_and_updates_only_imported_fields(self): + self.sync() + uid = self.mapped_id("Users") + self.db.execute("UPDATE Users SET email='keep@example.test', firstName='Keep' WHERE id=?", (uid,)) + self.db.execute("INSERT INTO DeviceTokens (userId,organizationId,token,expiresAt,createdAt,updatedAt,isRevoked) VALUES (?,1,'token','2099','2026','2026',0)", (uid,)) + self.db.commit() + self.source["Users"][0]["password"] = "new-hash" + self.source["Users"][0]["role"] = "Coach" + self.sync() + account = self.db.execute('SELECT * FROM Users WHERE id=?', (uid,)).fetchone() + self.assertEqual(account["password"], "new-hash") + self.assertEqual(account["role"], "Coach") + self.assertEqual(account["email"], "keep@example.test") + self.assertTrue(account["passwordChangedAt"]) + self.assertEqual(self.db.execute('SELECT isRevoked FROM DeviceTokens').fetchone()[0], 1) + + def test_content_comparison_and_v1_wins_over_v2_edits(self): + self.sync() + self.source["Workouts"][0]["number1"] = 20 # No V1 updatedAt field. + self.assertEqual(self.sync()["Workouts.updated"], 1) + self.db.execute("UPDATE Workouts SET name='V2 edit'") + self.db.commit() + self.assertEqual(self.sync()["Workouts.updated"], 1) + self.assertEqual(self.db.execute('SELECT name,number1 FROM Workouts').fetchone()[:], ('Day one', 20)) + + def test_dry_run_and_invalid_reference_roll_back_entire_merge(self): + self.sync(apply=False) + self.assertEqual(self.db.execute('SELECT COUNT(*) FROM Users').fetchone()[0], 2) + self.assertIsNone(self.db.execute("SELECT name FROM sqlite_master WHERE name='V1SyncMap'").fetchone()) + self.source["Workouts"][0]["video1"] = 99 + with self.assertRaisesRegex(ValueError, "missing video"): + self.sync() + self.assertEqual(self.db.execute('SELECT COUNT(*) FROM Videos').fetchone()[0], 0) + self.assertEqual(self.db.execute('SELECT COUNT(*) FROM Users').fetchone()[0], 2) + + def test_deletes_only_imported_data_and_deactivates_deleted_accounts(self): + self.sync() + uid = self.mapped_id("Users") + self.source = {table: [] for table in self.source} + counts = self.sync() + self.assertEqual(counts["Users.deactivated"], 1) + self.assertEqual(counts["Workouts.deleted"], 1) + self.assertEqual(counts["Videos.deleted"], 1) + self.assertEqual(self.db.execute('SELECT isActive FROM Users WHERE id=?', (uid,)).fetchone()[0], 0) + self.assertEqual(self.db.execute('SELECT isActive FROM Users WHERE id=1').fetchone()[0], 1) + self.assertEqual(self.sync(), {}) + + def test_keeps_deleted_video_referenced_by_v2_assignment(self): + self.sync() + vid = self.mapped_id("Videos") + self.db.execute("INSERT INTO ClientWorkouts (organizationId,clientId,coachId,items,createdAt,updatedAt) VALUES (1,1,1,?,'2026','2026')", (json.dumps([{"videoId": vid}]),)) + self.db.commit() + self.source["Workouts"] = [] + self.source["Videos"] = [] + self.assertEqual(self.sync()["Videos.retained_referenced"], 1) + self.assertEqual(self.db.execute('SELECT COUNT(*) FROM Videos').fetchone()[0], 1) + + def test_refuses_username_collision_and_cross_org_mapping(self): + self.source["Users"][0]["username"] = "system-admin" + with self.assertRaisesRegex(ValueError, "username collision"): + self.sync() + self.source["Users"][0]["username"] = "v1-admin" + self.sync() + self.db.execute('UPDATE Users SET organizationId=999 WHERE id=?', (self.mapped_id("Users"),)) + self.db.commit() + with self.assertRaisesRegex(ValueError, "organization"): + self.sync() + + def test_restores_imported_record_deleted_in_v2_using_same_id(self): + self.sync() + wid = self.mapped_id("Workouts") + self.db.execute('DELETE FROM Workouts WHERE id=?', (wid,)) + self.db.commit() + self.sync() + self.assertEqual(self.db.execute('SELECT id FROM Workouts').fetchone()[0], wid) + + def test_explicit_systemadmin_override_survives_sync_and_password_changes(self): + self.sync() + def overridden_sync(): + return merge(self.db, self.source, self.media, 1, 1, 'v1-source', + apply=True, user_role_overrides={'1': 'SystemAdmin'}) + self.assertEqual(overridden_sync()['Users.updated'], 1) + self.assertEqual(overridden_sync(), {}) + self.source['Users'][0]['password'] = 'changed-v1-hash' + overridden_sync() + user = self.db.execute('SELECT role,password FROM Users WHERE id=?', (self.mapped_id('Users'),)).fetchone() + self.assertEqual(tuple(user), ('SystemAdmin', 'changed-v1-hash')) + with self.assertRaisesRegex(ValueError, 'SystemAdmin'): + self.sync() # A privileged mapping still requires an explicit override. + + def test_video_copy_rerun_collision_and_missing_file(self): + root = Path(self.directory.name) + source, stage, uploads = (root / name for name in ("source", "stage", "uploads")) + for directory in (source, stage, uploads): + directory.mkdir() + (source / 'video.mp4').write_bytes(b'video-content') + staged = stage_videos(self.source["Videos"], source, stage) + publish_videos(staged, uploads) + publish_videos(staged, uploads) + target = uploads / staged[1]["filename"] + self.assertEqual(target.read_bytes(), b'video-content') + target.write_bytes(b'damaged') + with self.assertRaisesRegex(ValueError, "collision"): + publish_videos(staged, uploads) + (source / 'video.mp4').unlink() + with self.assertRaises(FileNotFoundError): + stage_videos(self.source["Videos"], source, stage) + + def test_full_pipeline_snapshots_dry_run_apply_and_source_unchanged(self): + root = Path(self.directory.name) + v1 = root / "v1.sqlite" + source_db = sqlite3.connect(v1) + try: + for table, rows in self.source.items(): + columns = ','.join(f'"{key}" {"INTEGER" if isinstance(value, int) or value is None else "TEXT"}' + for key, value in rows[0].items()) + source_db.execute(f'CREATE TABLE "{table}" ({columns})') + for row in rows: + source_db.execute(f'INSERT INTO "{table}" VALUES ({",".join("?" for _ in row)})', tuple(row.values())) + source_db.commit() + finally: + source_db.close() + original = v1.read_bytes() + for name in ("source-uploads", "target-uploads", "backups"): + (root / name).mkdir() + (root / 'source-uploads' / 'video.mp4').write_bytes(b'example-video') + config = {"v1_db": str(v1), "v2_db": str(self.target), + "v1_uploads": str(root / 'source-uploads'), "v2_uploads": str(root / 'target-uploads'), + "backup_dir": str(root / 'backups'), "organization_id": 1, "creator_id": 1, + "timezone": "UTC"} + with redirect_stdout(io.StringIO()): + self.assertFalse(run(config)["applied"]) + self.assertEqual(self.db.execute('SELECT COUNT(*) FROM Videos').fetchone()[0], 0) + self.assertEqual(list((root / 'target-uploads').iterdir()), []) + self.assertTrue(run(config, apply=True)["applied"]) + self.assertEqual(run(config, apply=True)["counts"], {}) + self.assertEqual(v1.read_bytes(), original) + self.assertEqual(len(list((root / 'backups').glob('*/result.json'))), 3) + self.assertEqual(len(list((root / 'backups').glob('*/v2-before.sqlite'))), 3) + video = self.db.execute('SELECT filename FROM Videos').fetchone()[0] + self.assertEqual((root / 'target-uploads' / video).read_bytes(), b'example-video') + config['retain_snapshots'] = False + config['creator_id'] = None + with redirect_stdout(io.StringIO()): + self.assertEqual(run(config, apply=True)['counts'], {}) + config['sync_hour'] = (datetime.now(timezone.utc).hour + 1) % 24 + self.assertEqual(run(config, apply=True, scheduled=True)['skipped'], 'outside scheduled hour') + config['sync_hour'] = datetime.now(timezone.utc).hour + self.assertEqual(run(config, apply=True, scheduled=True)['skipped'], 'already synced today') + self.assertEqual(len(list((root / 'backups').glob('*/result.json'))), 3) + self.assertEqual(len([p for p in (root / 'backups').iterdir() if p.is_dir()]), 3) + self.assertTrue((root / 'backups' / 'last-success.json').exists()) + + def test_overlapping_sync_is_rejected(self): + filename = Path(self.directory.name) / 'sync.lock' + with exclusive_lock(filename): + with self.assertRaises(OSError): + with exclusive_lock(filename): + self.fail('Second sync acquired an occupied lock') + + +if __name__ == '__main__': + unittest.main() diff --git a/V2/deploy/coexist/vitfit-v2-sync.cron b/V2/deploy/coexist/vitfit-v2-sync.cron new file mode 100644 index 0000000..ac965a4 --- /dev/null +++ b/V2/deploy/coexist/vitfit-v2-sync.cron @@ -0,0 +1,6 @@ +# /etc/cron.d/vitfit-v2-sync -- runs at 05:00 in the SERVER timezone. +# Confirm ubuntu is UID 1000 and can read V1 and write V2 storage/backups/logs. +# Install only after editing the config and completing a successful manual sync. +SHELL=/bin/sh +PATH=/usr/local/bin:/usr/bin:/bin +0 5 * * * ubuntu /usr/bin/timeout 3h /usr/bin/python3 /opt/VitFit/V2/deploy/coexist/sync.py --config /etc/vitfit-v2-sync.json --apply >> /var/log/vitfit-v2-sync.log 2>&1 diff --git a/V2/deploy/coexist/vitfit-v2-sync.logrotate b/V2/deploy/coexist/vitfit-v2-sync.logrotate new file mode 100644 index 0000000..e9a5d5c --- /dev/null +++ b/V2/deploy/coexist/vitfit-v2-sync.logrotate @@ -0,0 +1,8 @@ +/var/log/vitfit-v2-sync.log { + weekly + rotate 8 + compress + missingok + notifempty + create 0600 ubuntu ubuntu +} diff --git a/V2/deploy/coexist/vitfit-v2.service b/V2/deploy/coexist/vitfit-v2.service new file mode 100644 index 0000000..af02740 --- /dev/null +++ b/V2/deploy/coexist/vitfit-v2.service @@ -0,0 +1,22 @@ +[Unit] +Description=VitFit V2 +After=network.target + +[Service] +Type=simple +User=ubuntu +Group=ubuntu +WorkingDirectory=/home/ubuntu/vitfit-v2/app +EnvironmentFile=/home/ubuntu/vitfit-v2/app/.env +ExecStart=/home/ubuntu/vitfit-v2/runtime/bin/node app.js +Restart=always +RestartSec=5 +UMask=0077 +NoNewPrivileges=true +PrivateTmp=true +ProtectSystem=strict +ProtectHome=read-only +ReadWritePaths=/home/ubuntu/vitfit-v2/data + +[Install] +WantedBy=multi-user.target diff --git a/V2/deploy/coexist/workouttv.conf b/V2/deploy/coexist/workouttv.conf new file mode 100644 index 0000000..bb0e1c5 --- /dev/null +++ b/V2/deploy/coexist/workouttv.conf @@ -0,0 +1,20 @@ +# Use ONLY if the current domain has no existing Nginx server block. +# Otherwise retain its existing configuration and let Certbot update it. +server { + listen 80; + listen [::]:80; + server_name workouttv.vitalityfitnessva.com; + client_max_body_size 520m; + + location / { + proxy_pass http://127.0.0.1:3000; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + proxy_buffering off; + } +} diff --git a/V2/deploy/coexist/workouttv2-ssl.conf b/V2/deploy/coexist/workouttv2-ssl.conf new file mode 100644 index 0000000..bd8b0cc --- /dev/null +++ b/V2/deploy/coexist/workouttv2-ssl.conf @@ -0,0 +1,38 @@ +map $http_upgrade $vitfit_v2_connection_upgrade { + default upgrade; + '' close; +} + +server { + listen 80; + server_name workouttv2.vitalityfitnessva.com; + location ^~ /.well-known/acme-challenge/ { + root /var/www/letsencrypt; + } + location / { + return 301 https://$host$request_uri; + } +} + +server { + listen 443 ssl http2; + server_name workouttv2.vitalityfitnessva.com; + ssl_certificate /etc/letsencrypt/live/workouttv2.vitalityfitnessva.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/workouttv2.vitalityfitnessva.com/privkey.pem; + ssl_protocols TLSv1.2 TLSv1.3; + client_max_body_size 520m; + + location / { + proxy_pass http://127.0.0.1:3001; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $vitfit_v2_connection_upgrade; + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + proxy_buffering off; + } +} diff --git a/V2/deploy/coexist/workouttv2.conf b/V2/deploy/coexist/workouttv2.conf new file mode 100644 index 0000000..ec18b63 --- /dev/null +++ b/V2/deploy/coexist/workouttv2.conf @@ -0,0 +1,31 @@ +# HTTP bootstrap configuration. Certbot adds HTTPS and redirects after issuance. +# Install in /etc/nginx/sites-available and symlink into sites-enabled. +map $http_upgrade $vitfit_v2_connection_upgrade { + default upgrade; + '' close; +} + +server { + listen 80; + listen [::]:80; + server_name workouttv2.vitalityfitnessva.com; + client_max_body_size 520m; + + location ^~ /.well-known/acme-challenge/ { + root /var/www/letsencrypt; + } + + location / { + proxy_pass http://127.0.0.1:3001; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $vitfit_v2_connection_upgrade; + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + proxy_buffering off; + } +}