what a script folder

This commit is contained in:
lda
2026-04-11 15:56:21 +07:00 Unverified
parent e2feaeab8c
commit a945d4933e
11 changed files with 79 additions and 52 deletions
+1 -1
View File
@@ -1 +1 @@
We work towards clean, maintainable design. im not the best at this thing, so you help me. We work towards clean, modular, maintainable design, clear documentation... im not the best at this thing, so you help me.
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
+7 -6
View File
@@ -6,7 +6,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, [int]$Port = 22,
[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",
@@ -14,6 +14,7 @@ param(
[string]$BinName = "wakey", [string]$BinName = "wakey",
[string]$AgentBinName = "wakey-agent", [string]$AgentBinName = "wakey-agent",
[string]$HostKey, [string]$HostKey,
[switch]$ForcePassword,
[switch]$Restart = $true, [switch]$Restart = $true,
[switch]$Quiet = $true [switch]$Quiet = $true
) )
@@ -26,7 +27,7 @@ try { $PSStyle.OutputRendering = 'Host' } catch {}
$Pass = Get-DefaultPassword $Pass $Pass = Get-DefaultPassword $Pass
# Backward compatibility: allow HostName in the form host:port. # Backward compatibility: allow HostName in the form host:port.
$hostPort = Split-HostPort -Host $HostName -DefaultPort $Port $hostPort = Split-HostPort -HostName $HostName -DefaultPort $Port
$HostName = $hostPort.Host $HostName = $hostPort.Host
$Port = $hostPort.Port $Port = $hostPort.Port
@@ -66,12 +67,12 @@ 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 -Port $Port -Quiet:$Quiet Invoke-Scp -Local $localBin -Dest $destTmp -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet -ForcePassword:$ForcePassword
Invoke-Scp -Local $localAgentBin -Dest $agentDestTmp -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet Invoke-Scp -Local $localAgentBin -Dest $agentDestTmp -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet -ForcePassword:$ForcePassword
# 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 -Port $Port -Quiet:$Quiet Invoke-Scp -Local $localDeploy -Dest "$User@${HostName}:$deployTmp" -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet -ForcePassword:$ForcePassword
} }
# Build and run remote deploy command # Build and run remote deploy command
@@ -83,7 +84,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 -Port $Port -Quiet:$Quiet Invoke-Ssh -Cmd $remoteCmd -User $User -Remote $HostName -Pass $Pass -Port $Port -Quiet:$Quiet -ForcePassword:$ForcePassword
Write-Host "done ✔" -ForegroundColor Green Write-Host "done ✔" -ForegroundColor Green
} }
Regular → Executable
View File
Regular → Executable
+53 -31
View File
@@ -2,27 +2,8 @@
# Platform detection globals # Platform detection globals
$script:IsWsl = $null $script:IsWsl = $null
$script:IsWindows = $null
$script:WarnedNoSshpass = $false $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 { function Test-IsWsl {
if ($null -ne $script:IsWsl) { if ($null -ne $script:IsWsl) {
return $script:IsWsl return $script:IsWsl
@@ -135,28 +116,28 @@ function Join-PosixPath {
function Split-HostPort { function Split-HostPort {
param( param(
[string]$Host, [string]$HostName,
[int]$DefaultPort = 22 [int]$DefaultPort = 22
) )
$result = [ordered]@{ $result = [ordered]@{
Host = $Host Host = $HostName
Port = $DefaultPort Port = $DefaultPort
} }
if ([string]::IsNullOrWhiteSpace($Host)) { if ([string]::IsNullOrWhiteSpace($HostName)) {
return [PSCustomObject]$result return [PSCustomObject]$result
} }
# IPv6 with brackets: [2001:db8::1]:2222 # IPv6 with brackets: [2001:db8::1]:2222
if ($Host -match '^\[(.+)\]:(\d+)$') { if ($HostName -match '^\[(.+)\]:(\d+)$') {
$result.Host = $matches[1] $result.Host = $matches[1]
$result.Port = [int]$matches[2] $result.Port = [int]$matches[2]
return [PSCustomObject]$result return [PSCustomObject]$result
} }
# host:port (single colon only, avoids plain IPv6 addresses) # host:port (single colon only, avoids plain IPv6 addresses)
if ($Host -match '^([^:]+):(\d+)$') { if ($HostName -match '^([^:]+):(\d+)$') {
$result.Host = $matches[1] $result.Host = $matches[1]
$result.Port = [int]$matches[2] $result.Port = [int]$matches[2]
return [PSCustomObject]$result return [PSCustomObject]$result
@@ -179,7 +160,7 @@ 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*') { if ($displayArgs[$i] -ceq '-p' -and ($i + 1) -lt $displayArgs.Count -and $Exe -like '*sshpass*') {
$displayArgs[$i + 1] = '****' $displayArgs[$i + 1] = '****'
} }
} }
@@ -187,6 +168,9 @@ function Invoke-Ext {
$out = & $Exe @Arguments 2>&1 $out = & $Exe @Arguments 2>&1
if ($LASTEXITCODE -ne 0) { if ($LASTEXITCODE -ne 0) {
if ($Exe -like '*sshpass*' -and $LASTEXITCODE -eq 6) {
Write-Error ("{0} failed: sshpass could not confirm host key automatically (code 6). Use ssh once manually, or enable StrictHostKeyChecking=accept-new." -f $Label)
}
Write-Error ("{0} exited with code {1}:`n{2}" -f $Label, $LASTEXITCODE, ($out -join "`n")) Write-Error ("{0} exited with code {1}:`n{2}" -f $Label, $LASTEXITCODE, ($out -join "`n"))
throw ("{0} exited with code {1}" -f $Label, $LASTEXITCODE) throw ("{0} exited with code {1}" -f $Label, $LASTEXITCODE)
} }
@@ -194,13 +178,13 @@ function Invoke-Ext {
} }
function Invoke-Scp { function Invoke-Scp {
param($Local, $Dest, $Pass, $HostKey, [int]$Port = 22, [switch]$Quiet, [switch]$Recurse) param($Local, $Dest, $Pass, $HostKey, [int]$Port = 22, [switch]$Quiet, [switch]$Recurse, [switch]$ForcePassword)
$isWindows = Test-IsWindows $isWin = [bool]$IsWindows
$isWsl = Test-IsWsl $isWsl = Test-IsWsl
# Only use PuTTY on Windows (not in WSL) # Only use PuTTY on Windows (not in WSL)
if ($isWindows -and -not $isWsl -and ($pscp = Get-Command pscp.exe -ErrorAction SilentlyContinue)) { if ($isWin -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' }
@@ -221,6 +205,15 @@ function Invoke-Scp {
$arguments = @('-O') $arguments = @('-O')
if ($Quiet) { $arguments += '-q' } if ($Quiet) { $arguments += '-q' }
if ($Recurse) { $arguments += '-r' } if ($Recurse) { $arguments += '-r' }
# Avoid interactive host-key prompts (important for sshpass usage)
$arguments += @('-o', 'StrictHostKeyChecking=accept-new')
if ($ForcePassword) {
$arguments += @('-o', 'PreferredAuthentications=keyboard-interactive,password')
$arguments += @('-o', 'PubkeyAuthentication=no')
$arguments += @('-o', 'KbdInteractiveAuthentication=yes')
$arguments += @('-o', 'PasswordAuthentication=yes')
$arguments += @('-o', 'NumberOfPasswordPrompts=1')
}
if ($Port -gt 0) { $arguments += @('-P', $Port) } if ($Port -gt 0) { $arguments += @('-P', $Port) }
$arguments += @($Local) $arguments += @($Local)
$arguments += $Dest $arguments += $Dest
@@ -238,20 +231,30 @@ function Invoke-Scp {
} }
} }
if ($ForcePassword -and -not $Pass) {
Write-Error 'ForcePassword requires Pass to be provided.'
throw 'ForcePassword requires Pass'
}
if ($ForcePassword -and $Pass -and -not (Get-SshpassCommand)) {
Write-Error 'ForcePassword was requested but sshpass is not installed. Install sshpass or disable ForcePassword.'
throw 'ForcePassword requires sshpass'
}
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, [int]$Port = 22, [switch]$Quiet) param($Cmd, $User, $Remote, $Pass, [int]$Port = 22, [switch]$Quiet, [switch]$ForcePassword)
$Cmd = Normalize-LineEndings $Cmd $Cmd = Normalize-LineEndings $Cmd
$isWindows = Test-IsWindows $isWin = [bool]$IsWindows
$isWsl = Test-IsWsl $isWsl = Test-IsWsl
# Only use PuTTY on Windows (not in WSL) # Only use PuTTY on Windows (not in WSL)
if ($isWindows -and -not $isWsl -and ($plink = Get-Command plink.exe -ErrorAction SilentlyContinue)) { if ($isWin -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 ($Port -gt 0) { $arguments += @('-P', $Port) }
if ($Pass) { $arguments += @('-pw', $Pass) } if ($Pass) { $arguments += @('-pw', $Pass) }
@@ -262,6 +265,15 @@ function Invoke-Ssh {
else { else {
$arguments = @() $arguments = @()
if ($Quiet) { $arguments += '-q' } if ($Quiet) { $arguments += '-q' }
# Avoid interactive host-key prompts (important for sshpass usage)
$arguments += @('-o', 'StrictHostKeyChecking=accept-new')
if ($ForcePassword) {
$arguments += @('-o', 'PreferredAuthentications=keyboard-interactive,password')
$arguments += @('-o', 'PubkeyAuthentication=no')
$arguments += @('-o', 'KbdInteractiveAuthentication=yes')
$arguments += @('-o', 'PasswordAuthentication=yes')
$arguments += @('-o', 'NumberOfPasswordPrompts=1')
}
if ($Port -gt 0) { $arguments += @('-p', $Port) } 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
@@ -279,6 +291,16 @@ function Invoke-Ssh {
} }
} }
if ($ForcePassword -and -not $Pass) {
Write-Error 'ForcePassword requires Pass to be provided.'
throw 'ForcePassword requires Pass'
}
if ($ForcePassword -and $Pass -and -not (Get-SshpassCommand)) {
Write-Error 'ForcePassword was requested but sshpass is not installed. Install sshpass or disable ForcePassword.'
throw 'ForcePassword requires sshpass'
}
Invoke-Ext -Exe 'ssh' -Arguments $arguments -Label 'ssh' Invoke-Ext -Exe 'ssh' -Arguments $arguments -Label 'ssh'
} }
} }
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
+13 -9
View File
@@ -15,6 +15,7 @@ param(
[string]$RemoteTestPath = "/tmp/tmp/wakey-test", [string]$RemoteTestPath = "/tmp/tmp/wakey-test",
[string]$RemoteHost = "[email protected]", [string]$RemoteHost = "[email protected]",
[int]$RemotePort = 2222, [int]$RemotePort = 2222,
[switch]$ForcePassword,
[switch]$Ignored, [switch]$Ignored,
[switch]$IncludeIgnored, [switch]$IncludeIgnored,
[switch]$NoCapture, [switch]$NoCapture,
@@ -29,7 +30,7 @@ $ErrorActionPreference = "Stop"
$password = Get-DefaultPassword $password $password = Get-DefaultPassword $password
$sshRemote = $RemoteHost $sshRemote = $RemoteHost
$hostPort = Split-HostPort -Host $sshRemote -DefaultPort $RemotePort $hostPort = Split-HostPort -HostName $sshRemote -DefaultPort $RemotePort
$sshRemote = $hostPort.Host $sshRemote = $hostPort.Host
$RemotePort = $hostPort.Port $RemotePort = $hostPort.Port
@@ -134,14 +135,15 @@ function Ensure-RemoteParentDir {
[string]$RemoteDirPath, [string]$RemoteDirPath,
[string]$RemoteHost, [string]$RemoteHost,
[string]$Password, [string]$Password,
[int]$Port [int]$Port,
[switch]$ForcePassword
) )
$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 -Port $Port -Quiet Invoke-Ssh -Cmd ("mkdir -p " + (Quote-ShArg $dir)) -Remote $RemoteHost -Pass $Password -Port $Port -Quiet -ForcePassword:$ForcePassword
} }
$packages = if ($AllPackages) { Get-WorkspacePackages } else { @($Package) } $packages = if ($AllPackages) { Get-WorkspacePackages } else { @($Package) }
@@ -181,7 +183,8 @@ foreach ($packageName in $packages) {
if ($testBinaries.Count -eq 0) { if ($testBinaries.Count -eq 0) {
$reason = if ($BinaryFilter) { $reason = if ($BinaryFilter) {
"no test executables matched binary filter '$BinaryFilter'" "no test executables matched binary filter '$BinaryFilter'"
} else { }
else {
"no test executables" "no test executables"
} }
Write-Host "Skipping ${packageName}: $reason" -ForegroundColor DarkYellow Write-Host "Skipping ${packageName}: $reason" -ForegroundColor DarkYellow
@@ -191,7 +194,7 @@ 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 $sshRemote -Password $password -Port $RemotePort Ensure-RemoteParentDir -RemoteDirPath $remoteRunDir -RemoteHost $sshRemote -Password $password -Port $RemotePort -ForcePassword:$ForcePassword
try { try {
$remoteUploadDir = (Normalize-PosixPath $remoteRunDir).TrimEnd('/') + '/' $remoteUploadDir = (Normalize-PosixPath $remoteRunDir).TrimEnd('/') + '/'
@@ -200,12 +203,13 @@ foreach ($packageName in $packages) {
$localFilePaths = @($testBinaries | ForEach-Object { $localFilePaths = @($testBinaries | ForEach-Object {
if (Test-IsWsl) { if (Test-IsWsl) {
ConvertTo-WslPath $_.FullName ConvertTo-WslPath $_.FullName
} else { }
else {
$_.FullName $_.FullName
} }
}) })
Invoke-Scp -Local $localFilePaths -Dest "${sshRemote}:$remoteUploadDir" -Pass $password -Port $RemotePort -Quiet Invoke-Scp -Local $localFilePaths -Dest "${sshRemote}:$remoteUploadDir" -Pass $password -Port $RemotePort -Quiet -ForcePassword:$ForcePassword
foreach ($testBinary in $testBinaries) { foreach ($testBinary in $testBinaries) {
Write-Host "`nTesting: $packageName / $($testBinary.Name)" -ForegroundColor Cyan Write-Host "`nTesting: $packageName / $($testBinary.Name)" -ForegroundColor Cyan
@@ -226,7 +230,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 $sshRemote -Pass $password -Port $RemotePort Invoke-Ssh -Cmd $remoteCmd -Remote $sshRemote -Pass $password -Port $RemotePort -ForcePassword:$ForcePassword
} }
catch { catch {
$failures.Add("$packageName / $($testBinary.Name)") $failures.Add("$packageName / $($testBinary.Name)")
@@ -238,7 +242,7 @@ foreach ($packageName in $packages) {
} }
finally { finally {
try { try {
Invoke-Ssh -Cmd ("rm -rf " + (Quote-ShArg $remoteRunDir)) -Remote $sshRemote -Pass $password -Port $RemotePort -Quiet Invoke-Ssh -Cmd ("rm -rf " + (Quote-ShArg $remoteRunDir)) -Remote $sshRemote -Pass $password -Port $RemotePort -Quiet -ForcePassword:$ForcePassword
} }
catch { catch {
Write-Warning "Failed to remove remote test dir: $remoteRunDir" Write-Warning "Failed to remove remote test dir: $remoteRunDir"