fix: SSH key detection via batch file; add director port-forward

This commit is contained in:
Reditus committed 2026-05-14 05:10:50 +01:00
1 parent 6e7bb75f17
commit 49b38918ad
1 file changed
+68 -34
+68 -34
View File
@@ -6,7 +6,6 @@ $ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$SettingsFile = Join-Path $ProjectRoot "settings.yaml"
# Check if settings exist
if (-not (Test-Path $SettingsFile)) {
Write-Host "[WARN] settings.yaml not found. Running setup..." -ForegroundColor Yellow
& (Join-Path $ProjectRoot "setup.ps1")
@@ -16,7 +15,6 @@ if (-not (Test-Path $SettingsFile)) {
}
}
# Read settings via Python script (avoids YAML parsing issues in PowerShell)
$readSettingsScript = @"
import yaml, json, sys, os
path = sys.argv[1]
@@ -35,42 +33,68 @@ if (-not $settingsJson) {
}
$settings = $settingsJson | ConvertFrom-Json
$SSHHost = $settings.server.host
$ServerHost = $settings.server.host
$SSHUser = $settings.server.user
$LocalPort = [int]$settings.database.port
$Namespace = $settings.kubernetes.namespace
$DashboardPort = [int]$settings.dashboard.port
$DirectorPort = [int]$settings.director.port
# Find SSH key (prefer project-local for portability)
function Test-SshKey($keyPath, $targetServer) {
if (-not (Test-Path $keyPath)) { return $false }
$sshCmd = 'ssh -i "' + $keyPath + '" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 -o BatchMode=yes ' + $targetServer + ' "echo ok" 2>NUL'
$sshCmd | Out-File -FilePath "$env:TEMP\ssh_cmd.bat" -Encoding ascii
$out = cmd /c "$env:TEMP\ssh_cmd.bat"
return $out -eq "ok"
}
$SSHKeySrc = $settings.server.ssh_key
$LocalKey = Join-Path $ProjectRoot "internal-scripts\ssh\sshKey"
if (Test-Path $LocalKey) {
$SSHKeySrc = $LocalKey
} elseif (-not $SSHKeySrc -or $SSHKeySrc -eq 'null' -or [string]::IsNullOrEmpty($SSHKeySrc)) {
$ParentKey = Join-Path (Split-Path $ProjectRoot) "internal-scripts\ssh\sshKey"
if ($SSHKeySrc -and $SSHKeySrc -ne 'null' -and -not [string]::IsNullOrEmpty($SSHKeySrc)) {
if (Test-SshKey $SSHKeySrc ($SSHUser + '@' + $ServerHost)) {
Write-Host " Using key from settings.yaml" -ForegroundColor Green
} else {
Write-Host " Key in settings.yaml failed, searching..." -ForegroundColor Yellow
$SSHKeySrc = $null
}
}
if (-not $SSHKeySrc -or -not (Test-Path $SSHKeySrc) -or -not (Test-SshKey $SSHKeySrc ($SSHUser + '@' + $ServerHost))) {
$keyPaths = @(
$LocalKey,
(Join-Path (Split-Path $ProjectRoot) "internal-scripts\ssh\sshKey"),
$ParentKey,
"$env:LOCALAPPDATA\DuneAwakeningServer\sshKey",
"$env:USERPROFILE\.ssh\id_ed25519",
"$env:USERPROFILE\.ssh\id_rsa",
"$env:USERPROFILE\.ssh\id_ecdsa",
"$env:TEMP\dune-tunnel-key",
"$env:TEMP\dune-awakening-server-sshKey"
)
foreach ($kp in $keyPaths) {
if (Test-Path $kp) {
$SSHKeySrc = $kp
break
if ($kp -ne $SSHKeySrc -and (Test-Path $kp)) {
if (Test-SshKey $kp ($SSHUser + '@' + $ServerHost)) {
$SSHKeySrc = $kp
Write-Host " Found working key: $SSHKeySrc" -ForegroundColor Green
break
}
}
}
}
if (-not $SSHKeySrc -or -not (Test-Path $SSHKeySrc)) {
Write-Host "[ERROR] SSH key not found. Set ssh_key in settings.yaml or place key in:" -ForegroundColor Red
Write-Host " - $ProjectRoot\internal-scripts\ssh\sshKey" -ForegroundColor Yellow
Write-Host " - (parent)\internal-scripts\ssh\sshKey" -ForegroundColor Yellow
Write-Host " - %TEMP%\dune-tunnel-key" -ForegroundColor Yellow
Write-Host "[ERROR] No working SSH key found." -ForegroundColor Red
Write-Host " Place a valid key in: $ProjectRoot\internal-scripts\ssh\sshKey" -ForegroundColor Yellow
Write-Host " Or update ssh_key path in settings.yaml" -ForegroundColor Yellow
exit 1
}
# Copy key to temp with proper permissions
if ($SSHKeySrc -ne $LocalKey -and (Test-Path $LocalKey) -and (Get-Item $LocalKey).Length -eq 0) {
Copy-Item $SSHKeySrc $LocalKey -Force
Write-Host " Updated internal key file" -ForegroundColor Green
}
$SSHKey = "$env:TEMP\dune-tunnel-key"
$ResolvedSrc = [System.IO.Path]::GetFullPath($SSHKeySrc)
$ResolvedDest = [System.IO.Path]::GetFullPath($SSHKey)
@@ -93,7 +117,6 @@ Write-Host " Dune Awakening Dashboard" -ForegroundColor Cyan
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host ""
# Kill any existing SSH tunnels on the target port
Get-NetTCPConnection -LocalPort $LocalPort -ErrorAction SilentlyContinue | ForEach-Object {
Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue | Where-Object { $_.ProcessName -eq 'ssh' } | Stop-Process -Force -ErrorAction SilentlyContinue
}
@@ -106,8 +129,8 @@ $sshArgs = @(
"-o", "ServerAliveInterval=30",
"-o", "ServerAliveCountMax=3",
"-L", "${LocalPort}:localhost:${LocalPort}",
"-L", "${DirectorPort}:${SSHHost}:${DirectorPort}",
"-N", "${SSHUser}@${SSHHost}"
"-L", "${DirectorPort}:${ServerHost}:${DirectorPort}",
"-N", "${SSHUser}@${ServerHost}"
)
$sshTunnel = Start-Process ssh -ArgumentList $sshArgs -PassThru -WindowStyle Hidden
@@ -133,7 +156,6 @@ if (-not $connected) {
}
Write-Host "[OK] SSH tunnel up on localhost:$LocalPort" -ForegroundColor Green
# Start DB port-forward on VM
Write-Host "[2/4] Starting DB port-forward on VM..." -ForegroundColor Yellow
if (-not $Namespace -or $Namespace -eq '') {
@@ -145,8 +167,7 @@ if (-not $Namespace -or $Namespace -eq '') {
$DBService = "${Namespace}-db-dbdepl-svc"
# Try to find the actual DB service name
$pfCheck = ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 ${SSHUser}@${SSHHost} "sudo kubectl get svc -n ${Namespace} -o name" 2>$null
$pfCheck = ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 ${SSHUser}@${ServerHost} "sudo kubectl get svc -n ${Namespace} -o name" 2>$null
if ($pfCheck) {
$dbSvc = ($pfCheck -split "`n") | Where-Object { $_ -match 'db.*svc' -or $_ -match 'postgres' -or $_ -match 'pg' } | Select-Object -First 1
if ($dbSvc) {
@@ -156,16 +177,32 @@ if ($pfCheck) {
}
$RemotePort = 15432
$pfCmd = "ssh -i `"$SSHKey`" -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=30 ${SSHUser}@${SSHHost} `"nohup sudo kubectl port-forward -n ${Namespace} svc/${DBService} ${LocalPort}:${RemotePort} > /tmp/pf.log 2>&1 &`""
cmd /c $pfCmd
Start-Sleep -Seconds 5
$remoteCmd = 'nohup kubectl port-forward -n ' + $Namespace + ' svc/' + $DBService + ' ' + $LocalPort + ':' + $RemotePort + ' > /tmp/pf.log 2>&1 &'
$remoteCmdEscaped = $remoteCmd -replace '&', '`&'
$pfRemote = 'ssh -i "' + $SSHKey + '" -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=30 ' + $SSHUser + '@' + $ServerHost + ' "' + $remoteCmdEscaped + '"'
cmd /c $pfRemote
$bgdSvc = "${Namespace}-bgd-svc"
$pfCheckBgd = ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 ${SSHUser}@${ServerHost} "sudo kubectl get svc -n ${Namespace} -o name" 2>$null
if ($pfCheckBgd) {
$bgdMatch = ($pfCheckBgd -split "`n") | Where-Object { $_ -match 'bgd.*svc' } | Select-Object -First 1
if ($bgdMatch) { $bgdSvc = $bgdMatch -replace 'service/', '' }
}
Write-Host " Forwarding director port $DirectorPort -> ${ServerHost}:30822 (via bgd NodePort)..." -ForegroundColor Cyan
$directorRemotePort = 30822
$directorCmd = 'nohup kubectl port-forward -n ' + $Namespace + ' svc/' + $bgdSvc + ' ' + $DirectorPort + ':' + $directorRemotePort + ' > /tmp/director_pf.log 2>&1 &'
$directorCmdEscaped = $directorCmd -replace '&', '`&'
$directorPf = 'ssh -i "' + $SSHKey + '" -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=30 ' + $SSHUser + '@' + $ServerHost + ' "' + $directorCmdEscaped + '"'
cmd /c $directorPf
Start-Sleep -Seconds 3
# Check DB
Write-Host "[3/4] Checking database..." -ForegroundColor Yellow
$dbTest = $false
$dbScript = Join-Path $ProjectRoot "scripts\db_check.py"
for ($i = 0; $i -lt 15; $i++) {
try {
$result = python -W ignore -c "import psycopg2; c = psycopg2.connect(host='localhost', port=$LocalPort, user='postgres', password='postgres', dbname='dune', connect_timeout=3); c.close(); print('ok')" 2>$null
$result = python $dbScript $LocalPort 2>$null
if ($result -match 'ok') { $dbTest = $true; break }
} catch {}
Start-Sleep -Seconds 1
@@ -173,17 +210,16 @@ for ($i = 0; $i -lt 15; $i++) {
if (-not $dbTest) {
Write-Host "[ERROR] Database connection failed." -ForegroundColor Red
Write-Host "Checking port-forward log on VM..." -ForegroundColor Yellow
$pfLog = ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 ${SSHUser}@${SSHHost} "cat /tmp/pf.log" 2>$null
$pfLog = ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 ${SSHUser}@${ServerHost} "cat /tmp/pf.log" 2>$null
if ($pfLog) { Write-Host $pfLog -ForegroundColor Red }
else { Write-Host "Log empty or unreadable." -ForegroundColor Yellow }
Write-Host ""
Write-Host "Verify DB service exists:" -ForegroundColor Yellow
Write-Host " ssh -i $SSHKey ${SSHUser}@${SSHHost} 'sudo kubectl get svc -n $Namespace'" -ForegroundColor Cyan
Write-Host " ssh -i $SSHKey ${SSHUser}@${ServerHost} 'sudo kubectl get svc -n $Namespace'" -ForegroundColor Cyan
exit 1
}
Write-Host "[OK] Database connected" -ForegroundColor Green
# Check firewall rules
$fwRuleName = "DuneDashboard"
$fwExists = $null
try { $fwExists = Get-NetFirewallRule -DisplayName $fwRuleName -ErrorAction SilentlyContinue } catch {}
@@ -209,7 +245,6 @@ if (-not $fwExists) {
Write-Host ""
}
# Check dependencies
Write-Host "[4/4] Starting dashboard..." -ForegroundColor Yellow
$pipCheck = python -c "import flask_socketio, yaml, flask_login" 2>$null
if ($LASTEXITCODE -ne 0) {
@@ -220,8 +255,7 @@ if ($LASTEXITCODE -ne 0) {
Set-Location -LiteralPath $ProjectRoot
python run.py
# Cleanup
Write-Host "Stopping tunnels..." -ForegroundColor Cyan
Stop-Process -Id $sshTunnel.Id -Force -ErrorAction SilentlyContinue
$pkillCmd = "ssh -i `"$SSHKey`" -o StrictHostKeyChecking=accept-new ${SSHUser}@${SSHHost} `"pkill -f kubectl-port-forward`""
cmd /c $pkillCmd 2>$null
$pkillCmd = 'ssh -i "' + $SSHKey + '" -o StrictHostKeyChecking=accept-new ' + $SSHUser + '@' + $ServerHost + ' "pkill -f kubectl-port-forward"'
cmd /c $pkillCmd 2>$null