From ebff79dbeb98e1289677f00e2a0340ec789f167f Mon Sep 17 00:00:00 2001 From: Reditus Date: Tue, 12 May 2026 01:36:43 +0100 Subject: [PATCH] Auto-detect server IP from SSH known_hosts --- setup.ps1 | 13 +++++++++++++ setup.sh | 11 +++++++++++ start.ps1 | 10 +++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/setup.ps1 b/setup.ps1 index 5bbb5de..e19d9ee 100644 --- a/setup.ps1 +++ b/setup.ps1 @@ -161,6 +161,19 @@ $FileBrowserPort = "18888" $AuthUser = "admin" $AuthPass = "changeme" +# Try to detect server IP from known_hosts +$KnownHosts = Join-Path $env:USERPROFILE ".ssh\known_hosts" +if (Test-Path $KnownHosts) { + $hosts = Select-String -Path $KnownHosts -Pattern '^\d+\.\d+\.\d+\.\d+' | ForEach-Object { + if ($_ -match '^(\d+\.\d+\.\d+\.\d+)') { $matches[1] } + } | Select-Object -Unique + if ($hosts.Count -gt 0) { + $ServerHost = $hosts[-1] + Write-Host " Detected previous server IP: $ServerHost" -ForegroundColor Green + } +} + +# Test SSH if we have a key and a real IP if ($FoundKey -and $ServerHost -ne "YOUR_SERVER_IP") { # Test SSH $testOut = ssh -i $FoundKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o BatchMode=yes "dune@$ServerHost" "echo ok" 2>$null diff --git a/setup.sh b/setup.sh index 326e855..3b8a994 100644 --- a/setup.sh +++ b/setup.sh @@ -130,6 +130,17 @@ FILEBROWSER_PORT="18888" AUTH_USER="admin" AUTH_PASS="changeme" +# Try to detect server IP from known_hosts +KNOWN_HOSTS="$HOME/.ssh/known_hosts" +if [ -f "$KNOWN_HOSTS" ]; then + DETECTED_IP=$(grep -oE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' "$KNOWN_HOSTS" | tail -1) + if [ -n "$DETECTED_IP" ]; then + SERVER_HOST="$DETECTED_IP" + echo " Detected previous server IP: $SERVER_HOST" + fi +fi + +# Test SSH if we have a key and a real IP if [ -n "$FOUND_KEY" ] && [ "$SERVER_HOST" != "YOUR_SERVER_IP" ]; then if ssh -i "$FOUND_KEY" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o BatchMode=yes "dune@$SERVER_HOST" "echo ok" &>/dev/null; then echo " SSH connection OK" diff --git a/start.ps1 b/start.ps1 index 60ded86..1d1563e 100644 --- a/start.ps1 +++ b/start.ps1 @@ -133,9 +133,17 @@ 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 '') { + Write-Host "[ERROR] Kubernetes namespace is empty." -ForegroundColor Red + Write-Host " Edit settings.yaml and set kubernetes.namespace, or re-run setup.ps1 with SSH access." -ForegroundColor Yellow + Stop-Process -Id $sshTunnel.Id -Force -ErrorAction SilentlyContinue + exit 1 +} + $DBService = "${Namespace}-db-dbdepl-svc" -# Try to find the actual DB service name if the default fails +# 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 if ($pfCheck) { $dbSvc = ($pfCheck -split "`n") | Where-Object { $_ -match 'db.*svc' -or $_ -match 'postgres' -or $_ -match 'pg' } | Select-Object -First 1