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 = { 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 server'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 server after a confirmation, and a dialog * follows it every POLL_SECONDS with its output. The upgrade restarts the manager and the server'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 server 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(null) const [error, setError] = useState(null) const [upgrade, setUpgrade] = useState(null) const [unreachable, setUnreachable] = useState(false) const [watching, setWatching] = useState(false) const [confirming, setConfirming] = useState<'upgrade' | 'reboot' | null>(null) const [problem, setProblem] = useState(null) const [rebooting, setRebooting] = useState(false) const read = useCallback(async () => { await request('GET', upgradePath, UPGRADE_SHAPE).then((next) => { setUpgrade(next) setUnreachable(false) if (next.state !== 'running') { request('GET', updatesPath, UPDATES_SHAPE).then(setUpdates, (caught: Error) => setError(caught.message)) } }, () => setUnreachable(true)) }, [updatesPath, upgradePath]) useEffect(() => { request('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('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('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?.['linux-manager'] !== upgrade.after?.['linux-manager'] 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 ( {props.system ? 'Packages to upgrade' : 'Packages'} {checkedAt} {newer.length > 0 && ( )} {error && Update information is not available: {error}} {!error && updates && !updates.available && ( Update information is not available here: apt cannot be asked. )} {!error && updates?.available && ( Package Version {updates.packages.length === 0 && ( None; every package is up to date. )} {updates.packages.map((item) => ( {item.name} {item.installed ?? 'not installed'} {item.update ? `, ${item.candidate} available` : ''} ))}
)} {problem && setProblem(null)}>{problem}} {upgrade && ['running', 'failed', 'interrupted'].includes(upgrade.state) && !watching && ( setWatching(true)}>Show} > {STATES[upgrade.state]} )} {upgrade?.reboot_required && ( setConfirming('reboot')}> Reboot } > {upgrade.rebooting || rebooting ? 'The server is rebooting; the page reloads when the Manager answers again.' : `The server needs a reboot${upgrade.reboot_packages.length > 0 ? ` for ${upgrade.reboot_packages.join(', ')}` : ''}.`} )} {confirming === 'upgrade' && ( void start('upgrade')} onCancel={() => setConfirming(null)} /> )} {confirming === 'reboot' && ( void start('reboot')} onCancel={() => setConfirming(null)} /> )} !running && setWatching(false)} fullWidth maxWidth="md"> {props.system ? 'System upgrade' : 'Upgrade'} {unreachable && The Manager is restarting...} {upgrade && ( {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}.` : ''} )} {upgrade?.state === 'succeeded' && upgrade.after && ( {Object.entries(upgrade.after).map(([name, version]) => `${name} ${upgrade.before?.[name] ?? 'none'} -> ${version ?? 'none'}`).join(', ')} )} {managerChanged && } {(upgrade?.state === 'failed' || upgrade?.state === 'interrupted') && ( )}
) }