source: Klonkt/src/routes/admin-updates.js@ 929d98d

main
Last change on this file since 929d98d was ff08153, checked in by roboburr <roboburr@…>, 3 months ago

Update feature: Admin -> Updates (git-based v1, per instance)

Admin panel shows app version + current vs. latest commit (from .klonkt-version
vs. bare repo) + an 'Update now' button. POST spawns a detached update script
that checks out the latest main, runs npm ci on lock changes, writes
.klonkt-version, and pm2 reloads. God-only. Signed release feed for external
self-hosters = v2.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@…>

  • Property mode set to 100644
File size: 3.1 KB
Line 
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
13import express from 'express';
14import { execFileSync, spawn } from 'child_process';
15import fs from 'fs';
16import path from 'path';
17import { renderPage } from '../middleware/render.js';
18import { requireGod } from '../middleware/auth.js';
19
20const router = express.Router();
21
22const HOME = process.env.HOME || '';
23const GIT_DIR = process.env.KLONKT_GIT_DIR || path.join(HOME, 'git-repos/prutfolio.git');
24const UPDATE_SCRIPT = process.env.KLONKT_UPDATE_SCRIPT || path.join(HOME, 'bin/klonkt-self-update.sh');
25
26function versionFile() { return path.join(process.cwd(), '.klonkt-version'); }
27function appVersion() {
28 try { return JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8')).version || null; }
29 catch { return null; }
30}
31function git(args) {
32 try { return execFileSync('git', ['--git-dir', GIT_DIR, ...args], { encoding: 'utf8', timeout: 5000 }).trim(); }
33 catch { return null; }
34}
35function currentSha() {
36 try { return fs.readFileSync(versionFile(), 'utf8').trim() || null; } catch { return null; }
37}
38
39router.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
58router.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
72export default router;
Note: See TracBrowser for help on using the repository browser.