ps/Modules/Alkami.PowerShell.Database/Public/Get-AllAWSEnvironmentDataTenants.ps1

106 lines
4.1 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-AllAWSEnvironmentDataTenants {
<#
.SYNOPSIS
Build a tenant list from the given folder of the aws-environment-data repository. If not provided, assumed to be the current folder.
.DESCRIPTION
This function uses the aws-environment-data folder to parse for all the servers to go to to find the connection string.
It tries to spam this work out so we don't have to spend too much effort waiting on responses.
.PARAMETER RepositoryPath
Where to find the aws-environment-data folder
.PARAMETER FileFilter
Which files do we look for? This is used in the context of Get-ChildItem
.EXAMPLE
Get-AllAWSEnvironmentDataTenants -RepositoryPath C:\git\aws-environment-data -FileFilter "Dev\*.txt"
A long array of sites
.EXAMPLE
Get-AllAWSEnvironmentDataTenants
A long array of sites
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, Position = 0)]
[string]$RepositoryPath = ".",
[Parameter(Mandatory = $false, Position = 1)]
[string]$FileFilter = "*.txt"
)
$logLead = Get-LogLeadName
$RepositoryPath = Resolve-Path $RepositoryPath
Write-Host "$logLead : Fetching all paths from $RepositoryPath (filter $FileFilter) for parsing"
$fullPath = Join-Path $RepositoryPath $FileFilter
$files = (Get-ChildItem -Path $fullPath -Recurse -ErrorAction Ignore)
$allEnvServers = Invoke-Parallel -Objects $files -ReturnObjects -Script {
param($file)
# The next line wasn't working in my testing, so I went with the Path.GetFnameWoExt method below
# $envName = $file.BaseName
$envName = [System.IO.Path]::GetFilenameWithoutExtension($file.Name)
$rawServers = (Get-Content $file.FullName) -split ','
$testableServers = @()
$testableServers += Select-AlkamiMicServers $rawServers
$testableServers += Select-AlkamiAppServers $rawServers
if (!(Test-IsCollectionNullOrEmpty $testableServers.Count)) {
return @{
EnvName = $envName
Servers = $testableServers
# We use this to know if we should go to NewRelic for data or the database
# Some files are called "preprod" but are _not_ prod with synthetics
# We should only match if the parent path matches Prod (or maybe DR if that filter is used?)
IsProd = $file.Directory -match 'Prod'
}
}
}
$allConnections = Invoke-Parallel -Objects $allEnvServers -ReturnObjects -Script {
param($envServers)
$connectionString = ""
# iterate each server because if the first one gives us the result, stop there
# No need to go to four servers if one works
foreach($server in $envServers.Servers) {
$connectionString = Invoke-Command -ComputerName $server -ScriptBlock { Get-MasterConnectionString } -ErrorAction Ignore
if (![string]::IsNullOrWhitespace($connectionString)) {
break
}
}
if (![string]::IsNullOrWhitespace($connectionString)) {
return @{ EnvName = $envServers.EnvName; ConnectionString = $connectionString; IsProd = $envServers.IsProd; }
}
}
# Now that we have all of the connection strings, let's go get all the tenants from each environment:
$allTenants = Invoke-Parallel -Objects $allConnections -ReturnObjects -Script {
param($envConnectionString)
$errorString = ""
try {
Write-Verbose "Connecting to $($envConnectionString.ConnectionString)"
$tenants = @(Get-FullTenantListFromServer $envConnectionString.ConnectionString)
} catch {
$errorString = "Could not connect to client: $($envConnectionString.EnvName) error: $($_.Exception.Message)"
}
if (!(Test-IsCollectionNullOrEmpty $tenants) -or !([string]::IsNullOrWhitespace($errorString))) {
return @{ EnvName = $envConnectionString.EnvName; IsProd = $envConnectionString.IsProd; ConnectionString = $envConnectionString.ConnectionString; Tenants = $tenants; ErrorString = $errorString; }
}
}
return $allTenants
}