Files
lst_v3/scripts/updateServer.ps1
Blake Matthes cb00addee9
All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 2m27s
feat(admin): moved server build/update to full app
2026-04-21 07:36:04 -05:00

150 lines
4.4 KiB
PowerShell

param(
[string]$Server,
[string]$Destination,
[string]$Token,
[string]$ADM_USER,
[string]$ADM_PASSWORD,
[string]$AppDir
)
# $credFile = Join-Path $AppDir ".scriptCreds"
# $credData = @{}
# Get-Content $credFile | ForEach-Object {
# if ($_ -match "=") {
# $key, $value = $_ -split "=", 2
# $credData[$key.Trim()] = $value.Trim()
# }
# }
$username = $ADM_USER
$password = $ADM_PASSWORD
$securePass = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($username, $securePass)
function Update-Server {
param (
[string]$Destination,
[string]$Server,
[string]$Token
)
$buildFile = Join-Path $AppDir ".buildNumber"
$BuildNumber = 1
$BuildFolder = Join-Path $AppDir "builds"
if (Test-Path $BuildFile) {
$content = Get-Content $BuildFile | Select-Object -First 1
$num = $content.Trim() -as [int] # safe cast
if ($num) {
$BuildNumber = $num + 1
}
else {
$BuildNumber = 1
}
}
# Get The current Build we have zipped up
$BuildNumber = ([int]$BuildNumber - 1).ToString()
# copy the latest build over
Write-Host "Forcing the removal of the mapped drive."
Get-PSDrive -Name "z" -ErrorAction SilentlyContinue | Remove-PSDrive -Force
try {
New-PSDrive -Name "z" -PSProvider FileSystem -Root "\\$Server\$Destination" -Credential $credentials
# Create the update folder if it doesn't exist
if (-not (Test-Path -Path "\\$Server\$Destination")) {
New-Item -ItemType Directory -Path "\\$Server\$Destination" -Force
}
# Copying files to the server
Write-Host "Copying files to $($Server)"
$zipFile = Join-Path $BuildFolder "LSTV3-$BuildNumber.zip"
Copy-Item -Path $zipFile -Destination "z:\" -Force
Write-Host "Files copied to $($Server)"
}
catch {
Write-Host "Error: $_"
}
finally {
# Remove the mapped drive after copying
if (Get-PSDrive -Name "z" -ErrorAction SilentlyContinue) {
Write-Host "Removing mapped drive..."
Remove-PSDrive -Name "z"
}
}
Write-Host "Updating the app to LSTV3-$BuildNumber.zip"
# do the stop services, unzip, and restart service and pool
$AppUpdate = {
param ($Server, $Token, $Destination, $BuildFile)
#convert everything to the server fun
$LocalPath = $Destination -replace '\$', ':'
$BuildFileLoc = "$LocalPath\$BuildFile"
Write-Host "Updating the app to $($BuildFile)"
Write-Host "Stopping the services to do the updates, pkgs and db changes."
$app_name = "LSTV3_app$(if ($Token -eq "usiow2") { "_2" })"
# TODO: add in the iis reset later
Write-Host "Stopping $($app_name)"
Stop-Service -DisplayName $app_name -Force
Start-Sleep -Seconds 1
Write-Host "Unzipping the folder..."
try {
# Expand the archive
Expand-Archive -Path $BuildFileLoc -DestinationPath $LocalPath -Force
}
catch {
Write-Host "Error: $_"
exit 1 # Exit with a non-zero code if there's an error
}
# Delete the zip file after extraction
Write-Host "Deleting the zip file..."
Remove-Item -Path $BuildFileLoc -Force
Start-Sleep -Seconds 1
try {
# do the install/update
Push-Location $LocalPath
Write-Host "Running install/update in: $LocalPath"
npm install --omit=dev
Start-Sleep -Seconds 3
Write-Host "Install/update completed."
# do the migrations
# Push-Location $LocalPath
Write-Host "Running migrations"
npm run dev:db:migrate
Start-Sleep -Seconds 3
Write-Host "Migrations Completed."
}
catch {
Write-Host "Migration: $_"
}
finally {
Pop-Location
}
Write-Host "Starting $($app_name)"
Start-Service -DisplayName $app_name -ErrorAction Stop
Start-Sleep -Seconds 1
#Write-Host "Update completed on $($Server)-$($Token)"
}
Invoke-Command -ComputerName $Server -ScriptBlock $AppUpdate -ArgumentList $Server, $Token, $Destination, "LSTV3-$BuildNumber.zip" -Credential $credentials
}
Update-Server -Server $Server -Destination $Destination -Token $Token