Privilege split 9/11: setup installs the hardware service and moves its settings

- kurgan/hw/install.py (run elevated by setup.ps1): checks the base
  interpreter can run hw_service.py, creates %ProgramData%\Kurgan or resets
  its owner and access (Administrators own it; SYSTEM and Administrators full
  control, users read), moves the fan and RAM settings there, then creates or
  updates KurganHw through the service manager API (pywin32; sc.exe's quoting
  from Windows PowerShell 5.1 isn't reliable): LocalSystem, automatic start,
  restart after 5 s, 10 s and 60 s, the user's SID for the pipe. A running
  service is stopped first; then it's started and must reach "running".
- kurgan/hw/migrate.py: "copy" (elevated) creates hw-settings.json from the
  user's cooling and lighting.memory, never overwriting an existing file;
  "strip" (as the user) removes them from settings.json once hw-settings.json
  exists, keeping a dated backup. An invalid file on either side stops it.
- setup.ps1: creates the .venv if needed (the service runs from it), passes
  the base interpreter, the user's SID and settings path to the elevated part,
  which installs the service; then strips settings.json, registers and starts
  the app's task, and prints the service's status.
- uninstall.ps1: also stops and deletes the service.
- python -m kurgan.hw.client prints the service's status (setup runs it).
- The pipe's "isn't running", "refused this user" and "closed the
  connection" errors no longer repeat the Windows error text.
- Tests: migration (move, re-run, defaults, invalid files) and the install
  module's parts that don't need admin rights.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
paulandClaude Opus 5.5 committed 2026-12-05 19:51:42 +13:00
1 parent a7ebf42838
commit 8f91a38695
8 files changed
+492 -22

No files matched your search

+41 -17
View File
@@ -1,17 +1,21 @@
# Sets up The Kurgan:
# 1. installs the Python packages it needs (into the project's .venv if there is one)
# 1. installs the Python packages it needs into the project's .venv (creating it if needed)
# 2. installs PawnIO (signed driver used by LibreHardwareMonitor) for CPU sensors, top fans and RAM lighting
# 3. removes the old startup tasks ("TT LCD Dashboard", and "The Kurgan" from when it ran with admin rights)
# 4. with -RetireAIServices: stops and disables the "Strata Local Service" and "Qwen4 Local Service"
# tasks, so The Kurgan runs whichever LLM server you select (re-enable them with uninstall.ps1)
# 5. registers "The Kurgan" (hidden, at logon) as you, WITHOUT admin rights, so the tray's
# 5. installs or updates the "The Kurgan hardware" Windows service (KurganHw: fans, CPU sensors,
# RAM lighting; LocalSystem, starts at boot) and moves the fan and RAM lighting settings from
# your settings.json to %ProgramData%\Kurgan\hw-settings.json (once)
# 6. registers "The Kurgan" (hidden, at logon) as you, WITHOUT admin rights, so the tray's
# "Start with Windows" can change it, and starts it; the web UI is at http://127.0.0.1:8080/
#
# Run from a normal (not elevated) PowerShell window:
# powershell -ExecutionPolicy Bypass -File .\setup.ps1 [-RetireAIServices]
# It asks for administrator rights (UAC) itself for steps 2-4.
# It asks for administrator rights (UAC) itself for steps 2-5. Run it again after updating The Kurgan.
param([switch]$Elevated, [switch]$RetireAIServices)
param([switch]$Elevated, [switch]$RetireAIServices, [string]$Python, [string]$BasePython, [string]$UserSid,
[string]$UserSettings)
$ErrorActionPreference = 'Stop'
$TaskName = 'The Kurgan'
@@ -29,30 +33,39 @@ if ($isAdmin -and -not $Elevated) {
}
if (-not $isAdmin) {
# Prefer the project's own .venv; otherwise use whichever python is on PATH.
$venvPython = Join-Path $Here '.venv\Scripts\python.exe'
if (Test-Path $venvPython) {
$python = $venvPython
} else {
$python = (Get-Command python -ErrorAction Stop).Source
Set-Location $Here
# The project's own .venv: the hardware service runs from it too (hw_service.py adds its packages).
$python = Join-Path $Here '.venv\Scripts\python.exe'
if (-not (Test-Path $python)) {
Write-Host "Creating the project's .venv..." -ForegroundColor Cyan
& (Get-Command python -ErrorAction Stop).Source -m venv (Join-Path $Here '.venv')
if ($LASTEXITCODE -ne 0) { throw "creating .venv failed" }
}
$inVenv = (& $python -c "import sys; print(int(sys.prefix != sys.base_prefix))") -eq '1'
$pipArgs = if ($inVenv) { @() } else { @('--user') }
$pythonw = Join-Path (Split-Path $python) 'pythonw.exe'
if (-not (Test-Path $pythonw)) { throw "pythonw.exe not found next to $python" }
Write-Host "Installing Python packages into $(Split-Path (Split-Path $python))..." -ForegroundColor Cyan
& $python -m pip install @pipArgs --quiet --disable-pip-version-check -r (Join-Path $Here 'requirements.txt')
& $python -m pip install --quiet --disable-pip-version-check -r (Join-Path $Here 'requirements.txt')
if ($LASTEXITCODE -ne 0) { throw "pip install failed" }
Write-Host "Requesting administrator rights for PawnIO and the old startup tasks..." -ForegroundColor Cyan
$argList = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Elevated"
# What the elevated part needs to know about you and this Python.
$basePython = (& $python -c "import sys; print(sys._base_executable)").Trim() # the service runs this one
$userSettings = (& $python -c "from kurgan import paths; print(paths.SETTINGS_PATH)").Trim()
$userSid = [Security.Principal.WindowsIdentity]::GetCurrent().User.Value
Write-Host "Requesting administrator rights for PawnIO, the old startup tasks and the hardware service..." -ForegroundColor Cyan
$argList = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Elevated -Python `"$python`" " +
"-BasePython `"$basePython`" -UserSid $userSid -UserSettings `"$userSettings`""
if ($RetireAIServices) { $argList += ' -RetireAIServices' }
$p = Start-Process powershell -Verb RunAs -ArgumentList $argList -Wait -PassThru
if ($p.ExitCode -ne 0) { Write-Host "Setup stopped (the elevated part failed)." -ForegroundColor Red; exit $p.ExitCode }
# --- The Kurgan's startup task: as you, without admin rights --------------------------
try {
# --- the fan and RAM settings now live in hw-settings.json: tidy settings.json ------
& $python -m kurgan.hw.migrate strip
if ($LASTEXITCODE -ne 0) { throw "tidying settings.json failed (see above)" }
# --- The Kurgan's startup task: as you, without admin rights ----------------------
$user = [Security.Principal.WindowsIdentity]::GetCurrent().Name
$action = New-ScheduledTaskAction -Execute $pythonw -Argument '-m kurgan' -WorkingDirectory $Here
$trigger = New-ScheduledTaskTrigger -AtLogOn -User $user
@@ -65,9 +78,10 @@ if (-not $isAdmin) {
-Force | Out-Null
Start-ScheduledTask -TaskName $TaskName
} catch {
Write-Host "Registering the startup task failed: $_" -ForegroundColor Red
Write-Host "Setup failed: $_" -ForegroundColor Red
exit 1
}
& $python -m kurgan.hw.client # what the hardware service says
Write-Host "Done. '$TaskName' is running and will start at every logon." -ForegroundColor Green
Write-Host "Web UI: http://127.0.0.1:8080/ Log: $env:LOCALAPPDATA\Kurgan\kurgan.log"
if (-not $RetireAIServices) {
@@ -121,6 +135,16 @@ try {
($_.Name -eq 'llama-server.exe' -and $_.CommandLine -like '*qwen4-35b-a3b\service\presets.ini*') }
}
# --- the hardware service ------------------------------------------------------------
# Checks that the base interpreter can run it, sets up %ProgramData%\Kurgan (admins write, users
# read), moves the fan and RAM settings there once, then installs or updates and starts KurganHw.
if (-not ($Python -and $BasePython -and $UserSid -and $UserSettings)) {
throw "run setup.ps1 from a normal PowerShell window; it passes these on to the elevated part"
}
Set-Location $Here
& $Python -m kurgan.hw.install --python $BasePython --user-sid $UserSid --user-settings $UserSettings
if ($LASTEXITCODE -ne 0) { throw "installing the hardware service failed (see above)" }
} catch {
Write-Host "Setup failed: $_" -ForegroundColor Red
Read-Host "Press Enter to close"