From fb090eb2c8eb7d3cdab05a8e334547d2c96ec013 Mon Sep 17 00:00:00 2001 From: iivanov Date: Tue, 23 Jul 2024 16:05:04 +0300 Subject: [PATCH] fix: stabilizaiton fixes - Fixed issues in the WLD Startup/Shutdown sequence. - Polished messages. - Added TODOs - Removed unused code - Implemented various fixes: variable reuse, log file generation, remove wrong "multi-cluster" implementation - Updated changelog, dependencies, documentation Signed-off-by: Ivaylo Ivanov --- CHANGELOG.md | 11 +++-- .../PowerManagement-ManagementDomain.ps1 | 49 ++++++++++--------- VMware.CloudFoundation.PowerManagement.psd1 | 10 ++-- VMware.CloudFoundation.PowerManagement.psm1 | 8 +-- docs/snippets/import-module-local-linux.ps1 | 6 +-- docs/snippets/import-module.ps1 | 4 -- docs/snippets/install-module.ps1 | 6 +-- docs/snippets/save-module-local-linux.ps1 | 6 +-- docs/snippets/save-module-local-windows.ps1 | 6 +-- 9 files changed, 53 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d2314cb..3967a9d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,23 +9,26 @@ Bugfix: - Added support to `PowerManagement-ManagmentDomain.ps1` and `PowerManagement-WorkloadDomain.ps1` for vSAN File Services to be excluded from customer virtual machines. [GH-95](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/95) - Added exit to Tanzu Code in `PowerManagement-WorkloadDomain.ps1`[GH-97](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/97) - Added support to `PowerManagement-ManagmentDomain.ps1` and `PowerManagement-WorkloadDomain.ps1` for testing ESXi host connection before SSH connection. [GH-98](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/98) +- Fix for issue [#101](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/issues/101)[GH-106](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/106) +- Fix for issue [#104](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/issues/104)[GH-106](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/106) Enhancement: -- Added check for VMware Aria Operations for Logs deployment and shutdown if it exists. [GH-87](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/87) - Added check to verify if workload domain vCenter Server instances are still powered on before starting the shutdown of the management domain to `PowerManagement-ManagmentDomain.ps1`. [GH-90](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/90) -- Added check for virtual machines running on an NSX overlay segment to `PowerManagement-ManagmentDomain.ps1`. [GH-86](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/86) -- Added support for more than one cluster in management domain for shut down and start up to `PowerManagement-ManagmentDomain.ps1`. [GH-93](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/93) +- Various improvements in workflows, log messages, file syntax. [GH-106](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/106) and [GH-107](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/107) Refactor: - Replaced the use of `Test-NetConnection` with `Test-EndpointConnection` from the PowerShell module dependency `PowerValidatedSolutions`. [GH-85](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/85) - Updated `PowerManagement-ManagmentDomain.ps1` to allow for FQDN or IP address of SDDC Manager. [GH-92](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/92) +- Removed obsolete and not used code in the sample scripts. +- Apply common formatting for the files. [GH-107](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/107) Chore: -- Updated `PowerVCF` from v2.3.0 to v2.4.0. [GH-85](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/85) +- Updated `PowerVCF` from v2.3.0 to v2.4.1. [GH-108](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/108) - Added `PowerValidatedSolutions` v2.8.0 as a PowerShell module dependency. [GH-38](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/38) +- Updated `PowerValidatedSolutions` from v2.8.0 to 2.11.0. [GH-108](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/108) - Updated `Write-PowerManagementLogMessage` to set color for message types. This will allow for all references to use color based on function. [GH-89](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/89) ## v1.4.0 diff --git a/SampleScripts/PowerManagement-ManagementDomain.ps1 b/SampleScripts/PowerManagement-ManagementDomain.ps1 index c4c7eef7..e99f0cb5 100644 --- a/SampleScripts/PowerManagement-ManagementDomain.ps1 +++ b/SampleScripts/PowerManagement-ManagementDomain.ps1 @@ -76,8 +76,9 @@ Function Get-Password { #EndRegion Non Exported Functions ###### ########################################################################## - -$pass = Get-Password -User $user -Password $pass +if(($PSBoundParameters.ContainsKey("shutdown") -or $PSBoundParameters.ContainsKey("genJson"))) { + $pass = Get-Password -User $user -Password $pass +} # Error Handling (script scope function) Function Debug-CatchWriterForPowerManagement { @@ -157,10 +158,6 @@ Try { Exit } -# Temp workaround for a "$pass" variable reuse in the script -# TODO - fix usage of "pass" variable in the script -$sddcManagerPassword = $pass - # Shutdown procedure and json generation if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKey("genjson")) { Try { @@ -361,7 +358,6 @@ if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKe $cluster = Get-VCFCluster | Where-Object { $_.id -eq ($workloadDomain.clusters.id) } - >>>(bug:Missing command) $var = @{} $var["Domain"] = @{} $var["Domain"]["name"] = $workloadDomain.name @@ -435,13 +431,13 @@ if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKe $esxi_block["name"] = $esxDetails.name $esxi_block["fqdn"] = $esxDetails.fqdn $esxi_block["user"] = $esxDetails.username - $Pass = $esxDetails.password - if ($Pass) { - $PassEncrypted = $Pass | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString + $esxiPass = $esxDetails.password + if ($esxiPass) { + $esxiPassEncrypted = $esxiPass | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString } else { - $PassEncrypted = $null + $esxiPassEncrypted = $null } - $esxi_block["password"] = $Pass_encrypted + $esxi_block["password"] = $esxiPassEncrypted $var["Hosts"] += $esxi_block } @@ -458,10 +454,14 @@ if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKe } $nsxtManagerVIP | Add-Member -Type NoteProperty -Name adminPassword -Value $nsxmgrPass $nsxtNodesFQDN = $nsxtCluster.nodes.fqdn + $nsxtManagerVIP | Add-Member -Type NoteProperty -Name adminPassword -Value $nsxmgrPass + $nsxtNodesFQDN = $nsxtCluster.nodes.fqdn $nsxtNodes = @() + foreach ($node in $nsxtNodesFQDN) { foreach ($node in $nsxtNodesFQDN) { [Array]$nsxtNodes += $node.Split(".")[0] [Array]$vcfVMs += $node.Split(".")[0] + [Array]$vcfVMs += $node.Split(".")[0] } $var["NsxtManager"] = @{} $var["NsxtManager"]["vipfqdn"] = $nsxtManagerFQDN @@ -471,6 +471,7 @@ if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKe # Gather NSX-T Edge Node Details $nsxtManagerPowerOnVMs = 0 + $nsxtManagerPowerOnVMs = 0 foreach ($nsxtManager in $nsxtNodes) { $state = Get-VMsWithPowerStatus -powerstate "poweredon" -server $vcServer.fqdn -user $vcUser -pass $vcPass -pattern $nsxtManager -exactMatch -silence if ($state) { $nsxtManagerPowerOnVMs += 1 } @@ -483,12 +484,11 @@ if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKe Write-PowerManagementLogMessage -Type WARNING -Message "NSX Manager VMs have been stopped. NSX Edge VMs will not be handled automatically." } else { Try { - Write-Output "adminpssword: $nsxtManagerVIP.adminPassword" Write-PowerManagementLogMessage -Type INFO -Message "NSX Manager VMs are in running state. Trying to fetch information about the NSX Edge VMs..." [Array]$edgeNodes = (Get-EdgeNodeFromNSXManager -server $nsxtManagerFQDN -user $nsxtManagerVIP.adminUser -pass $nsxtManagerVIP.adminPassword -VCfqdn $VcServer.fqdn) $edgeNodesToString = $edgeNodes -join "," Write-PowerManagementLogMessage -Type INFO -Message "The NSX Edge VMs are $edgeNodesToString." - } Catch { + } catch { Write-PowerManagementLogMessage -Type ERROR -Message "Something went wrong! Cannot fetch NSX Edge nodes information from NSX Manager '$nsxtManagerFQDN'. Exiting!" } } @@ -593,8 +593,6 @@ if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKe } if (Test-VCFConnection -server $server) { - # TODO - remove this temp fix for multiple useage of "$pass" - $pass = $sddcManagerPassword if (Test-VCFAuthentication -server $server -user $user -pass $pass) { $allWorkloadvCenters = @() $allWorkloadvCenters = (Get-VCFWorkloadDomain | Select-Object type -ExpandProperty vcenters | Where-Object { $_.type -eq "VI" }).fqdn @@ -634,8 +632,8 @@ if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKe } Write-PowerManagementLogMessage -Type INFO -Message "Fetching all powered on vSAN File Services virtual machines from vCenter Server instance $($vcenter)..." - [Array]$vsanfsvms += Get-VMsWithPowerStatus -powerstate "poweredon" -server $vcServer.fqdn -user $vcUser -pass $vcPass -pattern "(vSAN File)" -silence - foreach ($vm in $vsanfsvms) { + [Array]$vsanFsVMs += Get-VMsWithPowerStatus -powerstate "poweredon" -server $vcServer.fqdn -user $vcUser -pass $vcPass -pattern "(vSAN File)" -silence + foreach ($vm in $vsanFsVMs) { [Array]$vcfVMs += $vm } @@ -740,9 +738,12 @@ if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKe foreach ($segment in $nsx_segments) { $segmentName = $segment.display_name $cloudVMs = Get-VM | Get-NetworkAdapter | Where-Object { $_.NetworkName -eq $segmentName } | Select-Object Parent + $cloudVMs = Get-VM | Get-NetworkAdapter | Where-Object { $_.NetworkName -eq $segmentName } | Select-Object Parent } $vmList = $cloudVMs.Parent + $vmList = $cloudVMs.Parent $stopExecuted = $false + foreach ($vm in $vmList) { foreach ($vm in $vmList) { $vmName = $vm.Name $powerState = $vm.PowerState @@ -924,13 +925,14 @@ if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKe $counter = 0 $sleepTime = 60 # in seconds - - while ($counter -lt 1800) { + $maxSecondsToWait = 1800 # 30 minutes + #TODO - Add better reporting which hosts are still up + while ($counter -lt $maxSecondsToWait) { $successCount = 0 #Verify if all ESXi hosts are down in here to conclude End of Shutdown sequence foreach ($esxiNode in $esxiWorkloadDomain) { if (Test-EndpointConnection -server $esxiNode.fqdn -Port 443) { - Write-PowerManagementLogMessage -Type WARNING -Message "Some hosts are still up. Sleeping for 60 seconds before next check..." + Write-PowerManagementLogMessage -Type WARNING -Message "Some hosts are still up. '$($esxiNode.fqdn)' responded. Sleeping for 60 seconds before next check..." break } else { $successCount++ @@ -1206,6 +1208,7 @@ if ($PsBoundParameters.ContainsKey("startup")) { #Startup the SDDC Manager Virtual Machine in the Management Workload Domain Start-CloudComponent -server $vcServer.fqdn -user $vcUser -pass $vcPass -nodes $sddcmVMName -timeout 600 + # TODO - Add check for SDDC Manager status # Startup the NSX Manager Nodes in the Management Workload Domain Start-CloudComponent -server $vcServer.fqdn -user $vcUser -pass $vcPass -nodes $nsxtNodes -timeout 600 @@ -1371,10 +1374,10 @@ if ($PsBoundParameters.ContainsKey("startup")) { Write-PowerManagementLogMessage -Type INFO -Message "Use the following command to automatically start VMs" Write-PowerManagementLogMessage -Type INFO -Message "Start-CloudComponent -server $($vcServer.fqdn) -user $vcUser -pass $vcPass -nodes -timeout 600" if ([float]$vcfVersion -lt [float]4.5) { - Write-PowerManagementLogMessage -Type INFO -Message "If you have enabled SSH for the ESXi hosts in management domain, disable it at this point." + Write-PowerManagementLogMessage -Type WARNING -Message "If you have enabled SSH for the ESXi hosts in management domain, disable it at this point." } if ([float]$vcfVersion -gt [float]4.4) { - Write-PowerManagementLogMessage -Type INFO -Message "If you have disabled lockdown mode for the ESXi hosts in management domain, enable it back at this point." + Write-PowerManagementLogMessage -Type WARNING -Message "If you have disabled lockdown mode for the ESXi hosts in management domain, enable it back at this point." } Write-PowerManagementLogMessage -Type INFO -Message "##################################################################################" Write-PowerManagementLogMessage -Type INFO -Message "End of the startup sequence!" diff --git a/VMware.CloudFoundation.PowerManagement.psd1 b/VMware.CloudFoundation.PowerManagement.psd1 index 63774440..e337a342 100644 --- a/VMware.CloudFoundation.PowerManagement.psd1 +++ b/VMware.CloudFoundation.PowerManagement.psd1 @@ -11,7 +11,7 @@ RootModule = 'VMware.CloudFoundation.PowerManagement.psm1' # Version number of this module. - ModuleVersion = '1.4.1.1010' + ModuleVersion = '1.4.1.2000' # Supported PSEditions # CompatiblePSEditions = @() @@ -32,7 +32,7 @@ Description = 'PowerShell Module for VMware Cloud Foundation Power Management' # Minimum version of the Windows PowerShell engine required by this module - PowerShellVersion = '7.2.0' + PowerShellVersion = '7.4.0' # Name of the Windows PowerShell host required by this module # PowerShellHostName = '' @@ -53,7 +53,7 @@ RequiredModules = @( @{ ModuleName = 'VMware.PowerCLI' - ModuleVersion = '12.5.0' + ModuleVersion = '13.3.0' } @{ ModuleName = 'Posh-SSH' @@ -61,11 +61,11 @@ } @{ ModuleName = 'PowerVCF' - ModuleVersion = '2.4.0' + ModuleVersion = '2.4.1' } @{ ModuleName = 'PowerValidatedSolutions' - ModuleVersion = '2.8.0' + ModuleVersion = '2.11.0' } ) diff --git a/VMware.CloudFoundation.PowerManagement.psm1 b/VMware.CloudFoundation.PowerManagement.psm1 index 0a03b014..88b110fe 100644 --- a/VMware.CloudFoundation.PowerManagement.psm1 +++ b/VMware.CloudFoundation.PowerManagement.psm1 @@ -654,7 +654,7 @@ Function Set-VsanClusterPowerStatus { $cluster = Get-Cluster $clustername - + # TODO - Add check if there is task ID returned $powerActionTask = $vsanClusterPowerSystem.PerformClusterPowerAction($cluster.ExtensionData.MoRef, $spec) $task = Get-Task -Id $powerActionTask $counter = 0 @@ -2211,7 +2211,6 @@ Function Get-EdgeNodeFromNSXManager { } else { Write-PowerManagementLogMessage -Type ERROR -Message "Connection to '$server' has failed. Check the console output for more details." } - } else { Write-PowerManagementLogMessage -Type ERROR -Message "Connection to '$server' has failed. Check your environment and try again" } @@ -2423,10 +2422,13 @@ Function Write-PowerManagementLogMessage { } else { Write-Host -ForegroundColor $colour " $type $message" } - $logContent = '[' + $timeStamp + '] ' + $type + ' ' + $message + if ($type -match "ERROR") { Write-Error -Message $Message } + + $logContent = '[' + $timeStamp + '] ' + $type + ' ' + $message + Add-Content -Path $logFile $logContent } Export-ModuleMember -Function Write-PowerManagementLogMessage diff --git a/docs/snippets/import-module-local-linux.ps1 b/docs/snippets/import-module-local-linux.ps1 index 06f7a1e9..f6ee1e04 100644 --- a/docs/snippets/import-module-local-linux.ps1 +++ b/docs/snippets/import-module-local-linux.ps1 @@ -1,5 +1 @@ -Import-Module -Name VMware.PowerCLI -Import-Module -Name PowerVCF -Import-Module -Name PowerValidatedSolutions -Import-Module -Name Posh-SSH -Import-Module -Name VMware.CloudFoundation.PowerManagement \ No newline at end of file +Import-Module -Name VMware.CloudFoundation.PowerManagement diff --git a/docs/snippets/import-module.ps1 b/docs/snippets/import-module.ps1 index 06ed4818..f6ee1e04 100644 --- a/docs/snippets/import-module.ps1 +++ b/docs/snippets/import-module.ps1 @@ -1,5 +1 @@ -Import-Module -Name VMware.PowerCLI -Import-Module -Name PowerVCF -Import-Module -Name PowerValidatedSolutions -Import-Module -Name Posh-SSH Import-Module -Name VMware.CloudFoundation.PowerManagement diff --git a/docs/snippets/install-module.ps1 b/docs/snippets/install-module.ps1 index 3e2ebb03..f4430fd8 100644 --- a/docs/snippets/install-module.ps1 +++ b/docs/snippets/install-module.ps1 @@ -1,6 +1,6 @@ Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -Install-Module -Name VMware.PowerCLI -MinimumVersion 13.1.0 -Repository PSGallery -Install-Module -Name PowerVCF -MinimumVersion 2.4.0 -Repository PSGallery -Install-Module -Name PowerValidatedSolutions -MinimumVersion 2.8.0 -Repository PSGallery +Install-Module -Name VMware.PowerCLI -MinimumVersion 13.3.0 -Repository PSGallery +Install-Module -Name PowerVCF -MinimumVersion 2.4.1 -Repository PSGallery +Install-Module -Name PowerValidatedSolutions -MinimumVersion 2.11.0 -Repository PSGallery Install-Module -Name Posh-SSH -MinimumVersion 3.0.8 -Repository PSGallery Install-Module -Name VMware.CloudFoundation.PowerManagement -Repository PSGallery diff --git a/docs/snippets/save-module-local-linux.ps1 b/docs/snippets/save-module-local-linux.ps1 index 71f85ad1..2a416cbe 100644 --- a/docs/snippets/save-module-local-linux.ps1 +++ b/docs/snippets/save-module-local-linux.ps1 @@ -1,5 +1,5 @@ -Save-Module -Name VMware.PowerCLI -MinimumVersion 13.1.0 -Path /home/modules -Repository PSGallery -Save-Module -Name PowerVCF -MinimumVersion 2.4.0 -Path /home/modules -Repository PSGallery -Save-Module -Name PowerValidatedSolutions -MinimumVersion 2.8.0 -Path /home/modules -Repository PSGallery +Save-Module -Name VMware.PowerCLI -MinimumVersion 13.3.0 -Path /home/modules -Repository PSGallery +Save-Module -Name PowerVCF -MinimumVersion 2.4.1 -Path /home/modules -Repository PSGallery +Save-Module -Name PowerValidatedSolutions -MinimumVersion 2.11.0 -Path /home/modules -Repository PSGallery Save-Module -Name Posh-SSH -MinimumVersion 3.0.8 -Path /home/modules -Repository PSGallery Save-Module -Name VMware.CloudFoundation.PowerManagement -Path /home/modules -Repository PSGallery diff --git a/docs/snippets/save-module-local-windows.ps1 b/docs/snippets/save-module-local-windows.ps1 index d109a640..c8cffaca 100644 --- a/docs/snippets/save-module-local-windows.ps1 +++ b/docs/snippets/save-module-local-windows.ps1 @@ -1,5 +1,5 @@ -Save-Module -Name VMware.PowerCLI -MinimumVersion 13.1.0 -Path F:\Module\ -Repository PSGallery -Save-Module -Name PowerVCF -MinimumVersion 2.4.0 -Path F:\Module\ -Repository PSGallery -Save-Module -Name PowerValidatedSolutions -MinimumVersion 2.8.0 -Path F:\Module\ -Repository PSGallery +Save-Module -Name VMware.PowerCLI -MinimumVersion 13.3.0 -Path F:\Module\ -Repository PSGallery +Save-Module -Name PowerVCF -MinimumVersion 2.4.1 -Path F:\Module\ -Repository PSGallery +Save-Module -Name PowerValidatedSolutions -MinimumVersion 2.11.0 -Path F:\Module\ -Repository PSGallery Save-Module -Name Posh-SSH -MinimumVersion 3.0.8 -Path F:\Module\ -Repository PSGallery Save-Module -Name VMware.CloudFoundation.PowerManagement -Path F:\Module\ -Repository PSGallery