Latest Post

Clone and connect Nutanix Volume Group using PowerShell

Hardev Sangera and I held a breakout session at Nutanix .Next on Tour in London. The session was predominately to demo’s of some of the new features released with AOS5.0. During the session we demonstrated how to present iSCSI storage to a physical workload and then subsequently take a clone of that data and so it could be used to perform development activities on.

Enabling a DBA or developer ‘live like’ data to work with, which  can take hours with traditional methods.

Rather than using the Prism interface to clone the volume group and the iScsi initiator in windows to connect to it, I wrote a PowerShell Script demonstrating the use of  API and the Nutanix PowerShell CMDlets.

Disclaimer : I’m not a professional coder so don’t rip it to shreds when you read it, as there are significant improvements that could be made and it is only to serve as an example of leveraging the Nutanix API.

#####################################################################################################
#####################################################################################################
###
###     Powershell Clone VolumeGroup on Nutanix Cluster
###
###     Written by Matthew Nichols 2017 
###
###
###     With help from Sandeep Melepunathil for the tricky bits.
###
###     This script was only created to demo the rapid cloning of VolumeGroups in Nutanix 
###     for the .Next London Breakout session and should be tailored to meet your requirements.
###
###     I take no responsibility for any loss of data that may result in the use of this script.
###
#####################################################################################################
#####################################################################################################
#####################################################################################################

$NXClusterUsername="admin" # The Username to connect to the cluster with (Password will Prompt)
$VolumeGroupName="prod-data" # Volume_group to clone 
$NXCLusterAddress="192.168.2.40" # The address of the Nutanix Cluster
$CloneVGName="prod-data-clone" # The name you want to give to the new VG Clone
$ClientIPAddress="192.168.2.124" #The Addess of the client you are connected to (Should really query this)

# Load the PowerCLI and Nutanix Modules
Add-PSSnapin NutanixCmdletsPSSnapin

# Ensure previous Nutanix Sessions are disconnected
Disconnect-NTNXCluster *

#Connect to the Nutanix Cluster
Connect-NutanixCluster -Server $NXCLusterAddress -UserName $NXClusterUsername  -AcceptInvalidSSLCerts

#Find the VolumeGroup with the Name defined in the Variable at the top
$sourceVG = Get-NTNXVolumeGroups | Where {$_.name -eq $VolumeGroupName}

#Clone the volumeGroup found in the above
$CloneTask = Clone-NTNXVolumeGroup -SourceVolumeGroupUuid $sourceVG.uuid -name $CloneVGName

#wait for 
Do {
    $CloneTaskStats = Get-NTNXTask -Taskid $CloneTask.taskUuid 
    Write-Host "Waiting for task to Complete "
    }
While ($CloneTaskStats.progressStatus -ne "Succeeded")

#Find the VolumeGroup with the Name defined in the Variable at the top
$CloneVG = Get-NTNXVolumeGroups | Where {$_.name -eq "prod-data-clone"}

#Give the iscsi client ip access to the VG 

$ipspec = New-NTNXObject -Name IscsiClientDTO
$ipspec.clientAddress = $ClientIPAddress
Set-NTNXVolumeGroup -Uuid $CloneVG.uuid -AttachedClients $ipspec


#Refresh the iscsi Targets and Connect All - Can sometimes be buggy and may need to 
Get-IscsiTargetPortal | Update-IscsiTargetPortal
Update-IscsiTarget

Do{
    Update-IscsiTarget
    $iscsi_connections = Get-IscsiTarget 
    }
While ($iscsi_connections = 0)

Get-IscsiTarget | Connect-IscsiTarget

The script to disconnect and remove the Volume Group…

#####################################################################################################
#####################################################################################################
###
###     Powershell Disconnect and Delete VolumeGroup on Nutanx Cluster
###
###     Written by Matthew Nichols 2017 
###
###
###     With help from Sandeep Melepunathil for the tricky bits.
###
###     This script was only created to demo the rapid cloning of VolumeGroups in Nutanix 
###     for the .Next London Breakout session and should be tailored to meet your requirements.
###
###     I take no responsibility for any loss of data that may result in the use of this script.
###
#####################################################################################################
#####################################################################################################
#####################################################################################################

$connection = Get-IscsiConnection
Disconnect-IscsiTarget

$NXCLusterAddress="192.168.2.40" # Address of the cluster
$NXClusterUsername="admin"
$CloneVGName="prod-data-clone" # Cluster Volume Group name
$client_address="192.168.2.124" # Client IP Address of the VM you are running script from

# Load the PowerCLI and Nutanix Modules
Add-PSSnapin NutanixCmdletsPSSnapin

# Ensure previous Nutanix Sessions are disconnected
Disconnect-NTNXCluster *

#Connect to the Nutanix Cluster
Connect-NutanixCluster -Server $NXCLusterAddress -UserName $NXClusterUsername  -AcceptInvalidSSLCerts

#Find the VolumeGroup with the Name defined in the Variable at the top
$ClonedVG = Get-NTNXVolumeGroups | Where {$_.name -eq $CloneVGName}

#Invoke Rest API to remove Client Address from Volume Group
#Block Written by Sandeep Melepunathil Nutanix SRE

$vmip = "192.168.2.124"

$body = "{""iscsi_client"": 
 {
        ""client_address"": ""$client_address""
    },
    ""operation"": ""DETACH""
    }"

$server = $NXCLusterAddress
$username = $NXClusterUsername
$password = "nutanix/4u"
$vguuid = $ClonedVG.uuid
$url = "https://${server}:9440/PrismGateway/services/rest/v2.0/volume_groups/${vguuid}/close"
$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))}
$out = Invoke-RestMethod -Uri $url -Headers $Header -Method Post -Body $body -ContentType application/json

Delete-NTNXVolumeGroup -Uuid $ClonedVG.uuid