-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathinstall.ps1
More file actions
143 lines (121 loc) · 5.04 KB
/
install.ps1
File metadata and controls
143 lines (121 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Installs the latest Capture release from GitHub and registers it as a Windows service.
.PARAMETER InstallDir
Directory where the Capture binary will be installed. Defaults to: C:\Program Files\Capture
.PARAMETER ServiceName
Name of the Windows service to create. Defaults to: capture
.PARAMETER APISecret
Authentication secret for the Capture API. Prompted if not provided.
.PARAMETER Port
Port the Capture server listens on. Defaults to: 59232
.EXAMPLE
.\install.ps1
.EXAMPLE
.\install.ps1 -APISecret "my-secret" -Port 8080
.EXAMPLE
.\install.ps1 -InstallDir "C:\capture" -ServiceName "capture-agent" -APISecret "my-secret"
#>
[CmdletBinding()]
param(
[string] $InstallDir = 'C:\Program Files\Capture',
[string] $ServiceName = 'capture',
[string] $APISecret = '',
[int] $Port = 59232
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# Detect architecture
$arch = switch ($env:PROCESSOR_ARCHITECTURE) {
'AMD64' { 'amd64' }
'ARM64' { 'arm64' }
default { Write-Error "Unsupported architecture: $env:PROCESSOR_ARCHITECTURE"; exit 1 }
}
Write-Host "Architecture: $arch"
# Fetch latest release from GitHub
$release = Invoke-RestMethod -Uri 'https://api.github.com/repos/bluewave-labs/capture/releases/latest' `
-Headers @{ 'User-Agent' = 'capture-install-script' }
$version = $release.tag_name
$versionNum = $version.TrimStart('v')
Write-Host "Latest version: $version"
# Find the download asset
$assetName = "capture_${versionNum}_windows_${arch}.zip"
$asset = $release.assets | Where-Object { $_.name -eq $assetName } | Select-Object -First 1
if (-not $asset) {
Write-Error "Asset '$assetName' not found in the latest release."
exit 1
}
Write-Host "Downloading: $($asset.browser_download_url)"
# Download and extract
$tmpDir = Join-Path $env:TEMP "capture_install_$([System.IO.Path]::GetRandomFileName())"
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
$zipPath = Join-Path $tmpDir $assetName
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $zipPath -UseBasicParsing
$extractDir = Join-Path $tmpDir 'extracted'
Expand-Archive -Path $zipPath -DestinationPath $extractDir -Force
$binaryPath = Get-ChildItem -Path $extractDir -Filter 'capture.exe' -Recurse |
Select-Object -First 1 -ExpandProperty FullName
if (-not $binaryPath) {
Write-Error "capture.exe not found in the extracted archive."
exit 1
}
# Install binary
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$targetBinary = Join-Path $InstallDir 'capture.exe'
$existingService = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($existingService) {
Write-Host "Stopping existing service '$ServiceName'..."
Stop-Service -Name $ServiceName -Force
$existingService.WaitForStatus('Stopped', [TimeSpan]::FromSeconds(30))
}
Copy-Item -Path $binaryPath -Destination $targetBinary -Force
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Installed: $targetBinary"
# Collect API secret
if ([string]::IsNullOrWhiteSpace($APISecret)) {
$secureSecret = Read-Host 'Enter API_SECRET' -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureSecret)
$APISecret = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
}
if ([string]::IsNullOrWhiteSpace($APISecret)) {
Write-Error 'API_SECRET must not be empty.'
exit 1
}
# Create or update the Windows service
if ($existingService) {
& sc.exe config $ServiceName binPath= "`"$targetBinary`"" | Out-Null
Write-Host "Updated service '$ServiceName'."
} else {
New-Service `
-Name $ServiceName `
-BinaryPathName $targetBinary `
-DisplayName 'Capture Monitoring Agent' `
-Description 'Capture hardware monitoring agent (https://github.com/bluewave-labs/capture)' `
-StartupType Automatic | Out-Null
Write-Host "Created service '$ServiceName'."
}
# Write environment variables into the service registry key
Set-ItemProperty `
-Path "HKLM:\SYSTEM\CurrentControlSet\Services\$ServiceName" `
-Name 'Environment' `
-Value @("API_SECRET=$APISecret", "PORT=$Port", "GIN_MODE=release") `
-Type MultiString
# Start or restart the service (Restart-Service starts it if stopped, restarts if running)
Restart-Service -Name $ServiceName -Force
Write-Host "Service status: $((Get-Service -Name $ServiceName).Status)"
Write-Host ""
Write-Host "Capture $version installed successfully."
Write-Host " Install directory : $InstallDir"
Write-Host " Service name : $ServiceName"
Write-Host " Port : $Port"
Write-Host ""
Write-Host " Get-Service $ServiceName"
Write-Host " Stop-Service $ServiceName"
Write-Host " Start-Service $ServiceName"
Write-Host " Restart-Service $ServiceName"
Write-Host ""
Write-Host " Invoke-RestMethod http://localhost:$Port/health"