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)); }
private static void Reserve(AssetInventoryContext entities, string hostName, string platformUsage, DateTime lastUpdated, string sessionId, DataEnvironment environment) { FrameworkClient vmReservation = Select(entities, hostName); vmReservation.UsageState = EnumUtil.GetDescription(VMUsageState.Reserved); vmReservation.PlatformUsage = platformUsage; vmReservation.LastUpdated = lastUpdated; vmReservation.SessionId = sessionId; vmReservation.Environment = environment.ToString(); }
/// <summary> /// Selects the <see cref="VirtualMachine"/> with the given host name. /// </summary> /// <param name="hostName">The host name</param> /// <returns></returns> public static VirtualMachine Select(string hostName) { FrameworkClient reservation = null; using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { reservation = context.FrameworkClients.FirstOrDefault(n => n.FrameworkClientHostName.Equals(hostName, StringComparison.OrdinalIgnoreCase)); } return((reservation == null) ? null : new VirtualMachine(reservation)); }
private static void SetEnvironment(string hostName, DataEnvironment environment, AssetInventoryContext entities) { FrameworkClient machine = Select(entities, hostName); if (machine != null) { machine.Environment = environment.ToString(); } else { TraceFactory.Logger.Error("{0} not found in database".FormatWith(hostName)); } }
private void UpdateVirtualMachineList(bool isChecked, FrameworkClientPlatform currentPlatform, VirtualMachineRow virtualMachineRow) { FrameworkClient frameworkClient = _assetInventoryContext.FrameworkClients.Find(virtualMachineRow.Machine.Name); if (isChecked && !frameworkClient.Platforms.Contains(currentPlatform)) { frameworkClient.Platforms.Add(currentPlatform); } else { frameworkClient.Platforms.Remove(currentPlatform); } }
public VirtualMachine(FrameworkClient reservation) { this.Name = reservation.FrameworkClientHostName; this.PowerState = reservation.PowerState; this.UsageState = reservation.UsageState; this.PlatformUsage = reservation.PlatformUsage; this.HoldId = reservation.HoldId; this.LastUpdated = reservation.LastUpdated; this.SortOrder = reservation.SortOrder; this.SessionId = reservation.SessionId; this.MachineType = "WindowsVirtual"; this.Platforms = new Collection <FrameworkClientPlatform>(reservation.Platforms.ToList()); }
/// <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)); } }
/// <summary> /// Gets a replacement VM if one is not responding. /// </summary> /// <param name="replacedVMName">The name of the replaced VM.</param> /// <returns></returns> public static VirtualMachine GetReplacement(string replacedVMName) { VirtualMachine replacement = null; using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { //Get the SessionId that tried to power-on the failed machine FrameworkClient replacedVirtualMachine = Select(context, replacedVMName); LockToken lockToken = new GlobalLockToken("VirtualMachineReservation", new TimeSpan(0, 1, 0), new TimeSpan(0, 2, 0)); ExecutionServices.CriticalSection.Run(lockToken, () => { //Get the next available machine replacement = VirtualMachine.SelectReplacement(replacedVirtualMachine.PlatformUsage, replacedVirtualMachine.HoldId); if (replacement != null) { Reserve ( context, replacement.Name, replacedVirtualMachine.PlatformUsage, DateTime.Now, replacedVirtualMachine.SessionId, GlobalSettings.Environment ); context.SaveChanges(); } else { throw new VMInventoryException("Insufficient VMs available."); } }); } return(replacement); }