Recover a Windows maintenance setup interrupted before policy activation
This commit is contained in:
@@ -70,10 +70,10 @@ $journal=Uuid $Witness.journal_id;$helper=Uuid $Witness.helper_id;$decision=Uuid
|
||||
$checkpoint=[long]$Witness.helper_checkpoint
|
||||
Check ($checkpoint -gt 0) 'Missing closed helper checkpoint'
|
||||
$rows=@($Witness.attempts|Where-Object {$null -ne $_})
|
||||
$gate=$null;$db=$null;$transaction=$false;$stopped=$false
|
||||
$gate=$null;$db=$null;$transaction=$false
|
||||
try {
|
||||
$gate=[IO.File]::Open((Join-Path $data 'release-job.lock'),'Open','ReadWrite','None')
|
||||
if((Get-Service $agent).Status -ne 'Stopped'){Stop-Service $agent;$stopped=$true}
|
||||
if((Get-Service $agent).Status -ne 'Stopped'){Stop-Service $agent}
|
||||
$apply=$Action -eq 'Apply'
|
||||
$db=[EnrollmentWindowDb]::new($dbPath,$apply)
|
||||
if($apply){
|
||||
@@ -84,9 +84,9 @@ try {
|
||||
if($apply){Check ($db.Scalar('PRAGMA local.journal_mode') -ceq 'delete') 'Local consumption journal mode differs';$db.Exec('PRAGMA local.synchronous=FULL')}
|
||||
$db.Exec($(if($apply){'BEGIN IMMEDIATE'}else{'BEGIN'}));$transaction=$true
|
||||
$done=$db.Scalar("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='retired_windows_update_windows'") -eq '1'
|
||||
if($done -and $db.Scalar("SELECT count(*) FROM retired_windows_update_windows WHERE case_id='$case' AND helper_id='$helper' AND journal_id='$journal'") -eq '1'){
|
||||
if($done -and $db.Scalar("SELECT count(*) FROM retired_windows_update_windows WHERE case_id='$case' AND helper_id='$helper' AND journal_id='$journal' AND decision_id='$decision' AND helper_checkpoint=$checkpoint") -eq '1'){
|
||||
$db.Exec('ROLLBACK');$transaction=$false
|
||||
[ordered]@{already_retired=$true;case_id=$case;attempts=$rows.Count}|ConvertTo-Json -Compress;return
|
||||
[ordered]@{already_retired=$true;verified=$true;applied=$true;case_id=$case;helper_id=$helper;decision_id=$decision;attempts=$rows.Count}|ConvertTo-Json -Compress;return
|
||||
}
|
||||
Check ($db.Scalar("SELECT count(*) FROM reconcile_meta WHERE id=1 AND journal_id='$journal' AND helper_id='$helper' AND helper_checkpoint=$checkpoint AND last_decision='$decision' AND pending_request IS NULL AND (fault IS NULL OR fault='HELPER_JOURNAL_DISAPPEARED')") -eq '1') 'Agent and signed central final checkpoint differ'
|
||||
Check ($db.Scalar("SELECT count(*) FROM proof.helper_sync_meta WHERE journal_id='$helper' AND agent_journal_id='$journal' AND checkpoint>=$checkpoint AND last_decision='$decision' AND fault IS NULL") -eq '1') 'Archived helper has not applied the same signed final decision'
|
||||
|
||||
@@ -88,6 +88,13 @@ $binding=[ordered]@{tenant_id=$clock.tenant_id;asset_id=$AssetId.ToString();agen
|
||||
$inputDto=[ordered]@{binding=$binding;release_id=$ReleaseId.ToString();agent_sha256=(GetHash $agentExe);helper_sha256=(GetHash $helperExe);max_runtime_seconds=$Seconds}
|
||||
$markerJson=([ordered]@{case_id=$CaseId.ToString();release_id=$ReleaseId.ToString();agent_id=$AgentId.ToString();asset_id=$AssetId.ToString()}|ConvertTo-Json -Compress)
|
||||
$archive=$root+'.closed-'+$CaseId.ToString()
|
||||
$staging=$root+'.preparing-'+$CaseId.ToString()
|
||||
function PublishMarker($destination,$readers) {
|
||||
if(Test-Path -LiteralPath $destination){VerifyMarker $destination;return}
|
||||
NewPrivateDir $staging $readers
|
||||
WriteFixed (Join-Path $staging 'windows-update-enrollment.json') $markerJson
|
||||
[IO.Directory]::Move($staging,$destination)
|
||||
}
|
||||
$backup=Join-Path $root 'original-agent-config.json'
|
||||
$written=Join-Path $root 'active-agent-config.json'
|
||||
$gate=$null
|
||||
@@ -111,7 +118,7 @@ function StartNormal {
|
||||
function RemoveWindow {
|
||||
if (!(Test-Path -LiteralPath $root)) {
|
||||
Check (!(Service $helper) -and !(JsonFile $cfg).windows_update_helper) 'Helper SCM or configuration present without its protected case marker.'
|
||||
if(Test-Path -LiteralPath $archive){VerifyMarker $archive}
|
||||
PublishMarker $archive @()
|
||||
if((Get-Service $actor).Status -ne 'Running'){Start-Service $actor}
|
||||
return
|
||||
}
|
||||
@@ -142,7 +149,14 @@ if(Test-Path -LiteralPath $root){VerifyMarker $root}
|
||||
if($Action -eq 'Plan'){[ordered]@{planned=$true;tenant_id=$binding.tenant_id;agent_id=$AgentId;asset_id=$AssetId;release_id=$ReleaseId;agent_sha256=$inputDto.agent_sha256;helper_sha256=$inputDto.helper_sha256;seconds=$Seconds;requires_helper_creation=(!$h);case_id=$CaseId}|ConvertTo-Json -Depth 6 -Compress;return}
|
||||
try {
|
||||
# The updater and agent hold the same file throughout a transition or execution.
|
||||
$gate=[IO.File]::Open((Join-Path $data 'release-job.lock'),'Open','ReadWrite','None')
|
||||
$until=[DateTime]::UtcNow.AddSeconds(60)
|
||||
do {
|
||||
try {$gate=[IO.File]::Open((Join-Path $data 'release-job.lock'),'Open','ReadWrite','None')}
|
||||
catch [IO.IOException] {
|
||||
if (($_.Exception.HResult -band 0xffff) -ne 32 -or $Action -ne 'Remove' -or [DateTime]::UtcNow -ge $until){throw}
|
||||
Start-Sleep -Milliseconds 400
|
||||
}
|
||||
} while (!$gate)
|
||||
Check ((JsonFile $active).release_id -ceq $ReleaseId.ToString()) 'Release switched during preparation.'
|
||||
if($Action -eq 'Resume'){
|
||||
Check (!(Service $helper) -and !(JsonFile $cfg).windows_update_helper -and !(Test-Path -LiteralPath $root)) 'Helper still active.'
|
||||
@@ -160,7 +174,7 @@ try {
|
||||
Check ((!$h) -or (Test-Path -LiteralPath $marker)) 'Existing helper service has no matching protected case.'
|
||||
$actorSid=GetSid $actor
|
||||
# The helper runs as SYSTEM; its service SID is optional on file DACLs.
|
||||
NewPrivateDir $root @($actorSid);WriteFixed $marker $markerJson
|
||||
PublishMarker $root @($actorSid)
|
||||
Check (!(Test-Path -LiteralPath (Join-Path $data 'release-state\release-transition.json'))) 'Release transition must finish before maintenance.'
|
||||
Set-Service 'RmmDiscoveryReleasePull' -StartupType Manual
|
||||
if((Get-Service 'RmmDiscoveryReleasePull').Status -ne 'Stopped'){Stop-Service 'RmmDiscoveryReleasePull'}
|
||||
|
||||
Reference in New Issue
Block a user