Пример #1
0
        /// <summary>
        /// Used to direct the user to the image for the default Sql Server 2012 image
        /// </summary>
        /// <returns>A persistent VM Role containing the data to execute the image</returns>
        public static PersistentVMRole GetDefaultSqlServer2012VMRole(VmSize vmSize, string storageAccount)
        {
            // build the default endpoints
            var inputEndpoints = new InputEndpoints();

            inputEndpoints.AddEndpoint(InputEndpoint.GetDefaultRemoteDesktopSettings());
            inputEndpoints.AddEndpoint(InputEndpoint.GetDefaultSqlServerSettings());
            // add the endpoints collections to a network configuration set
            var network = new NetworkConfigurationSet
            {
                InputEndpoints = inputEndpoints
            };
            // build the windows configuration set
            var windows = new WindowsConfigurationSet
            {
                AdminPassword             = "******",
                ResetPasswordOnFirstLogon = true
            };
            OSVirtualHardDisk   osDisk   = OSVirtualHardDisk.GetSqlServerOSImage(storageAccount);
            DataVirtualHardDisk dataDisk = DataVirtualHardDisk.GetDefaultDataDisk(storageAccount);
            var disks = new DataVirtualHardDisks();

            disks.HardDiskCollection.Add(dataDisk);
            return(new PersistentVMRole
            {
                NetworkConfigurationSet = network,
                OperatingSystemConfigurationSet = windows,
                RoleSize = vmSize,
                RoleName = "Elastarole",
                HardDisks = disks,
                OSHardDisk = osDisk
            });
        }
        void PopulateVmSizeProps(AzureResourceSku source, VmSize target)
        {
            foreach (var curCapability in source.Capabilities)
            {
                switch (curCapability.Name)
                {
                case "vCPUs":
                    target.NumberOfCores = Convert.ToInt32(curCapability.Value);
                    break;

                case "MaxDataDiskCount":
                    target.MaxDataDiskCount = Convert.ToInt32(curCapability.Value);
                    break;

                case "MaxNetworkInterfaces":
                    target.MaxNetworkInterfaces = Convert.ToInt32(curCapability.Value);
                    break;

                case "OSVhdSizeMB":
                    target.OsDiskSizeInMB = Convert.ToInt32(curCapability.Value);
                    break;

                case "MemoryGB":
                    target.MemoryGB = Convert.ToInt32(curCapability.Value);
                    break;
                }
            }
        }
Пример #3
0
        private static void UpdateVmSizes(IEnumerable <AzureCatalogue> azureCatalogueResult)
        {
            try
            {
                var cmpWapDb              = new CmpWapDb();
                var cmpWapVmSizes         = cmpWapDb.FetchVmSizeInfoList(onlyActiveOnes: false).ToList();
                var newVmSizes            = new List <VmSize>();
                var azureCatalogueVmSizes = new List <AzureVmSizeArmData>();

                //Eliminate dupes, we only need them to establish the mappings in another method.
                foreach (var regionInCatalogue in azureCatalogueResult)
                {
                    azureCatalogueVmSizes.AddRange(regionInCatalogue.VmSizes);
                }
                azureCatalogueVmSizes = azureCatalogueVmSizes.Distinct(new AzureVmSizeArmData.AzureVmSizeComparer()).ToList();

                //Translate and convert each AzureVmSizeArmData object into a VmSize object.
                foreach (var vmSize in azureCatalogueVmSizes)
                {
                    if (!cmpWapVmSizes.Any(x => string.Equals(vmSize.name, x.Name, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        var newVmSize = new VmSize
                        {
                            Name             = vmSize.name,
                            Cores            = vmSize.numberOfCores,
                            Memory           = vmSize.memoryInMB,
                            MaxDataDiskCount = vmSize.maxDataDiskCount,
                            IsActive         = true,
                            CreatedBy        = "CMP WAP Extension",
                            LastUpdatedBy    = "CMP WAP Extension",
                            CreatedOn        = DateTime.UtcNow,
                            LastUpdatedOn    = DateTime.UtcNow
                        };

                        if (vmSize.memoryInMB >= 1000)
                        {
                            newVmSize.Description = vmSize.name + " - " + vmSize.numberOfCores + " Cores, " + vmSize.memoryInMB / 1000 + " GB, " + vmSize.maxDataDiskCount + " Disk";
                        }
                        else
                        {
                            newVmSize.Description = vmSize.name + " - " + vmSize.numberOfCores + " Cores, " + vmSize.memoryInMB + " MB, " + vmSize.maxDataDiskCount + " Disk";
                        }

                        newVmSizes.Add(newVmSize);
                    }
                }


                if (newVmSizes.Any())
                {
                    cmpWapDb.InsertVmSizeByBatch(newVmSizes);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception caught in UpdateVmSizes: " + ex.ToString());
            }
        }
Пример #4
0
        public static string GetDisplayTextSizeForDropdown(VmSize vmSizeInfo)
        {
            if (vmSizeInfo == null)
            {
                return("unknown");
            }

            return($"{vmSizeInfo.Key} ({vmSizeInfo.NumberOfCores} cores, {vmSizeInfo.MemoryGB} GB Memory, os disk: {vmSizeInfo.OsDiskSizeInMB}, max data disks: {vmSizeInfo.MaxDataDiskCount})");
        }
Пример #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CreateSizeModel" /> class.
        /// </summary>
        /// <param name="createSizeFromApi">The size from API.</param>
        public CreateSizeModel(VmSize createSizeFromApi)
        {
            this.VMSizeId    = createSizeFromApi.VmSizeId;
            this.Name        = createSizeFromApi.Name;
            Cores            = createSizeFromApi.Cores;
            this.Description = createSizeFromApi.Description;

            this.IsActive    = createSizeFromApi.IsActive;
            Memory           = createSizeFromApi.Memory;
            MaxDataDiskCount = createSizeFromApi.MaxDataDiskCount;
        }
Пример #6
0
 // https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/
 /// <summary>
 /// Used to construct the command to create a virtual machine deployment including the creation of a role
 /// </summary>
 /// <param name="serviceName">the name of the cloud service</param>
 /// <param name="template">the image template of the virtual machine</param>
 /// <param name="storageAccountForVhd">the storage account used to store the virtual machine images of data and os</param>
 /// <param name="size">the required size of the virtual machine</param>
 internal CreateVirtualMachineDeploymentCommand(string serviceName, string storageAccountForVhd, VirtualMachineTemplates template, VmSize size = VmSize.Small)
 {
     AdditionalHeaders["x-ms-version"] = "2012-03-01";
     OperationId        = "hostedservices";
     ServiceType        = "services";
     HttpCommand        = serviceName + "/deployments";
     VirtualMachineType = template;
     CloudServiceName   = serviceName;
     VhdStorageAccount  = storageAccountForVhd;
     Size = size;
 }
 // https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/
 /// <summary>
 /// Used to construct the command to create a virtual machine deployment including the creation of a role
 /// </summary>
 /// <param name="serviceName">the name of the cloud service</param>
 /// <param name="template">the image template of the virtual machine</param>
 /// <param name="storageAccountForVhd">the storage account used to store the virtual machine images of data and os</param>
 /// <param name="size">the required size of the virtual machine</param>
 internal CreateVirtualMachineDeploymentCommand(string serviceName, string storageAccountForVhd, VirtualMachineTemplates template, VmSize size = VmSize.Small)
 {
     AdditionalHeaders["x-ms-version"] = "2012-03-01";
     OperationId = "hostedservices";
     ServiceType = "services";
     HttpCommand = serviceName + "/deployments";
     VirtualMachineType = template;
     CloudServiceName = serviceName;
     VhdStorageAccount = storageAccountForVhd;
     Size = size;
 }
 void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
 {
     writer.WriteStartObject();
     writer.WritePropertyName("name");
     writer.WriteStringValue(Name);
     writer.WritePropertyName("count");
     writer.WriteNumberValue(Count);
     writer.WritePropertyName("vmSize");
     writer.WriteStringValue(VmSize.ToString());
     writer.WritePropertyName("dnsPrefix");
     writer.WriteStringValue(DnsPrefix);
     writer.WriteEndObject();
 }
Пример #9
0
        public void Vm_getSizeCategory()
        {
            var vmSize = new VmSize()
            {
                Key = "first", NumberOfCores = 4, MemoryGB = 3, OsDiskSizeInMB = 1000, MaxDataDiskCount = 4
            };

            var result = VmSizeUtil.GetDisplayTextSizeForDropdown(vmSize);

            var expectedResult = "first (4 cores, 3 GB Memory, os disk: 1000, max data disks: 4)";

            Assert.Equal(expectedResult, result);
        }
        bool ShouldBeExcluded(VmSize curVmSizeInDb)
        {
            if (curVmSizeInDb.Category == "unknowncategory")
            {
                return(true);
            }

            if (curVmSizeInDb.Category == "gpu" && curVmSizeInDb.Key != "Standard_NV8as_v4")
            {
                return(true);
            }

            if (curVmSizeInDb.Key.Contains("_v4"))
            {
                return(true);
            }

            return(false);
        }
 /// <summary>
 /// Selects the size of the VM to provision
 /// </summary>
 /// <param name="size">the size of the VM to provision</param>
 /// <returns>An IVirtualMachineDeployment interface</returns>
 IVirtualMachineDeployment IVirtualMachineDeployment.WithVmOfSize(VmSize size)
 {
     Properties.VmSize = size;
     return(this);
 }
Пример #12
0
 /// <summary>
 /// Used to get a single deployment containing the SQL Server 2012 image
 /// </summary>
 /// <returns>A complete deployment of the SQL Server 2012 image</returns>
 public static Deployment GetDefaultSqlServer2012Deployment(string cloudServiceName, string storageAccount,
                                                            VmSize vmSize = VmSize.Small)
 {
     return(AddPersistentVMRole(cloudServiceName, PersistentVMRole.GetDefaultSqlServer2012VMRole(vmSize, storageAccount)));
 }
Пример #13
0
 public static PersistentVMRole GetDefaultVMRole(VmSize vmSize, string storageAccount, string password, string computerName, string roleName, InputEndpoints inputEndpoints, string imageName, List<string> subnetNames)
 {
     // add the endpoints collections to a network configuration set
     var network = new NetworkConfigurationSet
     {
         InputEndpoints = inputEndpoints,
         SubnetNames = subnetNames
     };
     // build the windows configuration set
     var windows = new WindowsConfigurationSet
     {
         AdminPassword = password,
         ResetPasswordOnFirstLogon = true,
         ComputerName = computerName
     };
     OSVirtualHardDisk osDisk = OSVirtualHardDisk.GetCustomServerOSImage(storageAccount, imageName);
     DataVirtualHardDisk dataDisk = DataVirtualHardDisk.GetDefaultDataDisk(storageAccount);
     var disks = new DataVirtualHardDisks();
     disks.HardDiskCollection.Add(dataDisk);
     return new PersistentVMRole
     {
         NetworkConfigurationSet = network,
         OperatingSystemConfigurationSet = windows,
         RoleSize = vmSize,
         RoleName = roleName,
         HardDisks = disks,
         OSHardDisk = osDisk
     };
 }
Пример #14
0
 /// <summary>
 /// Used to get a single deployment containing the SQL Server 2012 image
 /// </summary>
 /// <returns>A complete deployment of the SQL Server 2012 image</returns>
 public static Deployment GetDefaultSqlServer2012Deployment(string cloudServiceName, string storageAccount,
                                                            VmSize vmSize = VmSize.Small)
 {
     return AddPersistentVMRole(cloudServiceName, PersistentVMRole.GetDefaultSqlServer2012VMRole(vmSize, storageAccount));
 }
 /// <summary>
 /// Used to direct the user to the image for the default Sql Server 2012 image
 /// </summary>
 /// <returns>A persistent VM Role containing the data to execute the image</returns>
 public static PersistentVMRole GetDefaultSqlServer2012VMRole(VmSize vmSize, string storageAccount)
 {
     // build the default endpoints
     var inputEndpoints = new InputEndpoints();
     inputEndpoints.AddEndpoint(InputEndpoint.GetDefaultRemoteDesktopSettings());
     inputEndpoints.AddEndpoint(InputEndpoint.GetDefaultSqlServerSettings());
     // add the endpoints collections to a network configuration set
     var network = new NetworkConfigurationSet
                       {
                           InputEndpoints = inputEndpoints
                       };
     // build the windows configuration set
     var windows = new WindowsConfigurationSet
                       {
                           AdminPassword = "******",
                           ResetPasswordOnFirstLogon = true
                       };
     OSVirtualHardDisk osDisk = OSVirtualHardDisk.GetSqlServerOSImage(storageAccount);
     DataVirtualHardDisk dataDisk = DataVirtualHardDisk.GetDefaultDataDisk(storageAccount);
     var disks = new DataVirtualHardDisks();
     disks.HardDiskCollection.Add(dataDisk);
     return new PersistentVMRole
                {
                    NetworkConfigurationSet = network,
                    OperatingSystemConfigurationSet = windows,
                    RoleSize = vmSize,
                    RoleName = "Elastarole",
                    HardDisks = disks,
                    OSHardDisk = osDisk
                };
 }
 /// <summary>
 /// Selects the size of the VM to provision
 /// </summary>
 /// <param name="size">the size of the VM to provision</param>
 /// <returns>An IVirtualMachineDeployment interface</returns>
 IVirtualMachineDeployment IVirtualMachineDeployment.WithVmOfSize(VmSize size)
 {
     Properties.VmSize = size;
     return this;
 }
        public async Task UpdateVmSizeCache(CancellationToken cancellationToken = default)
        {
            var currentUser = await _userService.GetCurrentUserAsync();

            var regionsFromDb = await _db.Regions.Include(r => r.VmSizeAssociations).ThenInclude(va => va.VmSize).Where(r => !r.Disabled).ToListAsync();

            if (regionsFromDb == null || (regionsFromDb != null & regionsFromDb.Count() == 0))
            {
                throw new Exception($"Could not update Vm Size Cache, No regions found in DB");
            }

            foreach (var curRegionFromDb in regionsFromDb)
            {
                _logger.LogInformation($"Updating VM Size Cache for Region: {curRegionFromDb.Key}");

                try
                {
                    var resourceSkusFromAzure = await _azureResourceSkuService.GetSKUsForRegion(curRegionFromDb.Key, "virtualMachines", filterBasedOnResponseRestrictions : true, cancellationToken);

                    if (resourceSkusFromAzure == null || (resourceSkusFromAzure != null && resourceSkusFromAzure.Count() == 0))
                    {
                        throw new Exception($"No VM SKUs found in Azure for region {curRegionFromDb.Key}");
                    }

                    _logger.LogInformation($"Updating VM Size Cache for Region: {curRegionFromDb.Key}. Found {resourceSkusFromAzure.Count()} SKUs for region");

                    var existingSizeItemsForRegion = curRegionFromDb.VmSizeAssociations.ToDictionary(r => r.VmSize.Key, r => r.VmSize);

                    var validSkusFromAzure = new HashSet <string>();

                    VmSize curVmSizeInDb;

                    foreach (var curAzureSku in resourceSkusFromAzure)
                    {
                        if (existingSizeItemsForRegion.TryGetValue(curAzureSku.Name, out curVmSizeInDb))
                        {
                            curVmSizeInDb.Category = AzureVmUtil.GetSizeCategory(curAzureSku.Name);

                            if (ShouldBeExcluded(curVmSizeInDb))
                            {
                                var toRemoveFromDb = curRegionFromDb.VmSizeAssociations.FirstOrDefault(ra => ra.VmSizeKey == curVmSizeInDb.Key);
                                curRegionFromDb.VmSizeAssociations.Remove(toRemoveFromDb);
                                await _db.SaveChangesAsync();

                                continue;
                            }
                            else
                            {
                                PopulateVmSizeProps(curAzureSku, curVmSizeInDb);
                                curVmSizeInDb.DisplayText = VmSizeUtil.GetDisplayTextSizeForDropdown(curVmSizeInDb);

                                //Get updated price for VM Size and set on db entry
                                var regionAssociation = curVmSizeInDb.RegionAssociations.Where(ra => ra.RegionKey == curRegionFromDb.Key).SingleOrDefault();
                                regionAssociation.Price = await _azureCostManagementService.GetVmPrice(curRegionFromDb.Key, curVmSizeInDb.Key);

                                await _db.SaveChangesAsync();

                                validSkusFromAzure.Add(curVmSizeInDb.Key);
                            }
                        }
                        else
                        {
                            //Size item might exist in db for other region
                            curVmSizeInDb = await _db.VmSizes.FirstOrDefaultAsync(r => r.Key == curAzureSku.Name);

                            if (curVmSizeInDb == null)
                            {
                                curVmSizeInDb = new VmSize()
                                {
                                    Key = curAzureSku.Name, CreatedBy = currentUser.UserName, Category = AzureVmUtil.GetSizeCategory(curAzureSku.Name)
                                };
                            }
                            else
                            {
                                curVmSizeInDb.Category = AzureVmUtil.GetSizeCategory(curAzureSku.Name);
                            }

                            if (ShouldBeExcluded(curVmSizeInDb))
                            {
                                //Dont want to include these
                                continue;
                            }

                            PopulateVmSizeProps(curAzureSku, curVmSizeInDb);
                            curVmSizeInDb.DisplayText = VmSizeUtil.GetDisplayTextSizeForDropdown(curVmSizeInDb);

                            //Add to lookup
                            existingSizeItemsForRegion.Add(curAzureSku.Name, curVmSizeInDb);
                            var priceOfVm = await _azureCostManagementService.GetVmPrice(curRegionFromDb.Key, curVmSizeInDb.Key);

                            //Add to DB
                            curRegionFromDb.VmSizeAssociations.Add(new RegionVmSize()
                            {
                                Region = curRegionFromDb, VmSize = curVmSizeInDb, Price = priceOfVm
                            });

                            await _db.SaveChangesAsync();

                            validSkusFromAzure.Add(curVmSizeInDb.Key);
                        }
                    }

                    //Delete those that are no longer present in Azure, or that does not pass the filter
                    foreach (var curDbSize in existingSizeItemsForRegion.Values)
                    {
                        if (!validSkusFromAzure.Contains(curDbSize.Key))
                        {
                            var toRemoveFromDb = curRegionFromDb.VmSizeAssociations.FirstOrDefault(ra => ra.VmSizeKey == curDbSize.Key);

                            if (toRemoveFromDb != null)
                            {
                                curRegionFromDb.VmSizeAssociations.Remove(toRemoveFromDb);
                            }
                        }
                    }

                    await _db.SaveChangesAsync();

                    _logger.LogInformation($"Done updating VM Size Cache for Region: {curRegionFromDb.Name}");
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Update VM Size cache: Unable to update Size cache for Region {curRegionFromDb.Key}. Se inner exception for details", ex);
                    continue;
                }
            }

            _logger.LogInformation($"Deleting Vm Size entries not associated with any region");
            foreach (var curVmSize in await _db.VmSizes.Include(s => s.RegionAssociations).ToListAsync())
            {
                if (curVmSize.RegionAssociations == null || (curVmSize.RegionAssociations != null && curVmSize.RegionAssociations.Count == 0))
                {
                    _db.VmSizes.Remove(curVmSize);
                }
            }

            await _db.SaveChangesAsync();

            //TODO: Delete size records with no associated region?
            _logger.LogInformation($"Done updating VM Size Cache");
        }