From 5889964ea13dcb08d252ae479cbb5faf73ca353f Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Wed, 18 Nov 2026 12:28:52 +0100 Subject: [PATCH] Check Windows reboot scheduling through an explicit process result --- crates/server/src/script_library.rs | 8 ++-- migrations/0039_windows_restart_receipt.sql | 44 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 migrations/0039_windows_restart_receipt.sql diff --git a/crates/server/src/script_library.rs b/crates/server/src/script_library.rs index 645fb15..9403f75 100644 --- a/crates/server/src/script_library.rs +++ b/crates/server/src/script_library.rs @@ -264,12 +264,12 @@ pub(crate) mod tests { let initial = list(s, p, tenant, PageQuery::default()).await.unwrap(); let items = initial["items"].as_array().unwrap(); assert_eq!(items.len(), 15); - for (name, interpreter, identity) in [ - ("Linux neu starten", "posix_sh", "root"), - ("Windows neu starten", "powershell", "system"), + for (name, interpreter, identity, version) in [ + ("Linux neu starten", "posix_sh", "root", 1), + ("Windows neu starten", "powershell", "system", 2), ] { let revision = items.iter().find(|r| r["name"] == name).unwrap(); - assert_eq!(revision["version"], 1); + assert_eq!(revision["version"], version); assert_eq!(revision["interpreter"], interpreter); assert_eq!(revision["execution_identity"], identity); assert_eq!( diff --git a/migrations/0039_windows_restart_receipt.sql b/migrations/0039_windows_restart_receipt.sql new file mode 100644 index 0000000..7c9d783 --- /dev/null +++ b/migrations/0039_windows_restart_receipt.sql @@ -0,0 +1,44 @@ +-- Keep existing plans bound to the old revision. Only advance the original +-- bootstrap template; a newer operator revision retains its position. +CREATE FUNCTION seed_windows_restart_receipt(target_tenant uuid) RETURNS void +LANGUAGE plpgsql SET search_path = pg_catalog, public AS $seed$ +DECLARE previous_tenant text := coalesce(current_setting('app.tenant_id', true), ''); +BEGIN + PERFORM set_config('app.tenant_id', target_tenant::text, true); + INSERT INTO public.script_revisions(tenant_id, name, version, interpreter, execution_identity, created_by, script_utf8) + SELECT target_tenant, 'Windows neu starten', 2, 'powershell', 'system', 'system:bootstrap', $ps$$ErrorActionPreference = 'Stop' +$exe = [IO.Path]::Combine([Environment]::SystemDirectory, 'shutdown.exe') +$process = Start-Process -FilePath $exe -ArgumentList '/r /t 60 /d p:0:0 /c "Genehmigter Neustart durch die Geraeteverwaltung"' -NoNewWindow -Wait -PassThru +if ($null -eq $process -or $null -eq $process.ExitCode) { throw 'Windows hat keinen Exitcode gemeldet.' } +if ($process.ExitCode -ne 0) { throw ('Windows hat den Neustart abgelehnt. Exitcode ' + $process.ExitCode) } +Write-Output 'Neustart in einer Minute eingeplant.' +$ps$ + WHERE EXISTS (SELECT 1 FROM public.script_revisions WHERE tenant_id=target_tenant AND name='Windows neu starten' + AND version=1 AND created_by='system:bootstrap' AND execution_identity='system' AND interpreter='powershell') + AND NOT EXISTS (SELECT 1 FROM public.script_revisions WHERE tenant_id=target_tenant AND name='Windows neu starten' AND version>1) + ON CONFLICT (tenant_id, name, version) DO NOTHING; + PERFORM set_config('app.tenant_id', previous_tenant, true); +END +$seed$; +REVOKE ALL ON FUNCTION seed_windows_restart_receipt(uuid) FROM PUBLIC; + +CREATE OR REPLACE FUNCTION seed_tenant_script_library() RETURNS trigger +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public AS $$ +DECLARE previous_tenant text := coalesce(current_setting('app.tenant_id', true), ''); +BEGIN + PERFORM set_config('app.tenant_id', NEW.tenant_id::text, true); + PERFORM public.seed_windows_update_script(NEW.tenant_id); + PERFORM public.seed_standard_diagnostic_scripts(NEW.tenant_id); + PERFORM public.seed_restart_scripts(NEW.tenant_id); + PERFORM public.seed_windows_restart_receipt(NEW.tenant_id); + PERFORM set_config('app.tenant_id', previous_tenant, true); + RETURN NEW; +END +$$; + +DO $backfill$ +BEGIN + LOCK TABLE tenants IN SHARE ROW EXCLUSIVE MODE; + PERFORM public.seed_windows_restart_receipt(tenant_id) FROM tenants; +END +$backfill$;