Document updating the agents, and harden the Windows update check
- AGENT_UPDATE.md: when an agent needs updating, how (reuse the stored token or rotate it), how to do many servers, how to check it worked, and why a server can stay 'not reported'. - Windows agent: the Windows Update search runs in its own process with a 150 s limit (the scheduled task is stopped after five minutes), a failed search is not retried for an hour, and the cache is read the same in PowerShell 7 and Windows PowerShell 5.1. - Linux agent: says in the journal when the package manager could not be read. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
171511c6db
commit
3da5c639b1
5 files changed
+225
-18
No files matched your search
@@ -215,6 +215,11 @@ collect_updates() {
|
||||
[[ -f /run/reboot-needed ]] && reboot="true"
|
||||
fi
|
||||
|
||||
if [[ -n "$manager" && ! "$count" =~ ^[0-9]+$ ]]; then
|
||||
# In the journal (systemctl status / journalctl -u homelab-manager-agent) so "not reported" has a reason to look for.
|
||||
echo "Note: couldn't read the pending updates from $manager - the report goes without them." >&2
|
||||
fi
|
||||
|
||||
if [[ -n "$manager" && "$count" =~ ^[0-9]+$ ]]; then
|
||||
[[ "$security" =~ ^[0-9]+$ ]] || security="null"
|
||||
UPDATES_JSON=$(jq -n --arg manager "$manager" --argjson count "$count" --argjson security "$security" --argjson reboot "$reboot" \
|
||||
|
||||
@@ -6,9 +6,10 @@ runs as a scheduled task (as SYSTEM, every 15 minutes) and pushes to `POST /api/
|
||||
the machine never needs to be reachable from Homelab Manager.
|
||||
|
||||
|
||||
Each report also says how many updates Windows Update has waiting (and how many are security or critical, and whether a restart is due). Searching Windows Update goes over the network and can take a minute or two, so it is done at most every six hours; the answer is kept in `%ProgramData%\HomelabManager\updates.json` and reused between. Nothing is downloaded or installed.
|
||||
Each report also says how many updates Windows Update has waiting (and how many are security or critical, and whether a restart is due). Searching Windows Update goes over the network and can take a minute or two, so it runs in a separate process with a 150-second limit and is done at most every six hours; the answer is kept in `%ProgramData%\HomelabManager\updates.json` and reused between (a search that fails is not tried again for an hour, and that report simply leaves updates out). Nothing is downloaded or installed. To update an agent that was installed before this existed, see [AGENT_UPDATE.md](../../AGENT_UPDATE.md).
|
||||
|
||||
The installer limits `%ProgramData%\HomelabManager` to SYSTEM and Administrators. The agent runs as SYSTEM, so anyone who could change its script could run code as SYSTEM; the folder is not left to inherit ProgramData's default permissions. Re-running the install one-liner applies this to an existing install.
|
||||
|
||||
## Install
|
||||
|
||||
1. In Homelab Manager, **Servers → Manage servers → Add a server**, choose **Windows** as the
|
||||
|
||||
@@ -232,36 +232,55 @@ function Test-RebootRequired {
|
||||
return $false
|
||||
}
|
||||
|
||||
# Asks Windows Update what is waiting. That goes over the network and can take a minute, so it is done at most every six hours and the
|
||||
# last answer is reused in between; whether a restart is due is cheap and always read fresh. Nothing is installed or downloaded.
|
||||
# Asks Windows Update what is waiting. That goes over the network and can take a minute or two, and the scheduled task is stopped
|
||||
# after five minutes, so the search runs in a separate process with a time limit, and is repeated at most every six hours; the last
|
||||
# answer is reused in between. A search that fails or runs out of time is not tried again for an hour, so a broken Windows Update
|
||||
# can't slow down every report. Whether a restart is due is cheap and always read fresh. Nothing is installed or downloaded.
|
||||
function Get-PendingUpdates {
|
||||
$result = $null
|
||||
$cached = $null
|
||||
try {
|
||||
if (Test-Path $script:UpdatesCachePath) {
|
||||
$cached = Get-Content -Raw -Path $script:UpdatesCachePath | ConvertFrom-Json
|
||||
$checked = [datetime]::Parse([string]$cached.checked_at, [Globalization.CultureInfo]::InvariantCulture, [Globalization.DateTimeStyles]::RoundtripKind)
|
||||
if (([datetime]::UtcNow - $checked.ToUniversalTime()).TotalHours -lt 6) { $result = $cached }
|
||||
# PowerShell 7 hands a stored date back as a date, Windows PowerShell 5.1 as the text it was written as.
|
||||
$checked = $cached.checked_at
|
||||
if ($checked -isnot [datetime]) { $checked = [datetime]::Parse([string]$checked, [Globalization.CultureInfo]::InvariantCulture, [Globalization.DateTimeStyles]::RoundtripKind) }
|
||||
$limitHours = 6
|
||||
if ($cached.failed) { $limitHours = 1 }
|
||||
if (([datetime]::UtcNow - $checked.ToUniversalTime()).TotalHours -ge $limitHours) { $cached = $null }
|
||||
}
|
||||
} catch { $result = $null }
|
||||
} catch { $cached = $null }
|
||||
|
||||
if (-not $result) {
|
||||
$session = New-Object -ComObject Microsoft.Update.Session
|
||||
$found = $session.CreateUpdateSearcher().Search("IsInstalled=0 and IsHidden=0 and Type='Software'")
|
||||
$security = 0
|
||||
foreach ($u in $found.Updates) {
|
||||
foreach ($c in $u.Categories) {
|
||||
if ($c.Name -eq 'Security Updates' -or $c.Name -eq 'Critical Updates') { $security++; break }
|
||||
if (-not $cached) {
|
||||
$job = Start-Job -ScriptBlock {
|
||||
$session = New-Object -ComObject Microsoft.Update.Session
|
||||
$found = $session.CreateUpdateSearcher().Search("IsInstalled=0 and IsHidden=0 and Type='Software'")
|
||||
$security = 0
|
||||
foreach ($u in $found.Updates) {
|
||||
foreach ($c in $u.Categories) {
|
||||
if ($c.Name -eq 'Security Updates' -or $c.Name -eq 'Critical Updates') { $security++; break }
|
||||
}
|
||||
}
|
||||
[pscustomobject]@{ count = [int]$found.Updates.Count; security = [int]$security }
|
||||
}
|
||||
$result = [pscustomobject][ordered]@{ checked_at = [datetime]::UtcNow.ToString('o'); count = [int]$found.Updates.Count; security = [int]$security }
|
||||
$answer = $null
|
||||
try {
|
||||
if (Wait-Job -Job $job -Timeout 150) { $answer = Receive-Job -Job $job -ErrorAction Stop } else { Stop-Job -Job $job }
|
||||
} catch { $answer = $null } finally { Remove-Job -Job $job -Force -ErrorAction SilentlyContinue }
|
||||
|
||||
$count = 0
|
||||
$security = 0
|
||||
if ($null -ne $answer) { $count = [int]$answer.count; $security = [int]$answer.security }
|
||||
$cached = [pscustomobject][ordered]@{ checked_at = [datetime]::UtcNow.ToString('o'); failed = ($null -eq $answer); count = $count; security = $security }
|
||||
try {
|
||||
$dir = Split-Path $script:UpdatesCachePath
|
||||
if (-not (Test-Path $dir)) { $null = New-Item -ItemType Directory -Path $dir -Force }
|
||||
ConvertTo-Json -InputObject $result | Set-Content -Path $script:UpdatesCachePath -Encoding UTF8
|
||||
ConvertTo-Json -InputObject $cached | Set-Content -Path $script:UpdatesCachePath -Encoding UTF8
|
||||
} catch { }
|
||||
}
|
||||
|
||||
return [ordered]@{ manager = 'windows-update'; count = [int]$result.count; security = [int]$result.security; reboot_required = (Test-RebootRequired) }
|
||||
# No answer is no answer: the report then leaves updates out, and the server keeps what it was last told.
|
||||
if ($cached.failed) { return $null }
|
||||
return [ordered]@{ manager = 'windows-update'; count = [int]$cached.count; security = [int]$cached.security; reboot_required = (Test-RebootRequired) }
|
||||
}
|
||||
|
||||
function Get-SystemInfo {
|
||||
|
||||
Reference in new issue
Block a user