Friday, May 27, 2016

Storing backups of Virtual machines
This example is for a VMWare vCenter which allows virtual machines to be backed up and recovered.  Generally some space is reserved on the VMWare vCenter for storing backups of VMs provisioned on the vCenter. Typically about 20% of the storage on the vCenter is earmarked for this purpose.  A backup is typically a portable tar file comprising of vmdk and other relevant files.
The benefit of using the storage is that the vCenter keeps track of these files that can be queried and used from different clients through command line, API or web interface.  
However, clients can also choose to download these files to their repository of choice and manage it themselves. This is especially helpful if the archives have to be used outside of the vCenter they originated from.
An example of download is given from pyvmomi library
   def download_file(url, local_filename, cookie, headers):
        r = requests.get(url, stream=True, headers=headers,cookies=cookie,verify=False)
        with open(local_filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024*1024*64):
                if chunk:
                    f.write(chunk)
                    f.flush()

        return local_filename

Now vmdks  can be exported with
    def export_vmdks(vm, prefix_clone, esxhost, server_name):
        import pyVmomi
        HttpNfcLease = vm.ExportVm()
        try:
            infos = HttpNfcLease.info
            device_urls = infos.deviceUrl
            vmdks = []        
            for device_url in device_urls:
                deviceId = device_url.key
                deviceUrlStr = device_url.url
                diskFileName =  vm.name.replace(prefix_clone,'') + "-" + device_url.targetId
                diskUrlStr = deviceUrlStr.replace("*", esxhost)
                diskLocalPath = './' + diskFileName

                cookie = make_compatible_cookie(si._stub.cookie)
                headers = {'Content-Type': 'application/octet-stream'}
                logger.debug("[%s] exporting disk: %s" %(server_name,diskFileName))
                
                download_file(diskUrlStr, diskFileName, cookie, headers)
                vmdks.append({"filename":diskFileName,"id":deviceId})
        finally:
            HttpNfcLease.Complete()
        return vmdks

#codingexercise
convert a binary tree into doubly linked list
Node BT2DLLHelper(Node root)
{
if (root==null) return root;
if (root.left != null)
{
Node left = BT2DLLHelper(root.left);
for (; left.right != null; left = left.right);
left.right = root;
root.left = left;
}
if (root.right != null)
{
Node right = BT2DLLHelper(root.right);
for (; right.left != null; right = right.left);
right.left = root;
root.right = right;
}
return root;
}
Node BT2DLL(Node root)
{
if (root == null) return root;
root = BT2DLLHelper (root);
for( ; root.left != null; root = root.left);
return root;
}

No comments:

Post a Comment