|
1 | 1 | function Sync-CippContainerUpdateState { |
2 | 2 | <# |
3 | 3 | .SYNOPSIS |
4 | | - Reconcile the stored container update-check result with the build that is actually running. |
| 4 | + Resolve container update settings and reconcile the stored check result with the running build. |
5 | 5 |
|
6 | 6 | .DESCRIPTION |
| 7 | + Returns the effective container update settings. Fields that have never been saved resolve |
| 8 | + to the defaults: auto-restart enabled, check every hour, preferred time 23:00. Explicitly |
| 9 | + saved values are respected — including CheckInterval '0' (disabled) and a CheckTime saved |
| 10 | + as an empty string, which means "no preferred time"; only a missing field falls back to |
| 11 | + the default. Both the Status endpoint and the update-check timer consume this, so the |
| 12 | + defaults apply identically to both. |
| 13 | +
|
7 | 14 | The ContainerUpdateSettings table is only written when an update check runs, so after a |
8 | 15 | restart that applied an update the table keeps reporting the previous build's state — |
9 | 16 | "update available" with the old running version — until the next check. This compares the |
10 | 17 | stored result against the running APP_VERSION/IMAGE_TAG and clears or recomputes the flag |
11 | | - locally, without a registry call. Returns the current settings entity, or $null when no |
12 | | - settings have been saved yet. |
| 18 | + locally, without a registry call. |
13 | 19 | #> |
14 | 20 | [CmdletBinding()] |
15 | 21 | param() |
16 | 22 |
|
17 | 23 | $SettingsTable = Get-CippTable -tablename 'ContainerUpdateSettings' |
18 | 24 | $Settings = Get-CIPPAzDataTableEntity @SettingsTable -Filter "PartitionKey eq 'Settings' and RowKey eq 'UpdateConfig'" | Select-Object -First 1 |
19 | | - if (-not $Settings) { return $null } |
| 25 | + |
| 26 | + # Effective schedule settings — defaults apply only to never-saved fields |
| 27 | + $AutoUpdate = if ([string]::IsNullOrWhiteSpace([string]$Settings.AutoUpdate)) { 'true' } else { [string]$Settings.AutoUpdate } |
| 28 | + $CheckInterval = if ([string]::IsNullOrWhiteSpace([string]$Settings.CheckInterval)) { '1h' } else { [string]$Settings.CheckInterval } |
| 29 | + $CheckTime = if ($null -eq $Settings.CheckTime) { '23' } else { [string]$Settings.CheckTime } |
20 | 30 |
|
21 | 31 | $RunningVersion = $env:APP_VERSION |
22 | 32 | $StoredRunning = [string]($Settings.RunningVersion ?? '') |
23 | | - if (-not $RunningVersion -or -not $StoredRunning -or $StoredRunning -eq $RunningVersion) { |
24 | | - return $Settings |
25 | | - } |
26 | | - |
27 | | - # The container restarted onto a different build since the last check. |
28 | 33 | $StoredRemote = [string]($Settings.RemoteVersion ?? '') |
29 | 34 | $CheckedTag = [string]($Settings.CheckedTag ?? '') |
30 | | - $RunningTag = $env:IMAGE_TAG |
31 | | - if ($CheckedTag -and $RunningTag -and $CheckedTag -ne $RunningTag) { |
32 | | - # The last check ran against a different channel tag — its result no longer applies. |
33 | | - $UpdateAvailable = $false |
34 | | - } else { |
35 | | - $UpdateAvailable = [bool]($StoredRemote -and $StoredRemote -ne $RunningVersion) |
| 35 | + $UpdateAvailable = [string]($Settings.UpdateAvailable ?? 'false') |
| 36 | + |
| 37 | + $NeedsWrite = $false |
| 38 | + if ($Settings -and $RunningVersion -and $StoredRunning -and $StoredRunning -ne $RunningVersion) { |
| 39 | + # The container restarted onto a different build since the last check. |
| 40 | + $RunningTag = $env:IMAGE_TAG |
| 41 | + if ($CheckedTag -and $RunningTag -and $CheckedTag -ne $RunningTag) { |
| 42 | + # The last check ran against a different channel tag — its result no longer applies. |
| 43 | + $UpdateAvailable = 'False' |
| 44 | + } else { |
| 45 | + $UpdateAvailable = [string][bool]($StoredRemote -and $StoredRemote -ne $RunningVersion) |
| 46 | + } |
| 47 | + Write-Information "Container update state reconciled: running build changed '$StoredRunning' -> '$RunningVersion', UpdateAvailable=$UpdateAvailable" |
| 48 | + $StoredRunning = $RunningVersion |
| 49 | + $NeedsWrite = $true |
36 | 50 | } |
37 | 51 |
|
38 | 52 | $Entity = @{ |
39 | 53 | PartitionKey = 'Settings' |
40 | 54 | RowKey = 'UpdateConfig' |
41 | | - AutoUpdate = [string]($Settings.AutoUpdate ?? 'false') |
42 | | - CheckInterval = [string]($Settings.CheckInterval ?? '0') |
43 | | - CheckTime = [string]($Settings.CheckTime ?? '') |
| 55 | + AutoUpdate = $AutoUpdate |
| 56 | + CheckInterval = $CheckInterval |
| 57 | + CheckTime = $CheckTime |
44 | 58 | LastCheck = [string]($Settings.LastCheck ?? '') |
45 | | - UpdateAvailable = [string]$UpdateAvailable |
46 | | - RunningVersion = [string]$RunningVersion |
| 59 | + UpdateAvailable = $UpdateAvailable |
| 60 | + RunningVersion = $StoredRunning |
47 | 61 | RemoteVersion = $StoredRemote |
48 | 62 | RemoteDigest = [string]($Settings.RemoteDigest ?? '') |
49 | 63 | RemoteBuildDate = [string]($Settings.RemoteBuildDate ?? '') |
50 | 64 | CheckedTag = $CheckedTag |
51 | 65 | } |
52 | | - Add-CIPPAzDataTableEntity @SettingsTable -Entity $Entity -Force | Out-Null |
53 | | - Write-Information "Container update state reconciled: running build changed '$StoredRunning' -> '$RunningVersion', UpdateAvailable=$UpdateAvailable" |
| 66 | + if ($NeedsWrite) { |
| 67 | + Add-CIPPAzDataTableEntity @SettingsTable -Entity $Entity -Force | Out-Null |
| 68 | + } |
54 | 69 | return [pscustomobject]$Entity |
55 | 70 | } |
0 commit comments