private Boolean checkRelocation(ManagedObjectReference vmMOR, ManagedObjectReference hostMOR, ManagedObjectReference poolMOR, ManagedObjectReference dsMOR) { Boolean relocate = false; try { VirtualMachineRelocateSpec relSpec = new VirtualMachineRelocateSpec(); relSpec.datastore = (dsMOR); relSpec.host = (hostMOR); relSpec.pool = (poolMOR); ManagedObjectReference taskMOR = cb.getConnection().Service.CheckRelocate_Task(provisionChkr, vmMOR, relSpec, null); String res = monitorTask(taskMOR); if (res.Equals("sucess")) { relocate = true; } else { relocate = false; } } catch (Exception) { relocate = false; } return(relocate); }
public IVMConnection CreateLinkedClone(string snapshotName, string linkedVMName) { var snapshot = VimHelper.GetDynamicProperty <VirtualMachineSnapshotInfo>(MOB, "snapshot"); if (snapshot != null) { var snapshotMOB = FindSnapshotByName(snapshot.rootSnapshotList, snapshotName); if (snapshotMOB != null) { var relSpec = new VirtualMachineRelocateSpec(); relSpec.diskMoveType = "createNewChildDiskBacking"; var cloneSpec = new VirtualMachineCloneSpec(); cloneSpec.powerOn = false; cloneSpec.template = false; cloneSpec.location = relSpec; cloneSpec.snapshot = snapshotMOB; ManagedObjectReference folder = VimHelper.GetDynamicProperty <ManagedObjectReference>(MOB, "parent"); ManagedObjectReference task = VimHelper.ServiceInstance.CloneVM_Task(MOB, folder, linkedVMName, cloneSpec); while (true) { TaskInfo info = VimHelper.GetDynamicProperty <TaskInfo>(task, "info"); if (info.state == TaskInfoState.success) { return(new VSphereVMConnection(info.result as ManagedObjectReference)); } else if (info.state == TaskInfoState.error) { throw new Exception(info.error.localizedMessage); } System.Threading.Thread.Sleep(500); } } } return(null); }
private void cloneVM() { _service = cb.getConnection()._service; _sic = cb.getConnection()._sic; String cloneName = cb.get_option("CloneName"); String vmPath = cb.get_option("vmPath"); String datacenterName= cb.get_option("DatacenterName"); // Find the Datacenter reference by using findByInventoryPath(). ManagedObjectReference datacenterRef = _service.FindByInventoryPath(_sic.searchIndex, datacenterName); if (datacenterRef == null) { Console.WriteLine("The specified datacenter is not found"); return; } // Find the virtual machine folder for this datacenter. ManagedObjectReference vmFolderRef = (ManagedObjectReference)cb.getServiceUtil().GetMoRefProp(datacenterRef, "vmFolder"); if (vmFolderRef == null) { Console.WriteLine("The virtual machine is not found"); return; } ManagedObjectReference vmRef = _service.FindByInventoryPath(_sic.searchIndex, vmPath); if (vmRef == null) { Console.WriteLine("The virtual machine is not found"); return; } VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec(); cloneSpec.location=relocSpec; cloneSpec.powerOn=false; cloneSpec.template=false; String clonedName = cloneName; Console.WriteLine("Launching clone task to create a clone: " + clonedName); try { ManagedObjectReference cloneTask = _service.CloneVM_Task(vmRef, vmFolderRef, clonedName, cloneSpec); String status = cb.getServiceUtil().WaitForTask(cloneTask); if(status.Equals("failure")) { Console.WriteLine("Failure -: Virtual Machine cannot be cloned"); } if (status.Equals("sucess")) { Console.WriteLine("Virtual Machine Cloned successfully."); } else{ Console.WriteLine("Virtual Machine Cloned cannot be cloned"); } } catch(Exception e) { } }
public void relocateVM(String vmname, String pool, String tHost, String tDS, String srcHost) { VirtualMachineMovePriority pri = getPriority(); try { ManagedObjectReference srcMOR = getMOR(srcHost, "HostSystem", null); ManagedObjectReference vmMOR = getMOR(vmname, "VirtualMachine", srcMOR); ManagedObjectReference poolMOR = getMOR(pool, "ResourcePool", null); ManagedObjectReference hMOR = getMOR(tHost, "HostSystem", null); ManagedObjectReference[] dsTarget = (ManagedObjectReference[])cb.getServiceUtil().GetDynamicProperty(hMOR, "datastore"); ManagedObjectReference dsMOR = browseDSMOR(dsTarget, tDS); if (dsMOR == null) { Console.WriteLine("Datastore " + tDS + " not found"); } if (vmMOR == null || srcMOR == null || poolMOR == null || hMOR == null || dsMOR == null) { return; } VirtualMachineRelocateSpec relSpec = new VirtualMachineRelocateSpec(); relSpec.datastore = (dsMOR); relSpec.host = (hMOR); relSpec.pool = (poolMOR); Console.WriteLine("Relocating the Virtual Machine " + vmname); ManagedObjectReference taskMOR = cb._connection._service.RelocateVM_Task(vmMOR, relSpec, pri, true); String res = cb.getServiceUtil().WaitForTask(taskMOR); if (res.Equals("sucess")) { Console.WriteLine("Relocation done successfully of " + vmname + " to host " + tHost); } else { Console.WriteLine("Error:: Relocation failed"); } } catch (Exception e) { Console.WriteLine(e.Message); } }
public string MigrateVirtualMachine(MigrationVM virtualMachine) { MigrationVMDetail vmDetail = new MigrationVMDetail(virtualMachine, this); if (!vmDetail.Verified) { Logger.Error("{0}: Skipped: Verify Failed", virtualMachine.Name); return("skipped-verifyfailed"); } try { VirtualMachineRelocateSpec vmRelocSpec = GetRelocationSpec(vmDetail); if (vmRelocSpec != null) { Logger.Info("=========================================="); Logger.Info("Migrating {0} to {1}{2}", virtualMachine.Name, vmDetail.DestinationClusterComputeResource?.Name, vmDetail.DestinationHostSystem?.Name); Logger.Info("Migrating {0} storage to {1}{2}", virtualMachine.Name, vmDetail.DestinationDatastore?.Name, vmDetail.DestinationStoragePod?.Name); if (vmRelocSpec.DeviceChange?.Count() > 0) { foreach (var deviceChange in vmRelocSpec.DeviceChange) { Logger.Info("Migrating networking to {@0}", deviceChange); } } Logger.Info("=========================================="); return(((VirtualMachine)GetViewByName <VirtualMachine>(virtualMachine.Name)) .RelocateVM_Task(vmRelocSpec, VirtualMachineMovePriority.highPriority).ToString()); } } catch (Exception e) { Logger.Error("EXCEPTION: {0}", e.Message); Logger.Error("EXCEPTION: {0}", e.StackTrace); throw; } Logger.Trace("return null {@value}", vmDetail); return(null); }
public void deploy(VM guest) { cb = AppUtil.AppUtil.initialize("VMDeploy", this.connectString); cb.connect(); /************Start Deploy Code*****************/ _service = cb.getConnection()._service; _sic = cb.getConnection()._sic; // ManagedObjectReferences ManagedObjectReference datacenterRef; ManagedObjectReference vmFolderRef; ManagedObjectReference vmRef; ManagedObjectReference hfmor; // hostFolder reference ArrayList crmors; // ArrayList of ComputeResource references ManagedObjectReference hostmor; ManagedObjectReference crmor = null; // ComputeResource reference ManagedObjectReference resourcePool; // Find the Datacenter reference by using findByInventoryPath(). datacenterRef = _service.FindByInventoryPath(_sic.searchIndex, this.dataCenter); if (datacenterRef == null) { Console.WriteLine("The specified datacenter is not found"); return; } // Find the virtual machine folder for this datacenter. vmFolderRef = (ManagedObjectReference)cb.getServiceUtil().GetMoRefProp(datacenterRef, "vmFolder"); if (vmFolderRef == null) { Console.WriteLine("The virtual machine is not found"); return; } vmRef = _service.FindByInventoryPath(_sic.searchIndex, guest.getVmPath()); if (vmRef == null) { Console.WriteLine("The virtual machine is not found"); return; } // Code for obtaining managed object reference to resource root hfmor = cb.getServiceUtil().GetMoRefProp(datacenterRef, "hostFolder"); crmors = cb.getServiceUtil().GetDecendentMoRefs(hfmor, "ComputeResource", null); if (this.hostName != null) { hostmor = cb.getServiceUtil().GetDecendentMoRef(hfmor, "HostSystem", this.hostName); if (hostmor == null) { Console.WriteLine("Host " + this.hostName + " not found"); return; } } else { hostmor = cb.getServiceUtil().GetFirstDecendentMoRef(datacenterRef, "HostSystem"); } hostName = (String)cb.getServiceUtil().GetDynamicProperty(hostmor, "name"); for (int i = 0; i < crmors.Count; i++) { ManagedObjectReference[] hrmors = (ManagedObjectReference[])cb.getServiceUtil().GetDynamicProperty((ManagedObjectReference)crmors[i], "host"); if (hrmors != null && hrmors.Length > 0) { for (int j = 0; j < hrmors.Length; j++) { String hname = (String)cb.getServiceUtil().GetDynamicProperty(hrmors[j], "name"); if (hname.Equals(this.hostName)) { crmor = (ManagedObjectReference)crmors[i]; i = crmors.Count + 1; j = hrmors.Length + 1; } } } } if (crmor == null) { Console.WriteLine("No Compute Resource Found On Specified Host"); return; } resourcePool = cb.getServiceUtil().GetMoRefProp(crmor, "resourcePool"); /***********************************/ /*Setup cloning sysprep preferences*/ /***********************************/ VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec(); // Set resource pool for relocspec(compulsory since deploying template) relocSpec.pool = resourcePool; cloneSpec.location = relocSpec; cloneSpec.powerOn = true; //Specifies whether or not the new VirtualMachine should be powered on after creation. As part of a customization, this flag is normally set to true, since the first power-on operation completes the customization process. This flag is ignored if a template is being created. cloneSpec.template = false; //Specifies whether or not the new virtual machine should be marked as a template. // Customization CustomizationSpec custSpec = new CustomizationSpec(); // Make NIC settings CustomizationAdapterMapping[] custAdapter = new CustomizationAdapterMapping[1]; custAdapter[0] = new CustomizationAdapterMapping(); CustomizationIPSettings custIPSettings = new CustomizationIPSettings(); CustomizationDhcpIpGenerator custDhcp = new CustomizationDhcpIpGenerator(); custIPSettings.ip = custDhcp; custAdapter[0].adapter = custIPSettings; // Set NIC settings custSpec.nicSettingMap = custAdapter; // Make DNS entry CustomizationGlobalIPSettings custIP = new CustomizationGlobalIPSettings(); custIP.dnsServerList = guest.getDnsList(); ; // Set DNS entry custSpec.globalIPSettings = custIP; // Make Sysprep entries CustomizationSysprep custPrep = new CustomizationSysprep(); //An object representation of a Windows sysprep.inf answer file. The sysprep type encloses all the individual keys listed in a sysprep.inf file // Make guiRunOnce entries(to change autologon settings to login to domain) //CustomizationGuiRunOnce custGuiRunOnce = new CustomizationGuiRunOnce(); //string deleteKey = "reg delete \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\" /v \"DefaultDomainName\" /f"; //string addKey = "reg add \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\" /v \"DefaultDomainName\" /t REG_SZ /d " + this.joinDomain; //string shutdownKey = "shutdown -r -t 00 -c \"Rebooting computer\""; //custGuiRunOnce.commandList = new string[] { deleteKey, addKey, shutdownKey }; // Set guiRunOnce //custPrep.guiRunOnce = custGuiRunOnce; // Make guiUnattended settings CustomizationGuiUnattended custGui = new CustomizationGuiUnattended(); //The GuiUnattended type maps to the GuiUnattended key in the sysprep.inf answer file //The GuiUnattended type maps to the GuiUnattended key in the sysprep.inf answer file if (Int32.Parse(guest.getAutoLogonCount()) == 0) { custGui.autoLogon = false; } else { custGui.autoLogon = true; custGui.autoLogonCount = Int16.Parse(guest.getAutoLogonCount()); //If the AutoLogon flag is set, then the AutoLogonCount property specifies the number of times the machine should automatically log on as Administrator } CustomizationPassword custWorkPass = new CustomizationPassword(); if (guest.getWorkGroupPassword() != null) { custWorkPass.plainText = true; //Flag to specify whether or not the password is in plain text, rather than encrypted. custWorkPass.value = guest.getWorkGroupPassword(); custGui.password = custWorkPass; } custGui.timeZone = 190; //IST The time zone for the new virtual machine. Numbers correspond to time zones listed in sysprep documentation at in Microsoft Technet. Taken from unattend.txt // Set guiUnattend settings custPrep.guiUnattended = custGui; // Make identification settings CustomizationIdentification custId = new CustomizationIdentification(); custId.domainAdmin = guest.getDomainAdmin(); CustomizationPassword custPass = new CustomizationPassword(); custPass.plainText = true; //Flag to specify whether or not the password is in plain text, rather than encrypted. custPass.value = guest.getDomainPassword(); custId.domainAdminPassword = custPass; custId.joinDomain = guest.getJoinDomain(); // Set identification settings custPrep.identification = custId; // Make userData settings CustomizationUserData custUserData = new CustomizationUserData(); CustomizationFixedName custName = new CustomizationFixedName(); custName.name = guest.getName(); custUserData.computerName = custName; custUserData.fullName = "ePO"; custUserData.orgName = "McAfee"; if (guest.getProductId() != null) { custUserData.productId = guest.getProductId(); } // Set userData settings custPrep.userData = custUserData; // Set sysprep custSpec.identity = custPrep; // clonespec customization cloneSpec.customization = custSpec; // clone power on cloneSpec.powerOn = true; String clonedName = guest.getName(); Console.WriteLine("Launching clone task to create a clone: " + clonedName); try { ManagedObjectReference cloneTask = _service.CloneVM_Task(vmRef, vmFolderRef, clonedName, cloneSpec); String status = cb.getServiceUtil().WaitForTask(cloneTask); if (status.Equals("failure")) { Console.WriteLine("Failure -: Virtual Machine cannot be cloned"); } if (status.Equals("sucess")) { Console.WriteLine("Virtual Machine Cloned successfully."); } else { Console.WriteLine("Virtual Machine Cloned cannot be cloned"); } } catch (Exception e) { } /************End Deploy Code*******************/ cb.disConnect(); }
public void relocateVM(String vmname, String pool, String tHost, String tDS, String srcHost) { VirtualMachineMovePriority pri = getPriority(); try { ManagedObjectReference srcMOR = getMOR(srcHost, "HostSystem", null); ManagedObjectReference vmMOR = getMOR(vmname, "VirtualMachine", srcMOR); ManagedObjectReference poolMOR = getMOR(pool, "ResourcePool", null); ManagedObjectReference hMOR = getMOR(tHost, "HostSystem", null); ManagedObjectReference[] dsTarget = (ManagedObjectReference[])cb.getServiceUtil().GetDynamicProperty(hMOR, "datastore"); ManagedObjectReference dsMOR = browseDSMOR(dsTarget, tDS); if (dsMOR == null) { Console.WriteLine("Datastore " + tDS + " not found"); } if (vmMOR == null || srcMOR == null || poolMOR == null || hMOR == null || dsMOR == null) { return; } VirtualMachineRelocateSpec relSpec = new VirtualMachineRelocateSpec(); relSpec.datastore = (dsMOR); relSpec.host = (hMOR); relSpec.pool = (poolMOR); Console.WriteLine("Relocating the Virtual Machine " + vmname); ManagedObjectReference taskMOR = cb._connection._service.RelocateVM_Task(vmMOR, relSpec, pri, true); String res = cb.getServiceUtil().WaitForTask(taskMOR); if (res.Equals("sucess")) { Console.WriteLine("Relocation done successfully of " + vmname + " to host " + tHost); } else { Console.WriteLine("Error:: Relocation failed"); } } catch (Exception e) { Console.WriteLine(e.Message); } }
private Boolean checkRelocation(ManagedObjectReference vmMOR, ManagedObjectReference hostMOR, ManagedObjectReference poolMOR, ManagedObjectReference dsMOR) { Boolean relocate = false; try { VirtualMachineRelocateSpec relSpec = new VirtualMachineRelocateSpec(); relSpec.datastore = (dsMOR); relSpec.host = (hostMOR); relSpec.pool = (poolMOR); ManagedObjectReference taskMOR = cb.getConnection().Service.CheckRelocate_Task(provisionChkr, vmMOR, relSpec, null); String res = monitorTask(taskMOR); if (res.Equals("sucess")) { relocate = true; } else { relocate = false; } } catch (Exception) { relocate = false; } return relocate; }
public IVMConnection CreateLinkedClone(string snapshotName, string linkedVMName) { var snapshot = VimHelper.GetDynamicProperty<VirtualMachineSnapshotInfo>(MOB, "snapshot"); if (snapshot != null) { var snapshotMOB = FindSnapshotByName(snapshot.rootSnapshotList, snapshotName); if (snapshotMOB != null) { var relSpec = new VirtualMachineRelocateSpec(); relSpec.diskMoveType = "createNewChildDiskBacking"; var cloneSpec = new VirtualMachineCloneSpec(); cloneSpec.powerOn = false; cloneSpec.template = false; cloneSpec.location = relSpec; cloneSpec.snapshot = snapshotMOB; ManagedObjectReference folder = VimHelper.GetDynamicProperty<ManagedObjectReference>(MOB, "parent"); ManagedObjectReference task = VimHelper.ServiceInstance.CloneVM_Task(MOB, folder, linkedVMName, cloneSpec); while (true) { TaskInfo info = VimHelper.GetDynamicProperty<TaskInfo>(task, "info"); if (info.state == TaskInfoState.success) { return new VSphereVMConnection(info.result as ManagedObjectReference); } else if (info.state == TaskInfoState.error) { throw new Exception(info.error.localizedMessage); } System.Threading.Thread.Sleep(500); } } } return null; }
private void cloneVM() { _service = cb.getConnection()._service; _sic = cb.getConnection()._sic; String cloneName = cb.get_option("CloneName"); String vmPath = cb.get_option("vmPath"); String datacenterName = cb.get_option("DatacenterName"); // Find the Datacenter reference by using findByInventoryPath(). ManagedObjectReference datacenterRef = _service.FindByInventoryPath(_sic.searchIndex, datacenterName); if (datacenterRef == null) { Console.WriteLine("The specified datacenter is not found"); return; } // Find the virtual machine folder for this datacenter. ManagedObjectReference vmFolderRef = (ManagedObjectReference)cb.getServiceUtil().GetMoRefProp(datacenterRef, "vmFolder"); if (vmFolderRef == null) { Console.WriteLine("The virtual machine is not found"); return; } ManagedObjectReference vmRef = _service.FindByInventoryPath(_sic.searchIndex, vmPath); if (vmRef == null) { Console.WriteLine("The virtual machine is not found"); return; } VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec(); cloneSpec.location = relocSpec; cloneSpec.powerOn = false; cloneSpec.template = false; String clonedName = cloneName; Console.WriteLine("Launching clone task to create a clone: " + clonedName); try { ManagedObjectReference cloneTask = _service.CloneVM_Task(vmRef, vmFolderRef, clonedName, cloneSpec); String status = cb.getServiceUtil().WaitForTask(cloneTask); if (status.Equals("failure")) { Console.WriteLine("Failure -: Virtual Machine cannot be cloned"); } if (status.Equals("sucess")) { Console.WriteLine("Virtual Machine Cloned successfully."); } else { Console.WriteLine("Virtual Machine Cloned cannot be cloned"); } } catch (Exception e) { } }