ps/Modules/Alkami.PowerShell.Configuration/Public/Set-AlkamiConfigs.ps1

124 lines
6.4 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Set-AlkamiConfigs {
<#
.SYNOPSIS
Set web.config default value if not exist else copy from exising
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$enviroment,
[Parameter(Mandatory=$false)]
[String]$tempOrbPath = "C:\temp\Deploy\Orb\",
[Parameter(Mandatory=$false)]
[String]$orbPath
)
$logLead = (Get-LogLeadName);
if([string]::IsNullOrEmpty($orbPath)) {
$orbPath = (Get-OrbPath)
}
# Set Config
If((!(Test-Path $orbPath -PathType Container)) -and (Test-Path $tempOrbPath -PathType Container)) {
If(!(Test-Path $tempOrbPath -PathType Container)) {
Throw ("$logLead Folder not found '{0}'" -f $tempOrbPath)
}
$tempOrbfolders = Get-ChildItem -Directory $tempOrbPath -Exclude "Shared"
$XmlNodeValues = @(
@{NodeString = "//appSettings/add[@key='ShouldDisplayUncaughtExceptions']"; AttributeName = "value"; AttributeValue = "false" },
@{NodeString = "//system.web/compilation[@debug]"; AttributeName = "debug"; AttributeValue = "false" },
@{NodeString = "//httpErrors"; AttributeName = "errorMode"; AttributeValue = "DetailedLocalOnly"},
@{NodeString = "//authentication/forms[@cookieless]"; AttributeName = "cookieless"; AttributeValue = "UseCookies"},
@{NodeString = "//appSettings/add[@key='ForceUseOfProductionAssetsInDebug']"; AttributeName = "value"; AttributeValue = "true"},
@{NodeString = "//glimpse[@defaultRuntimePolicy]"; AttributeName = "defaultRuntimePolicy"; AttributeValue = "Off"},
@{NodeString = "//customErrors"; AttributeName = "mode"; AttributeValue = "RemoteOnly"}
)
$configfiles = Get-ChildItem $tempOrbfolders -Recurse -Include "new.web.config","new.Alkami.App.Nag.Host.Service.exe.config","new.Alkami.App.Radium.WindowsService.exe.config" | Select-Object name -ExpandProperty Fullname
foreach ($configfile in $configfiles) {
$files = Split-Path $configfile -Leaf
$files = $files -replace("new.","")
$folders = Split-Path $configfile -Parent
$newFiles = Join-Path -Path $folders -ChildPath $files
if (!(Test-Path -Path $newFiles)) {
Rename-Item "$configfile" "$newFiles"
}
}
foreach ($tempOrbfolder in $tempOrbfolders) {
$tempOrbConfig = Get-ChildItem $tempOrbfolder | Where-Object {$_.Name -match "^web.config|^Alkami.App.Radium.WindowsService.exe.config|^Alkami.App.Nag.Host.Service.exe.config"} | Select-Object -First 1
Write-Host ("$logLead : Updating Config File '{0}'" -f $tempOrbConfig.fullname)
[Xml]$doc = (Get-Content $tempOrbConfig.fullname)
foreach ($XmlNodeValue in $XmlNodeValues) {
Write-Host ("$logLead : Setting {0} to {1}" -f $XmlNodeValue.NodeString, $XmlNodeValue.AttributeValue)
Set-XmlNodeValue `
-NodeString $XmlNodeValue.NodeString `
-AttributeName $XmlNodeValue.AttributeName `
-AttributeValue $XmlNodeValue.AttributeValue |
Out-Null
}
# Remove diagnostics
$ChildNodes = $doc.SelectNodes("//system.diagnostics")
Write-Host ("$logLead : Removing System Diagnostics Child Nodes")
if ($ChildNodes) {
foreach($Child in $ChildNodes){
$Child.ParentNode.RemoveChild($Child)
}
}
$doc.Save($tempOrbConfig.fullname)
}
}
# Copy Config
If((Test-Path $orbPath -PathType Container) -and (Test-Path $tempOrbPath -PathType Container)){
Write-Host ("$logLead : Copying Configuration Values from {0} to {1}" -f $orbPath,$tempOrbPath)
$orbfolders = Get-ChildItem -Directory $tempOrbPath -Exclude "Shared"
$subfolders = Split-Path $orbfolders -Leaf -Resolve
foreach ($subfolder in $subfolders) {
$tempConfig = Get-ChildItem (Join-Path $tempOrbPath $subfolder) | where-Object {$_.Name -match "^web.config|^Alkami.App.Radium.WindowsService.exe.config|^Alkami.App.Nag.Host.Service.exe.config"}
$orbConfig = Get-ChildItem (Join-Path $orbPath $subfolder) | where-Object {$_.Name -match "^web.config|^Alkami.App.Radium.WindowsService.exe.config|^Alkami.App.Nag.Host.Service.exe.config"}
Write-Host ("$logLead : Setting node value from '{0}' to '{1}'" -f $orbConfig.FullName,$tempConfig.FullName)
[Xml]$orbXml = Get-Content $orbConfig.FullName
[Xml]$tempXml = Get-Content $tempConfig.FullName
$nodeStrings = ("//appSettings","//quartz","//glimpse")
foreach ($nodeString in $nodeStrings) {
$orb = $orbXml.SelectSingleNode($nodeString)
$temp = $tempXml.SelectSingleNode($nodeString)
foreach ($tempNode in $temp.ChildNodes) {
if ($tempNode.NodeType -ne "Comment") {
$orbKey = $orb.ChildNodes| Where-Object {$_.Key -eq $tempNode.Key} | Select-Object -First 1
if($orbKey.key) {
Write-Host ("$logLead : Setting Node '{0}' to value '{1}'. Default value was: '{2}" -f $orbKey.Key, $orbKey.Value, $tempNode.Value)
$tempNode.value = $orbKey.value
}
else
{
Write-Host ("$logLead : Key {0} Not Present in the New File -- It Will be Dropped. Previous value was: {1}" -f $orbKey.Key, $orbKey.Value)
}
}
}
}
$tempXml.Save($tempConfig.FullName)
}
}
# Update New Relic application names.
if(Test-Path $tempOrbPath)
{
$configFiles = @();
$configFiles += Get-ConfigurationFiles -stagedFilePath $tempOrbPath -findTempFiles $true;
$configFiles += Get-ConfigurationFiles -stagedFilePath $tempOrbPath -findTempFiles $false;
Write-Verbose ("$logLead : Calling Set-NewRelicAppName With Environment Key '{0}'" -f $enviroment);
Set-NewRelicAppName -enviroment $enviroment -configFiles $configfiles;
}
}