def __init__(self, module): super(CrossVCCloneManager, self).__init__(module) self.config_spec = vim.vm.ConfigSpec() self.clone_spec = vim.vm.CloneSpec() self.relocate_spec = vim.vm.RelocateSpec() self.service_locator = vim.ServiceLocator() self.destination_vcenter = self.params['destination_vcenter'] self.destination_vcenter_username = self.params['destination_vcenter_username'] self.destination_vcenter_password = self.params['destination_vcenter_password'] self.destination_vcenter_port = self.params.get('port', 443) self.destination_vcenter_validate_certs = self.params.get('destination_vcenter_validate_certs', None)
active_nw_config_spec = vim.vcha.FailoverClusterConfigurator.ClusterNetworkConfigSpec( ) active_nw_config_spec.networkPortGroup = vcha_network active_ipSettings = vim.vm.customization.IPSettings() active_ipSettings.subnetMask = vcha_subnet_mask active_ip_spec = vim.vm.customization.FixedIp() active_ip_spec.ipAddress = active_vcha_ip active_ipSettings.ip = active_ip_spec active_nw_config_spec.ipSettings = active_ipSettings deployment_spec.activeVcNetworkConfig = active_nw_config_spec #Active node service locator active_vc_spec = vim.vcha.FailoverClusterConfigurator.SourceNodeSpec() active_vc_vm = get_obj(content, [vim.VirtualMachine], active_vcha_vm_name) active_vc_spec.activeVc = active_vc_vm service_locator = vim.ServiceLocator() cred = vim.ServiceLocator.NamePassword() cred.username = active_vc_username cred.password = active_vc_password service_locator.credential = cred service_locator.instanceUuid = si.content.about.instanceUuid service_locator.url = active_vc_url #Source active VC service_locator.sslThumbprint = active_vc_thumprint active_vc_spec.managementVc = service_locator deployment_spec.activeVcSpec = active_vc_spec #Passive node configuration spec passive_vc_spec = vim.vcha.FailoverClusterConfigurator.PassiveNodeDeploymentSpec( ) passive_ipSettings = vim.vm.customization.IPSettings() passive_ipSettings.subnetMask = vcha_subnet_mask
def main(): print("This script is not supported by VMware. Use at your own risk") args = parseParameters() password = args.password if hasattr(ssl, 'SSLContext'): context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) context.verify_mode = ssl.CERT_NONE si = connect.SmartConnect(host=args.sourcevc, user=args.user, pwd=password, sslContext=context) else: si = connect.SmartConnect(host=args.sourcevc, user=args.user, pwd=password) if not si: print("Could not connect to source vcenter: %s " % args.sourcevc) return -1 else: print("Connect to vcenter: %s" % args.sourcevc) atexit.register(connect.Disconnect, si) if not args.destvc or args.sourcevc == args.destvc: di = si else: if hasattr(ssl, 'SSLContext'): context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) context.verify_mode = ssl.CERT_NONE di = connect.SmartConnect(host=args.destvc, user=args.user, pwd=password, sslContext=context) else: di = connect.SmartConnect(host=args.destvc, user=args.user, pwd=password) if not di: print("Could not connect to destination vcenter: %s " % args.destvc) return -1 else: print("Connect to vcenter: %s" % args.destvc) atexit.register(connect.Disconnect, di) sinv = si.RetrieveContent() sdc = sinv.rootFolder.childEntity[0] if args.destvc: dinv = di.RetrieveContent() ddc = dinv.rootFolder.childEntity[0] else: dinv = sinv ddc = sdc relocSpec = vim.vm.RelocateSpec() #print(sinv) #print(dinv) if sinv != dinv: if not args.server and not args.cluster and not args.datastore and not args.network: print("XV Vmotion requires host, cluster, datastore, and network") return None up = vim.ServiceLocator.NamePassword(username=args.user, password=password) sl = vim.ServiceLocator(credential=up, instanceUuid=dinv.about.instanceUuid, url="https://%s" % args.destvc) relocSpec.service = sl vm = getObject(sinv, [vim.VirtualMachine], args.name, verbose=False) rp = getObject(sinv, [vim.ResourcePool], "nsxt") print rp if not vm: print("VM %s not found" % args.name) return else: print("VM %s %s found" % (vm.name, vm._moId)) host = None if args.server: host = getObject(dinv, [vim.HostSystem], args.server) if not host: print("Host %s not found" % args.server) return else: print("Destination host %s found." % host.name) cluster = None if args.cluster: cluster = getObject(dinv, [vim.ClusterComputeResource], args.cluster) if not cluster: print("Cluster %s not found" % args.cluster) return else: print( "Destination cluster %s found, checking for DRS recommendation..." % cluster.name) if host and host.parent.resourcePool != cluster.resourcePool: print( "Destination host %s and cluster %s are not resource pool" % (host.name, cluster.name)) return if not cluster.configuration.drsConfig.enabled and not host: print( "Destination cluster %s is not DRS enabled, must specify host" % cluster.name) return if not host and cluster.resourcePool == vm.resourcePool: print("Must provide host when migrating within same cluster") return if not host: rhost = cluster.RecommendHostsForVm(vm=vm, pool=cluster.resourcePool) if len(rhost) == 0: print( "No hosts found in cluster %s from DRS recommendation for migration" % args.cluster) return else: print("DRS recommends %d hosts" % len(rhost)) host = rhost[0].host if host: relocSpec.host = host relocSpec.pool = rp if cluster: #relocSpec.pool = cluster.resourcePool relocSpec.pool = rp datastore = None if args.datastore: datastore = getObject(dinv, [vim.Datastore], args.datastore) if not datastore: print("Datastore %s not found" % args.datastore) return else: print("Destination datastore %s found." % datastore.name) relocSpec.datastore = datastore networks = [] for n in args.network: print("Searching VCenter for destination network(s)") network = getObject(dinv, [vim.Network], n, verbose=False) if not network: print("Network %s not found" % args.network) return else: print("Destination network %s found." % network.name) networks.append(network) devices = vm.config.hardware.device nic_devices = [ device for device in devices if isinstance(device, vim.vm.device.VirtualEthernetCard) ] vnic_changes = [] for device in nic_devices: vif_id = vm.config.instanceUuid + ":" + str(device.key) netSpec = setupNetworks(vm, host, networks, vif_id) relocSpec.deviceChange = netSpec print("Initiating migration of VM %s" % args.name) vm.RelocateVM_Task( spec=relocSpec, priority=vim.VirtualMachine.MovePriority.highPriority)