diff --git a/.gitignore b/.gitignore index f8050a9..6e195d1 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,9 @@ lstWrapper/obj lstWrapper/publish testScripts .build +.controller-build builds +controllerBuilds # ignoring the old app that will be built into this one to make deploying faster and more easy as we do the migration lstV2 @@ -186,3 +188,8 @@ go.work.sum lstWrapper/Program_working_node_ws.txt lstWrapper/web.config.txt +controller/-docker-compose.yml +controller/docker-compose.yml +controller/Dockerfile +controller/Dockerfile-ignore +controller/docker-compose.yml diff --git a/.includeControls b/.includeControls new file mode 100644 index 0000000..ef4852a --- /dev/null +++ b/.includeControls @@ -0,0 +1,7 @@ +lstWrapper/publish +controller/lst_ctl.exe +scripts/update-controller-bumpBuild.ps1 +scripts/update-controller-server.ps1 +scripts/update-controller-zip.ps1 +scripts/update-controllers.ps1 +scripts/services.ps1 \ No newline at end of file diff --git a/scripts/update-controller-bumpBuild.ps1 b/scripts/update-controller-bumpBuild.ps1 new file mode 100644 index 0000000..2e0128d --- /dev/null +++ b/scripts/update-controller-bumpBuild.ps1 @@ -0,0 +1,49 @@ +function Bump-Build { +param ( + [string]$AppRoot, + [int]$KeepLast = 5 # number of builds to keep, default 5 +) + +$BuildFile = Join-Path $AppRoot ".controller-build" +$BuildFolder = Join-Path $AppRoot "controllerBuilds" + +# Default to 1 +$BuildNumber = 1 + + +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 + } +} + +# Convert to string +$BuildNumber = $BuildNumber.ToString() + +# Save new build number +Set-Content -Path $BuildFile -Value $BuildNumber + +Write-Output "New Build Number: $BuildNumber" + +# --- Cleanup old builds --- +if (Test-Path $BuildFolder) { + # Get all zip files in build folder matching pattern + $zips = Get-ChildItem -Path $BuildFolder -Filter "controllers-*.zip" | + Sort-Object LastWriteTime -Descending + + # Keep only the newest $KeepLast, remove the rest + if ($zips.Count -gt $KeepLast) { + $toRemove = $zips[$KeepLast..($zips.Count - 1)] + foreach ($zip in $toRemove) { + Remove-Item $zip.FullName -Force + Write-Output "Removed old build: $($zip.Name)" + } + } +} + +} \ No newline at end of file diff --git a/scripts/update-controller-server.ps1 b/scripts/update-controller-server.ps1 new file mode 100644 index 0000000..31e7dcc --- /dev/null +++ b/scripts/update-controller-server.ps1 @@ -0,0 +1,142 @@ +function Update-Server { + param ( + [string]$ADM_USER, + [string]$ADM_PASS, + [string]$AppRoot, + [string]$Destination, + [string]$Server, + [string]$Token + + ) + + $BuildFile = Join-Path $AppRoot ".controller-build" + $BuildFolder = Join-Path $AppRoot "controllerBuilds" + + $securePass = ConvertTo-SecureString $ADM_PASS -AsPlainText -Force + $credentials = New-Object System.Management.Automation.PSCredential($ADM_USER, $securePass) + + + # Default to 1 + $BuildNumber = 1 + + + 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 = $BuildNumber - 1 + + # Convert to string + $BuildNumber = $BuildNumber.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 "controllers-$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" + } + } + + # do the stop services, unzip, and restart service and pool + +$ControllerUpdate = { + param ($Server, $Token, $Destination, $BuildFile) + + + write-host "Running the update process now" + # Change the destination to be local + $LocalPath = $Destination -replace '\$', ':' + $BuildFileLoc = "$LocalPath\$BuildFile" + + # where is nssm.exe + $nssmPath= Join-Path $LocalPath "nssm.exe" + $npmPath = "C:\Program Files\nodejs\npm.cmd" + + Write-Host "Stopping the services to do the updates, pkgs and db changes." + + $Controller = "LST_ctl$(if ($Token -eq "usiow2") { "_2" })" + $Wrapper = "LogisticsSupportTool$(if ($token -eq "usiow2") { "_2" })" + + Write-Host "Stopping $($Controller)" + Stop-Service -DisplayName $Controller -Force + Start-Sleep -Seconds 1 + + try { + Stop-WebAppPool -Name $Wrapper -ErrorAction Stop + + Start-Sleep -Seconds 2 + $state = (Get-WebAppPoolState -Name $Wrapper).Value + Write-Host "Result: $state" + } catch { + Write-Error $_.Exception.Message + } + + + Write-Host "Unzipping the folder..." + write-host $BuildFileLoc + write-host $LocalPath + + # Extract the files to the build path + 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 + + Write-Host "Starting $($Controller)" + Start-Service -DisplayName $Controller + Start-Sleep -Seconds 1 + + try { + Start-WebAppPool -Name $Wrapper + Start-Sleep -Seconds 2 + $state = (Get-WebAppPoolState -Name $Wrapper).Value + Write-Host "Result: $state" + } catch { + Write-Error $_.Exception.Message + exit 1 + } + + write-host "Controllers updated and restarted :D" + + } + + Invoke-Command -ComputerName $Server -ScriptBlock $ControllerUpdate -ArgumentList $Server, $Token, $Destination, "controllers-$BuildNumber.zip" -Credential $credentials +} diff --git a/scripts/update-controller-zip.ps1 b/scripts/update-controller-zip.ps1 new file mode 100644 index 0000000..5272200 --- /dev/null +++ b/scripts/update-controller-zip.ps1 @@ -0,0 +1,60 @@ +function Zip-Includes { + param ( + [string]$IncludesFile, + [string]$BuildNumber, + [string]$BuildFolder, + [string]$AppRoot + ) + + # Read all paths from .includes + $itemsToZip = @() + Get-Content $IncludesFile | ForEach-Object { + $relPath = $_.Trim() + if ($relPath -ne "") { + $fullPath = Join-Path $AppRoot $relPath + if (Test-Path $fullPath) { + $itemsToZip += [PSCustomObject]@{ + FullPath = $fullPath + RelativePath = $relPath + } + } else { + Write-Warning "Path not found: $fullPath" + } + } + } + + if (-Not $itemsToZip) { + Write-Warning "No valid files or folders to zip." + return + } + + $zipFileName = Join-Path $BuildFolder "controllers-$BuildNumber.zip" + + if (Test-Path $zipFileName) { + Remove-Item $zipFileName + } + + # Create zip preserving relative paths + $tempFolder = Join-Path $env:TEMP "zip-temp-$BuildNumber" + if (Test-Path $tempFolder) { Remove-Item $tempFolder -Recurse -Force } + New-Item -Path $tempFolder -ItemType Directory | Out-Null + + foreach ($item in $itemsToZip) { + $dest = Join-Path $tempFolder $item.RelativePath + $destDir = Split-Path $dest + if (-Not (Test-Path $destDir)) { New-Item -Path $destDir -ItemType Directory | Out-Null } + + if ((Get-Item $item.FullPath).PSIsContainer) { + Copy-Item -Path $item.FullPath -Destination $dest -Recurse + } else { + Copy-Item -Path $item.FullPath -Destination $dest + } + } + + Compress-Archive -Path (Join-Path $tempFolder "*") -DestinationPath $zipFileName + + # Cleanup temp folder + Remove-Item $tempFolder -Recurse -Force + + Write-Output "Created zip: $zipFileName" +} diff --git a/scripts/update-controllers.ps1 b/scripts/update-controllers.ps1 new file mode 100644 index 0000000..f9f2051 --- /dev/null +++ b/scripts/update-controllers.ps1 @@ -0,0 +1,111 @@ +param ( + [string]$App_Path = "C:\Users\matthes01\Documents\lst", # with this it makes it optional to pass a new parameter over. + [string]$Server = "usmcd1vms036", + [string]$Token = "test3", + [string]$Remote_Path = "E$\LST" +) + +# imports of the other functions +. ".\update-controller-zip" +. ".\update-controller-bumpBuild" +. ".\update-controller-server" + +# example string to pass over, you must be in the script dir when you run this script. or it will fail to find the linked scripts +# .\update-controllers.ps1 -App_Path "C:\Users\matthes01\Documents\lst" -Server "usmcd1vms036" -Token "test3" -Remote_Path "E$\LST" + +$EnvFile = Join-Path $App_Path ".env" +if (-Not (Test-Path $EnvFile)) { + Write-Error "File not found: $EnvFile" + exit 1 +} + +# Read and parse the .env file into a dictionary +$envDict = @{} + +Get-Content $EnvFile | ForEach-Object { + if ($_ -match "^\s*#") { return } # skip comments + if ([string]::IsNullOrWhiteSpace($_)) { return } # skip blank lines + $parts = $_ -split '=', 2 + if ($parts.Count -eq 2) { + $envDict[$parts[0].Trim()] = $parts[1].Trim() + } +} + +# Read the .env file and set each key as a variable +Get-Content $EnvFile | ForEach-Object { + if ($_ -match "^\s*#") { return } # skip comments + if ([string]::IsNullOrWhiteSpace($_)) { return } # skip blank lines + + $parts = $_ -split '=', 2 + if ($parts.Count -eq 2) { + $key = $parts[0].Trim() + $value = $parts[1].Trim() + + # Set as PowerShell variable + Set-Variable -Name $key -Value $value -Scope Global + } +} + +# create the new zip build. +# --------------------------------------------------------------------------------- +#check it file is there +$BuildNumber = "1" +$buildfile = Join-Path $App_Path ".controller-build" + +if (-Not (Test-Path $buildfile)) { + Write-Error "File not found: $buildfile, creating it." + New-Item -Path $buildfile -ItemType File | Out-Null + $BuildNumber = "1" + Set-Content -Path $BuildFile -Value $BuildNumber +} +else { + $content = Get-Content $BuildFile -Raw + if (![string]::IsNullOrWhiteSpace($content)) { + $BuildNumber = $content.Trim() + } + +} + + +# do the same check on the .includeControls file +$ControlIncludes = Join-Path $App_Path ".includeControls" +if (-Not (Test-Path $ControlIncludes)) { + Write-Error "File not found: $ControlIncludes, exiting as we dont want to zip up the entire app." + exit 1 +} + + +# last one to make sure we have is the build folder +$BuildFolder = Join-Path $App_Path "controllerBuilds" +if (-Not (Test-Path $BuildFolder)) { + write-host "Build folder missing creating it" + New-Item -Path $BuildFolder -ItemType Directory | Out-Null +} + +# now we can create the zip +Zip-Includes -IncludesFile $ControlIncludes -BuildNumber $BuildNumber -BuildFolder $BuildFolder -AppRoot $App_Path +Start-Sleep -Seconds 3 +#------------------------------------------------------------------------------------ + +# Checking to make sure the server is up and online +Write-Output "Checking if $($Token) is online to update." +$pingResult = Test-Connection -ComputerName $Server -Count 2 -Quiet + +if (-not $pingResult) { + Write-Output "Server $($Server), token $($Token) is NOT reachable. Exiting script." + exit 1 # Terminate the script with a non-zero exit code +} + +Write-Output "Server $($Server), token $($Token) is online." + + +# now that the server is up lets update the server with the current build +# do the update to the plant. +Update-Server -ADM_USER $ADM_USER -ADM_PASS $ADM_PASS -AppRoot $App_Path -Destination $Remote_Path -Server $Server -Token $Token + +# bump the build number +$BuildNumber = Bump-Build -AppRoot $App_Path + + +# function to create the zip controller-1.zip +