| 1 | /**
|
|---|
| 2 | * Admin: Updates (god-only).
|
|---|
| 3 | * GET /admin/updates -> current vs. latest version + status
|
|---|
| 4 | * POST /admin/updates/run -> fetch latest + restart (fleet only; see below)
|
|---|
| 5 | *
|
|---|
| 6 | * Two topologies are supported, detected automatically:
|
|---|
| 7 | * - CHECKOUT (external self-hoster): the app dir is itself a git clone with
|
|---|
| 8 | * origin = GitHub. "Latest" = origin/<branch> (fetched on view). Updating is
|
|---|
| 9 | * done out-of-band by `klonkt-update` (needs root for systemd), so the page
|
|---|
| 10 | * shows that command instead of an in-app button.
|
|---|
| 11 | * - BARE (Robin's own VPS fleet): a bare repo at KLONKT_GIT_DIR; the app dir is
|
|---|
| 12 | * a `checkout -f` work-tree (no .git). "Latest" = <branch>. The detached
|
|---|
| 13 | * self-update script runs in-app (no root needed) → the button works.
|
|---|
| 14 | * git stderr is ignored so a foreign/missing repo never spams "fatal: ...".
|
|---|
| 15 | */
|
|---|
| 16 |
|
|---|
| 17 | import express from 'express';
|
|---|
| 18 | import { execFileSync, spawn } from 'child_process';
|
|---|
| 19 | import fs from 'fs';
|
|---|
| 20 | import path from 'path';
|
|---|
| 21 | import { renderPage } from '../middleware/render.js';
|
|---|
| 22 | import { requireGod } from '../middleware/auth.js';
|
|---|
| 23 |
|
|---|
| 24 | const router = express.Router();
|
|---|
| 25 |
|
|---|
| 26 | const HOME = process.env.HOME || '';
|
|---|
| 27 | const APP_DIR = process.cwd();
|
|---|
| 28 | const BRANCH = process.env.KLONKT_BRANCH || 'main';
|
|---|
| 29 | const GIT_DIR = process.env.KLONKT_GIT_DIR || path.join(HOME, 'git-repos/prutfolio.git');
|
|---|
| 30 | const UPDATE_SCRIPT = process.env.KLONKT_UPDATE_SCRIPT || path.join(HOME, 'bin/klonkt-self-update.sh');
|
|---|
| 31 |
|
|---|
| 32 | // The app dir is a git CHECKOUT (GitHub install) when it has a .git; otherwise we
|
|---|
| 33 | // fall back to the BARE repo (fleet). This split keeps the version check pointed at
|
|---|
| 34 | // a repo that actually exists, so it never logs "fatal: not a git repository".
|
|---|
| 35 | const IS_CHECKOUT = (() => { try { return fs.existsSync(path.join(APP_DIR, '.git')); } catch { return false; } })();
|
|---|
| 36 | const REMOTE_REF = IS_CHECKOUT ? `origin/${BRANCH}` : BRANCH; // what "latest" resolves to
|
|---|
| 37 |
|
|---|
| 38 | function appVersion() {
|
|---|
| 39 | try { return JSON.parse(fs.readFileSync(path.join(APP_DIR, 'package.json'), 'utf8')).version || null; }
|
|---|
| 40 | catch { return null; }
|
|---|
| 41 | }
|
|---|
| 42 | // stderr is ignored on purpose → a missing/foreign repo fails silently (returns null).
|
|---|
| 43 | function git(args) {
|
|---|
| 44 | try {
|
|---|
| 45 | const base = IS_CHECKOUT ? ['-C', APP_DIR] : ['--git-dir', GIT_DIR];
|
|---|
| 46 | return execFileSync('git', [...base, ...args], { encoding: 'utf8', timeout: 8000, stdio: ['ignore', 'pipe', 'ignore'] }).trim();
|
|---|
| 47 | } catch { return null; }
|
|---|
| 48 | }
|
|---|
| 49 | function currentSha() {
|
|---|
| 50 | if (IS_CHECKOUT) return git(['rev-parse', 'HEAD']);
|
|---|
| 51 | try { return fs.readFileSync(path.join(APP_DIR, '.klonkt-version'), 'utf8').trim() || null; } catch { return null; }
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | // Last 5 commits = the "recent changes" you'll get when updating.
|
|---|
| 55 | function recentChanges() {
|
|---|
| 56 | const out = git(['log', '-5', '--format=%s%x1f%cd', '--date=short', REMOTE_REF]);
|
|---|
| 57 | if (!out) return [];
|
|---|
| 58 | return out.split('\n').map((l) => {
|
|---|
| 59 | const i = l.indexOf('\x1f');
|
|---|
| 60 | return i >= 0 ? { msg: l.slice(0, i), date: l.slice(i + 1) } : { msg: l, date: '' };
|
|---|
| 61 | });
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | router.get('/', requireGod, (req, res) => {
|
|---|
| 65 | // For a GitHub checkout, refresh the remote ref so "latest" is current. Quiet +
|
|---|
| 66 | // shallow; offline just leaves the last-known ref. stderr ignored (no log noise).
|
|---|
| 67 | if (IS_CHECKOUT) {
|
|---|
| 68 | try { execFileSync('git', ['-C', APP_DIR, 'fetch', '--quiet', '--depth', '1', 'origin', BRANCH], { timeout: 20000, stdio: 'ignore' }); } catch { /* offline / no remote */ }
|
|---|
| 69 | }
|
|---|
| 70 | const cur = currentSha();
|
|---|
| 71 | const latest = git(['rev-parse', REMOTE_REF]);
|
|---|
| 72 | // The in-app "Update now" button only works with the detached self-update script
|
|---|
| 73 | // (the fleet). A systemd install updates via `klonkt-update` (root) → show that.
|
|---|
| 74 | const canSelfUpdate = (() => { try { return fs.existsSync(UPDATE_SCRIPT); } catch { return false; } })();
|
|---|
| 75 | renderPage(req, res, 'pages/admin-updates', {
|
|---|
| 76 | pageTitleKey: 'admin.t_updates',
|
|---|
| 77 | bodyClass: 'on-admin',
|
|---|
| 78 | appVersion: appVersion(),
|
|---|
| 79 | currentSha: cur ? cur.slice(0, 8) : null,
|
|---|
| 80 | currentDesc: cur ? git(['log', '-1', '--format=%s · %cd', '--date=short', cur]) : null,
|
|---|
| 81 | latestSha: latest ? latest.slice(0, 8) : null,
|
|---|
| 82 | latestDesc: latest ? git(['log', '-1', '--format=%s · %cd', '--date=short', REMOTE_REF]) : null,
|
|---|
| 83 | upToDate: !!(cur && latest && cur === latest),
|
|---|
| 84 | canCheck: !!latest,
|
|---|
| 85 | canSelfUpdate,
|
|---|
| 86 | manualCommand: (!canSelfUpdate && IS_CHECKOUT) ? 'sudo klonkt-update' : null,
|
|---|
| 87 | behind: (cur && latest && cur !== latest) ? git(['rev-list', '--count', cur + '..' + REMOTE_REF]) : null,
|
|---|
| 88 | changes: recentChanges(),
|
|---|
| 89 | success: req.query.success || null,
|
|---|
| 90 | error: req.query.error || null,
|
|---|
| 91 | });
|
|---|
| 92 | });
|
|---|
| 93 |
|
|---|
| 94 | router.post('/run', requireGod, (req, res) => {
|
|---|
| 95 | if (!fs.existsSync(UPDATE_SCRIPT)) {
|
|---|
| 96 | return res.redirect('/admin/updates?error=' + encodeURIComponent('In-app updaten is hier niet beschikbaar — werk bij met `klonkt-update` op de server.'));
|
|---|
| 97 | }
|
|---|
| 98 | try {
|
|---|
| 99 | // Detached + unlinked: survives the reload that restarts this app.
|
|---|
| 100 | const child = spawn('bash', [UPDATE_SCRIPT, APP_DIR], { detached: true, stdio: 'ignore' });
|
|---|
| 101 | child.unref();
|
|---|
| 102 | } catch (e) {
|
|---|
| 103 | return res.redirect('/admin/updates?error=' + encodeURIComponent('Kon update niet starten: ' + (e.message || e)));
|
|---|
| 104 | }
|
|---|
| 105 | res.redirect('/admin/updates?success=' + encodeURIComponent('Bijwerken gestart — de site herstart over ~10 seconden. Ververs daarna deze pagina.'));
|
|---|
| 106 | });
|
|---|
| 107 |
|
|---|
| 108 | export default router;
|
|---|