-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-agent.ps1
More file actions
219 lines (181 loc) · 8.31 KB
/
install-agent.ps1
File metadata and controls
219 lines (181 loc) · 8.31 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<#
.SYNOPSIS
Installs LabSync Agent as a Windows service.
.DESCRIPTION
The script can install from:
- Published binaries folder (contains LabSync.Agent.exe or LabSync.Agent.dll), or
- Repository root (contains src/LabSync.Agent/LabSync.Agent.csproj), where it will publish automatically.
It also:
- configures AGENT_SERVER_URL (machine environment variable),
- updates appsettings.json ServerUrl for visibility,
- copies module DLL files to <InstallDir>\Modules.
If you see "running scripts is disabled", run as Administrator from this folder:
powershell -ExecutionPolicy Bypass -File ".\install-agent.ps1" -ServerUrl "http://SERVER:5038" -SourcePath "."
#>
param(
[Parameter(Mandatory = $false)]
[string]$ServerUrl,
[Parameter(Mandatory = $false)]
[string]$InstallDir = "C:\Program Files\LabSync.Agent",
[Parameter(Mandatory = $false)]
[string]$SourcePath = ".",
[Parameter(Mandatory = $false)]
[string]$ServiceName = "LabSyncAgent"
)
$ErrorActionPreference = "Stop"
function Write-Info([string]$Message) {
Write-Host "[LabSync] $Message" -ForegroundColor Cyan
}
function Write-Warn([string]$Message) {
Write-Host "[LabSync] $Message" -ForegroundColor Yellow
}
function Assert-Admin {
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).
IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
throw "Run this script as Administrator."
}
}
function Resolve-PathSafe([string]$PathValue) {
return [System.IO.Path]::GetFullPath((Resolve-Path -LiteralPath $PathValue).Path)
}
function Build-AgentAndModulesFromRepo([string]$RepoRoot, [string]$PublishDir) {
$agentProject = Join-Path $RepoRoot "src\LabSync.Agent\LabSync.Agent.csproj"
if (-not (Test-Path -LiteralPath $agentProject)) {
throw "Could not find LabSync.Agent.csproj under $RepoRoot"
}
Write-Info "Publishing agent from source..."
dotnet publish "$agentProject" -c Release -o "$PublishDir" --self-contained false
if ($LASTEXITCODE -ne 0) {
throw "dotnet publish failed."
}
$modulesRoot = Join-Path $RepoRoot "src\Modules"
if (Test-Path -LiteralPath $modulesRoot) {
$moduleProjects = Get-ChildItem -LiteralPath $modulesRoot -Directory -Filter "LabSync.Modules.*"
foreach ($moduleDir in $moduleProjects) {
$proj = Join-Path $moduleDir.FullName "$($moduleDir.Name).csproj"
if (Test-Path -LiteralPath $proj) {
Write-Info "Building module: $($moduleDir.Name)"
dotnet build "$proj" -c Release
if ($LASTEXITCODE -ne 0) {
throw "Module build failed: $($moduleDir.Name)"
}
}
}
}
}
function Copy-ModuleDlls([string]$SourceRoot, [string]$InstallModulesDir) {
New-Item -ItemType Directory -Path $InstallModulesDir -Force | Out-Null
$copied = 0
$sourceModulesDir = Join-Path $SourceRoot "Modules"
if (Test-Path -LiteralPath $sourceModulesDir) {
Get-ChildItem -LiteralPath $sourceModulesDir -File -Filter "*.dll" | ForEach-Object {
Copy-Item -LiteralPath $_.FullName -Destination (Join-Path $InstallModulesDir $_.Name) -Force
$copied++
}
}
$repoModules = Join-Path $SourceRoot "src\Modules"
if (Test-Path -LiteralPath $repoModules) {
$moduleDirs = Get-ChildItem -LiteralPath $repoModules -Directory -Filter "LabSync.Modules.*"
foreach ($moduleDir in $moduleDirs) {
$releaseDir = Join-Path $moduleDir.FullName "bin\Release\net9.0"
if (Test-Path -LiteralPath $releaseDir) {
Get-ChildItem -LiteralPath $releaseDir -File -Filter "*.dll" | ForEach-Object {
Copy-Item -LiteralPath $_.FullName -Destination (Join-Path $InstallModulesDir $_.Name) -Force
$copied++
}
}
}
}
if ($copied -eq 0) {
Write-Warn "No module DLLs copied. Agent will start, but dynamic modules may be unavailable."
} else {
Write-Info "Copied $copied module DLL files into $InstallModulesDir"
}
}
if ([string]::IsNullOrWhiteSpace($ServerUrl)) {
$ServerUrl = Read-Host "Enter LabSync server URL (e.g. https://labsync.example.com or http://192.168.1.10:5038)"
}
if ([string]::IsNullOrWhiteSpace($ServerUrl)) {
throw "Server URL is required."
}
$ServerUrl = $ServerUrl.Trim().TrimEnd("/")
if ($ServerUrl -notmatch '^(?i)https?://') {
$ServerUrl = "http://$ServerUrl"
}
Assert-Admin
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
$sourceResolved = Resolve-PathSafe $SourcePath
$tempPublishDir = Join-Path $env:TEMP "LabSync.Agent.publish.$([Guid]::NewGuid().ToString('N'))"
$effectiveSource = $sourceResolved
$cleanupTempPublish = $false
$sourceExe = Join-Path $sourceResolved "LabSync.Agent.exe"
$sourceDll = Join-Path $sourceResolved "LabSync.Agent.dll"
$repoProject = Join-Path $sourceResolved "src\LabSync.Agent\LabSync.Agent.csproj"
if ((-not (Test-Path -LiteralPath $sourceExe)) -and (-not (Test-Path -LiteralPath $sourceDll)) -and (Test-Path -LiteralPath $repoProject)) {
New-Item -ItemType Directory -Path $tempPublishDir -Force | Out-Null
Build-AgentAndModulesFromRepo -RepoRoot $sourceResolved -PublishDir $tempPublishDir
$effectiveSource = $tempPublishDir
$cleanupTempPublish = $true
}
if ((-not (Test-Path -LiteralPath (Join-Path $effectiveSource "LabSync.Agent.exe"))) -and
(-not (Test-Path -LiteralPath (Join-Path $effectiveSource "LabSync.Agent.dll")))) {
throw "No LabSync.Agent executable found in $effectiveSource. Provide published binaries or repository root."
}
Write-Info "Copying agent binaries to $InstallDir"
Copy-Item -Path (Join-Path $effectiveSource "*") -Destination $InstallDir -Recurse -Force
$modulesInstallDir = Join-Path $InstallDir "Modules"
Copy-ModuleDlls -SourceRoot $sourceResolved -InstallModulesDir $modulesInstallDir
[Environment]::SetEnvironmentVariable("AGENT_SERVER_URL", $ServerUrl, "Machine")
Write-Info "Set machine environment variable AGENT_SERVER_URL=$ServerUrl"
$configPath = Join-Path $InstallDir "appsettings.json"
if (Test-Path -LiteralPath $configPath) {
try {
$configObj = Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json
if ($configObj.PSObject.Properties.Name -contains "ServerUrl") {
$configObj.ServerUrl = $ServerUrl
} else {
$configObj | Add-Member -NotePropertyName "ServerUrl" -NotePropertyValue $ServerUrl
}
$configObj | ConvertTo-Json -Depth 12 | Set-Content -LiteralPath $configPath -Encoding UTF8
Write-Info "Updated ServerUrl in $configPath"
} catch {
Write-Warn "Failed to update appsettings.json automatically: $($_.Exception.Message)"
}
}
if (Get-Service -Name $ServiceName -ErrorAction SilentlyContinue) {
Write-Info "Stopping existing service $ServiceName"
Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue
sc.exe delete $ServiceName | Out-Null
Start-Sleep -Seconds 1
}
$exePath = Join-Path $InstallDir "LabSync.Agent.exe"
$dllPath = Join-Path $InstallDir "LabSync.Agent.dll"
if (Test-Path -LiteralPath $exePath) {
$binaryPath = "`"$exePath`""
} elseif (Test-Path -LiteralPath $dllPath) {
$dotnetCmd = (Get-Command dotnet).Source
$binaryPath = "`"$dotnetCmd`" `"$dllPath`""
} else {
throw "Installed agent executable was not found."
}
Write-Info "Creating Windows service $ServiceName"
New-Service -Name $ServiceName `
-BinaryPathName $binaryPath `
-DisplayName "LabSync Agent" `
-Description "LabSync managed endpoint agent." `
-StartupType Automatic
try {
Start-Service -Name $ServiceName
Write-Info "Service started successfully."
} catch {
Write-Warn "Start-Service failed: $($_.Exception.Message)"
Write-Warn "Check Windows Logs -> Application (from LabSync Agent / .NET Runtime)."
Write-Warn "Framework-dependent builds need .NET 9 runtime installed for this RID."
Write-Warn "Try running manually: & $binaryPath (quote dotnet path if service uses dotnet.exe)."
throw
}
if ($cleanupTempPublish -and (Test-Path -LiteralPath $tempPublishDir)) {
Remove-Item -LiteralPath $tempPublishDir -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Host "[LabSync] Installation completed." -ForegroundColor Green