enet-validator/scripts/session/deploy_set.ps1

40 lines
2.0 KiB
PowerShell
Raw Normal View History

[Console]::OutputEncoding = [Text.Encoding]::UTF8
$ErrorActionPreference = "Stop"
Set-ExecutionPolicy -Scope Process Bypass -Force
Import-Module Posh-SSH
$sec = ConvertTo-SecureString "0" -AsPlainText -Force
$cred = New-Object PSCredential("tonycao", $sec)
$s = New-SSHSession -ComputerName 192.168.58.7 -Credential $cred -AcceptKey -ConnectionTimeout 15
if (-not $s) { throw "SSH 连接失败" }
$files = @(
@("C:\Users\tonycao\work\light\enet-validator\src\tests.rs", "~/enet-validator/src/tests.rs"),
@("C:\Users\tonycao\work\light\enet-validator\src\enet.rs", "~/enet-validator/src/enet.rs"),
@("C:\Users\tonycao\work\light\enet-validator\samples\rdm_setack_68.hex", "~/enet-validator/samples/rdm_setack_68.hex")
)
foreach ($f in $files) {
$src = $f[0]; $dst = $f[1]
$b64 = [Convert]::ToBase64String([IO.File]::ReadAllBytes($src))
$chunkSize = 6000
$chunks = [Math]::Ceiling($b64.Length / $chunkSize)
$r = Invoke-SSHCommand -SSHSession $s -Command "rm -f /tmp/up.b64 && touch /tmp/up.b64"
for ($i = 0; $i -lt $chunks; $i++) {
$part = $b64.Substring($i * $chunkSize, [Math]::Min($chunkSize, $b64.Length - $i * $chunkSize))
$r = Invoke-SSHCommand -SSHSession $s -Command "echo '$part' >> /tmp/up.b64"
}
$r = Invoke-SSHCommand -SSHSession $s -Command "base64 -d /tmp/up.b64 > $dst && md5sum $dst"
$localMd5 = (Get-FileHash $src -Algorithm MD5).Hash.ToLower()
$remoteMd5 = ($r.Output -join "").Split(" ")[0]
$ok = if ($localMd5 -eq $remoteMd5) { "OK" } else { "MISMATCH!" }
Write-Host ("{0} -> {1}: {2} ({3})" -f (Split-Path $src -Leaf), $dst, $ok, $localMd5)
}
Write-Host "--- .7 编译 ---"
$r = Invoke-SSHCommand -SSHSession $s -Command "cd ~/enet-validator && ~/.cargo/bin/cargo build --release 2>&1 | tail -3" -TimeOut 300
Write-Host ($r.Output -join "`n")
Write-Host "--- 重启 butler ---"
$r = Invoke-SSHCommand -SSHSession $s -Command "bash ~/run_butler.sh device 2>&1 | tail -5; sleep 2; pgrep -af enet-validator | head -3"
Write-Host ($r.Output -join "`n")
Remove-SSHSession $s | Out-Null