feature: the package installs the Router Manager, which is no longer a package of its own.
Release / release (push) Failing after 4s

This commit is contained in:
slawek committed 2026-11-14 12:52:44 +01:00
1 parent 6d90441bee
commit 8efe21bfa6
129 files changed
+17381 -104

No files matched your search

+274
View File
@@ -0,0 +1,274 @@
import Refresh from '@mui/icons-material/Refresh'
import Upgrade from '@mui/icons-material/Upgrade'
import {
Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, Stack, Table, TableBody, TableCell, TableHead, TableRow, Typography,
} from '@mui/material'
import { useCallback, useEffect, useState } from 'react'
import ConfirmDialog from './ConfirmDialog.tsx'
import LogView from './LogView.tsx'
import { INFO_SHAPE, UPDATES_SHAPE, UPGRADE_SHAPE, request, type Updates, type UpgradeState } from './api.ts'
/** Seconds between reads while an upgrade or a reboot is watched. */
const POLL_SECONDS = 3
/** The words for each state of the upgrade. */
const STATES: Record<UpgradeState['state'], string> = {
idle: 'No upgrade has run.',
running: 'The upgrade is running.',
succeeded: 'The upgrade succeeded.',
failed: 'The upgrade failed.',
interrupted: 'The upgrade was interrupted before it ended; the next upgrade finishes what dpkg left.',
}
/**
* The update information of the router's package, read when shown: a table of the package with its installed
* version and, when the repository apt is configured with has a newer one, that version, as of apt's last update of
* its package lists. With system, that of the system's packages instead: a table of the packages apt-get upgrade
* would upgrade, each with its installed and its new version, from /system-updates, upgraded by /system-upgrade.
*
* Check update, always in the top right corner, has the manager update apt's package lists, and the information is
* read again every POLL_SECONDS until the update ends.
*
* With updates available, Upgrade is added beside it and starts the upgrade of the packages on the router after a confirmation, and a dialog
* follows it every POLL_SECONDS with its output. The upgrade restarts the manager and the router's web service, so a
* failed read is shown as a restart and reading goes on. When the manager's package changed, Reload page loads its new
* web application. A failed or interrupted upgrade is shown with its output and can be started again. When a package
* asks for a reboot, Reboot reboots the router after a confirmation, and the page reloads once the manager answers again.
*
* onBusy is called with true while a check, an upgrade or a reboot runs and with false when it ends.
*/
export default function UpdatesNotice(props: { onBusy?: (busy: boolean) => void; system?: boolean }) {
const updatesPath = props.system ? '/system-updates' : '/updates'
const upgradePath = props.system ? '/system-upgrade' : '/upgrade'
const [updates, setUpdates] = useState<Updates | null>(null)
const [error, setError] = useState<string | null>(null)
const [upgrade, setUpgrade] = useState<UpgradeState | null>(null)
const [unreachable, setUnreachable] = useState(false)
const [watching, setWatching] = useState(false)
const [confirming, setConfirming] = useState<'upgrade' | 'reboot' | null>(null)
const [problem, setProblem] = useState<string | null>(null)
const [rebooting, setRebooting] = useState(false)
const read = useCallback(async () => {
try {
const next = await request<UpgradeState>('GET', upgradePath, UPGRADE_SHAPE)
setUpgrade(next)
setUnreachable(false)
if (next.state !== 'running') {
request<Updates>('GET', updatesPath, UPDATES_SHAPE).then(setUpdates, (caught: Error) => setError(caught.message))
}
} catch {
setUnreachable(true)
}
}, [updatesPath, upgradePath])
useEffect(() => {
request<Updates>('GET', updatesPath, UPDATES_SHAPE).then(setUpdates, (caught: Error) => setError(caught.message))
void read()
}, [read, updatesPath])
// A check for updates is followed until the manager's update of the package lists ends.
const checking = updates?.checking === true
useEffect(() => {
if (!checking) {
return
}
const timer = window.setInterval(() => {
request<Updates>('GET', updatesPath, UPDATES_SHAPE).then(setUpdates, (caught: Error) => setError(caught.message))
}, POLL_SECONDS * 1000)
return () => window.clearInterval(timer)
}, [checking, updatesPath])
async function check() {
setProblem(null)
try {
await request('POST', '/updates', () => true)
setUpdates(await request<Updates>('GET', updatesPath, UPDATES_SHAPE))
} catch (caught) {
setProblem((caught as Error).message)
}
}
// An upgrade is followed while its dialog is open or while it runs.
const running = upgrade?.state === 'running'
// The owner is told while a check for updates, an upgrade or a reboot runs, so it reads nothing else meanwhile, and
// that none runs when this notice goes away, as in a folded panel.
const { onBusy } = props
const busy = checking || running || upgrade?.rebooting === true || rebooting
useEffect(() => {
onBusy?.(busy)
return () => onBusy?.(false)
}, [onBusy, busy])
useEffect(() => {
if (!watching && !running) {
return
}
const timer = window.setInterval(() => void read(), POLL_SECONDS * 1000)
return () => window.clearInterval(timer)
}, [watching, running, read])
// After a reboot, the page reloads once the manager has stopped answering and answers again.
useEffect(() => {
if (!rebooting) {
return
}
let gone = false
const timer = window.setInterval(() => {
request('GET', '/info', INFO_SHAPE).then(() => {
if (gone) {
window.location.reload()
}
}, () => {
gone = true
})
}, POLL_SECONDS * 1000)
return () => window.clearInterval(timer)
}, [rebooting])
async function start(action: 'upgrade' | 'reboot') {
setConfirming(null)
setProblem(null)
try {
await request('POST', action === 'upgrade' ? upgradePath : '/reboot', () => true)
if (action === 'upgrade') {
setWatching(true)
await read()
} else {
setRebooting(true)
}
} catch (caught) {
setProblem((caught as Error).message)
}
}
const managerChanged = upgrade?.state === 'succeeded' && upgrade.before?.['vpn-router'] !== upgrade.after?.['vpn-router']
const newer = updates?.available ? updates.packages.filter((item) => item.update) : []
const checkedAt = checking
? 'checking for updates...'
: updates?.checked ? `as of ${new Date(updates.checked).toLocaleString()}` : ''
return (
<Stack sx={{ mb: 2, gap: 1 }}>
<Stack direction="row" sx={{ alignItems: 'center', gap: 1, '& .MuiButton-root': { flexShrink: 0, whiteSpace: 'nowrap' } }}>
<Typography variant="subtitle2" sx={{ flex: '1 1 0', minWidth: 0 }}>
{props.system ? 'Packages to upgrade' : 'Packages'}
<Typography component="span" variant="caption" color="text.secondary" sx={{ ml: 1 }}>{checkedAt}</Typography>
</Typography>
{newer.length > 0 && (
<Button size="small" variant="contained" startIcon={<Upgrade />} disabled={running || checking} onClick={() => setConfirming('upgrade')}>
Upgrade
</Button>
)}
<Button size="small" variant="outlined" startIcon={<Refresh />} disabled={!updates?.available || running || checking} onClick={() => void check()}>
Check update
</Button>
</Stack>
{error && <Typography variant="body2" color="text.secondary">Update information is not available: {error}</Typography>}
{!error && updates && !updates.available && (
<Typography variant="body2" color="text.secondary">Update information is not available here: apt cannot be asked.</Typography>
)}
{!error && updates?.available && (
<Table size="small">
<TableHead>
<TableRow>
<TableCell sx={{ width: 260 }}>Package</TableCell>
<TableCell>Version</TableCell>
</TableRow>
</TableHead>
<TableBody>
{updates.packages.length === 0 && (
<TableRow>
<TableCell colSpan={2} sx={{ color: 'text.secondary' }}>None; every package is up to date.</TableCell>
</TableRow>
)}
{updates.packages.map((item) => (
<TableRow key={item.name}>
<TableCell>{item.name}</TableCell>
<TableCell sx={{ color: item.update ? 'warning.main' : undefined }}>
{item.installed ?? 'not installed'}
{item.update ? `, ${item.candidate} available` : ''}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
{problem && <Alert severity="error" onClose={() => setProblem(null)}>{problem}</Alert>}
{upgrade && ['running', 'failed', 'interrupted'].includes(upgrade.state) && !watching && (
<Alert
severity={running ? 'info' : 'warning'}
action={<Button color="inherit" size="small" onClick={() => setWatching(true)}>Show</Button>}
>
{STATES[upgrade.state]}
</Alert>
)}
{upgrade?.reboot_required && (
<Alert
severity="warning"
action={
<Button color="inherit" size="small" disabled={running || upgrade.rebooting || rebooting} onClick={() => setConfirming('reboot')}>
Reboot
</Button>
}
>
{upgrade.rebooting || rebooting
? 'The router is rebooting; the page reloads when the Router Manager answers again.'
: `The router needs a reboot${upgrade.reboot_packages.length > 0 ? ` for ${upgrade.reboot_packages.join(', ')}` : ''}.`}
</Alert>
)}
{confirming === 'upgrade' && (
<ConfirmDialog
title={props.system ? "Upgrade the system's packages" : "Upgrade the router's package"}
message={props.system
? `The ${newer.length} packages listed are upgraded on the router with apt-get upgrade; none is installed or removed. Services restart during the upgrade, the Router Manager among them when vpn-router is listed, and the page may not answer for a while.`
: 'vpn-router is upgraded on the router. The Router Manager and the router\'s web service restart during the upgrade, and the page may not answer for a while.'}
confirmLabel="Upgrade"
destructive={false}
onConfirm={() => void start('upgrade')}
onCancel={() => setConfirming(null)}
/>
)}
{confirming === 'reboot' && (
<ConfirmDialog
title="Reboot the router"
message={'The router reboots in a few seconds. Its VPN connections are interrupted until it is up again.'}
confirmLabel="Reboot"
destructive
onConfirm={() => void start('reboot')}
onCancel={() => setConfirming(null)}
/>
)}
<Dialog open={watching} onClose={() => !running && setWatching(false)} fullWidth maxWidth="md">
<DialogTitle>{props.system ? 'System upgrade' : 'Upgrade'}</DialogTitle>
<DialogContent>
<Stack sx={{ gap: 1 }}>
{unreachable && <Alert severity="info">The Router Manager is restarting...</Alert>}
{upgrade && (
<Typography variant="body2">
{STATES[upgrade.state]}
{upgrade.user && upgrade.started ? ` Started by ${upgrade.user} at ${new Date(upgrade.started).toLocaleString()}.` : ''}
{upgrade.exit_status !== null && upgrade.state !== 'succeeded' ? ` apt-get ended with status ${upgrade.exit_status}.` : ''}
</Typography>
)}
{upgrade?.state === 'succeeded' && upgrade.after && (
<Typography variant="body2">
{Object.entries(upgrade.after).map(([name, version]) => `${name} ${upgrade.before?.[name] ?? 'none'} -> ${version ?? 'none'}`).join(', ')}
</Typography>
)}
<LogView lines={upgrade?.log ?? null} empty="(no output yet)" />
</Stack>
</DialogContent>
<DialogActions>
{managerChanged && <Button variant="contained" onClick={() => window.location.reload()}>Reload page</Button>}
{(upgrade?.state === 'failed' || upgrade?.state === 'interrupted') && (
<Button onClick={() => setConfirming('upgrade')}>Upgrade again</Button>
)}
<Button disabled={running} onClick={() => setWatching(false)}>Close</Button>
</DialogActions>
</Dialog>
</Stack>
)
}