| 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 |
|
|---|
| 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 | // Last 5 commits on main = the "recent changes" you'll get when updating.
|
|---|
| 40 | function 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 |
|
|---|
| 49 | router.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 |
|
|---|
| 69 | router.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 |
|
|---|
| 83 | export default router;
|
|---|