private static void UpdateInventory() { int count = 0; TraceFactory.Logger.Debug("Synchronizing VM inventory with vSphere server."); using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { using (var vSphereController = GetVSphereController()) { foreach (VSphereVirtualMachine serverVM in vSphereController.GetVirtualMachines()) { FrameworkClient inventoryVM = Select(context, serverVM.HostName); if (inventoryVM != null) { VMUsageState currentState = EnumUtil.GetByDescription <VMUsageState>(inventoryVM.UsageState); if (currentState != VMUsageState.Unavailable && currentState != VMUsageState.DoNotSchedule && string.IsNullOrEmpty(inventoryVM.SessionId)) { inventoryVM.PowerState = EnumUtil.GetDescription(GetPowerState(serverVM)); inventoryVM.UsageState = EnumUtil.GetDescription(GetUsageState(serverVM)); inventoryVM.LastUpdated = DateTime.Now; } count++; } } } context.SaveChanges(); } TraceFactory.Logger.Debug("Syncronization complete. VM Count: {0}".FormatWith(count)); }
/// <summary> /// Gets the VM list. /// </summary> /// <param name="sessionId">The session id.</param> /// <param name="usageState">State of the usage.</param> /// <returns></returns> private static Collection <VirtualMachine> GetMachineList(string sessionId, VMUsageState usageState) { var reservedVMList = new Collection <VirtualMachine>(); foreach (var machine in VirtualMachine.Select(sessionId: sessionId, usageState: usageState)) { reservedVMList.Add(machine); } return(reservedVMList); }
private void SetUsage(GridViewSelectedRowsCollection selectedRows, VMUsageState state) { try { this.Cursor = Cursors.WaitCursor; var hostNames = selectedRows.Select(n => n.Cells[GridColumns.NAME_GRID_COLUMN].Value.ToString()); VMInventoryManager.SetUsageState(hostNames, state); RefreshGrid(); } finally { this.Cursor = Cursors.Default; } }
/// <summary> /// Gets the VM Power state. /// </summary> /// <param name="usageState">State of the usage.</param> /// <returns></returns> private static VMPowerState GetPowerState(VMUsageState usageState) { switch (usageState) { case VMUsageState.Available: case VMUsageState.DoNotSchedule: case VMUsageState.Unavailable: case VMUsageState.Reserved: return(VMPowerState.PoweredOff); case VMUsageState.InUse: return(VMPowerState.PoweredOn); default: throw new ArgumentException("Unable to return power state for {0}.".FormatWith(usageState), usageState.ToString()); } }
/// <summary> /// Sets the VMs Usage state. /// </summary> /// <param name="hostNames">The host names collection</param> /// <param name="usageState">The usage state</param> public static void SetUsageState(IEnumerable <string> hostNames, VMUsageState usageState) { if (hostNames == null) { throw new ArgumentNullException("hostNames"); } VMPowerState powerState = GetPowerState(usageState); using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { foreach (string hostName in hostNames) { SetState(hostName, powerState, usageState, (usageState == VMUsageState.Available), context); } context.SaveChanges(); } }
/// <summary> /// Selects all <see cref="VirtualMachineReservation"/>s with the specified parameters. /// </summary> /// <param name="entities">The entities.</param> /// <param name="powerState">If specified, filters results to those with the specified <see cref="VMPowerState"/>.</param> /// <param name="usageState">If specified, filters results to those with the specified <see cref="VMUsageState"/>.</param> /// <param name="sessionId">If specified, filters results to those with the specified session id.</param> /// <param name="holdId">If specified, filters results to those with the specified hold id.</param> /// <returns></returns> public static IQueryable <FrameworkClient> Select ( AssetInventoryContext entities, VMPowerState powerState = VMPowerState.None, VMUsageState usageState = VMUsageState.None, string sessionId = NoCriteria, string holdId = NoCriteria ) { if (entities == null) { throw new ArgumentNullException("entities"); } IQueryable <FrameworkClient> results = entities.FrameworkClients.Include(n => n.Platforms); if (powerState != VMPowerState.None) { string powerStateValue = EnumUtil.GetDescription(powerState); results = results.Where(n => n.PowerState == powerStateValue); } if (usageState != VMUsageState.None) { string usageStateValue = EnumUtil.GetDescription(usageState); results = results.Where(n => n.UsageState == usageStateValue); } if (sessionId != NoCriteria) { results = (string.IsNullOrEmpty(sessionId)) ? results.Where(n => string.IsNullOrEmpty(n.SessionId)) : results.Where(n => n.SessionId == sessionId); } if (holdId != NoCriteria) { results = (string.IsNullOrEmpty(holdId)) ? results.Where(n => string.IsNullOrEmpty(n.HoldId)) : results.Where(n => n.HoldId == holdId); } return(results.OrderBy(n => n.SortOrder)); }
/// <summary> /// Sets the state of the VM. /// </summary> /// <param name="hostName">Name of the machine.</param> /// <param name="powerState">State of the power.</param> /// <param name="usageState">State of the usage.</param> /// <param name="clearSession">if set to <c>true</c> [clear session].</param> /// <param name="entities">The entities.</param> private static void SetState(string hostName, VMPowerState powerState, VMUsageState usageState, bool clearSession, AssetInventoryContext entities) { FrameworkClient machine = Select(entities, hostName); if (machine != null) { if (clearSession) { machine.SessionId = null; machine.Environment = string.Empty; } machine.PowerState = EnumUtil.GetDescription(powerState); machine.UsageState = EnumUtil.GetDescription(usageState); machine.PlatformUsage = string.Empty; machine.LastUpdated = DateTime.Now; } else { TraceFactory.Logger.Error("{0} not found in database".FormatWith(hostName)); } }
private void SetUsage(VMUsageState state) { if (vm_GridView.SelectedRows.Count == 0) { return; } var selectedRows = vm_GridView.SelectedRows; var usages = selectedRows.Select(x => x.Cells[GridColumns.USAGESTATE_GRID_COLUMN].Value).Cast <string>(); if (!usages.All(x => x.Equals(usages.First()))) { var result = MessageBox.Show("You have selected to change usage state on machines that currently have different states. Do you really want to change all of them?", "Set Machine State", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (result != DialogResult.Yes) { return; } } SetUsage(selectedRows, state); }
public void SyncInventory(object state) { TraceFactory.Logger.Debug($"Synchronizing VM inventory with {_vCenterUri.ToString()}."); List <VSphereVirtualMachine> serverVMs = new List <VSphereVirtualMachine>(); using (var vSphereController = new VSphereVMController(_vCenterUri, _credential)) { serverVMs.AddRange(vSphereController.GetVirtualMachines()); } TraceFactory.Logger.Debug($"Synchronizing {serverVMs.Count} virtual machines."); int updated = 0; using (AssetInventoryContext context = new AssetInventoryContext(_connectionString)) { List <FrameworkClient> inventoryVMs = context.FrameworkClients.ToList(); foreach (FrameworkClient inventoryVM in inventoryVMs) { VSphereVirtualMachine serverVM = serverVMs.FirstOrDefault(n => n.HostName == inventoryVM.FrameworkClientHostName); if (serverVM != null) { VMUsageState currentState = EnumUtil.GetByDescription <VMUsageState>(inventoryVM.UsageState); if (currentState != VMUsageState.Unavailable && currentState != VMUsageState.DoNotSchedule && string.IsNullOrEmpty(inventoryVM.SessionId)) { inventoryVM.PowerState = EnumUtil.GetDescription(GetPowerState(serverVM)); inventoryVM.UsageState = EnumUtil.GetDescription(GetUsageState(serverVM)); inventoryVM.LastUpdated = DateTime.Now; updated++; } } } context.SaveChanges(); } TraceFactory.Logger.Debug($"Synchronization complete. Updated {updated} virtual machines."); }
/// <summary> /// Selects all <see cref="VirtualMachine"/>s with the specified parameters. /// </summary> /// <param name="powerState">If specified, filters results to those with the specified <see cref="VMPowerState"/>.</param> /// <param name="usageState">If specified, filters results to those with the specified <see cref="VMUsageState"/>.</param> /// <param name="sessionId">If specified, filters results to those with the specified session id.</param> /// <param name="holdId">If specified, filters results to those with the specified hold id.</param> /// <param name="platform">If specified, filters results to those that have an association with the specified platform.</param> /// <returns></returns> public static IEnumerable <VirtualMachine> Select ( VMPowerState powerState = VMPowerState.None, VMUsageState usageState = VMUsageState.None, string sessionId = NoCriteria, string holdId = NoCriteria, string platform = NoCriteria) { Collection <VirtualMachine> result = new Collection <VirtualMachine>(); using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { foreach (FrameworkClient reservation in Select(context, powerState, usageState, sessionId, holdId)) { result.Add(new VirtualMachine(reservation)); } } if (platform != NoCriteria) { return(result.Where(v => v.Platforms.Any(p => p.FrameworkClientPlatformId == platform)).OrderBy(n => n.SortOrder)); } return(result.OrderBy(n => n.SortOrder)); }