fix: make setup.ps1 fully non-interactive for automated deployment

- Add -NonInteractive switch parameter
- Auto-accept detected BenjaminTeam folder in non-interactive mode
- Fix SSH test to use StrictHostKeyChecking=no and ConnectTimeout
- Add try/catch for better error handling during SSH test
This commit is contained in:
MangoPig
2026-02-13 12:03:46 +00:00
parent fea6996041
commit 0be5782d9e

View File

@@ -27,6 +27,10 @@
Sync interval in minutes for the scheduled task.
Default: 5
.PARAMETER NonInteractive
Run without prompts. Auto-accepts detected BenjaminTeam folder.
Use this when running from scripts or scheduled tasks.
.EXAMPLE
.\setup.ps1
@@ -39,7 +43,8 @@ param(
[string]$ServerUser = "rclone-sync",
[string]$ServerPath = "/data/jingtian/BenjaminTeam",
[string]$LocalPath = "",
[int]$SyncInterval = 5
[int]$SyncInterval = 5,
[switch]$NonInteractive
)
# Configuration
@@ -202,13 +207,22 @@ if ($LocalPath -and (Test-Path $LocalPath)) {
if ($foundPath) {
Write-Info "Found BenjaminTeam folder at: $foundPath"
if ($NonInteractive) {
# Auto-accept in non-interactive mode
$LocalPath = $foundPath
} else {
$confirm = Read-Host " Use this path? (Y/n)"
if ($confirm -eq "" -or $confirm -match "^[Yy]") {
$LocalPath = $foundPath
}
}
}
if (-not $LocalPath -or -not (Test-Path $LocalPath)) {
if ($NonInteractive) {
Write-ErrorMsg "Could not auto-detect BenjaminTeam folder. Use -LocalPath parameter."
exit 1
}
Write-Info "Could not auto-detect BenjaminTeam folder."
$LocalPath = Read-Host " Enter the full path to your BenjaminTeam folder"
@@ -249,16 +263,30 @@ if (-not (Test-Path $RcloneKeySource)) {
Copy-Item -Path $RcloneKeySource -Destination $RcloneKeyDest -Force
Write-Success "SSH key copied to $RcloneKeyDest"
# Test SSH connection
# Test SSH connection (fully non-interactive)
Write-Info "Testing SSH connection to $ServerHost..."
$sshTest = & ssh -o StrictHostKeyChecking=accept-new -o BatchMode=yes -i $RcloneKeyDest "$ServerUser@$ServerHost" "echo 'SSH_OK'" 2>&1
$sshArgs = @(
"-o", "StrictHostKeyChecking=no",
"-o", "BatchMode=yes",
"-o", "ConnectTimeout=10",
"-o", "UserKnownHostsFile=$env:USERPROFILE\.ssh\known_hosts",
"-i", $RcloneKeyDest,
"$ServerUser@$ServerHost",
"echo 'SSH_OK'"
)
if ($sshTest -match "SSH_OK") {
try {
$sshTest = & ssh @sshArgs 2>&1
if ($sshTest -match "SSH_OK") {
Write-Success "SSH connection successful"
} else {
} else {
Write-ErrorMsg "SSH connection failed: $sshTest"
Write-Info "Please check the server is running and the key is correct."
exit 1
}
} catch {
Write-ErrorMsg "SSH test error: $_"
exit 1
}
# ------------------------------------------------------------