Files
rust-discovery/migrations/0038_restart_scripts.sql
T

45 lines
2.0 KiB
PL/PgSQL

-- The approved operation schedules a reboot through the OS and can return
-- its script output before services stop. Fresh inventory proves the new boot.
CREATE FUNCTION seed_restart_scripts(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)
VALUES
(target_tenant,'Linux neu starten',1,'posix_sh','root','system:bootstrap',$sh$#!/bin/sh
set -eu
/usr/sbin/shutdown -r +1 'Genehmigter Neustart durch die Geräteverwaltung'
printf '%s\n' 'Neustart in einer Minute eingeplant.'
$sh$),
(target_tenant,'Windows neu starten',1,'powershell','system','system:bootstrap',$ps$$ErrorActionPreference = 'Stop'
& "$env:SystemRoot\System32\shutdown.exe" /r /t 60 /d p:0:0 /c 'Genehmigter Neustart durch die Geraeteverwaltung'
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
Write-Output 'Neustart in einer Minute eingeplant.'
$ps$)
ON CONFLICT (tenant_id, name, version) DO NOTHING;
PERFORM set_config('app.tenant_id', previous_tenant, true);
END
$seed$;
REVOKE ALL ON FUNCTION seed_restart_scripts(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 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_restart_scripts(tenant_id) FROM tenants;
END
$backfill$;