source: Klonkt/src/routes/admin-updates.js@ 6bd25d1

main
Last change on this file since 6bd25d1 was 834bcc3, checked in by Robin Genis <roboburr@…>, 3 months ago

i18n: translate Dutch code comments to English across src/

Comments in routes/services/views/config/middleware/assets translated to
English for the public repo. A few dev-facing throw/console message strings
were Englished too. No user-facing UI strings or i18n dictionary values changed
(src/services/i18n.js untouched). Logic unchanged.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/**
2 * Admin: Updates (god-only).
3 * Git-based v1 for instances running from a bare repo.
4 * GET /admin/updates -> current vs. latest version + status
5 * POST /admin/updates/run -> fetch latest main + restart (detached script)
6 *
7 * The instance knows its "current" commit from .klonkt-version (written by the
8 * script) and the "latest" from the bare repo (KLONKT_GIT_DIR). For external
9 * self-hosters a SIGNED release feed will follow later (see monetization plan);
10 * this v1 is intentionally simple and only for Robin's own 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
39// Last 5 commits on main = the "recent changes" you'll get when updating.
40function recentChanges() {
41 const out = git(['log', '-5', '--format=%s%x1f%cd', '--date=short', 'main']);
42 if (!out) return [];
43 return out.split('\n').map((l) => {
44 const i = l.indexOf('\x1f');
45 return i >= 0 ? { msg: l.slice(0, i), date: l.slice(i + 1) } : { msg: l, date: '' };
46 });
47}
48
49router.get('/', requireGod, (req, res) => {
50 const cur = currentSha();
51 const latest = git(['rev-parse', 'main']);
52 renderPage(req, res, 'pages/admin-updates', {
53 pageTitle: 'Updates',
54 bodyClass: 'on-admin',
55 appVersion: appVersion(),
56 currentSha: cur ? cur.slice(0, 8) : null,
57 currentDesc: cur ? git(['log', '-1', '--format=%s · %cd', '--date=short', cur]) : null,
58 latestSha: latest ? latest.slice(0, 8) : null,
59 latestDesc: latest ? git(['log', '-1', '--format=%s · %cd', '--date=short', 'main']) : null,
60 upToDate: !!(cur && latest && cur === latest),
61 canCheck: !!latest,
62 behind: (cur && latest && cur !== latest) ? git(['rev-list', '--count', cur + '..main']) : null,
63 changes: recentChanges(),
64 success: req.query.success || null,
65 error: req.query.error || null,
66 });
67});
68
69router.post('/run', requireGod, (req, res) => {
70 if (!fs.existsSync(UPDATE_SCRIPT)) {
71 return res.redirect('/admin/updates?error=' + encodeURIComponent('Update-script ontbreekt op de server.'));
72 }
73 try {
74 // Detached + unlinked: survives the pm2-reload that restarts this app.
75 const child = spawn('bash', [UPDATE_SCRIPT, process.cwd()], { detached: true, stdio: 'ignore' });
76 child.unref();
77 } catch (e) {
78 return res.redirect('/admin/updates?error=' + encodeURIComponent('Kon update niet starten: ' + (e.message || e)));
79 }
80 res.redirect('/admin/updates?success=' + encodeURIComponent('Bijwerken gestart — de site herstart over ~10 seconden. Ververs daarna deze pagina.'));
81});
82
83export default router;
Note: See TracBrowser for help on using the repository browser.