-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrist-time-deploy.ps1
425 lines (347 loc) · 14.8 KB
/
frist-time-deploy.ps1
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
<#
.PREREQUISITE
- Make Sure to create Dir "C:\packages" and download all packages
* ActiveMQ with name "apache-activemq-5.18.2"
* Redis with name "Redis-x64-3.0.504.msi"
* HostBundle with name "dotnet-hosting-3.1.32-win.exe"
* Java JDK with name "jdk-19_windows-x64_bin.msi"
* URL Rewrite with name "rewrite_amd64_en-US.msi"
.INPUTS
- Redis password to add in redis config files
- Domain to add in iisstart.html
.PARAMETER Redis_Password
- redis password to add in redis.windows.conf file
.PARAMETER Domain
- Domain to add in iisstart.html
.PARAMETER Redis_Database_line
- where database line in redis.windows.conf file
.PARAMETER DefaultAppPath
- wwwroot path where all applications downloaded in
.PARAMETER DefaultPrgramPath
- path that found all programs and packages we need
.PARAMETER RedisConfigFile1 RedisConfigFile2
- where redis config files installed
.PARAMETER JavaVariable
- java variable name to add new Environment variable
.PARAMETER InetpubPath
- Inetpub path
.PARAMETER JavaInstallationPath
- where java files installed
.PARAMETER Users
- list of all users you want add to access DefaultAppPath
.PARAMETER ActiveMQserviceName
- ActiveMQ service name
.PARAMETER RedisServiceName
- redis service name
.PARAMETER ActiveMQinstallationFile
- activeMQ BAT file to install activeMQ
.PARAMETER RedisProgramName
- redis program
.PARAMETER ActiveMQ_JDK
- Java JDK to install ActiveMQ
.PARAMETER HostBundle
- ASP.NET Core Runtime 3.1.28 version Windows Hosting Bundle
.PARAMETER URLRewrite
- URL Rewrite program name
#>
param(
[Parameter(Position = 0, mandatory)]
[string]$Redis_Password,
[Parameter(Position = 1, mandatory)]
[string]$Domain
)
$Redis_Database_line = 113
$DefaultAppPath = "C:\inetpub\wwwroot"
$DefaultPrgramPath = "C:\packages"
$RedisConfigFile1 = "C:\Program Files\Redis\redis.windows.conf"
$RedisConfigFile2 = "C:\Program Files\Redis\redis.windows-service.conf"
$JavaVariable = "JAVA_HOME"
$InetpubPath = "C:\inetpub"
$JavaInstallationPath = "C:\Program Files\Java\jdk-19"
$Users = @("Users","IIS_IUSRS")
$ActiveMQserviceName = "ActiveMQ"
$RedisServiceName ="Redis"
$ActiveMQinstallation = "apache-activemq-5.18.2\bin\win64\InstallService.bat"
$RedisProgramName= "Redis-x64-3.0.504.msi"
$ActiveMQ_JDK = "jdk-19_windows-x64_bin.msi"
$HostBundle = "dotnet-hosting-3.1.32-win.exe"
$URLRewrite = "rewrite_amd64_en-US.msi"
$Dirs_Paths = @("root","root2","root/backend","root/bot","root/inbox","root/webchat","root/analytics","root/auth","root/integrations","root2/gateway","root2/frontend","root2/webchat","root2/uploads")
$App_Details = @(
# app name | site name | app path | app pool
@{ Name = "integrations"; Site = "Private"; Path = "$DefaultAppPath\root\integrations" ; App_Pool = "integrations" },
@{ Name = "analytics" ; Site = "Private"; Path = "$DefaultAppPath\root\analytics" ; App_Pool = "analytics" },
@{ Name = "backend" ; Site = "Private"; Path = "$DefaultAppPath\root\backend" ; App_Pool = "backend" },
@{ Name = "webchat" ; Site = "Private"; Path = "$DefaultAppPath\root\webchat" ; App_Pool = "webchatBackend" },
@{ Name = "inbox" ; Site = "Private"; Path = "$DefaultAppPath\root\inbox" ; App_Pool = "inbox" },
@{ Name = "auth" ; Site = "Private"; Path = "$DefaultAppPath\root\auth" ; App_Pool = "auth" },
@{ Name = "bot" ; Site = "Private"; Path = "$DefaultAppPath\root\bot" ; App_Pool = "bot" },
@{ Name = "gateway" ; Site = "Public" ; Path = "$DefaultAppPath\root2\gateway" ; App_Pool = "gateway" },
@{ Name = "webchat" ; Site = "Public" ; Path = "$DefaultAppPath\root2\webchat" ; App_Pool = "webchatFrontend"},
@{ Name = "uploads" ; Site = "Public" ; Path = "$DefaultAppPath\root2\uploads" ; App_Pool = "uploads" },
@{ Name = "frontend" ; Site = "Public" ; Path = "$DefaultAppPath\root2\frontend" ; App_Pool = "frontend" }
)
$WebSite_Details = @(
@{Site = "Public" ; Port = 80 ; Path = "$DefaultAppPath\root2"},
@{Site = "Private"; Port = 8080; Path = "$DefaultAppPath\root" }
)
$App_Pools = @("gateway","frontend","uploads","backend","inbox","bot","webchatBackend","analytics","auth","integrations","webchatFrontend")
function AddUesrToDir ($UserName) {
# $UserInfo = Get-LocalUser -Name $UserName -ErrorAction SilentlyContinue
LogInformation "- Adding user '$UserName' "
New-LocalUser -Name $UserName -NoPassword -ErrorAction SilentlyContinue
if ( $?) {
LogDebug " adding user '$UserName' "
LogSuccess " add user '$UserName' successfully!`n"
}
else {
LogWarning " The user '$UserName' exists.`n"
}
}
function AddFullPermissionToDir ($FolderPath,$UserName){
$ACLsForFolder = Get-Acl -Path $FolderPath
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($UserName, "FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$ACLsForFolder.AddAccessRule($accessRule)
Set-Acl -Path $FolderPath -AclObject $ACLsForFolder
LogInformation "- Giving user '$UserName' full permissions "
LogDebug " giving user '$UserName' full permissions "
LogSuccess " giving user '$UserName' full permissions successfully!`n"
}
function RunMSIProgram ($PogramName) {
$msiFilePath = "$DefaultPrgramPath\$PogramName"
LogInformation "- Executing program $PogramName "
LogDebug " executing program $PogramName "
$process = Start-Process msiexec.exe -ArgumentList "/i `"$msiFilePath`" /qn" -PassThru -Wait
if ($process.HasExited) {
if ($process.ExitCode -eq 0) {
LogSuccess " Installation completed successfully.`n"
} else {
LogError " Installation failed `n"
ExitFromScript $_.Exception.Message $_.InvocationInfo.ScriptLineNumber $_.CategoryInfo
}
}
}
function RunBatProgram ($PogramName) {
LogInformation "- Executing program $PogramName"
LogDebug " executing program $PogramName"
Start-Process -FilePath "$DefaultPrgramPath\$PogramName"
LogSuccess " execute program $PogramName successfully!`n"
}
function RunExeProgram ($PogramName) {
LogInformation "- Executing program $PogramName"
LogDebug " Executing program $PogramName"
$process = Start-Process -FilePath "$DefaultPrgramPath\$PogramName" -ArgumentList "/install", "/quiet" -PassThru -Wait
if ($process.HasExited) {
if ($process.ExitCode -eq 0) {
LogSuccess " Installation completed successfully.`n"
} else {
LogError " Installation failed `n"
ExitFromScript $_.Exception.Message $_.InvocationInfo.ScriptLineNumber $_.CategoryInfo
}
} else {
LogError " Installation did not complete within the specified timeout.`n"
ExitFromScript $_.Exception.Message $_.InvocationInfo.ScriptLineNumber $_.CategoryInfo
}
}
function StartService ($ServiceName) {
$service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($null -eq $service) {
LogError " Service '$ServiceName' does not exist.`n"
} else {
LogInformation "- Setting service $ServiceName as Automatic"
Set-Service -Name $ServiceName -StartupType Automatic
LogSuccess " set service $ServiceName as Automatic`n"
LogInformation " starting service $ServiceName successfully!"
Start-Service -Name $ServiceName
LogSuccess " started service $ServiceName successfully!`n"
}
}
function AddContent ($FilePath,$Value) {
LogInformation "- Adding values $Value "
LogDebug " Adding values $Value "
Add-Content -Path $FilePath -Value $Value
LogSuccess " added values $Value successfully!`n"
}
function EditContent ($FilePath,$Value,$Line_Number) {
LogInformation "- Adding values $Value "
LogDebug " Adding values $Value "
$FileContent = Get-Content -Path $FilePath
$FileContent[$Line_Number - 1] = $Value
$FileContent | Set-Content -Path $FilePath
LogSuccess " added values $Value successfully!`n"
}
function CreateFolder ($FolderName) {
LogInformation "- Creating dir $FolderName ..."
LogDebug " Creating dir $FolderName ..."
if (-not (Test-Path -Path $FolderName -PathType Container)) {
New-Item -ItemType Directory -Path $FolderName
LogSuccess " Create dir $FolderName successfully!`n"
}
else {
LogWarning " directory $FolderName already exists.`n"
}
}
function CreateAppPool ($applicationPoolName) {
$serverManager = Get-IISServerManager
$existingAppPool = $serverManager.ApplicationPools | Where-Object { $_.Name -eq $applicationPoolName }
LogInformation "- Creating application pool $applicationPoolName ..."
LogDebug " Creating application pool $applicationPoolName ..."
if ($null -eq $existingAppPool) {
$newAppPool = $serverManager.ApplicationPools.Add($applicationPoolName)
$newAppPool.ManagedRuntimeVersion = "v4.0"
$newAppPool.ManagedPipelineMode = "Integrated"
$serverManager.CommitChanges()
LogSuccess " Create application pool $applicationPoolName successfully!`n"
}
else {
LogWarning " Application pool $applicationPoolName already exists.`n"
}
}
function CreateWebsite($arrOfSitesInfo) {
$websiteName = $arrOfSitesInfo["Site"]
$Port = $arrOfSitesInfo["Port"]
$physicalPath = $arrOfSitesInfo["Path"]
LogInformation "- Creating website $websiteName ..."
LogDebug " Creating website $websiteName ..."
if (-not (Get-Website -Name "$websiteName" -ErrorAction SilentlyContinue)) {
New-Website -Name "$websiteName" -PhysicalPath "$physicalPath" -Port $Port #####
Start-Website -Name "$websiteName" # check is started
LogSuccess " Create website $websiteName successfully!`n"
}
else {
LogWarning " website already exists.`n"
}
}
function CreateApplication($arrOfAppInfo) {
$Name = $arrOfAppInfo["Name"]
$App = Get-WebApplication -Site $arrOfAppInfo["Site"] -Name $arrOfAppInfo["Name"]
LogInformation "- Creating app $Name ..."
LogDebug " Creating app $Name ..."
if ($null -ne $App) {
LogWarning " The application $Name exists in IIS.`n"
} else {
New-WebApplication -Name $arrOfAppInfo["Name"] -Site $arrOfAppInfo["Site"] -PhysicalPath $arrOfAppInfo["Path"] -ApplicationPool $arrOfAppInfo["App_Pool"]
LogSuccess " Create website $Name successfully!`n"
}
}
function AddDomainTohtml($FilePath,$Content){
LogInformation "- Adding domain ..."
LogDebug " Adding domain ..."
[System.IO.File]::ReadAllText($FilePath)
[System.IO.File]::WriteAllText($FilePath, "")
AddContent $FilePath $Content
LogSuccess " Added domain ...`n"
}
function DownloadFeature ($FeatureName) {
$Feature = Get-WindowsFeature -Name $FeatureName
LogInformation "- Installing $FeatureName ..."
LogDebug " Installing $FeatureName ..."
if ($Feature.Installed) {
LogWarning " $FeatureName is exist.`n"
} else {
Install-WindowsFeature -Name $FeatureName
LogSuccess " $FeatureName is installed successfully!`n"
}
}
function LogDebug($message) {
Write-Host -ForegroundColor DarkGray $message
}
function LogInformation($message) {
Write-Host -ForegroundColor Cyan $message
}
function LogSuccess($message) {
Write-Host -ForegroundColor Green $message
}
function LogWarning($message) {
Write-Host -ForegroundColor Yellow $message
}
function LogError($message) {
Write-Host -ForegroundColor Red $message
}
function ExitFromScript($message,$line,$details) {
$message = $_.Exception.Message
$line = $_.InvocationInfo.ScriptLineNumber
$details = $_.CategoryInfo
LogError "An Error has occurred: $message
`nline: $line
`nError Details: $details"
LogWarning "Stopping the script.`n"
LogWarning "Press any key...`n"
$null = Read-Host
Exit
}
#===============================================================
try{
Import-Module WebAdministration
# install IIS
DownloadFeature "Web-Server"
# WebSocket Protocol
DownloadFeature "Web-WebSockets"
# Media Foundation
DownloadFeature "Server-Media-Foundation"
DownloadFeature "Web-Asp-Net"
DownloadFeature "Web-Net-Ext"
# Host bundle
RunExeProgram $HostBundle
RunMSIProgram $URLRewrite
Set-Location $DefaultAppPath -ErrorAction Stop
LogInformation "- Go to $DefaultAppPath"
# Create dirs
for($i = 0; $i -lt $Dirs_Paths.length; $i++){
CreateFolder($Dirs_Paths[$i])
}
#Create App Pools
for($i = 0; $i -lt $App_Pools.length; $i++){
CreateAppPool $App_Pools[$i]
}
#Create WebSites
foreach ($args in $WebSite_Details) {
CreateWebsite $args
}
#Create Apps
foreach ($args in $App_Details) {
CreateApplication $args
}
#Add users and permissions
foreach ($user in $Users){
AddUesrToDir $user
AddFullPermissionToDir $InetpubPath $user
}
#ActiveMQ
RunMSIProgram $ActiveMQ_JDK
# system and Environment var
LogInformation " adding system and Environment var for JAVA ..."
LogDebug " adding system and Environment var for JAVA ..."
[Environment]::SetEnvironmentVariable($JavaVariable,$JavaInstallationPath, "User")
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name $JavaVariable -Value $JavaInstallationPath
$env:JAVA_HOME = $JavaInstallationPath
LogSuccess " added system and Environment var for JAVA successfully! ... `n"
RunBatProgram $ActiveMQinstallation
StartService $ActiveMQserviceName
#===================================================
#Redis
RunMSIProgram $RedisProgramName
StartService $RedisServiceName
$Files_Paths = @($RedisConfigFile1,$RedisConfigFile2)
for($i = 0; $i -lt $Files_Paths.length; $i++){
AddContent $Files_Paths[$i] "requirepass $Redis_Password"
AddContent $Files_Paths[$i] "bind 0.0.0.0"
EditContent $Files_Paths[$i] "databases 30" $Redis_Database_line
}
# Add content to iisstart.html
AddDomainTohtml "$DefaultAppPath\iisstart.htm" "window.location.href = `"$Domain`";`n"
Write-Host "Press Enter to continue...`n"
$null = Read-Host
}
catch{
$message = $_.Exception.Message
$line = $_.InvocationInfo.ScriptLineNumber
$details = $_.CategoryInfo
LogError "An Error has occurred: $message
`nline: $line
`nError Details: $details"
LogWarning "Stopping the script.`n"
LogWarning "Press any key...`n"
$null = Read-Host
Exit
}