def clone(self, hostname, template, clone_name): ''' cloneVM clones a vm based on a previous vm: **to clone based on a template see deployVM vm_name is the name of the new vm clone_vm is what you are cloning from ''' vm_name = self.vm_json.get('name') if template == "server": clname = "ServerTemplate" cluster_name = "TestNet" elif template == "test": clname = "TestingTemplate" cluster_name = "TestNet" elif template == "se": clname = "SETemplate" cluster_name = "TestNet" elif template == "controller": clname = "ControllerTemplate" cluster_name = "TestNet" elif template == "unittest": clname = "jenkins-clone-unit-test" cluster_name = "TestNet" all_vms = self.server._get_managed_objects(MORTypes.VirtualMachine, from_mor=None) vm = None target_name = clname for mor, name in all_vms.iteritems(): if name == target_name: vm = VIVirtualMachine(self.server, mor) break if not vm: raise Exception("VM template not found") resource_pool = "/Resources" cluster = [ k for k, v in self.server.get_clusters().items() if v == cluster_name ][0] rpmor = [ k for k, v in self.server.get_resource_pools( from_mor=cluster).items() if v == resource_pool ][0] try: logger.info("Starting Clone operation \n") vm.clone(clone_name, resourcepool=rpmor) logger.info( "Clone Successful, please check vmware in about 20 minutes to see it." ) except VIException: logger.info("VI Exception")
def deploy(self, cluster_name, template_name, new_vm): ''' deployVM is similar to a clone, but based off of a template Arguments: cluster_name: e.g.: TestNet template_name: template you wish to clone from, e.g.: apache-template new_vm: name of the new vm ''' # like clone but only based off of a template. clone can only work with # a vm s = self.server # find all vms that are templates all_vms = s._get_managed_objects(MORTypes.VirtualMachine, from_mor=None) # you can filter from cluster, datacenter, host, etc by setting # 'from_mor' parameter to a managed object reference vm = None target_name = template_name for mor, name in all_vms.iteritems(): if name == target_name: vm = VIVirtualMachine(s, mor) break if not vm: raise Exception("VM Template Not found") resource_pool = "/Resources" cluster = [ k for k, v in s.get_clusters().items() if v == cluster_name ][0] rpmor = [ k for k, v in s.get_resource_pools(from_mor=cluster).items() if v == resource_pool ][0] vm_name = new_vm try: test_exist = s.get_vm_by_name(name, self.datacenter) except VIException: test_exist = None if test_exist is None: logger.info('ERROR: %s already exists' % vm_name) else: clone = vm.clone(new_vm, resourcepool=rpmor) logger.info("Cloned! Powering on now") clone.power_on() logger.info("Powered on") return clone