more linux via wsl compat

This commit is contained in:
lda
2026-04-09 01:05:54 +07:00 Unverified
parent ba408af95c
commit fc2243ed8f
3 changed files with 199 additions and 16 deletions
+12 -6
View File
@@ -5,6 +5,7 @@ param(
[string]$Cargo = "cargo", [string]$Cargo = "cargo",
[string]$Pass, [string]$Pass,
[string]$HostName = "192.168.100.1", [string]$HostName = "192.168.100.1",
[int]$Port = 2222,
[string]$User = "root", [string]$User = "root",
[string]$RemotePath = "/root/.bin/wakey", [string]$RemotePath = "/root/.bin/wakey",
[string]$AgentRemotePath = "/root/.bin/wakey-agent", [string]$AgentRemotePath = "/root/.bin/wakey-agent",
@@ -23,6 +24,11 @@ try { $PSStyle.OutputRendering = 'Host' } catch {}
$Pass = Get-DefaultPassword $Pass $Pass = Get-DefaultPassword $Pass
# Backward compatibility: allow HostName in the form host:port.
$hostPort = Split-HostPort -Host $HostName -DefaultPort $Port
$HostName = $hostPort.Host
$Port = $hostPort.Port
function Get-DeployScript { function Get-DeployScript {
param($DeployPreferred, $DeployTmp, $RemoteTmp, $RemotePath, $RestartFlag) param($DeployPreferred, $DeployTmp, $RemoteTmp, $RemotePath, $RestartFlag)
return @" return @"
@@ -59,8 +65,8 @@ try {
$deployPreferred = '/root/.bin/remote_deploy_wakey.sh' $deployPreferred = '/root/.bin/remote_deploy_wakey.sh'
# Push binaries # Push binaries
Invoke-Scp -Local $localBin -Dest $destTmp -Pass $Pass -HostKey $HostKey -Quiet:$Quiet Invoke-Scp -Local $localBin -Dest $destTmp -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet
Invoke-Scp -Local $localAgentBin -Dest $agentDestTmp -Pass $Pass -HostKey $HostKey -Quiet:$Quiet Invoke-Scp -Local $localAgentBin -Dest $agentDestTmp -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet
# Push static assets # Push static assets
$localStatic = Join-Path $repoRoot "static" $localStatic = Join-Path $repoRoot "static"
@@ -69,16 +75,16 @@ try {
# So we push 'static' directory to /root/.bin/ # So we push 'static' directory to /root/.bin/
$remoteDir = (Split-Path $RemotePath -Parent) -replace '\\', '/' $remoteDir = (Split-Path $RemotePath -Parent) -replace '\\', '/'
# Ensure remote dir exists (ssh mkdir -p) # Ensure remote dir exists (ssh mkdir -p)
Invoke-Ssh -Cmd "mkdir -p $remoteDir" -User $User -Remote $HostName -Pass $Pass -Quiet:$Quiet Invoke-Ssh -Cmd "mkdir -p $remoteDir" -User $User -Remote $HostName -Pass $Pass -Port $Port -Quiet:$Quiet
# SCP -r static user@host:/root/.bin/ # SCP -r static user@host:/root/.bin/
# Note: pscp/scp behavior: if dest is a dir, it copies the source dir INTO it. # Note: pscp/scp behavior: if dest is a dir, it copies the source dir INTO it.
Invoke-Scp -Local $localStatic -Dest "$User@${HostName}:$remoteDir/" -Pass $Pass -HostKey $HostKey -Quiet:$Quiet -Recurse Invoke-Scp -Local $localStatic -Dest "$User@${HostName}:$remoteDir/" -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet -Recurse
} }
# Push deploy helper if exists # Push deploy helper if exists
if (Test-Path $localDeploy) { if (Test-Path $localDeploy) {
Invoke-Scp -Local $localDeploy -Dest "$User@${HostName}:$deployTmp" -Pass $Pass -HostKey $HostKey -Quiet:$Quiet Invoke-Scp -Local $localDeploy -Dest "$User@${HostName}:$deployTmp" -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet
} }
# Build and run remote deploy command # Build and run remote deploy command
@@ -90,7 +96,7 @@ $(Get-DeployScript $deployPreferred $deployTmp $agentRemoteTmp $AgentRemotePath
$remoteCmd = "sh -lc '$($script -replace "`r",'')'" $remoteCmd = "sh -lc '$($script -replace "`r",'')'"
if ($Quiet) { $remoteCmd += " >/dev/null 2>&1" } if ($Quiet) { $remoteCmd += " >/dev/null 2>&1" }
Invoke-Ssh -Cmd $remoteCmd -User $User -Remote $HostName -Pass $Pass -Quiet:$Quiet Invoke-Ssh -Cmd $remoteCmd -User $User -Remote $HostName -Pass $Pass -Port $Port -Quiet:$Quiet
Write-Host "done ✔" -ForegroundColor Green Write-Host "done ✔" -ForegroundColor Green
} }
+164 -4
View File
@@ -1,5 +1,73 @@
# Shared functions for wakey scripts # Shared functions for wakey scripts
# Platform detection globals
$script:IsWsl = $null
$script:IsWindows = $null
$script:WarnedNoSshpass = $false
function Test-IsWindows {
if ($null -ne $script:IsWindows) {
return $script:IsWindows
}
$script:IsWindows = $false
try {
$osInfo = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
$script:IsWindows = $osInfo
}
catch {
# Fallback for older PowerShell versions
$script:IsWindows = $PSVersionTable.OS -like '*Windows*'
}
return $script:IsWindows
}
function Test-IsWsl {
if ($null -ne $script:IsWsl) {
return $script:IsWsl
}
$script:IsWsl = $false
# Check for WSL environment variables
if ($env:WSL_DISTRO_NAME -or $env:WSL_INTEROP) {
$script:IsWsl = $true
return $true
}
# Check for /proc/version containing "microsoft" or "wsl"
if ((Test-Path '/proc/version' -ErrorAction SilentlyContinue) -and
(Select-String -Path '/proc/version' -Pattern 'microsoft|wsl' -Quiet -ErrorAction SilentlyContinue)) {
$script:IsWsl = $true
return $true
}
return $false
}
function ConvertTo-WslPath {
param([string]$WindowsPath)
if ([string]::IsNullOrWhiteSpace($WindowsPath)) {
return $WindowsPath
}
# If already a POSIX path, return as-is
if ($WindowsPath -match '^/') {
return $WindowsPath
}
# Handle Windows path (e.g., C:\path\to\file -> /mnt/c/path/to/file)
if ($WindowsPath -match '^([A-Z]):(.*)$') {
$drive = $matches[1].ToLower()
$path = $matches[2] -replace '\\', '/'
return "/mnt/$drive$path"
}
return $WindowsPath
}
function Get-DefaultPassword { function Get-DefaultPassword {
param([string]$Password) param([string]$Password)
@@ -65,6 +133,45 @@ function Join-PosixPath {
return "$leftTrim/$rightTrim" return "$leftTrim/$rightTrim"
} }
function Split-HostPort {
param(
[string]$Host,
[int]$DefaultPort = 22
)
$result = [ordered]@{
Host = $Host
Port = $DefaultPort
}
if ([string]::IsNullOrWhiteSpace($Host)) {
return [PSCustomObject]$result
}
# IPv6 with brackets: [2001:db8::1]:2222
if ($Host -match '^\[(.+)\]:(\d+)$') {
$result.Host = $matches[1]
$result.Port = [int]$matches[2]
return [PSCustomObject]$result
}
# host:port (single colon only, avoids plain IPv6 addresses)
if ($Host -match '^([^:]+):(\d+)$') {
$result.Host = $matches[1]
$result.Port = [int]$matches[2]
return [PSCustomObject]$result
}
return [PSCustomObject]$result
}
function Get-SshpassCommand {
if ($cmd = Get-Command sshpass -ErrorAction SilentlyContinue) {
return $cmd.Path
}
return $null
}
function Invoke-Ext { function Invoke-Ext {
param($Exe, $Arguments, $Label) param($Exe, $Arguments, $Label)
$displayArgs = $Arguments.Clone() $displayArgs = $Arguments.Clone()
@@ -72,6 +179,9 @@ function Invoke-Ext {
if ($displayArgs[$i] -eq '-pw' -and ($i + 1) -lt $displayArgs.Count) { if ($displayArgs[$i] -eq '-pw' -and ($i + 1) -lt $displayArgs.Count) {
$displayArgs[$i + 1] = '****' $displayArgs[$i + 1] = '****'
} }
if ($displayArgs[$i] -eq '-p' -and ($i + 1) -lt $displayArgs.Count -and $Exe -like '*sshpass*') {
$displayArgs[$i + 1] = '****'
}
} }
Write-Host ("[{0}] {1} {2}" -f $Label, $Exe, ($displayArgs -join ' ')) -ForegroundColor Cyan Write-Host ("[{0}] {1} {2}" -f $Label, $Exe, ($displayArgs -join ' ')) -ForegroundColor Cyan
@@ -84,11 +194,17 @@ function Invoke-Ext {
} }
function Invoke-Scp { function Invoke-Scp {
param($Local, $Dest, $Pass, $HostKey, [switch]$Quiet, [switch]$Recurse) param($Local, $Dest, $Pass, $HostKey, [int]$Port = 22, [switch]$Quiet, [switch]$Recurse)
if ($pscp = Get-Command pscp.exe -ErrorAction SilentlyContinue) {
$isWindows = Test-IsWindows
$isWsl = Test-IsWsl
# Only use PuTTY on Windows (not in WSL)
if ($isWindows -and -not $isWsl -and ($pscp = Get-Command pscp.exe -ErrorAction SilentlyContinue)) {
$arguments = @('-scp') $arguments = @('-scp')
if ($Quiet) { $arguments += '-q' } if ($Quiet) { $arguments += '-q' }
if ($Recurse) { $arguments += '-r' } if ($Recurse) { $arguments += '-r' }
if ($Port -gt 0) { $arguments += @('-P', $Port) }
if ($HostKey) { $arguments += @('-batch', '-hostkey', $HostKey) } if ($HostKey) { $arguments += @('-batch', '-hostkey', $HostKey) }
if ($Pass) { $arguments += @('-pw', $Pass) } if ($Pass) { $arguments += @('-pw', $Pass) }
$arguments += @($Local) $arguments += @($Local)
@@ -96,20 +212,48 @@ function Invoke-Scp {
Invoke-Ext -Exe $pscp.Path -Arguments $arguments -Label 'scp' Invoke-Ext -Exe $pscp.Path -Arguments $arguments -Label 'scp'
} }
else { else {
# Convert Windows paths to WSL paths if needed
if ($isWsl) {
$Local = @($Local | ForEach-Object { ConvertTo-WslPath $_ })
$Dest = ConvertTo-WslPath $Dest
}
$arguments = @('-O') $arguments = @('-O')
if ($Quiet) { $arguments += '-q' } if ($Quiet) { $arguments += '-q' }
if ($Recurse) { $arguments += '-r' } if ($Recurse) { $arguments += '-r' }
if ($Port -gt 0) { $arguments += @('-P', $Port) }
$arguments += @($Local) $arguments += @($Local)
$arguments += $Dest $arguments += $Dest
if ($Pass) {
$sshpassExe = Get-SshpassCommand
if ($sshpassExe) {
$wrapped = @('-p', $Pass, 'scp') + $arguments
Invoke-Ext -Exe $sshpassExe -Arguments $wrapped -Label 'scp'
return
}
if (-not $script:WarnedNoSshpass) {
Write-Warning 'Password was provided but sshpass is not installed; falling back to plain scp (key/agent auth expected).'
$script:WarnedNoSshpass = $true
}
}
Invoke-Ext -Exe 'scp' -Arguments $arguments -Label 'scp' Invoke-Ext -Exe 'scp' -Arguments $arguments -Label 'scp'
} }
} }
function Invoke-Ssh { function Invoke-Ssh {
param($Cmd, $User, $Remote, $Pass, [switch]$Quiet) param($Cmd, $User, $Remote, $Pass, [int]$Port = 22, [switch]$Quiet)
$Cmd = Normalize-LineEndings $Cmd $Cmd = Normalize-LineEndings $Cmd
if ($plink = Get-Command plink.exe -ErrorAction SilentlyContinue) {
$isWindows = Test-IsWindows
$isWsl = Test-IsWsl
# Only use PuTTY on Windows (not in WSL)
if ($isWindows -and -not $isWsl -and ($plink = Get-Command plink.exe -ErrorAction SilentlyContinue)) {
$arguments = @('-batch', '-ssh') $arguments = @('-batch', '-ssh')
if ($Port -gt 0) { $arguments += @('-P', $Port) }
if ($Pass) { $arguments += @('-pw', $Pass) } if ($Pass) { $arguments += @('-pw', $Pass) }
$dest = if ($User) { "$User@$Remote" } else { $Remote } $dest = if ($User) { "$User@$Remote" } else { $Remote }
$arguments += $dest, $Cmd $arguments += $dest, $Cmd
@@ -118,8 +262,24 @@ function Invoke-Ssh {
else { else {
$arguments = @() $arguments = @()
if ($Quiet) { $arguments += '-q' } if ($Quiet) { $arguments += '-q' }
if ($Port -gt 0) { $arguments += @('-p', $Port) }
$dest = if ($User) { "$User@$Remote" } else { $Remote } $dest = if ($User) { "$User@$Remote" } else { $Remote }
$arguments += $dest, $Cmd $arguments += $dest, $Cmd
if ($Pass) {
$sshpassExe = Get-SshpassCommand
if ($sshpassExe) {
$wrapped = @('-p', $Pass, 'ssh') + $arguments
Invoke-Ext -Exe $sshpassExe -Arguments $wrapped -Label 'ssh'
return
}
if (-not $script:WarnedNoSshpass) {
Write-Warning 'Password was provided but sshpass is not installed; falling back to plain ssh (key/agent auth expected).'
$script:WarnedNoSshpass = $true
}
}
Invoke-Ext -Exe 'ssh' -Arguments $arguments -Label 'ssh' Invoke-Ext -Exe 'ssh' -Arguments $arguments -Label 'ssh'
} }
} }
+23 -6
View File
@@ -14,6 +14,7 @@ param(
[switch]$Verbose, [switch]$Verbose,
[string]$RemoteTestPath = "/tmp/tmp/wakey-test", [string]$RemoteTestPath = "/tmp/tmp/wakey-test",
[string]$RemoteHost = "[email protected]", [string]$RemoteHost = "[email protected]",
[int]$RemotePort = 2222,
[switch]$Ignored, [switch]$Ignored,
[switch]$IncludeIgnored, [switch]$IncludeIgnored,
[switch]$NoCapture, [switch]$NoCapture,
@@ -27,6 +28,11 @@ $ErrorActionPreference = "Stop"
$password = Get-DefaultPassword $password $password = Get-DefaultPassword $password
$sshRemote = $RemoteHost
$hostPort = Split-HostPort -Host $sshRemote -DefaultPort $RemotePort
$sshRemote = $hostPort.Host
$RemotePort = $hostPort.Port
function Get-WorkspacePackages { function Get-WorkspacePackages {
$metadata = cargo metadata --no-deps --format-version 1 | ConvertFrom-Json $metadata = cargo metadata --no-deps --format-version 1 | ConvertFrom-Json
$workspaceMembers = [System.Collections.Generic.HashSet[string]]::new() $workspaceMembers = [System.Collections.Generic.HashSet[string]]::new()
@@ -127,14 +133,15 @@ function Ensure-RemoteParentDir {
param( param(
[string]$RemoteDirPath, [string]$RemoteDirPath,
[string]$RemoteHost, [string]$RemoteHost,
[string]$Password [string]$Password,
[int]$Port
) )
$dir = Normalize-PosixPath $RemoteDirPath $dir = Normalize-PosixPath $RemoteDirPath
if ([string]::IsNullOrWhiteSpace($dir)) { if ([string]::IsNullOrWhiteSpace($dir)) {
return return
} }
Invoke-Ssh -Cmd ("mkdir -p " + (Quote-ShArg $dir)) -Remote $RemoteHost -Pass $Password -Quiet Invoke-Ssh -Cmd ("mkdir -p " + (Quote-ShArg $dir)) -Remote $RemoteHost -Pass $Password -Port $Port -Quiet
} }
$packages = if ($AllPackages) { Get-WorkspacePackages } else { @($Package) } $packages = if ($AllPackages) { Get-WorkspacePackages } else { @($Package) }
@@ -184,11 +191,21 @@ foreach ($packageName in $packages) {
Write-Host "Found $($testBinaries.Count) test $($testBinaries.Count -eq 1 ? "binary" : "binaries") for $packageName" -ForegroundColor Green Write-Host "Found $($testBinaries.Count) test $($testBinaries.Count -eq 1 ? "binary" : "binaries") for $packageName" -ForegroundColor Green
$remoteRunDir = New-RemoteRunDir -BasePath $RemoteTestPath -PackageName $packageName $remoteRunDir = New-RemoteRunDir -BasePath $RemoteTestPath -PackageName $packageName
Ensure-RemoteParentDir -RemoteDirPath $remoteRunDir -RemoteHost $RemoteHost -Password $password Ensure-RemoteParentDir -RemoteDirPath $remoteRunDir -RemoteHost $sshRemote -Password $password -Port $RemotePort
try { try {
$remoteUploadDir = (Normalize-PosixPath $remoteRunDir).TrimEnd('/') + '/' $remoteUploadDir = (Normalize-PosixPath $remoteRunDir).TrimEnd('/') + '/'
Invoke-Scp -Local ($testBinaries | ForEach-Object { $_.FullName }) -Dest "${RemoteHost}:$remoteUploadDir" -Pass $password -Quiet
# Convert local file paths to WSL paths if running in WSL
$localFilePaths = @($testBinaries | ForEach-Object {
if (Test-IsWsl) {
ConvertTo-WslPath $_.FullName
} else {
$_.FullName
}
})
Invoke-Scp -Local $localFilePaths -Dest "${sshRemote}:$remoteUploadDir" -Pass $password -Port $RemotePort -Quiet
foreach ($testBinary in $testBinaries) { foreach ($testBinary in $testBinaries) {
Write-Host "`nTesting: $packageName / $($testBinary.Name)" -ForegroundColor Cyan Write-Host "`nTesting: $packageName / $($testBinary.Name)" -ForegroundColor Cyan
@@ -209,7 +226,7 @@ foreach ($packageName in $packages) {
# Run test binary (with chmod to ensure executable) # Run test binary (with chmod to ensure executable)
try { try {
Invoke-Ssh -Cmd $remoteCmd -Remote $RemoteHost -Pass $password Invoke-Ssh -Cmd $remoteCmd -Remote $sshRemote -Pass $password -Port $RemotePort
} }
catch { catch {
$failures.Add("$packageName / $($testBinary.Name)") $failures.Add("$packageName / $($testBinary.Name)")
@@ -221,7 +238,7 @@ foreach ($packageName in $packages) {
} }
finally { finally {
try { try {
Invoke-Ssh -Cmd ("rm -rf " + (Quote-ShArg $remoteRunDir)) -Remote $RemoteHost -Pass $password -Quiet Invoke-Ssh -Cmd ("rm -rf " + (Quote-ShArg $remoteRunDir)) -Remote $sshRemote -Pass $password -Port $RemotePort -Quiet
} }
catch { catch {
Write-Warning "Failed to remove remote test dir: $remoteRunDir" Write-Warning "Failed to remove remote test dir: $remoteRunDir"