-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperations.ps1
483 lines (364 loc) · 16.6 KB
/
Operations.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
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
<#
This script is for environment management and deployment purposes
by Pinega F.V. on july 2023
#>
param(
[string] $transactionId='',
[string] $customWokingFolder='',
[string] $callType='plain' # plain, external-from-self, external-from-other-script
)
<#
$userName0 = "root";
$pwd0 = ConvertTo-SecureString "4*cXVNAmP|gy" -AsPlainText -Force
$deployCreds0 = New-Object System.Management.Automation.PSCredential ($userName0, $pwd0)
$userName1 = "root01";
$pwd1 = ConvertTo-SecureString "64pVSprEtQ" -AsPlainText -Force
$deployCreds1 = New-Object System.Management.Automation.PSCredential ($userName1, $pwd1)
#>
#COMMON
[string] $scriptDir = $PSScriptRoot;
[string] $scriptFile=$MyInvocation.MyCommand;
[string] $scriptFileFullPath = [IO.Path]::Combine($scriptDir,$scriptFile);
[string] $pwshPath = "C:\Program Files\PowerShell\7\pwsh.exe";
[string] $operaExeFullPath = "C:\Users\Admin\AppData\Local\Programs\Opera\opera.exe"
#COMMON -- MENUMAKER
[string] $menuFileName='MenuDetail.txt';
[string] $menuFileFullPath=[IO.Path]::Combine($scriptDir,$menuFileName);
[string] $currentLogFileName = '';
[string] $logFileFullPath='';
[string] $logsDirectory=[IO.Path]::Combine($scriptDir,'Logs');
[int] $howManyLogFilesToLeaveInLogsDirectory = 3
[bool] $canExit=$false;
# CREDS AND LOGIN DATA
# file to store creds in
[string] $credsFilePath = "C:\Develop\Deploy\Creds\Creds.txt";
[string] $deployAddress01='';
[string] $deployHost01='';
$menuContent = Get-content $menuFileFullPath
function PerformCls()
{
# Clear-host
}
function ShowMenu() {
""
""
"MAIN MENU"
$menuContent
"99-Exit"
}
# setup TrustedHosts
if($transactionId -eq '')
{
# if ($tth -ne $tth_fact) { Set-Item WSMan:\localhost\Client\TrustedHosts -Value $tth -Force; }
$tth = '31.31.42.42';
$x0 = Set-Item WSMan:\localhost\Client\TrustedHosts -Value $tth -Force;
$tth_fact = (Get-Item WSMan:\localhost\Client\TrustedHosts).Value;
#Set-Item -Path WSMan:\localhost\Service\AllowEncrypted -Value $true;
}
function Pressenter ()
{
Read-Host -Prompt 'Press enter...'
}
function SetupCredentials()
{
[string] $fileData = "";
[bool] $credFileExists= [System.IO.File]::Exists($credsFilePath);
if (-not $credFileExists) {return;}
$fileData = [System.IO.File]::ReadAllText($credsFilePath);
log "fileData=$($fileData)"
$arr=$fileData.Split("`n");
log "arr.Length=$($arr.Length)"
if($arr.Length -gt 0)
{
$script:deployAddress01 = GetValueFromArray -arr $arr -key 'DeployAddress01' | Select-Object -Last 1
$script:deployHost01 = GetValueFromArray -arr $arr -key 'DeployHost01' | Select-Object -Last 1
}
Log "deployAddress01=$deployAddress01"
}
function GetValueFromArray($arr, [string]$key)
{
[string] $found = $arr | Where-Object { $_.Contains($key) } | Select-Object -Last 1
if($null -eq $found) {"";return;}
($found.Split(" ")[1]).Trim();
}
function Log ([string] $text)
{
if ($script:currentLogFileName -eq '')
{
[string]$dtVar = Get-Date -Format "dd-MM-yyyy_HH-mm-ss"
if($transactionId -eq '') {$tr=''} else {$tr="_$transactionId"}
$script:currentLogFileName = "$($dtVar)$($tr).txt"
}
$Script:logFileFullPath = [IO.Path]::Combine($logsDirectory, $script:currentLogFileName)
if ([System.IO.File]::Exists($Script:logFileFullPath)){}else{New-Item $Script:logFileFullPath | Out-Null}
"[$(Get-Date -Format "dd.MM.yyyy-HH:mm:ss")] $text" | Add-Content $Script:logFileFullPath;
}
#COMMON FUNCTION
function CheckLogsDirectory()
{
CreateDirectoryOnLocalHostIfNotExists -directoryName $logsDirectory
}
function CLearLogs()
{
KeepOnlyNLastFilesInDirectory -directory $logsDirectory -itemsCol $howManyLogFilesToLeaveInLogsDirectory
}
function CreateDirectoryOnLocalHostIfNotExists([string] $directoryName)
{
if (-not (Test-Path $directoryName -PathType Container)) {
New-Item -ItemType Directory -Force -Path $directoryName
}
}
function PerformTimeSpanFormat([timespan] $ts)
{
if ($ts.TotalMilliseconds -gt 86400000)
{
"{0:dd}:{0:hh}:{0:mm}:{0:ss}" -f $ts
}
else
{
"{0:hh}:{0:mm}:{0:ss}" -f $ts
}
}
function KeepOnlyNLastFilesInDirectory([string] $directory, [int] $itemsCol)
{
if (-not (Test-path -Path $directory)) {return};
Get-ChildItem $directory | Sort-Object CreationTime | Select-Object -SkipLast $itemsCol | Remove-Item -Force -Recurse
}
function PerformSecondsCountDown ([int] $seconds, [string]$prefix="Performing countdown", [bool] $performLog=$false)
{
[datetime] $currentTime= Get-Date;
[datetime] $targetTime= $currentTime.AddSeconds($seconds);
[bool] $flag=$true;
DO
{
$currentTime= Get-Date;
$delta = $targetTime - $currentTime;
if($flag)
{
[string] $ts= PerformTimeSpanFormat -ts $delta
write-host "`r$($prefix): $ts"
if($performLog)
{
Log -text "Performing countdown log, its $ts left"
}
$flag = $false;
}
else
{
$flag=$true;
}
Start-Sleep -Milliseconds 500
}
WHILE ($delta.TotalMilliseconds -ge 0)
}
#CORE CONTENT
function DeployStore01 ([string] $_transactionId)
{
if($callType -eq 'plain')
{
# this is made to start transaction in a separate window
[string] $argList = "-file $scriptFileFullPath -transactionId $_transactionId -callType 'external-from-self' ";
Start-process -FilePath $pwshPath -ArgumentList $argList -PassThru;
return;
}
[string] $remoteFolder = "/var/www/www-root/data/www/store01.t109.tech";
[string] $projectPath = "C:\Develop\T109ActivityFrontendFirstSampleVersion\Shop\T109.ActiveDive.FrontEnd.Blazor";
[string] $projectBuildPath = "C:\Develop\Deploy\BalzorDeployFolder";
[string] $siteUrl = "https://store01.t109.tech";
DeployBalzorWasm60SiteToUbuntuHost -remoteFolder $remoteFolder -projectPath $projectPath -projectBuildPath $projectBuildPath -siteUrl $siteUrl
}
function DeployApi01 ([string] $_transactionId)
{
if($callType -eq 'plain')
{
# this is made to start transaction in a separate window
[string] $argList = "-file $scriptFileFullPath -transactionId $_transactionId -callType 'external-from-self' ";
Start-process -FilePath $pwshPath -ArgumentList $argList -PassThru;
return;
}
[string] $remoteFolder = "/var/www/www-root/data/www/storeapi01.t109.tech";
[string] $projectPath = "C:\Develop\T109ActivityFrontendFirstSampleVersion\Shop\T109.ActiveDive.EventCatalogue.EventCatalogueApi";
[string] $projectBuildPath = "C:\Develop\T109ActivityFrontendFirstSampleVersion\Shop\T109.ActiveDive.EventCatalogue.EventCatalogueApi\bin\Release\net6.0\*";
[string] $wwwRootFolder = "C:\Develop\T109ActivityFrontendFirstSampleVersion\Shop\T109.ActiveDive.EventCatalogue.EventCatalogueApi\wwwroot";
[string] $executingFileFullPath = "/var/www/www-root/data/www/storeapi01.t109.tech/T109.ActiveDive.EventCatalogue.EventCatalogueApi.dll";
[string] $serviceName = "storeapi01.service";
[string] $siteUrl = "https://storeapi01.t109.tech";
DeployAspNetCore60ApiSiteToUbuntuHost -remoteFolder $remoteFolder -projectPath $projectPath -projectBuildPath $projectBuildPath -wwwRootFolder $wwwRootFolder -serviceName $serviceName -executingFileFullPath $executingFileFullPath -siteUrl $siteUrl
}
function LogAndExit ([string] $text)
{
log $text
throw $text
exit
}
function LogAndOutput ([string] $text)
{
log $text
$text
}
function DeleteAllFilesFromFolder ([string] $folderName)
{
Get-ChildItem -Path $folderName -recurse | Select-Object -ExpandProperty FullName | Remove-Item -recurse -force
}
function DeployAspNetCore60ApiSiteToUbuntuHost ([string] $remoteFolder, [string] $projectPath, [string] $projectBuildPath,[string] $wwwRootFolder, [string] $serviceName, [string] $executingFileFullPath, [string] $siteUrl)
{
LogAndOutput -text "Deploying site $siteUrl";
LogAndOutput -text "Creating session";
# session
try
{
$deploySession = New-PSSession -HostName $script:deployAddress01 -KeyFilePath "C:\Users\Admin\.ssh\id_rsa";
}
catch
{
log "Unable to crate session: $($_)"
exit
}
LogAndOutput -text "Session opened successfully";
# stop service and check if stopped
Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "sudo systemctl stop $($args[0])"; } -ArgumentList $serviceName
PerformSecondsCountDown -seconds 3;
$isActive = Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "sudo systemctl is-active $($args[0])"; } -ArgumentList $serviceName
$isActive
if ($isActive -ne 'inactive') {LogAndExit -text "Unable to stop service $serviceName on remote host, service status=$isActive" }
LogAndOutput -text "Service $serviceName moved to inactive state";
# delete folder
Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "sudo rm -r -f $($args[0])";} -ArgumentList $remoteFolder
# check if folder deleted
[string] $folderExists = Invoke-Command -Session $deploySession -ScriptBlock { test-path $args[0]} -ArgumentList $remoteFolder
if ($folderExists -ne 'false') { LogAndExit -text "Unable to delete folder" }
LogAndOutput -text "Remote folder $remoteFolder";
# --- deploy
# -- build
"dotnet build $projectPath --configuration Release" | cmd
log "Build completed";
# -- create dir and copy it all to host
Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "mkdir $($args[0])";} -ArgumentList $remoteFolder
LogAndOutput -text "Remote dir $remoteFolder created";
# -- copy items to remote
LogAndOutput -text "Copying data";
Copy-Item $projectBuildPath -ToSession $deploySession -Destination $remoteFolder -Recurse -Force ;
if (-not [string]::IsNullOrEmpty($wwwRootFolder))
{
Copy-Item $wwwRootFolder -ToSession $deploySession -Destination $remoteFolder -Recurse -Force ;
}
LogAndOutput -text "Finished data copy";
# give rights to folder
Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "chmod 777 $($args[0])";} -ArgumentList $remoteFolder
LogAndOutput -text "Given rights to $remoteFolder on remote host";
# give execution rights to dll
Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "chmod +X $($args[0])";} -ArgumentList $executingFileFullPath
LogAndOutput -text "Given execution rights to dll $executingFileFullPath";
# start service and check if started
LogAndOutput -text "Starting service $serviceName";
Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "sudo systemctl start $($args[0])"; } -ArgumentList $serviceName
$isActive = Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "sudo systemctl is-active $($args[0])"; } -ArgumentList $serviceName
$isActive
if ($isActive -ne 'active') { LogAndExit -text "Unable to start service $serviceName on remote host" }
LogAndOutput -text "Service started";
LogAndOutput -text "Deploy completed successfully";
Start-process -FilePath $operaExeFullPath -ArgumentList $siteUrl;
}
function DeployBalzorWasm60SiteToUbuntuHost ([string] $remoteFolder, [string] $projectPath, [string] $projectBuildPath, [string] $projectBuildZipPath, [string] $siteUrl)
{
LogAndOutput -text "Deploying Blazor on Net Core 6.0 site $siteUrl ";
LogAndOutput -text "Creating session";
# session
try {
$deploySession = New-PSSession -HostName $script:deployAddress01 -KeyFilePath "C:\Users\Admin\.ssh\id_rsa";
}
catch {
log "Unable to crate session: $($_)"
exit
}
LogAndOutput -text "Session opened successfully";
# delete folder
Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "sudo rm -r -f $($args[0])";} -ArgumentList $remoteFolder
# check if folder deleted
[string] $folderExists = Invoke-Command -Session $deploySession -ScriptBlock { test-path $args[0]} -ArgumentList $remoteFolder
if ($folderExists -ne 'false') { LogAndExit -text "Unable to delete folder" }
LogAndOutput -text "Remote folder deleted $remoteFolder";
# ---- deploy
# -- build
DeleteAllFilesFromFolder -folderName $projectBuildPath
"dotnet publish $projectPath -c Release -o $projectBuildPath --force " | cmd
log "Publish completed, now gonna copy";
# -- create dir and copy it all to host
Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "mkdir $($args[0])";} -ArgumentList $remoteFolder
LogAndOutput -text "Remote dir $remoteFolder created";
#copy files
Copy-Item "$($projectBuildPath)\*" -ToSession $deploySession -Destination $remoteFolder -Recurse -Force ;
# give rights to folder
Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "chmod 777 $($args[0])";} -ArgumentList $remoteFolder
LogAndOutput -text "Given rights to $remoteFolder on remote host";
LogAndOutput -text "Finished data copy";
LogAndOutput -text "Deploy completed successfully";
Start-process -FilePath $operaExeFullPath -ArgumentList $siteUrl;
}
function DeployHtmlAboutmeSite([string] $_transactionId)
{
if($callType -eq 'plain')
{
# this is made to start transaction in a separate window
[string] $argList = "-file $scriptFileFullPath -transactionId $_transactionId -callType 'external-from-self' ";
log "starting new process with args: $argList"
Start-process -FilePath $pwshPath -ArgumentList $argList -PassThru;
return;
}
$script:deployAddress01
# session
$deploySession = New-PSSession -HostName $script:deployAddress01 -KeyFilePath "C:\Users\Admin\.ssh\id_rsa";
# delete folder
Invoke-Command -Session $deploySession -ScriptBlock { Invoke-Expression "sudo rm -r -f /var/www/www-root/data/www/aboutme.t109.tech";}
# check if folder deleted
[string] $folderExists = Invoke-Command -Session $deploySession -ScriptBlock { test-path "/var/www/www-root/data/www/aboutme.t109.tech"}
if ($folderExists -ne 'false') { throw; }
Copy-Item "C:\Develop\SiteData\aboutme.t109.tech" -ToSession $deploySession -Destination "/var/www/www-root/data/www/" -Recurse -Force ;
Start-process -FilePath $operaExeFullPath -ArgumentList "https://aboutme.t109.tech";
# copy files
# robocopy "C:\Develop\SiteData\aboutme.t109.tech" "//$($script:deployHost01)/var/www/www-root/data/www/" /mir /xd;
# CopyFilesToRemoteHostViaRoboCopy -folderSrc "C:\Develop\SiteData\aboutme.t109.tech" -hostTarget "$($script:deployHost01)" -folderTarget "/var/www/www-root/data/www/";
# PerformSecondsCountDown -seconds 10
}
function CopyFilesToRemoteHostViaRoboCopy([string] $folderSrc, [string] $hostTarget, [string] $folderTarget )
{
# this doesnt work
[string] $srcPath = $folderSrc.Replace(":", "$");
[string] $targetPath = "\\$($hostTarget)\$($folderTarget)".Replace(":", "$");
$netTarget = new-object -ComObject WScript.Network;
$netTarget.RemoveNetworkDrive('j:', $true);
$netTarget.MapNetworkDrive('j:', "$targetPath", $false);
log "Performing data robocopy from srcPath=$srcPath targetPath=$targetPath"
# robocopy C:\Develop\SiteData\aboutme.t109.tech //31.31.201.152/var/www/www-root/data/www/ *.* /E
}
#MENU
function ExecMenuItem([string] $menuItem) {
Log "Executing menu item_$($menuItem)_in operation script"
$ex = $menuItem;
if ($ex -eq "00") { }
#menubegin
elseif ($ex -eq "100101") { DeployApi01 -_transactionId $ex }
elseif ($ex -eq "100102") { DeployStore01 -_transactionId $ex }
elseif ($ex -eq "100110") { DeployHtmlAboutmeSite -_transactionId $ex}
elseif ($ex -eq "99") { $script:canExit = $true }
else {
"Wrong menu number, try again please";
}
#menuend
}
CheckLogsDirectory
CLearLogs
SetupCredentials
Log "Script started with params: transactionId=$transactionId, customWokingFolder=$customWokingFolder, callType=$callType"
if ($script:transactionId -ne '') {
Log "Gonna exec menu item, script:transactionId= $($script:transactionId)";
ExecMenuItem -menuItem $script:transactionId;
exit
}
DO {
PerformCls
ShowMenu
$ex = read-host 'Please enter menu point number'
ExecMenuItem -menuItem $ex
}
WHILE ($script:canExit -eq $false)