Private/TenantUtil.ps1
$global:NameDelimiter = " " function Get-ServiceNameFromTenant($Tenant) { return $TenantPrefix + $Tenant } function Get-ServiceDisplayNameFromTenant($Tenant) { return $TenantPrefix + $NameDelimiter + $Tenant } function Get-ServicePath { return "$env:ProgramFiles\$TenantPrefix" } function Get-WebAppNameFromTenant($Tenant) { return $TenantPrefix + $Tenant } function Get-SiteNameFromTenant($Tenant) { return $TenantPrefix + $NameDelimiter + $Tenant + " Site" } function Get-PoolNameFromTenant($Tenant) { return $TenantPrefix + $NameDelimiter + $Tenant + " Pool" } function Get-DatabaseNameFromTenant($Tenant) { return $TenantPrefix + $Tenant } function Get-ServerPathFromTenant($Tenant) { $servicePath = Get-ServicePath return "$servicePath\$Tenant" } function Get-LegacyRolePath { return "$env:ProgramData\$TenantPrefix\PlannerOneInstall.config" } function Get-LegacyDBConfigPath($Tenant) { return "$env:ProgramData\$TenantPrefix\$Tenant\database.config" } function Get-TenantsPath { return "$env:ProgramData\$TenantPrefix\Tenants\" } function Get-AdapterConfigPathFromTenant($Tenant) { return "$env:ProgramData\$TenantPrefix\$Tenant\Config\Adapter\" } function Get-UIParamPathFromTenant($Tenant) { return "$env:ProgramData\$TenantPrefix\$Tenant\Config\GUI\" } function Get-CriteriaPathFromTenant($Tenant) { return "$env:ProgramData\$TenantPrefix\$Tenant\Config\Criteria\" } function Get-LocalStorePathFromTenant($Tenant) { return "$env:ProgramData\$TenantPrefix\$Tenant\" } function Get-EnvironmentsPathFromTenant($Tenant) { return "$env:ProgramData\$TenantPrefix\$Tenant\Environments\" } function Get-WebConfigPathFromTenant($Tenant) { return "$env:ProgramData\$TenantPrefix\$Tenant\Config\Web\" } function Get-ConfigPathFromTenant($Tenant) { return "$env:ProgramData\$TenantPrefix\$Tenant\Config\" } function Get-LogPathFromTenant($Tenant) { return "$env:ProgramData\$TenantPrefix\$Tenant\Logs\" } function Register-Tenant($Tenant, $RemotingPort, $WebServicePort, $WebAppName, $SitePort, $Adapter, [string] $BinPath) { if ($BinPath -eq "") { $serverPath = ServerPathFromTenant $Tenant } else { $serverPath = $BinPath } $serviceName = Get-ServiceNameFromTenant $Tenant if (!$WebAppName) { $WebAppName = Get-WebAppNameFromTenant $Tenant } $tenantInfo = @{ Role = $Tenant; Path = $serverPath; WebApplicationName = $WebAppName; SitePort = $SitePort; RemotingPort = $RemotingPort; WebServicePort = $WebServicePort; WindowsServiceName = $serviceName; WebHost = "localhost"; ServiceHost = "localhost"; Adapter = $Adapter; } Write-TenantInfo $Tenant $tenantInfo } function Write-TenantInfo($Tenant, $tenantInfo) { $data = $tenantInfo | ConvertTo-Json $storePath = Get-TenantsPath if (!(Test-Path $storePath)) { mkdir $storePath | Out-Null } $storePath = $storePath + $Tenant + ".json" [System.IO.File]::WriteAllText($storePath, $data) Write-Verbose "Tenant registered in $storePath" } function Remove-Tenant($Tenant, [switch]$RemoveData) { $storePath = Get-TenantsPath $storePath = $storePath + $Tenant + ".json" Write-Verbose "Removing tenant configuration $storePath" remove-item $storePath $dataPath = Get-LocalStorePathFromTenant $Tenant if ($RemoveData.IsPresent) { Write-Verbose "Removing data stored in $dataPath ..." Remove-Item $dataPath -Recurse -Force } else { Write-Verbose "Keeping data stored in $dataPath" } } function Get-TenantList() { $path = Get-TenantsPath Get-ChildItem $path -Filter *.json | format-table -property @{n="Tenant";e={$_.basename}} } function Get-TenantInfo($Tenant) { $storePath = Get-TenantsPath $path = $storePath + $Tenant + ".json" Write-Verbose "Reading $path" if ([System.IO.File]::Exists($path)) { $json = [system.IO.File]::ReadAllText($path) $tenant = $json | ConvertFrom-Json return $tenant } else { return "Tenant $Tenant does not exist" } } function Test-Tenant($Tenant) { $storePath = Get-TenantsPath $path = $storePath + $Tenant + ".json" return Test-Path $path } function Modify-TenantInfo([string] $Tenant, [int] $RemotingPort, [int] $WebServicePort, [int] $WebPort, [string] $WebAppName) { $tenantInfo = Get-TenantInfo $Tenant if ($RemotingPort -ne 0) { $tenantInfo.RemotingPort = $RemotingPort } if ($WebServicePort -ne 0) { $tenantInfo.WebServicePort = $WebServicePort } if ($WebPort -ne 0) { $tenantInfo.SitePort = $WebPort } if ($WebAppName -ne "") { $tenantInfo.WebApplicationName = $WebAppName } Write-TenantInfo $Tenant $tenantInfo } function Write-P1AdapterParametersList() { Write-Section "Adapter parameter list" foreach($parameter in $AdapterParameters) { Write-Output $parameter } } function Restart-Tenant($webApp, $tenantInfo) { Stop-P1ServiceInternal $tenantInfo $poolName = $webApp.applicationPool Write-Verbose "Restarting web application $poolName" Restart-WebAppPool "$poolName" Start-P1ServiceInternal $tenantInfo } function Stop-P1ServiceInternal($tenantInfo) { $serviceName = $tenantInfo.WindowsServiceName Write-Verbose "Stopping $serviceName" Stop-Service "$serviceName" } function Start-P1ServiceInternal($tenantInfo) { $serviceName = $tenantInfo.WindowsServiceName Write-Verbose "Starting $serviceName" Start-Service "$serviceName" } function Stop-P1WebInternal($webApp) { $poolName = $webApp.applicationPool Write-Verbose "Stopping web application $poolName" Stop-WebAppPool "$poolName" } function Start-P1WebInternal($webApp) { $poolName = $webApp.applicationPool Write-Verbose "Starting web application $poolName" Start-WebAppPool "$poolName" } function Test-ERPName($ERP) { [bool]$valid = $false foreach($erpName in $ERPS) { if ($ERP -eq $erpName) { $valid = $true break } } return $valid } function Write-ERPNames() { Write-Output "Valid ERP names:" foreach($erpName in $ERPS) { Write-Output $erpName } } function Get-AdapterStatus($wsPort) { Write-Output "Production Scheduler adapter status:" Get-ProductAdapterStatus $wsPort "adapter" Write-Output "Resoure Planner adapter status:" Get-ProductAdapterStatus $wsPort "adapterproject" } function Get-ProductAdapterStatus($wsPort, $service) { $url = "http://localhost:$wsPort/services/$service/nav/status" write-verbose "url: $url" $statusResp = Invoke-RestMethod -Method GET -Uri $url -UseDefaultCredentials Write-AdapterStatus $statusResp } function Write-AdapterStatus($statusResp) { $global = $statusResp.Global $status = $statusResp.Status Write-Output "Global: $global" foreach ($kvp in $statusResp.Environments) { $envName = $kvp.Key.Name $envGroup = $kvp.Key.PlanningGroupContainer $envStatus = $kvp.Value Write-Output "$envName - $envGroup`: $envStatus" } if ($status -eq "Ready") { Write-OK "Status: $status" } else { Write-KO "Status: $status" } } |