framework/Resources/Scripts/remove_machine.ps1
|
Param( [Parameter(Mandatory)] [string[]]$VmPrivateIps, [Parameter(Mandatory)] $EnvironmentName, [string]$ScriptsFolderPath = ".\Resources\Scripts" ) . "$ScriptsFolderPath\dashboard_common.ps1" . "$ScriptsFolderPath\Resources_Scripts\virtual_machine.ps1" . "$ScriptsFolderPath\common.ps1" $nc_resource_group = $null $resourceGroupName = $null $global:VmNames = $null function GetVmNamesFromPrivateIps { param( [string]$ResourceGroup ) $Names = @() foreach ($ip in $VmPrivateIps) { $vmName = GetVmDetailsFromPrivateIp -rgName $ResourceGroup -PrivateIp $ip $Names += $vmName.Name } $global:VmNames = $Names } function GetUploadedVms { param( [string]$ResourceGroup ) $vms = @(Get-AzVM -ResourceGroupName $ResourceGroup -ErrorAction Stop | ForEach-Object { $nicId = $_.NetworkProfile.NetworkInterfaces[0].Id $nicName = ($nicId -split "/")[-1] $nic = Get-AzNetworkInterface -ResourceGroupName $ResourceGroup -Name $nicName -ErrorAction Stop $privateIp = $nic.IpConfigurations[0].PrivateIpAddress [PSCustomObject]@{ Name = $_.Name Location = $_.Location VmSize = $_.HardwareProfile.VmSize OsType = $_.StorageProfile.OsDisk.OsType Zone = ($_.Zones[0]) Publisher = $_.StorageProfile.ImageReference.Publisher Offer = $_.StorageProfile.ImageReference.Offer Sku = $_.StorageProfile.ImageReference.Sku Version = $_.StorageProfile.ImageReference.Version NIC = $_.NetworkProfile.NetworkInterfaces.id StorageAccountType = $_.StorageProfile.OsDisk.ManagedDisk.StorageAccountType Disk = $_.StorageProfile.OsDisk.Name Tags = $_.Tags PrivateIp = $privateIp } } ) return $vms } function RemoveResources { param( [string]$VmName, [string]$DiskName, [string]$NetworkInterfaceName, [string]$PublicIpName, [string]$ResourceGroup, [bool]$UseForce ) if ($UseForce) { Remove-AzVM -ResourceGroupName $ResourceGroup -Name $VmName -Force -ErrorAction SilentlyContinue Remove-AzDisk -ResourceGroupName $ResourceGroup -DiskName $DiskName -Force -ErrorAction SilentlyContinue Remove-AzNetworkInterface -Name $NetworkInterfaceName -ResourceGroupName $ResourceGroup -Force -ErrorAction SilentlyContinue Remove-AzPublicIpAddress -Name $PublicIpName -ResourceGroupName $ResourceGroup -Force -ErrorAction SilentlyContinue } else { Remove-AzVM -ResourceGroupName $ResourceGroup -Name $VmName -ErrorAction SilentlyContinue Remove-AzDisk -ResourceGroupName $ResourceGroup -DiskName $DiskName -ErrorAction SilentlyContinue Remove-AzNetworkInterface -Name $NetworkInterfaceName -ResourceGroupName $ResourceGroup -ErrorAction SilentlyContinue Remove-AzPublicIpAddress -Name $PublicIpName -ResourceGroupName $ResourceGroup -ErrorAction SilentlyContinue } } $diskNames = [System.Collections.ArrayList]@() $networkInterfaceNames = [System.Collections.ArrayList]@() $publicIpNames = [System.Collections.ArrayList]@() function CreateLists { param( $vms ) for ($j = 0; $j -lt $global:VmNames.Length; $j++) { $vmFound = $false for ($i = 0; $i -lt $($vms | Measure-Object).Count ; $i++) { if ($global:VmNames[$j] -eq $vms[$i].name) { if ( -not $vms[$i].Tags.ContainsKey("ServerType") -or $vms[$i].Tags["ServerType"] -ne "NCache" ) { throw "$($global:VmNames[$j]) doesn't contain NCache Server" } $diskNames.Add($vms[$i].Disk) | Out-Null $nicList = $vms[$i].NIC -split '/' $nicCount = $nicList[$nicList.Count - 1] $networkInterfaceNames.Add($nicCount) | Out-Null $publicIpNames.Add("NC-PUBLIC_IP-$EnvironmentName-$($vms[$i].name)") | Out-Null $vmFound = $true break } } if ($vmFound -eq $false) { throw "$($global:VmNames[$j]) not found in ResourceGroup" } } } function RemoveNodeFromCachesLinux { param( $deletedVms ) $cmd = "" foreach ($vm in $deletedVms) { $cachesList = @() if ($vm.Tags.ContainsKey("Caches") -and -not [string]::IsNullOrWhiteSpace($vm.Tags["Caches"])) { $cachesList = $vm.Tags["Caches"].Split(",") | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } } $NCacheDir = "/opt/ncache" if ($vm.Tags.ContainsKey("InstallNCacheDir") -and $vm.Tags["InstallNCacheDir"]) { $NCacheDir = $vm.Tags["InstallNCacheDir"] } foreach ($cache in $cachesList) { $cmd += "$NCacheDir/bin/tools/remove-node -cacheName $cache -server $($vm.PrivateIp)" if ($cmd) { $cmd += "; " } } $cmd = DeactivateMachine -cmd $cmd -os "Linux" -NCacheDir $NCacheDir -VmDetails $vm if ($cachesList.Count -gt 0) { $result = Invoke-AzVMRunCommand -ResourceGroupName $resourceGroupName -VMName $vm.Name -CommandId 'RunShellScript' -ScriptString $cmd foreach ($val in $result.Value) { Write-Host $val.Message } } else { Write-Host "Skipping VM $($vm.Name), no caches found." } $cmd = "" } } function RemoveNodeFromCachesWindows { param( $deletedVms ) $cmd = "" foreach ($vm in $deletedVms) { $cachesList = @() if ($vm.Tags.ContainsKey("Caches") -and -not [string]::IsNullOrWhiteSpace($vm.Tags["Caches"])) { $cachesList = $vm.Tags["Caches"].Split(",") | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } } foreach ($cache in $cachesList) { $cmd += "Remove-Node -CacheName $cache -Server $($vm.PrivateIp)" $cmd += "`n" } $cmd = DeactivateMachine -cmd $cmd -os "Windows" -VmDetails $vm if ($cachesList.Count -gt 0) { $result = Invoke-AzVMRunCommand -ResourceGroupName $resourceGroupName -VMName $vm.Name -CommandId 'RunPowerShellScript' -ScriptString $cmd foreach ($val in $result.Value) { Write-Host $val.Message } } else { Write-Host "Skipping VM $($vm.Name), no caches found." } $cmd = "" } } function DeactivateMachine { param( [string]$cmd, [string]$os, [string]$NCacheDir, [psobject]$VmDetails ) if ($os -eq "Windows") { if ($vm.Tags.ContainsKey("NCacheActivated") -and [System.Convert]::ToBoolean($vm.Tags["NCacheActivated"])) { $Key = $nc_resource_group.Tags["LicenseKey"] $cmd += "Unregister-NCache -Key $Key" $cmd += "`n" } } else { if ($vm.Tags.ContainsKey("NCacheActivated") -and [System.Convert]::ToBoolean($vm.Tags["NCacheActivated"])) { $Key = $nc_resource_group.Tags["LicenseKey"] $cmd += "$NCacheDir/bin/tools/unregister-ncache -Key $Key ;" } } return $cmd } function RemoveVmsFromCaches { param( $vms ) $deletedVms = $vms | Where-Object { $global:VmNames -contains $_.Name } if ($nc_resource_group.Tags["OsType"] -eq "Windows") { RemoveNodeFromCachesWindows -deletedVms $deletedVms } else { RemoveNodeFromCachesLinux -deletedVms $deletedVms } } function UninstallNCache { param( $vms ) if ($nc_resource_group.Tags["OsType"] -eq "Windows") { Uninstall-NCacheFromVMWindows -ResourceGroupName $resourceGroupName -VMName $vms } else { Uninstall-NCacheFromVMLinux -ResourceGroupName $resourceGroupName -VMName $vms } } function ExecuteCommands { $nc_resource_group = Get-AzResourceGroup -ErrorAction Stop | Where-Object { $_.Tags -and $_.Tags.Contains("EnvironmentName") -and $_.Tags["EnvironmentName"] -eq $EnvironmentName } if (-not $nc_resource_group) { throw "No such environment exists" } $resourceGroupName = $nc_resource_group.ResourceGroupName $vms = GetUploadedVms -ResourceGroup $resourceGroupName GetVmNamesFromPrivateIps -ResourceGroup $resourceGroupName RemoveVmsFromCaches -vms $vms UninstallNCache -vms $global:VmNames CreateLists -vms $vms Write-Host "Removing virtual machines..." for ($i = 0; $i -lt $global:VmNames.Length; $i++) { RemoveResources -VmName $global:VmNames[$i] -DiskName $diskNames[$i] -NetworkInterfaceName $networkInterfaceNames[$i] -PublicIpName $publicIpNames[$i] -ResourceGroup $resourceGroupName -UseForce $true } $deletedVms = $vms | Where-Object { $global:VmNames -contains $_.Name } foreach ($vm in $deletedVms) { $cachesList = @() if ($vm.Tags.ContainsKey("Caches") -and -not [string]::IsNullOrWhiteSpace($vm.Tags["Caches"])) { $cachesList = $vm.Tags["Caches"].Split(",") | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } } foreach ($cache in $cachesList) { Update-NcAzDashboards -ScriptsFolderPath $ScriptsFolderPath -ResourceGroupName $resourceGroupName -CacheName $cache -SkipClient } } } ShowExecutingCommand if (-not (Get-AzContext)) { NoContext Connect-AzAccount if (Get-AzContext) { ExecuteCommands } } else { ExecuteCommands } |