-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeploy-Solution.ps1
More file actions
516 lines (425 loc) · 15.8 KB
/
Deploy-Solution.ps1
File metadata and controls
516 lines (425 loc) · 15.8 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<#
.SYNOPSIS
Deploys SecureBootDashboard API and Web projects using their publishing profiles.
.DESCRIPTION
This script automates the deployment of both SecureBootDashboard.Api and
SecureBootDashboard.Web projects to their configured network locations using
the FolderProfile publishing profiles. It can optionally stop/start IIS
Application Pools during deployment.
.PARAMETER Configuration
The build configuration to use (Debug or Release). Default is Release.
.PARAMETER ProjectFilter
Deploy only specific project: 'Api', 'Web', or 'All'. Default is All.
.PARAMETER Clean
Performs a clean build before publishing.
.PARAMETER VerboseOutput
Shows detailed output from dotnet publish command.
.PARAMETER ManageIIS
If specified, stops IIS Application Pools before deployment and starts them after.
.PARAMETER IISServer
The IIS server name where application pools are running. Default is 'srvcm00'.
.PARAMETER ApiAppPool
Name of the API Application Pool. Default is 'SecureBootDashboard.Api'.
.PARAMETER WebAppPool
Name of the Web Application Pool. Default is 'SecureBootDashboard.Web'.
.EXAMPLE
.\Deploy-Solution.ps1
Deploys both projects in Release configuration without managing IIS.
.EXAMPLE
.\Deploy-Solution.ps1 -ManageIIS
Deploys both projects and manages IIS Application Pools on default server.
.EXAMPLE
.\Deploy-Solution.ps1 -ManageIIS -IISServer "MyServer" -ApiAppPool "MyApiPool"
Deploys with custom IIS server and application pool names.
.EXAMPLE
.\Deploy-Solution.ps1 -Configuration Debug -ProjectFilter Api -Clean -ManageIIS
Deploys only API with clean build and IIS management.
.EXAMPLE
.\Deploy-Solution.ps1 -VerboseOutput
Deploys with detailed dotnet publish output.
#>
[CmdletBinding()]
param(
[Parameter()]
[ValidateSet('Debug', 'Release')]
[string]$Configuration = 'Release',
[Parameter()]
[ValidateSet('Api', 'Web', 'All')]
[string]$ProjectFilter = 'All',
[Parameter()]
[switch]$Clean,
[Parameter()]
[switch]$VerboseOutput,
[Parameter()]
[switch]$ManageIIS,
[Parameter()]
[string]$IISServer = 'srvcm00',
[Parameter()]
[string]$ApiAppPool = 'SecureBootDashboard.Api',
[Parameter()]
[string]$WebAppPool = 'SecureBootDashboard.Web'
)
# Configuration
$ErrorActionPreference = 'Stop'
$InformationPreference = 'Continue'
$projects = @(
@{
Name = 'SecureBootDashboard.Api'
Path = 'SecureBootDashboard.Api\SecureBootDashboard.Api.csproj'
Profile = 'FolderProfile'
Destination = '\\srvcm00\r$\Nimbus.SecureBootCert\SecureBootDashboard.Api'
Filter = 'Api'
AppPool = $ApiAppPool
},
@{
Name = 'SecureBootDashboard.Web'
Path = 'SecureBootDashboard.Web\SecureBootDashboard.Web.csproj'
Profile = 'FolderProfile'
Destination = '\\Srvcm00\r$\Nimbus.SecureBootCert\SecureBootDashboard.Web'
Filter = 'Web'
AppPool = $WebAppPool
}
)
# Functions
function Write-Header {
param([string]$Message)
Write-Host "`n$('=' * 80)" -ForegroundColor Cyan
Write-Host " $Message" -ForegroundColor Cyan
Write-Host "$('=' * 80)" -ForegroundColor Cyan
}
function Write-Step {
param([string]$Message)
Write-Host "`n? $Message" -ForegroundColor Yellow
}
function Write-Success {
param([string]$Message)
Write-Host "? $Message" -ForegroundColor Green
}
function Write-Error {
param([string]$Message)
Write-Host "? $Message" -ForegroundColor Red
}
function Write-Warning {
param([string]$Message)
Write-Host "? $Message" -ForegroundColor DarkYellow
}
function Test-ProjectFile {
param([string]$Path)
if (-not (Test-Path $Path)) {
Write-Error "Project file not found: $Path"
return $false
}
return $true
}
function Test-IISManagement {
Write-Step "Checking IIS Management capabilities..."
try {
# Check if WebAdministration module is available
if (-not (Get-Module -ListAvailable -Name WebAdministration)) {
Write-Warning "WebAdministration module not available. IIS management will be performed using Invoke-Command."
return $false
}
Import-Module WebAdministration -ErrorAction Stop
Write-Success "WebAdministration module loaded successfully"
return $true
} catch {
Write-Warning "Could not load WebAdministration module: $_"
return $false
}
}
function Get-AppPoolState {
param(
[string]$ServerName,
[string]$AppPoolName
)
try {
$scriptBlock = {
param($poolName)
Import-Module WebAdministration -ErrorAction Stop
$pool = Get-Item "IIS:\AppPools\$poolName" -ErrorAction Stop
return $pool.State
}
if ($ServerName -eq $env:COMPUTERNAME -or $ServerName -eq 'localhost') {
$state = & $scriptBlock -poolName $AppPoolName
} else {
$state = Invoke-Command -ComputerName $ServerName -ScriptBlock $scriptBlock -ArgumentList $AppPoolName -ErrorAction Stop
}
return $state
} catch {
Write-Warning "Could not get state for Application Pool '$AppPoolName' on '$ServerName': $_"
return $null
}
}
function Stop-AppPool {
param(
[string]$ServerName,
[string]$AppPoolName
)
Write-Step "Stopping Application Pool '$AppPoolName' on '$ServerName'..."
try {
$currentState = Get-AppPoolState -ServerName $ServerName -AppPoolName $AppPoolName
if ($null -eq $currentState) {
Write-Warning "Application Pool '$AppPoolName' not found or not accessible"
return $false
}
if ($currentState -eq 'Stopped') {
Write-Host " Application Pool is already stopped" -ForegroundColor Gray
return $true
}
$scriptBlock = {
param($poolName)
Import-Module WebAdministration -ErrorAction Stop
Stop-WebAppPool -Name $poolName -ErrorAction Stop
# Wait for pool to stop (max 30 seconds)
$timeout = 30
$elapsed = 0
while ($elapsed -lt $timeout) {
$pool = Get-Item "IIS:\AppPools\$poolName"
if ($pool.State -eq 'Stopped') {
return $true
}
Start-Sleep -Seconds 1
$elapsed++
}
return $false
}
if ($ServerName -eq $env:COMPUTERNAME -or $ServerName -eq 'localhost') {
$result = & $scriptBlock -poolName $AppPoolName
} else {
$result = Invoke-Command -ComputerName $ServerName -ScriptBlock $scriptBlock -ArgumentList $AppPoolName -ErrorAction Stop
}
if ($result) {
Write-Success "Application Pool '$AppPoolName' stopped successfully"
return $true
} else {
Write-Warning "Application Pool '$AppPoolName' did not stop within timeout"
return $false
}
} catch {
Write-Error "Failed to stop Application Pool '$AppPoolName': $_"
return $false
}
}
function Start-AppPool {
param(
[string]$ServerName,
[string]$AppPoolName
)
Write-Step "Starting Application Pool '$AppPoolName' on '$ServerName'..."
try {
$currentState = Get-AppPoolState -ServerName $ServerName -AppPoolName $AppPoolName
if ($null -eq $currentState) {
Write-Warning "Application Pool '$AppPoolName' not found or not accessible"
return $false
}
if ($currentState -eq 'Started') {
Write-Host " Application Pool is already started" -ForegroundColor Gray
return $true
}
$scriptBlock = {
param($poolName)
Import-Module WebAdministration -ErrorAction Stop
Start-WebAppPool -Name $poolName -ErrorAction Stop
# Wait for pool to start (max 30 seconds)
$timeout = 30
$elapsed = 0
while ($elapsed -lt $timeout) {
$pool = Get-Item "IIS:\AppPools\$poolName"
if ($pool.State -eq 'Started') {
return $true
}
Start-Sleep -Seconds 1
$elapsed++
}
return $false
}
if ($ServerName -eq $env:COMPUTERNAME -or $ServerName -eq 'localhost') {
$result = & $scriptBlock -poolName $AppPoolName
} else {
$result = Invoke-Command -ComputerName $ServerName -ScriptBlock $scriptBlock -ArgumentList $AppPoolName -ErrorAction Stop
}
if ($result) {
Write-Success "Application Pool '$AppPoolName' started successfully"
return $true
} else {
Write-Warning "Application Pool '$AppPoolName' did not start within timeout"
return $false
}
} catch {
Write-Error "Failed to start Application Pool '$AppPoolName': $_"
return $false
}
}
function Invoke-CleanBuild {
param([string]$ProjectPath, [string]$ProjectName)
Write-Step "Cleaning $ProjectName..."
$cleanArgs = @(
'clean',
$ProjectPath,
'-c', $Configuration
)
$result = & dotnet @cleanArgs 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Success "Clean completed for $ProjectName"
return $true
} else {
Write-Error "Clean failed for $ProjectName"
Write-Host $result -ForegroundColor Red
return $false
}
}
function Invoke-ProjectPublish {
param(
[string]$ProjectPath,
[string]$ProjectName,
[string]$ProfileName,
[string]$Destination
)
Write-Step "Publishing $ProjectName..."
Write-Host " Configuration: $Configuration" -ForegroundColor Gray
Write-Host " Profile: $ProfileName" -ForegroundColor Gray
Write-Host " Destination: $Destination" -ForegroundColor Gray
$publishArgs = @(
'publish',
$ProjectPath,
"/p:PublishProfile=$ProfileName",
"/p:Configuration=$Configuration"
)
if ($VerboseOutput) {
$publishArgs += '-v', 'detailed'
}
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$result = & dotnet @publishArgs 2>&1
$stopwatch.Stop()
if ($LASTEXITCODE -eq 0) {
Write-Success "$ProjectName published successfully in $($stopwatch.Elapsed.TotalSeconds.ToString('F2'))s"
Write-Host " Published to: $Destination" -ForegroundColor Gray
return $true
} else {
Write-Error "$ProjectName publish failed"
Write-Host $result -ForegroundColor Red
return $false
}
}
# Main execution
try {
Write-Header "SecureBootDashboard Deployment"
# Verify we're in the correct directory
if (-not (Test-Path "Nimbus.BootCertWatcher.sln")) {
Write-Error "Solution file not found. Please run this script from the solution root directory."
exit 1
}
# Filter projects
$projectsToDeploy = $projects | Where-Object {
$ProjectFilter -eq 'All' -or $_.Filter -eq $ProjectFilter
}
Write-Host "`nDeployment Configuration:" -ForegroundColor Cyan
Write-Host " Build Configuration: $Configuration" -ForegroundColor White
Write-Host " Projects to Deploy: $($projectsToDeploy.Count)" -ForegroundColor White
Write-Host " Clean Build: $Clean" -ForegroundColor White
Write-Host " Manage IIS: $ManageIIS" -ForegroundColor White
if ($ManageIIS) {
Write-Host " IIS Server: $IISServer" -ForegroundColor White
}
# Verify IIS management if requested
$canManageIIS = $false
if ($ManageIIS) {
Write-Header "IIS Management Check"
$canManageIIS = Test-IISManagement
if (-not $canManageIIS) {
Write-Warning "IIS management features may be limited. Continuing with deployment..."
}
}
# Stop Application Pools
$stoppedPools = @()
if ($ManageIIS) {
Write-Header "Stopping IIS Application Pools"
foreach ($project in $projectsToDeploy) {
if (Stop-AppPool -ServerName $IISServer -AppPoolName $project.AppPool) {
$stoppedPools += $project.AppPool
}
}
# Wait a moment for file locks to release
if ($stoppedPools.Count -gt 0) {
Write-Host "`n Waiting 3 seconds for file locks to release..." -ForegroundColor Gray
Start-Sleep -Seconds 3
}
}
# Deployment
Write-Header "Building and Publishing Projects"
$totalStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$successCount = 0
$failCount = 0
foreach ($project in $projectsToDeploy) {
Write-Header "Deploying $($project.Name)"
# Verify project file exists
if (-not (Test-ProjectFile -Path $project.Path)) {
$failCount++
continue
}
# Clean if requested
if ($Clean) {
if (-not (Invoke-CleanBuild -ProjectPath $project.Path -ProjectName $project.Name)) {
$failCount++
continue
}
}
# Publish
if (Invoke-ProjectPublish -ProjectPath $project.Path `
-ProjectName $project.Name `
-ProfileName $project.Profile `
-Destination $project.Destination) {
$successCount++
} else {
$failCount++
}
}
$totalStopwatch.Stop()
# Start Application Pools
if ($ManageIIS -and $stoppedPools.Count -gt 0) {
Write-Header "Starting IIS Application Pools"
foreach ($project in $projectsToDeploy) {
if ($stoppedPools -contains $project.AppPool) {
Start-AppPool -ServerName $IISServer -AppPoolName $project.AppPool | Out-Null
}
}
}
# Summary
Write-Header "Deployment Summary"
Write-Host "`nResults:" -ForegroundColor Cyan
Write-Host " Total Projects: $($projectsToDeploy.Count)" -ForegroundColor White
Write-Host " Successful: $successCount" -ForegroundColor Green
Write-Host " Failed: $failCount" -ForegroundColor $(if ($failCount -gt 0) { 'Red' } else { 'White' })
Write-Host " Total Time: $($totalStopwatch.Elapsed.TotalSeconds.ToString('F2'))s" -ForegroundColor White
if ($ManageIIS) {
Write-Host "`nIIS Management:" -ForegroundColor Cyan
Write-Host " Application Pools Managed: $($stoppedPools.Count)" -ForegroundColor White
foreach ($pool in $stoppedPools) {
$state = Get-AppPoolState -ServerName $IISServer -AppPoolName $pool
$color = if ($state -eq 'Started') { 'Green' } else { 'Red' }
Write-Host " - $pool`: $state" -ForegroundColor $color
}
}
if ($failCount -eq 0) {
Write-Host "`n? All deployments completed successfully!" -ForegroundColor Green
exit 0
} else {
Write-Host "`n? Some deployments failed. Please check the output above." -ForegroundColor Red
exit 1
}
} catch {
Write-Error "An unexpected error occurred: $_"
Write-Host $_.ScriptStackTrace -ForegroundColor Red
# Try to restart pools in case of error
if ($ManageIIS -and $stoppedPools.Count -gt 0) {
Write-Header "Emergency: Restarting Application Pools"
foreach ($poolName in $stoppedPools) {
try {
Start-AppPool -ServerName $IISServer -AppPoolName $poolName | Out-Null
} catch {
Write-Warning "Could not restart pool '$poolName': $_"
}
}
}
exit 1
}