| [ff08153] | 1 | /**
|
|---|
| 2 | * Admin: Updates (god-only).
|
|---|
| 3 | * Git-gebaseerde v1 voor instances die via de bare-repo draaien.
|
|---|
| 4 | * GET /admin/updates -> huidige vs. nieuwste versie + status
|
|---|
| 5 | * POST /admin/updates/run -> haal nieuwste main op + herstart (detached script)
|
|---|
| 6 | *
|
|---|
| 7 | * De instance kent z'n "huidige" commit uit .klonkt-version (door het script
|
|---|
| 8 | * geschreven) en de "nieuwste" uit de bare repo (KLONKT_GIT_DIR). Voor externe
|
|---|
| 9 | * self-hosters komt later een GESIGNEERDE release-feed (zie monetization-plan);
|
|---|
| 10 | * deze v1 is bewust simpel en alleen voor Robins eigen VPS-instances.
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | import express from 'express';
|
|---|
| 14 | import { execFileSync, spawn } from 'child_process';
|
|---|
| 15 | import fs from 'fs';
|
|---|
| 16 | import path from 'path';
|
|---|
| 17 | import { renderPage } from '../middleware/render.js';
|
|---|
| 18 | import { requireGod } from '../middleware/auth.js';
|
|---|
| 19 |
|
|---|
| 20 | const router = express.Router();
|
|---|
| 21 |
|
|---|
| 22 | const HOME = process.env.HOME || '';
|
|---|
| 23 | const GIT_DIR = process.env.KLONKT_GIT_DIR || path.join(HOME, 'git-repos/prutfolio.git');
|
|---|
| 24 | const UPDATE_SCRIPT = process.env.KLONKT_UPDATE_SCRIPT || path.join(HOME, 'bin/klonkt-self-update.sh');
|
|---|
| 25 |
|
|---|
| 26 | function versionFile() { return path.join(process.cwd(), '.klonkt-version'); }
|
|---|
| 27 | function appVersion() {
|
|---|
| 28 | try { return JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8')).version || null; }
|
|---|
| 29 | catch { return null; }
|
|---|
| 30 | }
|
|---|
| 31 | function git(args) {
|
|---|
| 32 | try { return execFileSync('git', ['--git-dir', GIT_DIR, ...args], { encoding: 'utf8', timeout: 5000 }).trim(); }
|
|---|
| 33 | catch { return null; }
|
|---|
| 34 | }
|
|---|
| 35 | function currentSha() {
|
|---|
| 36 | try { return fs.readFileSync(versionFile(), 'utf8').trim() || null; } catch { return null; }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | router.get('/', requireGod, (req, res) => {
|
|---|
| 40 | const cur = currentSha();
|
|---|
| 41 | const latest = git(['rev-parse', 'main']);
|
|---|
| 42 | renderPage(req, res, 'pages/admin-updates', {
|
|---|
| 43 | pageTitle: 'Updates',
|
|---|
| 44 | bodyClass: 'on-admin',
|
|---|
| 45 | appVersion: appVersion(),
|
|---|
| 46 | currentSha: cur ? cur.slice(0, 8) : null,
|
|---|
| 47 | currentDesc: cur ? git(['log', '-1', '--format=%s · %cd', '--date=short', cur]) : null,
|
|---|
| 48 | latestSha: latest ? latest.slice(0, 8) : null,
|
|---|
| 49 | latestDesc: latest ? git(['log', '-1', '--format=%s · %cd', '--date=short', 'main']) : null,
|
|---|
| 50 | upToDate: !!(cur && latest && cur === latest),
|
|---|
| 51 | canCheck: !!latest,
|
|---|
| 52 | behind: (cur && latest && cur !== latest) ? git(['rev-list', '--count', cur + '..main']) : null,
|
|---|
| 53 | success: req.query.success || null,
|
|---|
| 54 | error: req.query.error || null,
|
|---|
| 55 | });
|
|---|
| 56 | });
|
|---|
| 57 |
|
|---|
| 58 | router.post('/run', requireGod, (req, res) => {
|
|---|
| 59 | if (!fs.existsSync(UPDATE_SCRIPT)) {
|
|---|
| 60 | return res.redirect('/admin/updates?error=' + encodeURIComponent('Update-script ontbreekt op de server.'));
|
|---|
| 61 | }
|
|---|
| 62 | try {
|
|---|
| 63 | // Detached + losgekoppeld: overleeft de pm2-reload die deze app herstart.
|
|---|
| 64 | const child = spawn('bash', [UPDATE_SCRIPT, process.cwd()], { detached: true, stdio: 'ignore' });
|
|---|
| 65 | child.unref();
|
|---|
| 66 | } catch (e) {
|
|---|
| 67 | return res.redirect('/admin/updates?error=' + encodeURIComponent('Kon update niet starten: ' + (e.message || e)));
|
|---|
| 68 | }
|
|---|
| 69 | res.redirect('/admin/updates?success=' + encodeURIComponent('Bijwerken gestart — de site herstart over ~10 seconden. Ververs daarna deze pagina.'));
|
|---|
| 70 | });
|
|---|
| 71 |
|
|---|
| 72 | export default router;
|
|---|