2026-06-11 16:41:21 +01:00
param (
[ string ] $SshTarget = " nightwing " ,
[ string ] $MuninnExe = " /home/metacrat/.local/bin/muninn " ,
[ string ] $StorePath = " /home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc " ,
[ string ] $LogRoot = " /home/metacrat/.local/state/gamecult/muninn " ,
2026-06-14 15:18:08 +01:00
[ string[] ] $MoveState = @ ( ) ,
2026-07-12 19:41:21 +01:00
[ switch ] $DiscoverMoveState = $true ,
[ switch ] $ClaimUsbMoves = $true ,
2026-06-14 14:07:01 +01:00
[ string ] $MoveEvidenceStream = " muninn:nightwing:move-evidence " ,
2026-07-13 00:22:23 +01:00
[ string[] ] $MoveMarkerCamera = @ (
" nightwing-eye-0=/dev/video2 " ,
" nightwing-eye-1=/dev/video3 "
) ,
2026-07-12 21:11:20 +01:00
[ int ] $MoveTrackerExposureMilli = 100 ,
2026-07-13 01:23:11 +01:00
[ hashtable ] $MoveTrackerCameraExposure = @ {
" nightwing-eye-0 " = 300
" nightwing-eye-1 " = 100
} ,
2026-06-16 17:02:50 +01:00
[ int ] $IntervalSeconds = 15 ,
2026-07-04 22:38:43 +01:00
[ string ] $IdunnRudpHealth = $env:IDUNN_RUDP_HEALTH ,
2026-06-16 17:02:50 +01:00
[ string ] $IdunnDaemon = " nightwing-muninn " ,
2026-06-23 22:45:39 +01:00
[ string ] $IdunnHealthContract = " muninn.cultnet-rudp-remote-telemetry-and-move-hid " ,
2026-07-04 22:38:43 +01:00
[ string ] $OdinCultMeshUri = $ ( if ( $env:ODIN_CULTMESH_URI ) { $env:ODIN_CULTMESH_URI } else { " cultmesh://odin/rendezvous/provider-catalog " } ) ,
2026-07-12 19:49:36 +01:00
[ string ] $OdinCultMeshRudpEndpoint = $ ( if ( $env:CULTMESH_URI_ODIN_RUDP ) { $env:CULTMESH_URI_ODIN_RUDP } else { " 10.77.0.2:17871 " } ) ,
2026-07-04 22:38:43 +01:00
[ string ] $HidControllerRudpBind = " 0.0.0.0:17888 " ,
2026-07-12 22:54:41 +01:00
[ string ] $HidControllerRudpAdvertise = $ ( if ( $env:MUNINN_HID_CONTROLLER_RUDP_ADVERTISE ) { $env:MUNINN_HID_CONTROLLER_RUDP_ADVERTISE } else { " 10.77.0.3:17888 " } ) ,
[ string ] $CommandRudpBind = " 0.0.0.0:17889 " ,
2026-07-13 05:34:11 +01:00
[ string ] $CommandRudpAdvertise = " 10.77.0.3:17889 "
2026-06-11 16:41:21 +01:00
)
$ErrorActionPreference = " Stop "
2026-07-04 22:38:43 +01:00
if ( $env:IDUNN_ACTUATOR -ne " 1 " -or $env:IDUNN_COMMAND_AUTHORITY -ne " idunn-daemon " ) {
throw " restart-nightwing-muninn.ps1 is an Idunn actuator body. Redeploy by poking Idunn; direct service restart is not an owned path. "
}
if ( [ string ] :: IsNullOrWhiteSpace ( $IdunnRudpHealth ) ) {
throw " Idunn RUDP health endpoint must be supplied by -IdunnRudpHealth or IDUNN_RUDP_HEALTH; no Starfire LAN default is allowed. "
}
2026-07-12 19:41:21 +01:00
if ( $IdunnRudpHealth -match '^(127\.0\.0\.1|localhost):(\d+)$' ) {
$IdunnRudpHealth = " 10.77.0.2: $( $Matches [ 2 ] ) "
}
2026-07-04 22:38:43 +01:00
if ( -not [ string ] :: IsNullOrWhiteSpace ( $HidControllerRudpBind ) -and [ string ] :: IsNullOrWhiteSpace ( $HidControllerRudpAdvertise ) ) {
throw " HID controller RUDP advertise endpoint must be supplied by -HidControllerRudpAdvertise or MUNINN_HID_CONTROLLER_RUDP_ADVERTISE when HID RUDP bind is enabled; no Nightwing LAN default is allowed. "
}
2026-06-14 15:18:08 +01:00
function Quote-ShSingle([string ] $Value ) {
return " ' " + ( $Value -replace " ' " , " '\'' " ) + " ' "
}
2026-06-17 11:13:23 +01:00
function Set-AsciiFile {
param (
[ Parameter ( Mandatory = $true ) ] [ string ] $Path ,
[ Parameter ( Mandatory = $true ) ] [ string ] $Content
)
[ System.IO.File ] :: WriteAllText ( $Path , ( $Content -replace " `r ? `n " , " `n " ) , [ System.Text.Encoding ] :: ASCII )
}
function Invoke-UploadedShell {
param (
[ Parameter ( Mandatory = $true ) ] [ string ] $SshTarget ,
[ Parameter ( Mandatory = $true ) ] [ string ] $RemoteScriptContent
)
$uploadId = [ guid ] :: NewGuid ( ) . ToString ( " N " )
$localTempRoot = Join-Path $env:TEMP " odin-nightwing-muninn-restart- $uploadId "
$localRemoteScript = Join-Path $localTempRoot " restart-nightwing-muninn- $uploadId .sh "
$localSftpBatch = Join-Path $localTempRoot " restart-nightwing-muninn- $uploadId .sftp "
$remoteScriptPath = " /tmp/restart-nightwing-muninn- $uploadId .sh "
try {
New-Item -ItemType Directory -Force -Path $localTempRoot | Out-Null
Set-AsciiFile -Path $localRemoteScript -Content $RemoteScriptContent
Set-AsciiFile -Path $localSftpBatch -Content ( 'put "{0}" "{1}"' -f $localRemoteScript , $remoteScriptPath )
& sftp . exe -b $localSftpBatch $SshTarget
if ( $LASTEXITCODE -ne 0 ) {
exit $LASTEXITCODE
}
& ssh . exe -o BatchMode = yes -o ConnectTimeout = 10 $SshTarget " chmod +x ' $remoteScriptPath ' && bash ' $remoteScriptPath ' "
$remoteExit = $LASTEXITCODE
& ssh . exe -o BatchMode = yes -o ConnectTimeout = 10 $SshTarget " rm -f ' $remoteScriptPath ' "
exit $remoteExit
} finally {
Remove-Item -LiteralPath $localTempRoot -Recurse -Force -ErrorAction SilentlyContinue
}
}
2026-06-14 15:18:08 +01:00
$moveStateSpecs = @ ( $MoveState | Where-Object { -not [ string ] :: IsNullOrWhiteSpace ( $_ ) } )
2026-06-23 17:04:07 +01:00
if ( $ClaimUsbMoves . IsPresent -and -not $DiscoverMoveState . IsPresent -and $moveStateSpecs . Count -eq 0 ) {
throw " -ClaimUsbMoves requires -DiscoverMoveState or at least one explicit -MoveState value. "
}
if ( $DiscoverMoveState . IsPresent ) {
2026-06-14 21:14:40 +01:00
$claimScript = Join-Path $PSScriptRoot " claim-nightwing-usb-moves.ps1 "
2026-06-23 17:04:07 +01:00
if ( $ClaimUsbMoves . IsPresent ) {
& powershell . exe -NoProfile -ExecutionPolicy Bypass -File $claimScript -SshTarget $SshTarget
2026-06-17 11:27:33 +01:00
}
2026-06-14 15:18:08 +01:00
$sourceScript = Join-Path $PSScriptRoot " get-nightwing-move-state-sources.ps1 "
$moveStateSpecs = @ ( & powershell . exe -NoProfile -ExecutionPolicy Bypass -File $sourceScript -SshTarget $SshTarget )
2026-06-23 17:04:07 +01:00
if ( $moveStateSpecs . Count -eq 0 ) {
throw " Nightwing Move runtime was explicitly requested, but no live Move state sources were discovered. "
2026-06-17 11:27:33 +01:00
}
2026-06-14 15:18:08 +01:00
}
$moveStateSetLines = ( $moveStateSpecs | ForEach-Object {
" set -- "" `$ @ "" --move-state $( Quote-ShSingle $_ ) "
} ) -join " `n "
2026-06-23 17:04:07 +01:00
$moveRuntimeSetLines = @ ( )
if ( $moveStateSpecs . Count -gt 0 ) {
$moveRuntimeSetLines + = $moveStateSetLines
$moveRuntimeSetLines + = " set -- "" `$ @ "" --move-evidence-stream $( Quote-ShSingle $MoveEvidenceStream ) "
2026-07-12 21:34:04 +01:00
if ( $MoveMarkerCamera . Count -gt 0 ) {
foreach ( $camera in $MoveMarkerCamera ) {
$moveRuntimeSetLines + = " set -- "" `$ @ "" --move-marker-camera $( Quote-ShSingle $camera ) "
}
$moveRuntimeSetLines + = " set -- "" `$ @ "" --move-psmoveapi-tracker "
$moveRuntimeSetLines + = " set -- "" `$ @ "" --move-tracker-exposure-milli $( Quote-ShSingle ( [ string ] $MoveTrackerExposureMilli ) ) "
2026-07-13 01:23:11 +01:00
foreach ( $cameraId in @ ( $MoveTrackerCameraExposure . Keys | Sort-Object ) ) {
$moveRuntimeSetLines + = " set -- "" `$ @ "" --move-tracker-camera-exposure $( Quote-ShSingle ( " {0}={1} " -f $cameraId , $MoveTrackerCameraExposure [ $cameraId ] ) ) "
}
2026-07-12 21:11:20 +01:00
}
2026-06-23 17:04:07 +01:00
}
$moveRuntimeSetBlock = ( $moveRuntimeSetLines -join " `n " )
2026-07-04 22:38:43 +01:00
$odinCultMeshUriSetBlock = " "
if ( -not [ string ] :: IsNullOrWhiteSpace ( $OdinCultMeshUri ) ) {
$odinCultMeshUriSetBlock = " set -- "" `$ @ "" --odin-cultmesh-uri $( Quote-ShSingle $OdinCultMeshUri ) "
}
$hidControllerRudpSetLines = @ ( )
if ( -not [ string ] :: IsNullOrWhiteSpace ( $HidControllerRudpBind ) ) {
$hidControllerRudpSetLines + = " set -- "" `$ @ "" --hid-controller-rudp-bind $( Quote-ShSingle $HidControllerRudpBind ) "
}
if ( -not [ string ] :: IsNullOrWhiteSpace ( $HidControllerRudpAdvertise ) ) {
$hidControllerRudpSetLines + = " set -- "" `$ @ "" --hid-controller-rudp-advertise $( Quote-ShSingle $HidControllerRudpAdvertise ) "
2026-06-23 22:45:39 +01:00
}
2026-07-04 22:38:43 +01:00
$hidControllerRudpSetBlock = ( $hidControllerRudpSetLines -join " `n " )
2026-07-12 22:54:41 +01:00
$commandRudpSetBlock = " set -- "" `$ @ "" --command-rudp-bind $( Quote-ShSingle $CommandRudpBind ) --command-rudp-advertise $( Quote-ShSingle $CommandRudpAdvertise ) "
2026-06-11 16:41:21 +01:00
$remoteScript = @"
set -eu
mkdir -p '$LogRoot'
if [ ! -x '$MuninnExe' ] ; then
echo 'Muninn executable not found at $MuninnExe' > & 2
exit 1
fi
if [ -f '$LogRoot/muninn.pid' ] & & kill -0 " `$ (cat ' $LogRoot /muninn.pid') " 2 > / dev / null ; then
kill " `$ (cat ' $LogRoot /muninn.pid') " 2 > / dev / null | | true
sleep 1
fi
2026-07-13 01:14:47 +01:00
for pid in `$ ( pgrep -f '[m]uninn move-tracker-worker .*--host nightwing' 2 > / dev / null | | true ) ; do
kill " `$ pid " 2 > / dev / null | | true
done
2026-06-11 16:41:21 +01:00
for pid in `$ ( pgrep -f '[m]uninn serve .*--host nightwing' 2 > / dev / null | | true ) ; do
kill " `$ pid " 2 > / dev / null | | true
done
2026-07-12 23:56:39 +01:00
for pid in `$ ( pgrep -f '[m]uninn serve .*--host nightwing-eye0' 2 > / dev / null | | true ) ; do
kill " `$ pid " 2 > / dev / null | | true
done
2026-06-14 15:18:08 +01:00
set - - serve \
2026-06-11 16:41:21 +01:00
- -store '$StorePath' \
- -log -root '$LogRoot' \
2026-06-14 15:18:08 +01:00
- -host nightwing
2026-06-23 17:04:07 +01:00
$moveRuntimeSetBlock
2026-06-14 15:18:08 +01:00
set - - " `$ @ " \
2026-06-16 17:02:50 +01:00
- -interval -seconds '$IntervalSeconds' \
- -idunn -rudp -health '$IdunnRudpHealth' \
- -idunn -daemon '$IdunnDaemon' \
- -idunn -health -contract '$IdunnHealthContract'
2026-07-04 22:38:43 +01:00
$odinCultMeshUriSetBlock
$hidControllerRudpSetBlock
2026-07-12 22:54:41 +01:00
$commandRudpSetBlock
2026-07-12 23:22:43 +01:00
CULTNET_RUDP_TRACE = 1 CULTMESH_URI_ODIN_RUDP = '$OdinCultMeshRudpEndpoint' nohup '$MuninnExe' " `$ @ " \
2026-06-11 16:41:21 +01:00
> '$LogRoot/muninn-serve.out.log' \
2 > '$LogRoot/muninn-serve.err.log' \
< / dev / null &
echo `$ ! > '$LogRoot/muninn.pid'
sleep 1
2026-07-12 19:41:21 +01:00
muninn_pid = " `$ (cat ' $LogRoot /muninn.pid') "
kill -0 " `$ muninn_pid " 2 > / dev / null
muninn_cmdline = " `$ (tr '\0' ' ' < " / proc / `$ muninn_pid / cmdline " ) "
if ! printf '%s\n' " `$ muninn_cmdline " | grep -F - - '--host nightwing' > / dev / null 2 > & 1 | |
2026-07-13 00:22:23 +01:00
! printf '%s\n' " `$ muninn_cmdline " | grep -F - - '--idunn-rudp-health $IdunnRudpHealth' > / dev / null 2 > & 1 | |
! printf '%s\n' " `$ muninn_cmdline " | grep -F - - '--move-marker-camera nightwing-eye-0=/dev/video2' > / dev / null 2 > & 1 | |
! printf '%s\n' " `$ muninn_cmdline " | grep -F - - '--move-marker-camera nightwing-eye-1=/dev/video3' > / dev / null 2 > & 1 ; then
echo 'Nightwing Muninn serve command line is missing Idunn health or dual-camera arguments' > & 2
2026-06-16 17:02:50 +01:00
exit 1
fi
2026-06-11 16:41:21 +01:00
" @
2026-06-17 11:13:23 +01:00
Invoke-UploadedShell -SshTarget $SshTarget -RemoteScriptContent $remoteScript