8000 Implemented in-place upgrade and improved robustness by hkavula · Pull Request #3 · hetrixtools/agent-windows · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

8000 Implemented in-place upgrade and improved robustness #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 89 additions & 42 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,51 +45,40 @@ if ([System.Environment]::OSVersion.Version.Build -lt 17763 -and [Net.ServicePoi
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $commonSecurityProtocols
}

# Find and uninstall v1 agent
Write-Host "Checking for old agent..."
$processName = "HetrixToolsAgent.exe"
$processes = Get-WmiObject Win32_Process | Where-Object { $_.Name -eq $processName }
$paths = @()
if ($processes) {
foreach ($process in $processes) {
$processPath = $process.ExecutablePath
if ($processPath) {
Write-Host "Found process $($process.ProcessId) running from path $processPath"
$paths += $processPath.Trim()
} else {
Write-Host "Unable to retrieve the path for the process $($process.ProcessId)."
# Installation folder
$folderPath = "C:\Program Files\HetrixTools"

# Get the Server ID
$SID = $args[0]
if ([string]::IsNullOrWhiteSpace($SID)) {
# Try to reuse Server ID from existing v2 agent installation
$existingConfig = "$folderPath\hetrixtools.cfg"
if (Test-Path -Path $existingConfig) {
foreach ($line in Get-Content $existingConfig) {
if ($line -match "^\s*SID\s*=\s*([a-z0-9]{32})\s*$") {
$SID = $Matches[1]
break
}
}
}
$uniquePaths = $paths | Select-Object -Unique
if ($uniquePaths.Count -eq 1) {
$finalPath = $uniquePaths
Write-Host "The unique path for all instances is $finalPath"
Write-Host "Uninstalling the old agent..."
& "$finalPath" stop
& "$finalPath" remove
& taskkill /IM "HetrixToolsAgent.exe" /F
} else {
Write-Host "Error: Cannot uninstall the old agent because there are multiple instances running from different paths."
Write-Host "Please manually uninstall the old agent and then re-run this install script again."
exit 1
# Try to reuse Server ID from existing v1 agent installation
$existingSID = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HetrixToolsAgent\Parameters"
10000 if ([string]::IsNullOrWhiteSpace($SID) -and (Test-Path -Path $existingSID)) {
$SID = (Get-ItemProperty -Path $existingSID -Name 'sid').sid
}
}
Write-Host "... done."

# Get the Server ID
$SID = $args[0]

# Make sure the SID is not empty
# Make sure the SID is plausible
Write-Host "Checking Server ID (SID)..."
if ($SID -eq "") {
Write-Host "Error: Server ID is empty."
if ($SID -match "^[a-z0-9]{32}$") {
Write-Host "Server ID: $SID" -ForegroundColor Cyan
}
else {
Write-Host "Error: Server ID is empty." -ForegroundColor Yellow
exit
}
Write-Host "... done."

# Installation folder
$folderPath = "C:\Program Files\HetrixTools"

# Check if the folder exists
Write-Host "Checking installation folder..."
if (-Not (Test-Path -Path $folderPath)) {
Expand All @@ -108,21 +97,38 @@ if (-Not (Test-Path -Path $folderPath)) {
Write-Host "... done."

# Download the agent
Write-Host "Downloading the agent..."
$items = @(
[PSCustomObject] @{
description = "agent"
source = "https://raw.githubusercontent.com/hetrixtools/agent-windows/$BRANCH/hetrixtools_agent.ps1"
target = "$folderPath\hetrixtools_agent.ps1"
},
[PSCustomObject] @{
description = "config file"
source = "https://raw.githubusercontent.com/hetrixtools/agent-windows/$BRANCH/hetrixtools.cfg"
target = "$folderPath\hetrixtools.cfg"
}
)
$wc = New-Object System.Net.WebClient
$wc.DownloadFile("https://raw.githubusercontent.com/hetrixtools/agent-windows/$BRANCH/hetrixtools_agent.ps1", "$folderPath\hetrixtools_agent.ps1")
Write-Host "... done."
Write-Host "Downloading the config file..."
$wc.DownloadFile("https://raw.githubusercontent.com/hetrixtools/agent-windows/$BRANCH/hetrixtools.cfg", "$folderPath\hetrixtools.cfg")
Write-Host "... done."
foreach ($item in $items) {
Write-Host "Downloading the ${$item.description}..."
$wc.DownloadFile($item.source, $item.target)
if (Test-Path -Path $item.target) {
Write-Host "... done."
}
else {
Write-Host "... failed." -ForegroundColor Yellow
exit
}
}

# Insert the Server ID into the config file
Write-Host "Inserting the Server ID into the config file..."
(Get-Content "$folderPath\hetrixtools.cfg") | ForEach-Object { $_ -replace "SID=", "SID=$SID" } | Set-Content "$folderPath\hetrixtools.cfg"

# Check if any processes/services need to be monitored
Write-Host "Checking if any processes/services need to be monitored..."
if ($args[1] -ne "0") {
if ($null -ne $args[1] -and $args[1] -ne "0") {
# Insert the processes/services into the config file
Write-Host "Inserting the processes/services into the config file..."
# Split the string into an array and filter out empty elements
Expand Down Expand Up @@ -173,6 +179,7 @@ if ($existingTask) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
}
Write-Host "... done."

Write-Host "Creating the new scheduled task..."
# Calculate the next full minute
$currentTime = Get-Date
Expand All @@ -191,6 +198,46 @@ Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTri
$task = Get-ScheduledTask -TaskName $taskName
$task.Settings.ExecutionTimeLimit = "PT2M"
Set-ScheduledTask -TaskName $taskName -TaskPath "\" -Settings $task.Settings

# Check if agent is ready to replace v1 agent and/or to confirm installation
$taskExecutionTimeLimit = (Get-ScheduledTask -TaskName $taskName).Settings.ExecutionTimeLimit
if ($taskExecutionTimeLimit -eq $task.Settings.ExecutionTimeLimit) {
Write-Host "... done."
}
else {
Write-Host "... failed." -ForegroundColor Yellow
exit
}

# Find and uninstall v1 agent
Write-Host "Checking for old agent..."
$processName = "HetrixToolsAgent.exe"
$processes = Get-WmiObject Win32_Process | Where-Object { $_.Name -eq $processName }
$paths = @()
if ($processes) {
foreach ($process in $processes) {
$processPath = $process.ExecutablePath
if ($processPath) {
Write-Host "Found process $($process.ProcessId) running from path $processPath"
$paths += $processPath.Trim()
} else {
Write-Host "Unable to retrieve the path for the process $($process.ProcessId)."
}
}
$uniquePaths = $paths | Select-Object -Unique
if ($uniquePaths.Count -eq 1) {
$finalPath = $uniquePaths
Write-Host "The unique path for all instances is $finalPath"
Write-Host "Uninstalling the old agent..."
& "$finalPath" stop
& "$finalPath" remove
& taskkill /IM "HetrixToolsAgent.exe" /F
} else {
Write-Host "Error: Cannot uninstall the old agent because there are multiple instances running from different paths."
Write-Host "Please manually uninstall the old agent and then re-run this install script again."
exit 1
}
}
Write-Host "... done."

# Start the scheduled task
Expand Down
0