104 lines
3.3 KiB
PowerShell
104 lines
3.3 KiB
PowerShell
param(
|
|
[string]$ServerId,
|
|
[string]$Token,
|
|
[string]$ServerUrl,
|
|
[string]$InstallDir,
|
|
[switch]$Uninstall
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
$logDir = Join-Path $env:ProgramData "vantage"
|
|
New-Item -ItemType Directory -Force -Path $logDir | Out-Null
|
|
$log = Join-Path $logDir "install.log"
|
|
|
|
function Write-Log($msg) {
|
|
$line = "{0} {1}" -f (Get-Date -Format "s"), $msg
|
|
Add-Content -Path $log -Value $line
|
|
}
|
|
|
|
# Fail native-exe (nssm) calls loudly: check $LASTEXITCODE after each call
|
|
function Invoke-Native {
|
|
param([string]$File, [string[]]$Arguments)
|
|
Write-Log ("RUN: {0} {1}" -f $File, ($Arguments -join " "))
|
|
$out = & $File @Arguments 2>&1
|
|
if ($out) { Write-Log ("OUT: {0}" -f ($out -join "`n")) }
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw ("{0} exited {1}" -f $File, $LASTEXITCODE)
|
|
}
|
|
}
|
|
|
|
# Like Invoke-Native but never throws — for teardown, where a missing/stopped
|
|
# service must not abort the uninstall.
|
|
function Invoke-NativeSoft {
|
|
param([string]$File, [string[]]$Arguments)
|
|
Write-Log ("RUN(soft): {0} {1}" -f $File, ($Arguments -join " "))
|
|
$out = & $File @Arguments 2>&1
|
|
if ($out) { Write-Log ("OUT: {0}" -f ($out -join "`n")) }
|
|
Write-Log ("EXIT: {0}" -f $LASTEXITCODE)
|
|
}
|
|
|
|
if ($Uninstall) {
|
|
try {
|
|
Write-Log "=== teardown start ==="
|
|
if (-not $InstallDir) { $InstallDir = $PSScriptRoot }
|
|
$nssm = Join-Path $InstallDir "nssm.exe"
|
|
if (Test-Path $nssm) {
|
|
Invoke-NativeSoft -File $nssm -Arguments @("stop", "VantageAgent")
|
|
Invoke-NativeSoft -File $nssm -Arguments @("remove", "VantageAgent", "confirm")
|
|
} else {
|
|
Write-Log "nssm.exe not found at $nssm - using sc.exe fallback"
|
|
Invoke-NativeSoft -File "sc.exe" -Arguments @("stop", "VantageAgent")
|
|
Invoke-NativeSoft -File "sc.exe" -Arguments @("delete", "VantageAgent")
|
|
}
|
|
Write-Log "=== teardown ok ==="
|
|
exit 0
|
|
}
|
|
catch {
|
|
Write-Log ("TEARDOWN ERROR: {0}" -f $_.Exception.Message)
|
|
# Never block uninstall
|
|
exit 0
|
|
}
|
|
}
|
|
|
|
try {
|
|
Write-Log "=== setup start ==="
|
|
Write-Log ("ServerId={0} ServerUrl={1} InstallDir={2}" -f $ServerId, $ServerUrl, $InstallDir)
|
|
|
|
$cfgDir = Join-Path $env:ProgramData "vantage"
|
|
New-Item -ItemType Directory -Force -Path $cfgDir | Out-Null
|
|
|
|
$cfg = @"
|
|
server_url: "$ServerUrl"
|
|
server_id: "$ServerId"
|
|
pre_reg_token: "$Token"
|
|
agent_token: ""
|
|
poll_interval: 30s
|
|
tls: true
|
|
"@
|
|
$cfgPath = Join-Path $cfgDir "config.yaml"
|
|
Set-Content -Path $cfgPath -Value $cfg -Encoding utf8
|
|
Write-Log "wrote $cfgPath"
|
|
|
|
# Lock down ACL: SYSTEM + Administrators only
|
|
Invoke-Native -File "icacls" -Arguments @($cfgPath, "/inheritance:r", "/grant:r", "SYSTEM:F", "Administrators:F")
|
|
|
|
if (-not $InstallDir) { $InstallDir = $PSScriptRoot }
|
|
$nssm = Join-Path $InstallDir "nssm.exe"
|
|
$exe = Join-Path $InstallDir "vantage-agent.exe"
|
|
|
|
if (-not (Test-Path $nssm)) { throw "nssm.exe not found at $nssm" }
|
|
if (-not (Test-Path $exe)) { throw "vantage-agent.exe not found at $exe" }
|
|
|
|
Invoke-Native -File $nssm -Arguments @("install", "VantageAgent", $exe)
|
|
Invoke-Native -File $nssm -Arguments @("set", "VantageAgent", "Start", "SERVICE_AUTO_START")
|
|
Invoke-Native -File $nssm -Arguments @("start", "VantageAgent")
|
|
|
|
Write-Log "=== setup ok ==="
|
|
exit 0
|
|
}
|
|
catch {
|
|
Write-Log ("ERROR: {0}" -f $_.Exception.Message)
|
|
Write-Log ($_.ScriptStackTrace)
|
|
exit 1
|
|
}
|