1
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
param(
|
||||
[string]$SshAlias = "test-docker",
|
||||
[switch]$InstallCron,
|
||||
[switch]$RunOnce,
|
||||
[int]$WarnPercent = 85,
|
||||
[int]$CleanupPercent = 85,
|
||||
[int]$CriticalPercent = 95,
|
||||
[int]$MinTmpAgeMinutes = 360,
|
||||
[string]$RemoteScriptPath = "/home/test/bin/rap-test-docker-disk-guard",
|
||||
[string]$StatusUrl = "http://docker-test.cin.su:18080/downloads/ops/test-docker-disk-guard-status.json"
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
$localScript = Join-Path $scriptDir "test-docker-disk-guard.sh"
|
||||
if (-not (Test-Path $localScript)) {
|
||||
throw "Guard script not found: $localScript"
|
||||
}
|
||||
|
||||
function Invoke-Remote {
|
||||
param([string]$Command)
|
||||
& ssh $SshAlias $Command
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "ssh command failed with exit code $LASTEXITCODE"
|
||||
}
|
||||
}
|
||||
|
||||
function Test-RemoteCommand {
|
||||
param([string]$CommandName)
|
||||
& ssh $SshAlias "command -v $CommandName >/dev/null 2>&1"
|
||||
return $LASTEXITCODE -eq 0
|
||||
}
|
||||
|
||||
$remoteUploadPath = "/home/test/.rap-test-docker-disk-guard.upload"
|
||||
|
||||
Write-Host "Uploading disk guard to ${SshAlias}:${RemoteScriptPath}"
|
||||
& scp $localScript "${SshAlias}:$remoteUploadPath"
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "scp failed with exit code $LASTEXITCODE"
|
||||
}
|
||||
|
||||
Invoke-Remote "mkdir -p `$(dirname $RemoteScriptPath) && install -m 0755 $remoteUploadPath $RemoteScriptPath"
|
||||
|
||||
$envPrefix = "WARN_PERCENT=$WarnPercent CLEANUP_PERCENT=$CleanupPercent CRITICAL_PERCENT=$CriticalPercent MIN_TMP_AGE_MINUTES=$MinTmpAgeMinutes"
|
||||
$runCommand = "$envPrefix $RemoteScriptPath"
|
||||
|
||||
if ($InstallCron) {
|
||||
if (Test-RemoteCommand "crontab") {
|
||||
$cronLine = "*/15 * * * * $runCommand >/tmp/rap-ops/test-docker-disk-guard.cron.log 2>&1"
|
||||
$escapedCronLine = $cronLine.Replace("'", "'\''")
|
||||
$installCronCommand = "(crontab -l 2>/dev/null | grep -v 'rap-test-docker-disk-guard'; printf '%s\n' '$escapedCronLine') | crontab -"
|
||||
Write-Host "Installing cron: $cronLine"
|
||||
Invoke-Remote $installCronCommand
|
||||
} else {
|
||||
Write-Host "crontab is not available; installing user systemd timer."
|
||||
$serviceContent = @"
|
||||
[Unit]
|
||||
Description=RAP test-docker disk guard
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
Environment=WARN_PERCENT=$WarnPercent
|
||||
Environment=CLEANUP_PERCENT=$CleanupPercent
|
||||
Environment=CRITICAL_PERCENT=$CriticalPercent
|
||||
Environment=MIN_TMP_AGE_MINUTES=$MinTmpAgeMinutes
|
||||
ExecStart=$RemoteScriptPath
|
||||
"@
|
||||
$timerContent = @"
|
||||
[Unit]
|
||||
Description=Run RAP test-docker disk guard every 15 minutes
|
||||
|
||||
[Timer]
|
||||
OnBootSec=2min
|
||||
OnUnitActiveSec=15min
|
||||
AccuracySec=1min
|
||||
Persistent=true
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
"@
|
||||
$tmpBase = Join-Path ([System.IO.Path]::GetTempPath()) ("rap-disk-guard-" + [System.Guid]::NewGuid().ToString("N"))
|
||||
New-Item -ItemType Directory -Path $tmpBase | Out-Null
|
||||
$servicePath = Join-Path $tmpBase "rap-test-docker-disk-guard.service"
|
||||
$timerPath = Join-Path $tmpBase "rap-test-docker-disk-guard.timer"
|
||||
Set-Content -Path $servicePath -Value $serviceContent -Encoding ascii
|
||||
Set-Content -Path $timerPath -Value $timerContent -Encoding ascii
|
||||
Invoke-Remote "mkdir -p /home/test/.config/systemd/user"
|
||||
& scp $servicePath "${SshAlias}:/home/test/.config/systemd/user/rap-test-docker-disk-guard.service"
|
||||
if ($LASTEXITCODE -ne 0) { throw "scp service failed with exit code $LASTEXITCODE" }
|
||||
& scp $timerPath "${SshAlias}:/home/test/.config/systemd/user/rap-test-docker-disk-guard.timer"
|
||||
if ($LASTEXITCODE -ne 0) { throw "scp timer failed with exit code $LASTEXITCODE" }
|
||||
Remove-Item -Recurse -Force -LiteralPath $tmpBase
|
||||
Invoke-Remote "systemctl --user daemon-reload && systemctl --user enable --now rap-test-docker-disk-guard.timer && systemctl --user start rap-test-docker-disk-guard.service"
|
||||
}
|
||||
}
|
||||
|
||||
if ($RunOnce -or -not $InstallCron) {
|
||||
Write-Host "Running disk guard once..."
|
||||
& ssh $SshAlias $runCommand
|
||||
$exitCode = $LASTEXITCODE
|
||||
if ($exitCode -ne 0 -and $exitCode -ne 2) {
|
||||
throw "disk guard failed with exit code $exitCode"
|
||||
}
|
||||
if ($exitCode -eq 2) {
|
||||
Write-Warning "Disk guard reports critical state after cleanup."
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Status: $StatusUrl"
|
||||
Reference in New Issue
Block a user