Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 168455

Linked Clone Python API

$
0
0

Hello folks,

 

I'm charged with dynamically spinning up Linked Clone sparse as part of our build farm / CI testing.

 

I am using VSPHERE 6.0 and its associated API.

 

I can spin up clones all day long, but, when it get to the linked clone part, I'm falling on my face.  I've read the API ad nauseam, and found some great stuff here: Using the vSphere CloneVM API to create linked clones, clones from snapshots, and every other kind of clone « vmdev.inf…

 

and here Creating SE Sparse Linked Clones Using the vSphere 5.1 API | virtuallyGhetto.

 

I can do the PowerCLI, but, I'm directed to do this in Pyhon and I seem to be struggling with the specs for the clone.

 

Does anyone have a working example?

 

 

from __future__ import print_function

 

import time

 

import atexit

 

debug = True

 

__author__ = "Chuck Gudgel"

 

__copyright__ = "Copyright 2015, Netscout"

 

__credits__ = ["Chuck Gudgel", "SteveGee"]

 

__license__ = "GPL"

 

__version__ = "1.0.0"

 

__maintainer__ = "Chuck Gudgel"

 

__email__ = "chuck.gudgel@tekcomms.com"

 

__status__ = "Development"

 

importvim

 

from pyVmomi importvim

 

from pyVim import connect

 

import ssl

 

import requests.packages

 

from requests.packages.urllib3.exceptions import InsecureRequestWarning

 

deffind_vm(si,name):

 

  obj_view = content.viewManager.CreateContainerView(content.rootFolder,[vim.VirtualMachine],True)

 

  vm_count = 0

 

   if debug:

 

   for vm in vm_list:

 

  vm_count = vm_count +1

 

   if debug:

 

   print("VM # " + str(vm_count) + str(vm.name) + ".")

 

   print("VM" + str(vm)) 

 

   for vm in vm_list:

 

  vm_count = vm_count +1

 

   if vm.name == name:

 

   if debug:

 

   print("Found vm / template by the name of " + str(name) + " Returning vm to caller.")

 

   return vm

 

   returnNone

 

defwait_for_task(task):

 

   if debug:

 

   print(task)

 

   print(task.info)

 

   """ wait for a vCenter task to finish """

 

  task_done = False

 

   print("Starting Vsphere task " + str(task.info.key) + ".")

 

   whilenot task_done:

 

   if debug:

 

   print(task.info.state)

 

   if task.info.state == 'success':

 

   return task.info.result

 

   print("VM creation was successful for task" + str(task.info.key) + ".")

 

  exit(0)

 

   if task.info.state == 'error':

 

   if debug:

 

   print(task.info)

 

   print("VM creation task" + str(task.info.key) + " failed.")

 

   print("Error message: " + str(task.info.error.msg))

 

  task_done = True

 

  time.sleep(5)

 

defmain():

 

   #http://www.vmdev.info/

 

   #Connect to the VSphere ESXI host using the SmartConnect method of connect. Get back a service instance virtual machine summary.

 

   #Note that linked clones are based upon a non powered up CLONED snapshot of a virtual machine.

 

  si = None

 

   try:

 

  si = connect.SmartConnect(host='134.64.83.109',user='chuck@vsphere.local',pwd="supersecretpassword",port=443)

 

   except IOError, err:

 

   print (err)

 

   if debug: 

 

   print("Value of si ")

 

   print(si)

 

   print("")

 

   print("Value of si.content")

 

   print(si.content)

 

   print("")

 

   print("Value of si.content.rootFolder")

 

   print(si.content.rootFolder)

 

   print("")

 

   print("Value of si.content.about.fullName")

 

   print(si.content.about.fullName)

 

   print("")

 

   print("VM to be cloned instance UUID")

 

   print(si.content.about.instanceUuid)

 

   #print("VM Capablility")

 

   #print(si.Capability)

 

   #exit()

 

   #Find the VM that I wish to clone from the list of virtual machines in Vcenter

 

   #template_vm = find_vm(si,'demo_build_template')

 

   #template_vm = find_vm(si,'debian-linked-Clone01') #Works

 

   #template_vm = find_vm(si,'debian-8-model') #Works

 

   #template_vm = find_vm(si,'chucktest_snap')#Does not work

 

  template_vm = find_vm(si,'chucktest3')#Works

 

   #I need the clone folder to be the same as the parent folder.

 

   if debug:

 

   print("Clone folder" + str(clone_folder))

 

   #Get relocate spec structure and pass in some values for our relocation spec of our soon to be

 

   #created VM.

 

   #https://github.com/vmware/pyvmomi/blob/master/docs/vim/vm/RelocateSpec.rst

 

   #https://github.com/vmware/pyvmomi/blob/master/docs/vim/vm/RelocateSpec/DiskMoveOptions.rst

 

   #relocateSpec = vim.vm.RelocateSpec(datastore="datastore1")

 

   #relocateSpec = vim.vm.RelocateSpec(diskMoveType="createNewChildDiskBacking") #Must be from a snapshot on a linked cone because snapshot is read only.

 

   #relocateSpec = vim.vm.RelocateSpec(diskMoveType="")

 

   if debug:

 

   print("Relocate Spec" )

 

   print(relocateSpec)

 

   print("")

 

   #snapshotSpec = vim.vm.Snapshot(snapshot="chucktest_snap")

 

   #Get clone spec

 

   #,snapshot="chucktest_snap"

 

  cloneSpec = vim.vm.CloneSpec(powerOn=False,template=False,location=relocateSpec)

 

   if debug:

 

   print("Clone Spec")

 

   print(cloneSpec)

 

   print("")

 

   #Stumbling here. I don't think I have my snapshot setup right.

 

   #Blocking version of task

 

   #template_vm.Clone()

 

   #Create blocking clone task

 

   try:

 

   print("Attempting clone operation."

 

  task = template_vm.Clone(name='chucktest5',folder=template_vm.parent,spec=cloneSpec)

 

   except Exception as err:

 

   print(err)

 

  exit(-1)

 

   if debug:

 

   print("End of main.")

 

   #Non blocking version clone task

 

   #template_vm.vm.CloneVM_Task()

 

if __name__ == "__main__":

 

  exit(0)


Viewing all articles
Browse latest Browse all 168455

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>