github.com/openshift/installer@v1.4.17/upi/vsphere/powercli/upi-destroy.ps1 (about)

     1  #!/usr/bin/pwsh
     2  
     3  . .\variables.ps1
     4  
     5  $ErrorActionPreference = "Stop"
     6  
     7  # since we do not have ca for vsphere certs, we'll just set insecure
     8  Set-PowerCLIConfiguration -InvalidCertificateAction:Ignore -Confirm:$false | Out-Null
     9  $Env:GOVC_INSECURE = 1
    10  
    11  # Connect to vCenter
    12  Connect-VIServer -Server $vcenter -Credential (Import-Clixml $vcentercredpath)
    13  
    14  # Convert the installer metadata to a powershell object
    15  $metadata = Get-Content -Path ./metadata.json | ConvertFrom-Json
    16  
    17  # Get tag for all resources we created
    18  $tagCategory = Get-TagCategory -Name "openshift-$($metadata.infraID)"
    19  $tag = Get-Tag -Category $tagCategory -Name "$($metadata.infraID)"
    20  
    21  # Clean up all VMs
    22  $vms = Get-VM -Tag $tag
    23  foreach ($vm in $vms) {
    24      if ($vm.PowerState -eq "PoweredOn") {
    25          Write-Output "Stopping VM $vm"
    26          Stop-VM -VM $vm -confirm:$false > $null
    27      }
    28      Write-Output "Removing VM $vm"
    29      Remove-VM -VM $vm -DeletePermanently -confirm:$false
    30  }
    31  
    32  # Clean up all templates
    33  $templates = Get-TagAssignment -Tag $tag -Entity (Get-Template)
    34  foreach ($template in $templates) {
    35      Write-Output "Removing template $($template.Entity)"
    36      Remove-Template -Template $($template.Entity) -DeletePermanently -confirm:$false
    37  }
    38  
    39  # Clean up all resource pools
    40  $rps = Get-TagAssignment -Tag $tag -Entity (Get-ResourcePool)
    41  foreach ($rp in $rps) {
    42      Write-Output "Removing resource pool $($rp.Entity)"
    43      Remove-ResourcePool -ResourcePool $($rp.Entity) -confirm:$false
    44  }
    45  
    46  # Clean up all folders
    47  $folders = Get-TagAssignment -Tag $tag -Entity (Get-Folder)
    48  foreach ($folder in $folders) {
    49      Write-Output "Removing folder $($folder.Entity)"
    50      Remove-Folder -Folder $($folder.Entity) -DeletePermanently -confirm:$false
    51  }
    52  
    53  # Clean up storage policy.  Must be done after all other object cleanup except tag/tagCategory
    54  $storagePolicies = Get-SpbmStoragePolicy -Tag $tag
    55  
    56  foreach ($policy in $storagePolicies) {
    57  
    58      $clusterInventory = @()
    59      $splitResults = @($policy.Name -split "openshift-storage-policy-")
    60  
    61      if ($splitResults.Count -eq 2) {
    62          $clusterId = $splitResults[1]
    63          if ($clusterId -ne "") {
    64              Write-Host "Checking for storage policies for "$clusterId
    65              $clusterInventory = @(Get-Inventory -Name "$($clusterId)*" -ErrorAction Continue)
    66  
    67              if ($clusterInventory.Count -eq 0) {
    68                  Write-Host "Removing policy: $($policy.Name)"
    69                  $policy | Remove-SpbmStoragePolicy -Confirm:$false
    70              }
    71              else {
    72                  Write-Host "not deleting: $($clusterInventory)"
    73              }
    74          }
    75      }
    76  }
    77  
    78  # Clean up tags
    79  Remove-Tag -Tag $tag -confirm:$false
    80  Remove-TagCategory -Category $tagCategory -confirm:$false
    81  
    82  Disconnect-VIServer -Server $vcenter -Force:$true -Confirm:$false