Datrium vCenter Orchestrator Snapshot, Clone and Register VM
I have been working with several Datrium customers over the last few weeks on how to integrate the new DVX 4.0 API into vCenter Orchestrator workflows. In this example demo I show how easy it is to use vCenter Orchestrator to build a workflow which will take a VMware virtual machine, initiate a Datrium snapshot then clone that snapshot and present the VM directly to vCenter and give the user the ability to power-on the VM.
There are many use cases where this would be a beneficial workflow.
- Test and Development
- Application Deployment testing and verification
- Testing patches
- Recovery of items for an application or an end-user
- Root cause analysis of a failed application
This list is not exhaustive of all possibilities but it does give you an idea of where and when this might be useful.
Datrium DVX API
New within DVX 4.0 is a Python-based SDK (Python 2.7 > or Python 3.3 >) which interacts with Datrium REST-based APIs behind the scenes. The first set of public facing APIs which the engineering team has delivered provides 3 main functions.
- Snapshot
- Clone
- Replicate
The set of functions available will continue to grow, but for a first jaunt into this space I’d agree and say these are the most impactful for a customer. Below is a sample Python script which will connect to the DVX and initiate a snapshot of a VM running on Datrium. The SDK references as well as some sample scripts is available off the Datrium Support site at Support.Datrium.com.
Note: The SDK will be placed into /root/SDK and all of your Python scripts for Datrium DVX should be placed into /root/SDK/Python/.
To get the Datrium DVX API there’s a few quick steps you should follow on your Linux scripting host:
#install pip curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py #install pyvmomi pip install --upgrade pyvmomi #install SDK curl http://daweb.datrium.com/~piyush/SDK.zip -o sdk.zip yum install unzip unzip sdk.zip
Below is a sample script which uses the Datrium DVX API to initiate a snapshot of a running VM:
#!/usr/bin/env python #import syspath before any other imports import syspath from api.vms import snapshots from api.dvx import Dvx from api import vms import argparse import getpass from utils import wait_for_task import random if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--dvx', required=True, help='DVX IP/DNS name') parser.add_argument('--username', required=True, help='DVX username') parser.add_argument('--password', required=False, help='DVX password') parser.add_argument('--vm', required=False, help='VM name') args = parser.parse_args() args = vars(args) password = args['password'] if args['password'] else getpass.getpass(prompt="Enter password: ") dvx1 = Dvx(ip=args['dvx'], user_name=args['username'], password=password) request = vms.GetRequest() # Get VMs with name starting from dev print ("Looking for %s..." % args['vm']) request.namePattern.extend([args['vm']]) request.datastore="Datastore1" get_response = vms.get(dvx=dvx1, request=request) dev_vm = get_response.vm[0] # Assuming only one VM matched the pattern print ("VM name %s with VMX path %s" % (dev_vm.name, dev_vm.path)) # Take snapshot of the VM request = vms.TakeSnapshotRequest() request.id = dev_vm.id request.snapshotName = args['vm'] + "-temp-snapshot" request.retention = '60' print ("Taking a snapshot of VM %s" % dev_vm.name) task_response = vms.take_snapshot(dvx=dvx1, request=request) task = wait_for_task(dvx1, task_response) snapId = None for kv in task.keyValues: if kv.key == "vmSnapId": snapId = kv.val.stringVal
VMware vCenter Orchestrator
We can now take our scripts and utilize vCener Orchestrator to be the initiation engine behind the scenes. VMware vCenter Orchestrator is delivered as a virtual appliance which can be grabbed by logging into your my.vmware.com – Item of note here – vCO can be deployed independently or together with VMware vRealize Automation. I found the easiest way to integrate with vCenter SSO to be to deploy the standalone vCO appliance.
In the design view you should create a folder to organize your Workflows – with the new workflow and the vCO connected to vCenter we’re ready to rock! I won’t go into these steps since they’re out of the scope of this particular blog but let me know if you have questions anywhere along the way.
VMware vCenter Orchestrator Scriptable Tasks supports Javascript, for this we’re going to create a Scriptable Task which will call the Linux Python host and execute the necessary actions against Datrium DVX.
// SSH to Host // var session = null; function guid() { return Math.random(); } try { if (port) { session = new SSHSession(SSHHostName, SSHUserName, port); } else { System.log("A port value is not provided! Using default port 22"); session = new SSHSession(SSHHostName, SSHUserName); } if (passworAuthentication){ System.log("Connecting with password"); } else { if (path == null || path == ""){ System.log("using default"); path = passphrase; } System.log("Connecting with key pair (" + path + ")"); password = passphrase; } var guid = guid(); session.connectWithPasswordOrIdentity(passworAuthentication, SSHPassword, path); System.log("Connected!"); var cmd = ("/usr/bin/python2.7 /root/SDK/Python/Da_Snap_CloneVM.py --dvx " + DVXName + " --username " + DVXUserName + " --password '" + DVXPW + "' --vm " + VCVMName.displayName + " --retention " + Retention + " --id " + guid); System.log("Executing '" + cmd + "' using encoding '" + (encoding ? encoding : "Default System Encoding") + "'"); session.setEncoding(encoding); session.executeCommand(cmd, true); output = session.getOutput(); error = session.getError(); exitCode = session.exitCode; System.log("Output: '" + output + "'"); System.log("Error: '" + error + "'"); System.log("Exit code: '" + exitCode + "'"); } catch (e) { throw "Unable to execute command: " + e; } finally { if (session) { session.disconnect(); } } //Get The Full VMCloned Name and define the VM Datastore Path for adding to inventory within the Register Virtual Machine task var ClonedVMName = VCVMName.displayName + "-vCO-ClonedVM-" + guid var VMPath = "[" + VCDatastore.name + "] " + ClonedVMName + "/" + ClonedVMName + ".vmx"
Wrap-Up
Once we head over to the vCO Plug-in in vCenter we can add this Workflow to the right-click context of vCenter and users can exectute directly from vCenter simply by right-clicking on a VM! There’s more complex workflows in the works, but for now happy scripting and I hope this helps!