/// <summary> /// Initializes a new instance of the <see cref="Module"/> class. /// </summary> /// <param name="resourceGroupName"> /// The resource group name. /// </param> /// <param name="automationAccountName"> /// The account name. /// </param> /// <param name="webhook"> /// The Webhook. /// </param> /// <param name="webhookUri"> /// The Webhook URI /// </param> public Webhook( string resourceGroupName, string automationAccountName, Azure.Management.Automation.Models.Webhook webhook, string webhookUri = "") { Requires.Argument("resourceGroupName", resourceGroupName).NotNull(); Requires.Argument("automationAccountName", automationAccountName).NotNull(); Requires.Argument("webhook", webhook).NotNull(); this.ResourceGroupName = resourceGroupName; this.AutomationAccountName = automationAccountName; this.Name = webhook.Name; if (webhook.Properties == null) return; this.CreationTime = webhook.Properties.CreationTime.ToLocalTime(); this.Description = webhook.Properties.Description; this.ExpiryTime = webhook.Properties.ExpiryTime.ToLocalTime(); this.IsEnabled = webhook.Properties.IsEnabled; if (webhook.Properties.LastInvokedTime.HasValue) { this.LastInvokedTime = webhook.Properties.LastInvokedTime.Value.ToLocalTime(); } this.LastModifiedTime = webhook.Properties.LastModifiedTime.ToLocalTime(); this.Parameters = new Hashtable(webhook.Properties.Parameters.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)); this.RunbookName = webhook.Properties.Runbook.Name; this.WebhookURI = webhookUri; }
/// <summary> /// Initializes a new instance of the <see cref="Job"/> class. /// </summary> /// <param name="resourceGroupName"> /// The resource group name. /// </param> /// <param name="accountName"> /// The account name. /// </param> /// <param name="job"> /// The Job. /// </param> /// <exception cref="System.ArgumentException"> /// </exception> public Job(string resourceGroupName, string accountName, Azure.Management.Automation.Models.Job job) { Requires.Argument("job", job).NotNull(); Requires.Argument("accountName", accountName).NotNull(); this.ResourceGroupName = resourceGroupName; this.AutomationAccountName = accountName; if (job.Properties == null) return; this.Id = job.Properties.JobId; this.CreationTime = job.Properties.CreationTime.ToLocalTime(); this.LastModifiedTime = job.Properties.LastModifiedTime.ToLocalTime(); this.StartTime = job.Properties.StartTime.HasValue ? job.Properties.StartTime.Value.ToLocalTime() : (DateTimeOffset?)null; this.Status = job.Properties.Status; this.StatusDetails = job.Properties.StatusDetails; this.RunbookName = job.Properties.Runbook.Name; this.Exception = job.Properties.Exception; this.EndTime = job.Properties.EndTime.HasValue ? job.Properties.EndTime.Value.ToLocalTime() : (DateTimeOffset?) null; this.LastStatusModifiedTime = job.Properties.LastStatusModifiedTime; this.HybridWorker = job.Properties.RunOn; this.JobParameters = new Hashtable(StringComparer.InvariantCultureIgnoreCase); foreach (var kvp in job.Properties.Parameters) { if (0 != String.Compare(kvp.Key, Constants.JobStartedByParameterName, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase) && 0 != String.Compare(kvp.Key, Constants.JobRunOnParameterName, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase)) { this.JobParameters.Add(kvp.Key, (object)PowerShellJsonConverter.Deserialize(kvp.Value)); } } }
/// <summary> /// Initializes a new instance of the <see cref="Module"/> class. /// </summary> /// <param name="resourceGroupName"> /// The resource group name. /// </param> /// <param name="automationAccountName"> /// The account name. /// </param> /// <param name="webhook"> /// The Webhook. /// </param> /// <param name="webhookUri"> /// The Webhook URI /// </param> public Webhook( string resourceGroupName, string automationAccountName, Azure.Management.Automation.Models.Webhook webhook, string webhookUri = "") { Requires.Argument("resourceGroupName", resourceGroupName).NotNull(); Requires.Argument("automationAccountName", automationAccountName).NotNull(); Requires.Argument("webhook", webhook).NotNull(); this.ResourceGroupName = resourceGroupName; this.AutomationAccountName = automationAccountName; this.Name = webhook.Name; if (webhook.Properties == null) return; this.CreationTime = webhook.Properties.CreationTime.ToLocalTime(); this.Description = webhook.Properties.Description; this.ExpiryTime = webhook.Properties.ExpiryTime.ToLocalTime(); this.IsEnabled = webhook.Properties.IsEnabled; if (webhook.Properties.LastInvokedTime.HasValue) { this.LastInvokedTime = webhook.Properties.LastInvokedTime.Value.ToLocalTime(); } this.LastModifiedTime = webhook.Properties.LastModifiedTime.ToLocalTime(); this.Parameters = new Hashtable(StringComparer.InvariantCultureIgnoreCase); foreach (var kvp in webhook.Properties.Parameters) { this.Parameters.Add(kvp.Key, (object)PowerShellJsonConverter.Deserialize(kvp.Value)); } this.RunbookName = webhook.Properties.Runbook.Name; this.WebhookURI = webhookUri; }
/// <summary> /// Initializes a new instance of the <see cref="Job"/> class. /// </summary> /// <param name="accountName"> /// The account name. /// </param> /// <param name="Job"> /// The Job. /// </param> /// <exception cref="System.ArgumentException"> /// </exception> public Job(string accountName, Azure.Management.Automation.Models.Job job) { Requires.Argument("job", job).NotNull(); Requires.Argument("accountName", accountName).NotNull(); this.AutomationAccountName = accountName; this.Name = job.Name; this.Location = job.Location; this.Type = job.Type; this.Tags = job.Tags ?? new Dictionary<string, string>(); this.Id = job.Id; if (job.Properties == null) return; this.CreationTime = job.Properties.CreationTime.ToLocalTime(); this.LastModifiedTime = job.Properties.LastModifiedTime.ToLocalTime(); this.StartTime = job.Properties.StartTime; this.Status = job.Properties.Status; this.StatusDetails = job.Properties.StatusDetails; this.RunbookName = job.Properties.Runbook.Name; this.Exception = job.Properties.Exception; this.EndTime = job.Properties.EndTime; this.LastStatusModifiedTime = job.Properties.LastStatusModifiedTime; this.Parameters = job.Properties.Parameters ?? new Dictionary<string, string>(); }
public static PSWorkspaceCollection Create(Azure.Management.PowerBIEmbedded.Models.WorkspaceCollection workspaceCollection) { return new PSWorkspaceCollection { Id = workspaceCollection.Id, Name = workspaceCollection.Name, Location = workspaceCollection.Location }; }
public void PopulateData() { Oracle oracle = new Oracle(); string queryOracle = "SELECT * FROM " + SourceName; oracle.PopulateData(ref SourceData, queryOracle); Azure azure = new Azure(); azure.DeleteTempTable(DestinationTableName); }
public ARMStorageService(Azure.Management.Storage.Models.StorageAccount account, params string[] authenticationKeys) { _account = account; foreach (var key in authenticationKeys) { _authenticationKeys.Add(key); } }
public void PopulateData() { Oracle oracle = new Oracle(); string queryOracle = "SELECT * FROM " + SourceName + " WHERE CREATEDDATE >= SYSDATE OR MODIFIEDDATE >= SYSDATE"; oracle.PopulateData(ref SourceData, queryOracle); //Delete temp tables in destination Azure azure = new Azure(); azure.DeleteTempTable(DestinationTableName); }
public void PopulateData() { //Get source data Sanyo sanyo = new Sanyo(); sanyo.PopulateData(ref SourceData, "SELECT * FROM " + SourceName); //Delete temp tables in destination Azure azure = new Azure(); azure.DeleteTempTable(DestinationName); }
public void PopulateData() { //Get last field pk Azure azure = new Azure(); string queryAzure = "SELECT MAX() FROM " + RealTableName; string maxID = azure.GetSingleValue(queryAzure); //Get source data //Delete temp tables in destination }
public void PopulateData() { //Get all details, process in azure Oracle oracle = new Oracle(); string query = "SELECT * FROM " + SourceName; oracle.PopulateData(ref SourceData, query); //Delete temp table in destination Azure azure = new Azure(); azure.DeleteTempTable(DestinationTableName); }
internal void SetObjectIdentifier(VaultUriHelper vaultUriHelper, Azure.KeyVault.ObjectIdentifier identifier) { if (vaultUriHelper == null) throw new ArgumentNullException("vaultUriHelper"); if (identifier == null) throw new ArgumentNullException("identifier"); VaultName = vaultUriHelper.GetVaultName(identifier.Identifier); Name = identifier.Name; Version = identifier.Version; Id = identifier.Identifier; }
/// <summary> /// Initializes a new instance of the <see cref="Credential"/> class. /// </summary> /// <param name="Credential"> /// The Credential. /// </param> public Credential(string accountAcccountName, Azure.Management.Automation.Models.Credential credential) { Requires.Argument("credential", credential).NotNull(); this.AutomationAccountName = accountAcccountName; this.Name = credential.Name; if (credential.Properties == null) return; this.Description = credential.Properties.Description; this.CreationTime = credential.Properties.CreationTime.ToLocalTime(); this.LastModifiedTime = credential.Properties.LastModifiedTime.ToLocalTime(); this.UserName = credential.Properties.UserName; }
public void PopulateData() { //Get last field pk Azure azure = new Azure(); string queryAzure = "SELECT MAX(PK) FROM " + RealTableName; string maxID = azure.GetSingleValue(queryAzure); //Get source data Oracle oracle = new Oracle(); string queryOracle = String.Format("SELECT * FROM {0} WHERE {1} > {2}", SourceName, UpdateIdentifier, maxID); oracle.PopulateData(ref SourceData, queryOracle); //DELETE temp tables in destination azure.DeleteTempTable(DestinationTableName); }
/// <summary> /// Initializes a new instance of the <see cref="JobSchedule"/> class. /// </summary> /// <param name="resourceGroupName"> /// The resource group name. /// </param> /// <param name="automationAccountName"> /// The account name. /// </param> /// <param name="jobSchedule"> /// The job schedule. /// </param> public JobSchedule(string resourceGroupName, string automationAccountName, Azure.Management.Automation.Models.JobSchedule jobSchedule) { Requires.Argument("jobSchedule", jobSchedule).NotNull(); this.ResourceGroupName = resourceGroupName; this.AutomationAccountName = automationAccountName; this.JobScheduleId = jobSchedule.Properties.Id.ToString(); this.RunbookName = jobSchedule.Properties.Runbook.Name; this.ScheduleName = jobSchedule.Properties.Schedule.Name; this.Parameters = new Hashtable(StringComparer.InvariantCultureIgnoreCase); foreach (var kvp in jobSchedule.Properties.Parameters.Where(kvp => 0 != String.Compare(kvp.Key, Constants.JobStartedByParameterName, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase))) { this.Parameters.Add(kvp.Key, (object)PowerShellJsonConverter.Deserialize(kvp.Value)); } }
public void PopulateData() { //Get last field PK Azure azure = new Azure(); string queryAzure = "SELECT MAX(TransactionNumber) FROM " + RealTableName; string maxID = azure.GetSingleValue(queryAzure); //Get source data Sanyo sanyo = new Sanyo(); string querySanyo = String.Format("SELECT * FROM {0} WHERE {1} > {2}", SourceName, UpdateIdentifier, maxID); sanyo.PopulateData(ref SourceData, querySanyo); //Delete temp tables in destination azure.DeleteTempTable(DestinationTableName); }
public void ListTablesDoesNotThrowAnException() { IAzure azure = new Azure() { Account = "marksandbox", SharedKey = "ELIDED" }; var tableService = new TableService(azure); Trace.WriteLine("Working"); Assert.That(() => tableService.ListAllTables(), Throws.Nothing); Assert.True(true); }
/// <summary> /// Initializes a new instance of the <see cref="CertificateInfo"/> class. /// </summary> /// <param name="resourceGroupName"> /// The resource group name. /// </param> /// <param name="accountAcccountName"> /// The account name. /// </param> /// <param name="certificate"> /// The connection. /// </param> /// <exception cref="System.ArgumentException"> /// </exception> public CertificateInfo(string resourceGroupName, string accountAcccountName, Azure.Management.Automation.Models.Certificate certificate) { Requires.Argument("certificate", certificate).NotNull(); this.AutomationAccountName = accountAcccountName; this.ResourceGroupName = resourceGroupName; this.Name = certificate.Name; if (certificate.Properties == null) return; this.Description = certificate.Properties.Description; this.CreationTime = certificate.Properties.CreationTime.ToLocalTime(); this.LastModifiedTime = certificate.Properties.LastModifiedTime.ToLocalTime(); this.ExpiryTime = certificate.Properties.ExpiryTime.ToLocalTime(); this.Thumbprint = certificate.Properties.Thumbprint; this.Exportable = certificate.Properties.IsExportable; }
/// <summary> /// Initializes a new instance of the <see cref="OneTimeSchedule"/> class. /// </summary> /// <param name="schedule"> /// The schedule. /// </param> public OneTimeSchedule(Azure.Management.Automation.Models.Schedule schedule) { Requires.Argument("schedule", schedule).NotNull(); this.Id = new Guid(schedule.Id); this.AccountId = new Guid(schedule.AccountId); this.Name = schedule.Name; this.Description = schedule.Description; this.StartTime = DateTime.SpecifyKind(schedule.StartTime, DateTimeKind.Utc).ToLocalTime(); this.ExpiryTime = DateTime.SpecifyKind(schedule.ExpiryTime, DateTimeKind.Utc).ToLocalTime(); this.CreationTime = DateTime.SpecifyKind(schedule.CreationTime, DateTimeKind.Utc).ToLocalTime(); this.LastModifiedTime = DateTime.SpecifyKind(schedule.LastModifiedTime, DateTimeKind.Utc).ToLocalTime(); this.IsEnabled = schedule.IsEnabled; this.NextRun = schedule.NextRun.HasValue ? DateTime.SpecifyKind(schedule.NextRun.Value, DateTimeKind.Utc).ToLocalTime() : this.NextRun; }
/// <summary> /// Initializes a new instance of the <see cref="Schedule"/> class. /// </summary> /// <param name="schedule"> /// The schedule. /// </param> public Schedule(string automationAccountName, Azure.Management.Automation.Models.Schedule schedule) { Requires.Argument("schedule", schedule).NotNull(); this.AutomationAccountName = automationAccountName; this.Name = schedule.Name; this.Description = schedule.Properties.Description; this.StartTime = schedule.Properties.StartTime.ToLocalTime(); this.ExpiryTime = schedule.Properties.ExpiryTime.ToLocalTime(); this.CreationTime = schedule.Properties.CreationTime.ToLocalTime(); this.LastModifiedTime = schedule.Properties.LastModifiedTime.ToLocalTime(); this.IsEnabled = schedule.Properties.IsEnabled; this.NextRun = schedule.Properties.NextRun.HasValue ? schedule.Properties.NextRun.Value.ToLocalTime() : this.NextRun; this.Interval = schedule.Properties.Interval.HasValue ? schedule.Properties.Interval.Value : this.Interval; this.Frequency = (ScheduleFrequency)Enum.Parse(typeof(ScheduleFrequency), schedule.Properties.Frequency, true); }
public void PopulateData() { //Get last field PK Azure azure = new Azure(); //string queryAzure = "SELECT MAX(CONVERT(varchar, GamingDate, 6)) FROM " + RealTableName; string queryAzure = "SELECT MAX(GamingDate) FROM " + RealTableName; string maxID = azure.GetSingleValue(queryAzure); DateTime dateMax = Convert.ToDateTime(maxID); //Get source data Oracle oracle = new Oracle(); string queryOracle = String.Format("SELECT * FROM {0} WHERE {1} > '{2}'", SourceName, UpdateIdentifier, dateMax.ToString("dd/MMM/yy")); oracle.PopulateData(ref SourceData, queryOracle); //Delete temp tables in destination azure.DeleteTempTable(DestinationTableName); }
internal KeyIdentityItem(Azure.KeyVault.Models.KeyItem keyItem, VaultUriHelper vaultUriHelper) { if (keyItem == null) throw new ArgumentNullException("keyItem"); if (keyItem.Attributes == null) throw new ArgumentException(KeyVaultProperties.Resources.InvalidKeyAttributes); if (keyItem.Identifier == null) throw new ArgumentException(KeyVaultProperties.Resources.InvalidKeyIdentifier); SetObjectIdentifier(vaultUriHelper, keyItem.Identifier); Enabled = keyItem.Attributes.Enabled; Expires = keyItem.Attributes.Expires; NotBefore = keyItem.Attributes.NotBefore; Created = keyItem.Attributes.Created; Updated = keyItem.Attributes.Updated; Tags = (keyItem.Tags == null) ? null : keyItem.Tags.ConvertToHashtable(); }
internal SecretIdentityItem(Azure.KeyVault.SecretItem secretItem, VaultUriHelper vaultUriHelper) { if (secretItem == null) throw new ArgumentNullException("secretItem"); if (secretItem.Attributes == null) throw new ArgumentException(KeyVaultProperties.Resources.InvalidSecretAttributes); if (secretItem.Identifier == null) throw new ArgumentException(KeyVaultProperties.Resources.InvalidSecretIdentifier); SetObjectIdentifier(vaultUriHelper, secretItem.Identifier); Enabled = secretItem.Attributes.Enabled; Expires = secretItem.Attributes.Expires; NotBefore = secretItem.Attributes.NotBefore; Created = secretItem.Attributes.Created; Updated = secretItem.Attributes.Updated; ContentType = secretItem.ContentType; Tags = (secretItem.Tags == null) ? null : secretItem.Tags.ConvertToHashtable(); }
/// <summary> /// Internal constructor used by KeyVaultDataServiceClient /// </summary> /// <param name="secret">secret returned from service</param> /// <param name="vaultUriHelper">helper class</param> internal Secret(Azure.KeyVault.Secret secret, VaultUriHelper vaultUriHelper) { if (secret == null) throw new ArgumentNullException("secret"); SetObjectIdentifier(vaultUriHelper, secret.SecretIdentifier); if (secret.Value != null) SecretValue = secret.Value.ConvertToSecureString(); Attributes = new SecretAttributes( secret.Attributes.Enabled, secret.Attributes.Expires, secret.Attributes.NotBefore, secret.Attributes.Created, secret.Attributes.Updated, secret.ContentType, secret.Tags); }
/// <summary> /// Initializes a new instance of the <see cref="Module"/> class. /// </summary> /// <param name="Module"> /// The Module. /// </param> public Module(string automationAccountName, Azure.Management.Automation.Models.Module module) { Requires.Argument("module", module).NotNull(); this.AutomationAccountName = automationAccountName; this.Name = module.Name; this.Location = module.Location; this.Type = module.Type; this.Tags = module.Tags ?? new Dictionary<string, string>(); if (module.Properties == null) return; this.CreationTime = module.Properties.CreationTime.ToLocalTime(); this.LastPublishTime = module.Properties.LastPublishTime.ToLocalTime(); this.IsGlobal = module.Properties.IsGlobal; this.Version = module.Properties.Version; this.ProvisioningState = module.Properties.ProvisioningState.ToString(); this.ActivityCount = module.Properties.ActivityCount; this.SizeInBytes = module.Properties.SizeInBytes; }
public HybridRunbookWorkerGroup(string resourceGroupName, string accountName, Azure.Management.Automation.Models.HybridRunbookWorkerGroup hybridRunbookWorkerGroup) { Requires.Argument("accountName", accountName).NotNull(); Requires.Argument("hybridRunbookWorkerGroup", hybridRunbookWorkerGroup).NotNull(); this.ResourceGroupName = resourceGroupName; this.AutomationAccountName = accountName; this.Name = hybridRunbookWorkerGroup.Name; RunbookWorker = new List<HybridRunbookWorker>(); foreach (var worker in hybridRunbookWorkerGroup.HybridRunbookWorkers) { var hbworker = new HybridRunbookWorker(worker.IpAddress, worker.Name, worker.RegistrationDateTime); this.RunbookWorker.Add(hbworker); } }
/// <summary> /// Initializes a new instance of the <see cref="Connection"/> class. /// </summary> /// <param name="resourceGroupName"> /// The resource group name. /// </param> /// <param name="accountAcccountName"> /// The account name. /// </param> /// <param name="connection"> /// The connection. /// </param> /// <exception cref="System.ArgumentException"> /// </exception> public Connection(string resourceGroupName, string accountAcccountName, Azure.Management.Automation.Models.Connection connection) { Requires.Argument("connection", connection).NotNull(); this.ResourceGroupName = resourceGroupName; this.AutomationAccountName = accountAcccountName; this.Name = connection.Name; if (connection.Properties == null) return; this.Description = connection.Properties.Description; this.CreationTime = connection.Properties.CreationTime.ToLocalTime(); this.LastModifiedTime = connection.Properties.LastModifiedTime.ToLocalTime(); this.ConnectionTypeName = connection.Properties.ConnectionType.Name; this.FieldDefinitionValues = new Hashtable(StringComparer.InvariantCultureIgnoreCase); foreach (var kvp in connection.Properties.FieldDefinitionValues) { this.FieldDefinitionValues.Add(kvp.Key, kvp.Value.ToString()); } }
/// <summary> /// Initializes a new instance of the <see cref="Module"/> class. /// </summary> /// <param name="resourceGroupName"> /// The resource group name. /// </param> /// <param name="automationAccountName"> /// The account name. /// </param> /// <param name="module"> /// The Module. /// </param> public Module(string resourceGroupName, string automationAccountName, Azure.Management.Automation.Models.Module module) { Requires.Argument("resourceGroupName", resourceGroupName).NotNull(); Requires.Argument("automationAccountName", automationAccountName).NotNull(); Requires.Argument("module", module).NotNull(); this.ResourceGroupName = resourceGroupName; this.AutomationAccountName = automationAccountName; this.Name = module.Name; if (module.Properties == null) return; this.CreationTime = module.Properties.CreationTime.ToLocalTime(); this.LastModifiedTime = module.Properties.LastModifiedTime.ToLocalTime(); this.IsGlobal = module.Properties.IsGlobal; this.Version = module.Properties.Version; this.ProvisioningState = module.Properties.ProvisioningState.ToString(); this.ActivityCount = module.Properties.ActivityCount; this.SizeInBytes = module.Properties.SizeInBytes; }
internal KeyBundle(Azure.KeyVault.KeyBundle keyBundle, VaultUriHelper vaultUriHelper) { if (keyBundle == null) throw new ArgumentNullException("keyBundle"); if (keyBundle.Key == null || keyBundle.Attributes == null) throw new ArgumentException(KeyVaultProperties.Resources.InvalidKeyBundle); SetObjectIdentifier(vaultUriHelper,keyBundle.KeyIdentifier); Key = keyBundle.Key; Attributes = new KeyAttributes( keyBundle.Attributes.Enabled, keyBundle.Attributes.Expires, keyBundle.Attributes.NotBefore, keyBundle.Key.Kty, keyBundle.Key.KeyOps, keyBundle.Attributes.Created, keyBundle.Attributes.Updated, keyBundle.Tags); }
/// <summary> /// Attempts to add the device as a new device and swallows all exceptions /// </summary> /// <param name="oldIotHubDevice">The IoT Hub Device to add back into the IoT Hub</param> /// <returns>true if the device was added successfully, false if there was a problem adding the device</returns> public async Task<bool> TryAddDeviceAsync(Azure.Devices.Device oldIotHubDevice) { try { // the device needs to be added as a new device as the one that was saved // has an eTag value that cannot be provided when registering a new device var newIotHubDevice = new Azure.Devices.Device(oldIotHubDevice.Id) { Authentication = oldIotHubDevice.Authentication, Status = oldIotHubDevice.Status }; await AzureRetryHelper.OperationWithBasicRetryAsync<Azure.Devices.Device>(async () => await _deviceManager.AddDeviceAsync(newIotHubDevice)); } catch (Exception) { return false; } return true; }
internal async Task RunAsync(Rootobject rootobject) { var cred = SdkContext.AzureCredentialsFactory .FromServicePrincipal(S.AzureClientID, S.AzureClientSecret, S.AzureTenantId, AzureEnvironment.AzureGlobalCloud); var azure = Azure.Authenticate(cred) .WithSubscription(S.AzureSubscription); var id = DateTime.UtcNow.Ticks.ToString(); var server = azure.SqlServers .GetByResourceGroup(S.ResourceGroup, S.SqlServer); var pool = azure.SqlServers.ElasticPools .GetBySqlServer(server, S.SqlPool); var newDB = azure.SqlServers.Databases .Define($"t-{id}") .WithExistingSqlServer(server) .WithExistingElasticPool(pool) .Create(); newDB.ImportBacpac(S.DbImage) .WithStorageAccessKey(S.StorageKey) .WithSqlAdministratorLoginAndPassword(S.SqlUser, S.SqlPassword) .Execute(); var plan = azure.AppServices.AppServicePlans .GetByResourceGroup(S.AppServicePlanRG, S.AppServicePlan); var webApp = azure.WebApps .Define($"cm-web-t-{id}") .WithExistingWindowsPlan(plan) .WithExistingResourceGroup(S.AppServicePlanRG) .WithClientAffinityEnabled(false) .WithDefaultDocuments(Enumerable.Empty <string>().ToList()) .WithoutPhp() .WithPlatformArchitecture(Microsoft.Azure.Management.AppService.Fluent.PlatformArchitecture.X64) .WithWebAppAlwaysOn(true) .WithAppSettings(new Dictionary <string, string>() { { S.TenantKey, id }, { S.DbConnectionKey, string.Format(S.DbConnectionValue, id, S.SqlUser, S.SqlPassword) } }) .Create(); var host = webApp.HostNames.First(); var dns = azure.DnsZones .GetByResourceGroup(S.ResourceGroup, S.CustomDomain) .Update() .DefineCNameRecordSet(id) .WithAlias(host) .WithTimeToLive(S.DNSTtl) .Attach().Apply(); webApp.Update() .DefineHostnameBinding() .WithThirdPartyDomain(S.CustomDomain) .WithSubDomain(id) .WithDnsRecordType(Microsoft.Azure.Management.AppService.Fluent.Models.CustomHostNameDnsRecordType.CName) .Attach().Apply(); var deploy = webApp.Deploy() .WithPackageUri(S.WebAppImage) .WithExistingDeploymentsDeleted(true) .Execute(); if (deploy.Complete) { Console.WriteLine($"Deployed {rootobject.RequestID}-{rootobject.TenantName} with ID: {id}"); var client = new EventGridClient(new TopicCredentials(S.EventGridTopicKey)); await client.PublishEventsAsync(new Uri(S.EventGridTopicEndpoint).Host, new List <EventGridEvent> { new EventGridEvent() { Id = rootobject.RequestID, Subject = id, DataVersion = "1.0", EventTime = DateTime.UtcNow, EventType = "CMakers.Tenant.Created", Data = new DataModel { URL = $"http://{id}.{S.CustomDomain}" } } }); } }
static async Task CreateVM(string[] args) { bool invalidOptions = false; // parse args var agentConfig = new ArgsOption(); var result = Parser.Default.ParseArguments <ArgsOption>(args) .WithParsed(options => agentConfig = options) .WithNotParsed(error => { Util.Log($"Fail to parse the options: {error}"); invalidOptions = true; }); if (invalidOptions) { Util.Log("Invalid options"); return; } var sw = new Stopwatch(); sw.Start(); // auth file Util.Log($"auth file: {agentConfig.AuthFile}"); var credentials = SdkContext.AzureCredentialsFactory .FromFile(agentConfig.AuthFile); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); var img = await Util.GetVMImageWithRetry(azure, agentConfig.ImgResourceGroup, agentConfig.ImgName, agentConfig.MaxRetry); if (img == null) { throw new Exception("Fail to get custom image"); } Util.Log($"Customized image id: {img.Id}"); var region = img.Region; Util.Log($"target region: {region.Name}"); var VmSize = VirtualMachineSizeTypes.Parse(agentConfig.VmSize); Util.Log($"VM size: {VmSize}"); string sshPubKey = null; if (agentConfig.SshPubKeyFile == null && agentConfig.VmType == 1) { Util.Log("SSH public key is not set for Linux VM"); throw new Exception("SSH public key is not specified!"); } else if (agentConfig.SshPubKeyFile != null && agentConfig.VmType == 1) { sshPubKey = File.ReadAllText(agentConfig.SshPubKeyFile); Util.Log($"SSH public key: {sshPubKey}"); Util.Log($"Accelerated Network: {agentConfig.AcceleratedNetwork}"); } if (agentConfig.VmType == 2 && agentConfig.Password == null) { Util.Log($"You must specify password for windows VM by -p XXXX"); return; } var resourceGroupName = agentConfig.ResourceGroup; if (!azure.ResourceGroups.Contain(resourceGroupName)) { await azure.ResourceGroups.Define(resourceGroupName).WithRegion(region).CreateAsync(); } INetwork network = null; string subNetName = null; if (!string.IsNullOrEmpty(agentConfig.VirtualNetwork)) { Util.Log("Query virtual network..."); network = await Util.GetVirtualNetworkAsync(azure, resourceGroupName, agentConfig.VirtualNetwork); if (!string.IsNullOrEmpty(agentConfig.Subnet)) { var sub = agentConfig.Subnet; foreach (var k in network.Subnets.Keys) { if (k == sub) { subNetName = k; break; } } if (subNetName == null) { throw new Exception($"The input '{agentConfig.Subnet}' is invalid"); } } else { Util.Log("No subnet is specified, the system will select one"); foreach (var k in network.Subnets.Keys) { subNetName = k; break; } } if (subNetName == null) { throw new Exception($"No valid subnet is found"); } } else { // create virtual net Util.Log("Creating virtual network..."); subNetName = agentConfig.Prefix + "Subnet"; network = await Util.CreateVirtualNetworkWithRetry(azure, subNetName, resourceGroupName, agentConfig.Prefix + "VNet", region, agentConfig.MaxRetry); } if (network == null) { throw new Exception("Fail to create virtual network"); } Util.Log("Finish creating virtual network"); // Prepare a batch of Creatable Virtual Machines definitions var creatableVirtualMachines = new List <ICreatable <IVirtualMachine> >(); // create vms Util.Log("Creating public IP address..."); var publicIpTaskList = await Util.CreatePublicIPAddrListWithRetry(azure, agentConfig.VmCount, agentConfig.Prefix, resourceGroupName, region, agentConfig.MaxRetry); if (publicIpTaskList == null) { throw new Exception("Fail to create Public IP Address"); } Util.Log("Finish creating public IP address..."); Util.Log($"Creating network security group..."); var nsg = await Util.CreateNetworkSecurityGroupWithRetry(azure, resourceGroupName, agentConfig.Prefix + "NSG", agentConfig, region); if (nsg == null) { throw new Exception("Fail to create network security group"); } Util.Log($"Finish creating network security group..."); Util.Log("Creating network interface..."); var nicTaskList = await Util.CreateNICWithRetry(azure, resourceGroupName, agentConfig, network, publicIpTaskList, subNetName, nsg, region); if (nicTaskList == null) { throw new Exception("Fail to create NIC task list"); } Util.Log("Finish creating network interface..."); if (agentConfig.VmType == 1) { for (var i = 0; i < agentConfig.VmCount; i++) { var vmName = agentConfig.VmCount == 1 ? agentConfig.Prefix : agentConfig.Prefix + Convert.ToString(i); var vm = azure.VirtualMachines.Define(vmName) .WithRegion(region) .WithExistingResourceGroup(resourceGroupName) .WithExistingPrimaryNetworkInterface(nicTaskList[i].Result) .WithLinuxCustomImage(img.Id) .WithRootUsername(agentConfig.Username) .WithSsh(sshPubKey) .WithComputerName(agentConfig.Prefix + Convert.ToString(i)) .WithSize(VmSize); creatableVirtualMachines.Add(vm); } } else if (agentConfig.VmType == 2) { for (var i = 0; i < agentConfig.VmCount; i++) { var vmName = agentConfig.VmCount == 1 ? agentConfig.Prefix : agentConfig.Prefix + Convert.ToString(i); var vm = azure.VirtualMachines.Define(vmName) .WithRegion(region) .WithExistingResourceGroup(resourceGroupName) .WithExistingPrimaryNetworkInterface(nicTaskList[i].Result) .WithWindowsCustomImage(img.Id) .WithAdminUsername(agentConfig.Username) .WithAdminPassword(agentConfig.Password) .WithComputerName(agentConfig.Prefix + Convert.ToString(i)) .WithSize(VmSize); creatableVirtualMachines.Add(vm); } } Util.Log("Ready to create virtual machine..."); sw.Stop(); Util.Log($"prepare for creating vms elapsed time: {sw.Elapsed.TotalMinutes} min"); sw.Restart(); Util.Log($"Creating vms"); var virtualMachines = azure.VirtualMachines.Create(creatableVirtualMachines.ToArray()); Util.Log($"Finish creating vms"); Util.Log("Check SSH port"); var portCheckTaskList = new List <Task>(); for (var i = 0; i < agentConfig.VmCount; i++) { var publicIPAddress = azure.PublicIPAddresses .GetByResourceGroup(resourceGroupName, agentConfig.Prefix + Convert.ToString(i) + "PubIP"); portCheckTaskList.Add(Task.Run(async() => await WaitPortOpen(publicIPAddress.IPAddress, agentConfig.SshPort))); } if (Task.WaitAll(portCheckTaskList.ToArray(), TimeSpan.FromSeconds(120))) { Util.Log("All ports are ready"); } else { Util.Log("Not all ports are ready"); } sw.Stop(); Util.Log($"creating vms elapsed time: {sw.Elapsed.TotalMinutes} minutes"); if (agentConfig.VMHostFile != null) { var builder = new StringBuilder(); for (var i = 0; i < agentConfig.VmCount; i++) { if (i != 0) { builder.Append('|'); } builder.Append(agentConfig.Prefix).Append(i).Append(".") .Append(region.Name).Append(".cloudapp.azure.com"); } File.WriteAllText(agentConfig.VMHostFile, builder.ToString()); } if (agentConfig.BenchClientListFile != null) { var builder = new StringBuilder(); for (var i = 0; i < agentConfig.VmCount; i++) { if (i != 0) { builder.Append('|'); } builder.Append(agentConfig.Prefix).Append(i).Append(".") .Append(region.Name).Append(".cloudapp.azure.com") .Append(':').Append(agentConfig.SshPort).Append(':').Append(agentConfig.Username); } File.WriteAllText(agentConfig.BenchClientListFile, builder.ToString()); } }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = "firewall/rules")] HttpRequestMessage req, TraceWriter log) { var tenantId = Config.TenantId; var subscriptionId = Config.SubscriptionId; var clientId = Config.ClientId; var clientSecret = Config.ClientSecret; var credentials = SdkContext.AzureCredentialsFactory .FromServicePrincipal( clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud); var logLevel = HttpLoggingDelegatingHandler.Level.Basic; var azure = Azure.Configure() .WithLogLevel(logLevel) .Authenticate(credentials) .WithSubscription(subscriptionId); var resourceGroupName = Config.ResourceGroupName; log.Info($"Firewall rules on database servers in {resourceGroupName} are updating..."); var res = await azure.WebApps .Inner .ListByResourceGroupWithHttpMessagesAsync(resourceGroupName) .ConfigureAwait(false); var webapps = res.Body.ToList(); var outboundIps = webapps.SelectMany(p => p.OutboundIpAddresses.Split(',')) .Distinct() .ToList(); var tasks = new List <Task>(); var servers = await azure.SqlServers .ListByResourceGroupAsync(resourceGroupName) .ConfigureAwait(false); foreach (var server in servers) { var registeredIps = server.FirewallRules .List() .ToDictionary(p => p.Name, p => p.StartIPAddress); var ipsToExclude = registeredIps.Where(p => !outboundIps.Contains(p.Value)) .Select(p => p.Key) .ToList(); var IpsToInclude = outboundIps.Where(p => !registeredIps.ContainsValue(p)) .ToList(); var tasksToExclude = ipsToExclude.Select(ip => server.FirewallRules .DeleteAsync(ip)); var tasksToInclude = IpsToInclude.Select(ip => server.FirewallRules .Define($"webapp-{ip.Replace(".", "-")}") .WithIPAddressRange(ip, ip) .CreateAsync()); tasks.AddRange(tasksToExclude); tasks.AddRange(tasksToInclude); } await Task.WhenAll(tasks).ConfigureAwait(false); log.Info($"Firewall rules on database servers in {resourceGroupName} have been updated."); return(req.CreateResponse(HttpStatusCode.OK)); }
internal async Task <string> BuildSbConnectionString() { try { var instanceName = (_msiConfig != null) ? _msiConfig.InstanceName : _spConfig.InstanceName; // If we already have the connection string for this instance - don't go get it again. if (ConnectionStrings.TryGetValue(instanceName, out var connStr)) { return(connStr); } const string azureManagementAuthority = "https://management.azure.com/"; const string windowsLoginAuthority = "https://login.windows.net/"; string token, subscriptionId, sharedAccessPolicy; // Generate authentication token using Msi Config if it's been specified, otherwise, use Service principle. if (_msiConfig != null) { // Managed Service Identity (MSI) authentication. var provider = new AzureServiceTokenProvider(); token = provider.GetAccessTokenAsync(azureManagementAuthority, _msiConfig.TenantId).GetAwaiter().GetResult(); if (string.IsNullOrEmpty(token)) { throw new InvalidOperationException("Could not authenticate using Managed Service Identity, ensure the application is running in a secure context"); } subscriptionId = _msiConfig.SubscriptionId; instanceName = _msiConfig.InstanceName; sharedAccessPolicy = _msiConfig.SharedAccessPolicyName; } else { // Grab an authentication token from Azure. var context = new AuthenticationContext(windowsLoginAuthority + _spConfig.TenantId); var credential = new ClientCredential(_spConfig.AppId, _spConfig.AppSecret); var tokenResult = await context.AcquireTokenAsync(azureManagementAuthority, credential); if (tokenResult == null) { throw new InvalidOperationException($"Could not authenticate to {azureManagementAuthority} using supplied AppId: {_spConfig.AppId}"); } token = tokenResult.AccessToken; subscriptionId = _spConfig.SubscriptionId; instanceName = _spConfig.InstanceName; sharedAccessPolicy = _spConfig.SharedAccessPolicyName; } // Set credentials and grab the authenticated REST client. var tokenCredentials = new TokenCredentials(token); var client = RestClient.Configure() .WithEnvironment(AzureEnvironment.AzureGlobalCloud) .WithLogLevel(HttpLoggingDelegatingHandler.Level.None) .WithCredentials(new AzureCredentials(tokenCredentials, tokenCredentials, string.Empty, AzureEnvironment.AzureGlobalCloud)) .WithRetryPolicy(new RetryPolicy(new HttpStatusCodeErrorDetectionStrategy(), new FixedIntervalRetryStrategy(3, TimeSpan.FromMilliseconds(500)))) .Build(); // Authenticate against the management layer. var azureManagement = Azure.Authenticate(client, string.Empty).WithSubscription(subscriptionId); var sbNamespace = (await azureManagement.ServiceBusNamespaces.ListAsync()).FirstOrDefault(s => s.Name == instanceName); // If the namespace is not found, throw an exception. if (sbNamespace == null) { throw new InvalidOperationException($"Could not find the a service bus instance in the subscription with ID {subscriptionId}"); } // Get the built connection string. var name = await sbNamespace.AuthorizationRules.GetByNameAsync(sharedAccessPolicy); var keys = await name.GetKeysAsync(); var connectionString = keys.PrimaryConnectionString; // Cache the connection string off so we don't have to re-authenticate. if (!ConnectionStrings.ContainsKey(instanceName)) { ConnectionStrings.TryAdd(instanceName, connectionString); } // Return the connection string. return(connectionString); // NOTE: Re-implement the code in previous version of Master Branch when the intent is to use shared access policies that are topic or queue level. // When this happens, a connection string may need to be added to both the sender and receiver as a connection string generated using a lower level // access policy will mean it needs a connection per send/receive. This is why I have left this out for now (I can see from our solution we do not use // low level Shared Access Policies at the moment either. } catch (Exception e) { Logger?.LogError(e, "An exception occurred during service bus connection"); throw new ArgumentException("An exception occurred during service connection, see inner exception for more detail", e); } }
private static async Task Run() { string subscriptionIds = ConfigurationManager.AppSettings["subscriptionIds"]; string ownerTagName = ConfigurationManager.AppSettings["ownerTagName"]; string startDate = DateTime.Now.ToUniversalTime().AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); string endDate = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); string clientId = ConfigurationManager.AppSettings["clientId"]; string clientSecret = ConfigurationManager.AppSettings["clientSecret"]; string tenantId = ConfigurationManager.AppSettings["tenantId"]; AzureCredentialsFactory factory = new AzureCredentialsFactory(); AzureCredentials azureCreds = factory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud); Azure.IAuthenticated azure = Azure.Configure().Authenticate(azureCreds); string body = @" { ""type"": ""Usage"", ""timeframe"": ""Custom"", ""timePeriod"": { ""from"": """ + startDate + @""", ""to"": """ + endDate + @""", }, ""dataset"": { ""granularity"": ""Daily"", ""aggregation"": { ""totalCost"": { ""name"": ""PreTaxCost"", ""function"": ""Sum"" } }, ""grouping"": [ { ""type"": ""Dimension"", ""name"": ""ResourceGroup"" } ] } } "; string currency = String.Empty; Dictionary <string, Double> costPerUser = new Dictionary <string, Double>(); Dictionary <string, Double> costPerGroup = new Dictionary <string, Double>(); Dictionary <string, string> groupOwner = new Dictionary <string, string>(); string token = await GetOAuthTokenFromAAD(); foreach (var subscriptionId in subscriptionIds.Split(",", StringSplitOptions.RemoveEmptyEntries)) { var azureSub = azure.WithSubscription(subscriptionId); var resourceGroups = azureSub.ResourceGroups.List(); string uri = $"https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.CostManagement/query?api-version=2019-01-01"; while (!String.IsNullOrEmpty(uri)) { QueryResult result = null; int costIndex = -1; int currencyIndex = -1; int groupIndex = -1; var request = HttpWebRequest.CreateHttp(uri); request.ContentType = "application/json"; request.Method = "POST"; request.Headers.Add("Authorization", $"Bearer {token}"); try { using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(body); writer.Flush(); } var response = await request.GetResponseAsync(); var responseString = String.Empty; using (StreamReader reader = new StreamReader(response.GetResponseStream())) { responseString = reader.ReadToEnd(); } result = JsonConvert.DeserializeObject <QueryResult>(responseString); uri = result.properties.nextLink; costIndex = GetColumnIndex(result.properties.columns, "PreTaxCost"); currencyIndex = GetColumnIndex(result.properties.columns, "Currency"); groupIndex = GetColumnIndex(result.properties.columns, "ResourceGroup"); if (costIndex < 0) { Console.WriteLine($"Could not find cost index for subscription {subscriptionId}"); continue; } } catch (WebException wex) { string errorMessage = string.Empty; if (wex.Response != null) { using (StreamReader reader = new StreamReader(wex.Response.GetResponseStream())) { errorMessage = reader.ReadToEnd(); } } Console.WriteLine($"Error while calculating costs for subscription {subscriptionId}: {wex} ({errorMessage})"); } catch (Exception ex) { Console.WriteLine($"Error while calculating costs for subscription {subscriptionId}: {ex}"); } if (result != null) { foreach (var group in resourceGroups) { var resourceGroupOwner = OWNER_UNKNOWN; var defaultKeyValuePair = default(KeyValuePair <String, String>); var ownerTag = defaultKeyValuePair; if (group.Tags != null) { ownerTag = group.Tags.Where(tag => tag.Key.Equals(ownerTagName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault(); } if (!ownerTag.Equals(defaultKeyValuePair)) { resourceGroupOwner = ownerTag.Value; } //Console.WriteLine($"Calculating costs for resource group {group.Name} in subscription {subscriptionId} which belongs to {resourceGroupOwner}"); string keyNameGroup = $"{subscriptionId}/{group.Name}"; if (!costPerUser.ContainsKey(resourceGroupOwner)) { costPerUser.Add(resourceGroupOwner, 0); } if (!costPerGroup.ContainsKey(keyNameGroup)) { costPerGroup.Add(keyNameGroup, 0); } if (!groupOwner.ContainsKey(keyNameGroup)) { groupOwner.Add(keyNameGroup, resourceGroupOwner); } var groupRows = result.properties.rows.Where(rTemp => rTemp[groupIndex].ToString().Equals(group.Name, StringComparison.InvariantCultureIgnoreCase)).ToArray(); foreach (var row in groupRows) { costPerUser[resourceGroupOwner] += (Double)row[costIndex]; costPerGroup[keyNameGroup] += (Double)row[costIndex]; var currencyOfRow = (string)row[currencyIndex]; if (String.IsNullOrEmpty(currency)) { currency = currencyOfRow; } else if (!currency.Equals(currencyOfRow)) { throw new Exception("There are different currencies"); } } } } } Console.WriteLine($"##########################################"); Console.WriteLine($"Cost between {startDate} and {endDate} per resource group for unknown owner"); Console.WriteLine($"##########################################"); var subscriptionRgUnknown = costPerGroup.Where(temp => temp.Key.Split("/")[0]. Equals(subscriptionId, StringComparison.InvariantCultureIgnoreCase)); foreach (KeyValuePair <string, double> costEntry in subscriptionRgUnknown.OrderByDescending(temp => temp.Value)) { if (groupOwner[costEntry.Key].Equals(OWNER_UNKNOWN, StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine($"{costEntry.Key}: {currency} {costEntry.Value}"); } } } Console.WriteLine($"##########################################"); Console.WriteLine($"Cost between {startDate} and {endDate} per user"); Console.WriteLine($"##########################################"); foreach (KeyValuePair <string, double> costEntry in costPerUser.OrderByDescending(temp => temp.Value)) { Console.WriteLine($"{costEntry.Key}: {currency} {costEntry.Value}"); } Console.WriteLine($"##########################################"); Console.WriteLine($"Cost between {startDate} and {endDate} per resource group"); Console.WriteLine($"##########################################"); foreach (KeyValuePair <string, double> costEntry in costPerGroup.OrderByDescending(temp => temp.Value)) { Console.WriteLine($"{costEntry.Key}: {currency} {costEntry.Value} (owner: {groupOwner[costEntry.Key]})"); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogWarning("AutoScaleManager:::: Task to check Scaling requirement.. Started (ASM Version : V3.1)"); var subscriptionId = System.Environment.GetEnvironmentVariable("SUBSCRIPTION_ID", EnvironmentVariableTarget.Process); var resoureGroupName = System.Environment.GetEnvironmentVariable("RESOURCE_GROUP_NAME", EnvironmentVariableTarget.Process); var vmScalesetName = System.Environment.GetEnvironmentVariable("VMSS_NAME", EnvironmentVariableTarget.Process); var minFTDCountStr = System.Environment.GetEnvironmentVariable("MIN_FTD_COUNT", EnvironmentVariableTarget.Process); var maxFTDCountStr = System.Environment.GetEnvironmentVariable("MAX_FTD_COUNT", EnvironmentVariableTarget.Process); var sampleTimeMin = System.Environment.GetEnvironmentVariable("SAMPLING_TIME_MIN", EnvironmentVariableTarget.Process); var scaleOutThresholdCpuStr = System.Environment.GetEnvironmentVariable("SCALE_OUT_THRESHLD_CPU", EnvironmentVariableTarget.Process); var scaleInThresholdCpuStr = System.Environment.GetEnvironmentVariable("SCALE_IN_THRESHLD_CPU", EnvironmentVariableTarget.Process); var scaleOutThresholdMemStr = System.Environment.GetEnvironmentVariable("SCALE_OUT_THRESHLD_MEM", EnvironmentVariableTarget.Process); var scaleInThresholdMemStr = System.Environment.GetEnvironmentVariable("SCALE_IN_THRESHLD_MEM", EnvironmentVariableTarget.Process); var initialDeployMethod = System.Environment.GetEnvironmentVariable("INITIAL_DEPLOYMENT_MODE", EnvironmentVariableTarget.Process); //supported STEP / BULK var scalingPolicy = System.Environment.GetEnvironmentVariable("SCALING_POLICY", EnvironmentVariableTarget.Process); // POLICY-1 / POLICY-2 var metrics = System.Environment.GetEnvironmentVariable("SCALING_METRICS_LIST", EnvironmentVariableTarget.Process).ToLower(); int minFTDCount = Convert.ToInt32(minFTDCountStr); int maxFTDCount = Convert.ToInt32(maxFTDCountStr); double scaleOutThresholdCpu = Convert.ToDouble(scaleOutThresholdCpuStr); double scaleInThresholdCpu = Convert.ToDouble(scaleInThresholdCpuStr); double scaleOutThresholdMem = Convert.ToDouble(scaleOutThresholdMemStr); double scaleInThresholdMem = Convert.ToDouble(scaleInThresholdMemStr); int currentVmCapacity = 0; string scaleStr = ""; log.LogInformation("CPU Scale Out threshold: {0}%, Scale In threshold : {1}%", scaleOutThresholdCpu, scaleInThresholdCpu); // log.LogInformation("Memory Scale Out threshold: {0}%, Scale In threshold : {1}%", scaleOutThresholdMem, scaleInThresholdMem); //Reject if CPU scale Out Threshold < scale In Threshold if (scaleOutThresholdCpu <= scaleInThresholdCpu) { log.LogError("AutoScaleManager:::: CPU metrics ScaleOut Threshold ({0}) is less than or equal to ScaleIn Threshold ({1}) this is not correct", scaleOutThresholdCpu, scaleInThresholdCpu); return((ActionResult) new BadRequestObjectResult("ERROR: CPU Metrics ScaleOut threshold is less than or equal to ScaleIn threshold")); } //Validate Metrics if ((!metrics.Contains("cpu")) && (!metrics.Contains("memory"))) { log.LogError("AutoScaleManager:::: Invalid metrics specified : {0} (valid metrics are CPU or CPU, Memory)", metrics); return((ActionResult) new BadRequestObjectResult("ERROR: Invalid Metrics..Can not continue")); } //Check FMC connection, If we can not connect to FMC do not continue log.LogInformation("AutoScaleManager:::: Checking FMC connection"); var getAuth = new fmcAuthClass(); string authToken = getAuth.getFmcAuthToken(log); if ("ERROR" == authToken) { log.LogError("AutoScaleManager:::: Failed to connect to FMC..Can not continue"); return((ActionResult) new BadRequestObjectResult("ERROR: Failed to connet to FMC..Can not continue")); } log.LogInformation("AutoScaleManager:::: Sampling Resource Utilization at {0}min Average", sampleTimeMin); var factory = new AzureCredentialsFactory(); var msiCred = factory.FromMSI(new MSILoginInformation(MSIResourceType.AppService), AzureEnvironment.AzureGlobalCloud); var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(msiCred).WithSubscription(subscriptionId); string resourceUri = null; var vmss = azure.VirtualMachineScaleSets.GetByResourceGroup(resoureGroupName, vmScalesetName); resourceUri = vmss.Id; if (null == resourceUri) { log.LogError("AutoScaleManager:::: Unable to get resource uri"); return((ActionResult) new BadRequestObjectResult("ERROR: Unable to get resource uri")); } currentVmCapacity = vmss.Capacity; log.LogWarning("AutoScaleManager:::: Current capacity of VMSS : {0}", currentVmCapacity); //If the VMSS capacity is '0' consider this as first deployment and spawn 'minimum FTD count' at a time if ((0 == currentVmCapacity) && (0 != minFTDCount)) { log.LogWarning("AutoScaleManager:::: Current VMSS capacity is 0, considering it as first deployment (min FTD count needed : {0}", minFTDCount); if ("BULK" == initialDeployMethod) { log.LogWarning("AutoScaleManager:::: Selected initial deployment mode is BULK"); log.LogWarning("AutoScaleManager:::: Deploying {0} number of FTDvs in scale set", minFTDCount); scaleStr = "{ \"COMMAND\": \"SCALEOUT\", \"COUNT\": \"" + minFTDCount + "\", \"TYPE\": \"INIT\" }"; return((ActionResult) new OkObjectResult(scaleStr)); } else { log.LogWarning("AutoScaleManager:::: BULK method is not selected for initial deployment.. proceeding with STEP"); scaleStr = "{ \"COMMAND\": \"SCALEOUT\", \"COUNT\": \"1\", \"TYPE\": \"REGULAR\"}"; return((ActionResult) new OkObjectResult(scaleStr)); } } //If current capacity is less than minimum FTD count requied then we need to scale-out if (currentVmCapacity < minFTDCount) { log.LogWarning("AutoScaleManager:::: Current VMSS Capacity({0}) is less than minimum FTD count ({1}) needed.. time to SCALE-OUT", currentVmCapacity, minFTDCount); scaleStr = "{ \"COMMAND\": \"SCALEOUT\", \"COUNT\": \"1\", \"TYPE\": \"REGULAR\"}"; return((ActionResult) new OkObjectResult(scaleStr)); } //-------------------------------------------------Scaling decission based on Metrics------------------------------------------------------ log.LogWarning("AutoScaleManager:::: Scaling Policy : {0}", scalingPolicy); var sampleIntervalMin = System.TimeSpan.FromMinutes(Convert.ToDouble(sampleTimeMin)); MonitorManagementClient metricClient = new MonitorManagementClient(msiCred); double ftdCpuUsage = 0; double groupCpuUsage = 0; double consolidatedCpuUsage = 0; bool scaleInRejectFlag = false; double minFtdCpuUsage = 9999; string leastCpuLoadedFtd = ""; string leastCpuLoadedFtdIndex = ""; bool memoryMetricsEnabled = false; double ftdMemUsage = 0; double groupMemUsage = 0; double consolidatedMemUsage = 0; string ftdNameWithHighMemUtilization = ""; //Get FTD's Memory if 'Memory' metrics is enabled if (metrics.Contains("memory")) { memoryMetricsEnabled = true; log.LogInformation("Memory metrics enabled"); log.LogInformation("Memory Scale Out threshold: {0}%, Scale In threshold : {1}%", scaleOutThresholdMem, scaleInThresholdMem); //Reject if Memory scale Out Threshold < scale In Threshold if (scaleOutThresholdMem <= scaleInThresholdMem) { log.LogError("AutoScaleManager:::: Memory metrics ScaleOut Threshold ({0}) is less than or equal to ScaleIn Threshold ({1}) this is not correct", scaleOutThresholdMem, scaleInThresholdMem); return((ActionResult) new BadRequestObjectResult("ERROR: Memory Metrics ScaleOut threshold is less than or equal to ScaleIn threshold")); } var getMetrics = new getMetricsClass(); var getId = new getDevIdByNameClass(); var devIds = getId.getAllDevId(authToken, log); if ("ERROR" == devIds) { log.LogError("AutoScaleManager::::Unable to get device IDs"); return((ActionResult) new StatusCodeResult(StatusCodes.Status500InternalServerError)); } //parse json object JObject o = JObject.Parse(devIds); foreach (var vm in vmss.VirtualMachines.List()) { var vmName = vm.Name.ToString(); var devId = ""; try { foreach (var item in o["items"]) { if (vmName == item["name"].ToString()) { devId = item["id"].ToString(); break; } } if (0 == devId.Length) { log.LogError("AutoScaleManager:::: Unable to get Device ID for Device Name({0})", vmName); return((ActionResult) new StatusCodeResult(StatusCodes.Status500InternalServerError)); } } catch { log.LogError("AutoScaleManager:::: Exception Occoured while parsing device id response"); return((ActionResult) new StatusCodeResult(StatusCodes.Status500InternalServerError)); } ftdMemUsage = Convert.ToDouble(getMetrics.getFtdMemoryMetrics(devId, authToken, log)); if (-1 == ftdMemUsage) { log.LogError("AutoScaleManager:::: Failed to get Memory usage of {0}", vmName); return((ActionResult) new StatusCodeResult(StatusCodes.Status500InternalServerError)); } if (ftdMemUsage > scaleInThresholdMem) { //No need to Scale-In scaleInRejectFlag = true; } log.LogInformation("AutoScaleManager:::: Memory usage of {0} is {1} %", vmName, ftdMemUsage); if ("POLICY-1" == scalingPolicy) { if (ftdMemUsage > scaleOutThresholdMem) { log.LogWarning("AutoScaleManager:::: FTD {0} has Memory Utilization of {1} % which is greater than Scale Out threshold", vmName, ftdMemUsage); ftdNameWithHighMemUtilization = vmName; break; } } else if ("POLICY-2" == scalingPolicy) { groupMemUsage += ftdMemUsage; } } groupMemUsage /= vmss.Capacity; if ("POLICY-2" == scalingPolicy) { log.LogInformation("AutoScaleManager:::: Group Memory average usage : {0} %", groupMemUsage); } } else { //Memory metrics not enabled, reset thresholds scaleOutThresholdMem = 0; } if ("POLICY-2" == scalingPolicy) { log.LogInformation("AutoScaleManager:::: Scaling Policy-2 Selected..Getting average CPU utilization of scale set"); var response = await metricClient.Metrics.ListAsync(resourceUri, null, null, sampleIntervalMin, "Percentage CPU", "Average"); foreach (var metric in response.Value) { foreach (var series in metric.Timeseries) { foreach (var point in series.Data) { if (point.Average.HasValue) { groupCpuUsage = point.Average.Value; log.LogDebug("AutoScaleManager:::: avg cpu: {0}", groupCpuUsage); } } } } log.LogInformation("AutoScaleManager:::: Group CPU average usage : {0} %", groupCpuUsage); } foreach (var vm in vmss.VirtualMachines.List()) { var vmName = vm.Name; ftdCpuUsage = 0; //Metrics filter ODataQuery <MetadataValue> odataFilterMetrics = new ODataQuery <MetadataValue>(string.Format("VMName eq '{0}'", vmName)); // log.LogInformation("AutoScaleManager:::: Getting Metrics for : {0}", vmName); var response = await metricClient.Metrics.ListAsync(resourceUri, odataFilterMetrics, null, sampleIntervalMin, "Percentage CPU", "Average"); foreach (var metric in response.Value) { foreach (var series in metric.Timeseries) { foreach (var point in series.Data) { if (point.Average.HasValue) { ftdCpuUsage = point.Average.Value; log.LogDebug("AutoScaleManager:::: avg cpu: {0}", ftdCpuUsage); } } } } log.LogInformation("AutoScaleManager:::: Avg CPU Utilizatio of VM({0}) in last {1}min : {2}%", vmName, sampleTimeMin, ftdCpuUsage); //Maintain the FTD with minimum utilization to scale-in if needed if (ftdCpuUsage < minFtdCpuUsage) { minFtdCpuUsage = ftdCpuUsage; leastCpuLoadedFtd = vmName; leastCpuLoadedFtdIndex = vm.InstanceId; } if ("POLICY-1" == scalingPolicy) { //Average usage of individual Instance consolidatedCpuUsage = ftdCpuUsage; consolidatedMemUsage = ftdMemUsage; } else if ("POLICY-2" == scalingPolicy) { //Scale Set average utilization consolidatedCpuUsage = groupCpuUsage; consolidatedMemUsage = groupMemUsage; } else { log.LogError("AutoScaleManager:::: Invalid Scaling Policy {0}", scalingPolicy); return((ActionResult) new BadRequestObjectResult("ERROR: Invalid Scaling Policy")); } //If CPU utilization is greater than scale-out threshold then Scale-Out //Note: if memory metrics is not enabled then consolidatedMemUsage will be always 0 if ((consolidatedCpuUsage > scaleOutThresholdCpu) || (consolidatedMemUsage > scaleOutThresholdMem)) { //If current capacity is equal to max FTD count required then do nothing //If current capacity is more than max FTD count (This should never happen) do nothing if (currentVmCapacity >= maxFTDCount) { log.LogWarning("AutoScaleManager:::: Current VMSS Capacity({0}) is greater than or equal to max FTD count ({1}) needed.. No action needed", currentVmCapacity, maxFTDCount); return((ActionResult) new OkObjectResult("NOACTION")); } if ("POLICY-1" == scalingPolicy) { log.LogWarning("AutoScaleManager:::: Avg CPU Utilizatio of VM({0}) in last {1}min is {2}% ", vmName, sampleTimeMin, consolidatedCpuUsage); if (memoryMetricsEnabled && (consolidatedMemUsage > scaleOutThresholdMem)) { log.LogWarning("AutoScaleManager:::: Avg Memory Utilizatio of VM({0}) is {1}% ", ftdNameWithHighMemUtilization, consolidatedMemUsage); } log.LogWarning("AutoScaleManager:::: Time to SCALE OUT"); } else if ("POLICY-2" == scalingPolicy) { log.LogWarning("AutoScaleManager:::: Avg CPU Utilizatio of Scale Set in last {0}min is {1}% ", sampleTimeMin, consolidatedCpuUsage); if (memoryMetricsEnabled) { log.LogWarning("AutoScaleManager:::: Avg Memory Utilizatio of Scale Set is {0}% ", consolidatedMemUsage); } log.LogWarning("AutoScaleManager:::: Average resource utilization of scale set is more than Scale Out threshold.. Time to SCALE OUT"); } scaleStr = "{ \"COMMAND\": \"SCALEOUT\", \"COUNT\": \"1\", \"TYPE\": \"REGULAR\" }"; return((ActionResult) new OkObjectResult(scaleStr)); } //If any VM's CPU utilization is greater than scale-in threshold then Scale-In is not needed else if (ftdCpuUsage > scaleInThresholdCpu) { scaleInRejectFlag = true; } } //if scaleInRejectFlag is not set, it means all the VM's CPU & Memory utilization is less than or equal to Scale-In threshold //Hence considering only least CPU consuming FTDv for Scale-In operation if (false == scaleInRejectFlag) { //If current capacity is less than or equal to minimum FTD count requied then scale-in should not be done if (currentVmCapacity <= minFTDCount) { log.LogWarning("AutoScaleManager:::: Scale-In needed but Current VMSS Capacity({0}) is less than or equal to minimum FTD count ({1}) needed.. No Action done", currentVmCapacity, minFTDCount); return((ActionResult) new OkObjectResult("NOACTION")); } var networkInterfaceName = System.Environment.GetEnvironmentVariable("MNGT_NET_INTERFACE_NAME", EnvironmentVariableTarget.Process); var ipConfigurationName = System.Environment.GetEnvironmentVariable("MNGT_IP_CONFIG_NAME", EnvironmentVariableTarget.Process); var publicIpAddressName = System.Environment.GetEnvironmentVariable("MNGT_PUBLIC_IP_NAME", EnvironmentVariableTarget.Process); var NmClient = new NetworkManagementClient(msiCred) { SubscriptionId = azure.SubscriptionId }; var publicIp = NmClient.PublicIPAddresses.GetVirtualMachineScaleSetPublicIPAddress(resoureGroupName, vmScalesetName, leastCpuLoadedFtdIndex, networkInterfaceName, ipConfigurationName, publicIpAddressName).IpAddress; log.LogWarning("AutoScaleManager:::: CPU Utilization of all the FTD's is less than or equal to CPU Scale-In threshold({0}%).. Time to SCALE-IN", scaleInThresholdCpu); if (memoryMetricsEnabled) { log.LogWarning("AutoScaleManager:::: Memory Utilization of all the FTD's is less than or equal to Memory Scale-In threshold({0}%).. Time to SCALE-IN", scaleInThresholdMem); } log.LogWarning("AutoScaleManager:::: Least loaded FTD is : {0} with Utilization : {1}%", leastCpuLoadedFtd, minFtdCpuUsage); scaleStr = "{ \"COMMAND\": \"SCALEIN\", \"ftdDevName\": \"" + leastCpuLoadedFtd + "\", \"ftdPublicIp\": \"" + publicIp + "\", \"instanceid\" : \"" + leastCpuLoadedFtdIndex + "\" }"; return((ActionResult) new OkObjectResult(scaleStr)); } //Scaling not needed log.LogWarning("AutoScaleManager:::: FTD scaleset utilization is within threshold.. no action needed"); return((ActionResult) new OkObjectResult("NOACTION")); }
public ActionResult Delete(DeleteDataset model) { try { CloudTable endPoints = Azure.GetCloudTable(model.DataStorageName, model.DataStorageKey, Azure.Table.AvailableEndpoints); AvailableEndpoint catalog = endPoints.ExecuteQuery(new TableQuery <AvailableEndpoint>()).SingleOrDefault(x => x.alias == model.Catalog); CloudTable tableMetadata = Azure.GetCloudTable(catalog.storageaccountname, catalog.storageaccountkey, Azure.Table.TableMetadata); if (!tableMetadata.Exists()) { return(Json(new { Error = string.Format(Messages.TableDoesNotExist, Azure.Table.TableMetadata) })); } TableOperation retrieveOperation = TableOperation.Retrieve <TableMetadata>(model.PartitionKey, (model.RowKey ?? string.Empty)); TableResult retrievedResult = tableMetadata.Execute(retrieveOperation); if (retrievedResult.Result == null) { return(Json(new { Error = Messages.DatasetNotFound })); } TableMetadata entity = retrievedResult.Result as TableMetadata; string entitySet = entity.entityset; // Deleting row in TableMetadata tableMetadata.Execute(TableOperation.Delete(entity)); // Deleting rows in TableColumnsMetadata CloudTable tableColumnsMetadata = Azure.GetCloudTable(catalog.storageaccountname, catalog.storageaccountkey, Azure.Table.TableColumnsMetadata); if (tableColumnsMetadata.Exists()) { TableQuery <TableColumnsMetadata> rangeQuery = new TableQuery <TableColumnsMetadata>().Where(TableQuery.GenerateFilterCondition("entityset", QueryComparisons.Equal, entitySet)); foreach (TableColumnsMetadata tmpEntity in tableColumnsMetadata.ExecuteQuery(rangeQuery)) { tableColumnsMetadata.Execute(TableOperation.Delete(tmpEntity)); } } // Deleting rows in EntityMetadata CloudTable entityMetadata = Azure.GetCloudTable(catalog.storageaccountname, catalog.storageaccountkey, Azure.Table.EntityMetadata); if (entityMetadata.Exists()) { TableQuery <EntityMetadata> rangeQuery = new TableQuery <EntityMetadata>().Where(TableQuery.GenerateFilterCondition("entityset", QueryComparisons.Equal, entitySet)); foreach (EntityMetadata tmpEntity in entityMetadata.ExecuteQuery(rangeQuery)) { entityMetadata.Execute(TableOperation.Delete(tmpEntity)); } } // Deleting rows in ProcessorParams CloudTable processorParams = Azure.GetCloudTable(catalog.storageaccountname, catalog.storageaccountkey, Azure.Table.ProcessorParams); if (processorParams.Exists()) { TableQuery <ProcessorParams> rangeQuery = new TableQuery <ProcessorParams>().Where(TableQuery.GenerateFilterCondition("entityset", QueryComparisons.Equal, entitySet)); foreach (ProcessorParams tmpEntity in processorParams.ExecuteQuery(rangeQuery)) { processorParams.Execute(TableOperation.Delete(tmpEntity)); } } // Deleting the data table Azure.GetCloudTable(catalog.storageaccountname, catalog.storageaccountkey, entitySet).DeleteIfExists(); return(Json(new { Result = string.Empty })); } catch (Exception ex) { return(Json(new { Error = ex.Message })); } }
public static void Main(string[] args) { Logger.Info("Program startup"); Logger.Info($"Program Name: {AppName}"); _clp = new CommandLineProcessor(args, Logger); var AppId = "60c73d93-d5ab-42e8-b4d5-1b12c6ec4192"; var AuthKey = "afhuxg33dlxpu/V0lHtLMN9lhpurkCU6jENxtQ7b4uc="; var TenantID = "373afff1-2527-439e-a138-0cd72c9325e4"; var SubscriptionID = "cf8611e4-c7d7-4eac-a7cf-aa5d6c117499"; var SubscriptionName = "Pay-As-You-Go"; var ResGroups = "PROD-SVC, PROD-WEB, PROD-WEBJOB, Default-Web-EastUS"; var credentials = AzureCredentials.FromServicePrincipal(AppId, AuthKey, TenantID, AzureEnvironment.AzureGlobalCloud); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC) .Authenticate(credentials) .WithDefaultSubscription(); //GetResourceGroups(azure); var fullenchalada = new List <WebAppDto>(); foreach (var grp in ResGroups.Split(',')) { fullenchalada.AddRange(GetWebApps(azure, grp.Trim())); } foreach (WebAppDto dto in fullenchalada) { Logger.Info($"App {dto.AppName} URL: {dto.Url}"); } //var resourceGroupName = "PROD-SVC"; //TEST-SVC"; // var apigatewayresgroup = azure.ResourceGroups.GetByName("Api-Default-East-US-2"); //var export= apigatewayresgroup.ExportTemplate(ResourceGroupExportTemplateOptions.INCLUDE_BOTH); //var e = apigatewayresgroup.Inner.Name; //foreach (var gateway in azure.ResourceGroups.GetByName(["Api-Default-East-US-2"])) //{ // Console.WriteLine($"name: {gateway.Name}"); //} //azure.WebApps.Define() Logger.Info("Program End"); // Logger.Error("Something screwed up"); // Exception e = new ArgumentNullException(); // Logger.Error("Something screwed up and here is the exception", e); Console.ReadLine(); }
/// <summary> /// Initialize Resources on Azure. Create a resource group, a storage, a container inside the storage, /// and update template.json and parameter.json onto it. They will be used for deployment. /// </summary> public static void InitializeResources() { Console.WriteLine("Initialize Resources..."); // Getting the Azure AD application credentials AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile("../../azureauth.properties"); // Logging to my Azure AD azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credentials).WithDefaultSubscription(); // Creating a new ResourceGroup with the name of rgName spin.setMessage("Creating a resource Group..."); spin.Start(); var resourceGroup = azure.ResourceGroups.Define(rgName).WithRegion(location).Create(); spin.Stop(); Console.WriteLine($"Resource Group {rgName} created"); // Creating the storage linked to the resource Group spin.setMessage($"Creating the storage {accountStorageName}..."); spin.Start(); var storage = azure.StorageAccounts.Define(accountStorageName).WithRegion(location).WithExistingResourceGroup(resourceGroup).Create(); var storageKeys = storage.GetKeys(); string storageConnectionString = "DefaultEndpointsProtocol=https;" + "AccountName=" + storage.Name + ";AccountKey=" + storageKeys[0].Value + ";EndpointSuffix=core.windows.net"; //Connexion to our newly created storage account and setting up our service blob client CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnectionString); CloudBlobClient serviceClient = cloudStorageAccount.CreateCloudBlobClient(); // Creating our container //Console.WriteLine("Creating container..."); CloudBlobContainer container = serviceClient.GetContainerReference("templates"); container.CreateIfNotExistsAsync().GetAwaiter().GetResult(); BlobContainerPermissions containerPermissions = new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Container }; container.SetPermissionsAsync(containerPermissions).GetAwaiter().GetResult(); Console.WriteLine($"Storage {accountStorageName} created"); spin.Stop(); //Uploading to the container our template file spin.setMessage("Uploading template file..."); spin.Start(); CloudBlockBlob templateBlob = container.GetBlockBlobReference("template.json"); templateBlob.UploadFromFileAsync("..\\..\\Templates\\template.json").GetAwaiter().GetResult(); //uploading to the container our parameter file Console.WriteLine("Uploading parameter file..."); CloudBlockBlob paramBlob = container.GetBlockBlobReference("parameters.json"); paramBlob.UploadFromFileAsync("..\\..\\Templates\\parameters.json").GetAwaiter().GetResult(); spin.Stop(); }
public static void Main(string[] args) { // parse args var agentConfig = new ArgsOption(); var result = Parser.Default.ParseArguments <ArgsOption>(args) .WithParsed(options => agentConfig = options) .WithNotParsed(error => { }); var sw = new Stopwatch(); sw.Start(); // auth file Util.Log($"auth file: {agentConfig.AuthFile}"); var credentials = SdkContext.AzureCredentialsFactory .FromFile(agentConfig.AuthFile); //var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId:, clientSecret:, tenantId:, environment:AzureEnvironment.AzureGlobalCloud) var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); // create resource group var rnd = new Random(); var groupName = agentConfig.Prefix + "ResourceGroup"; var rndNum = Convert.ToString(rnd.Next(0, 100000) * rnd.Next(0, 100000)); var vmNameBase = agentConfig.Prefix.ToLower() + rndNum + "vm"; Region location = null; switch (agentConfig.Location.ToLower()) { case "useast": location = Region.USEast; break; case "westus": location = Region.USWest; break; default: location = Region.USEast; break; } VirtualMachineSizeTypes VmSize = null; switch (agentConfig.VmSize.ToLower()) { case "standardds1": VmSize = VirtualMachineSizeTypes.StandardDS1; break; case "d2v2": VmSize = VirtualMachineSizeTypes.StandardD2V2; break; default: break; } Console.WriteLine("Creating resource group..."); var resourceGroup = azure.ResourceGroups.Define(groupName) .WithRegion(location) .Create(); // create availability set Console.WriteLine("Creating availability set..."); var availabilitySet = azure.AvailabilitySets.Define(agentConfig.Prefix + "AVSet") .WithRegion(location) .WithExistingResourceGroup(groupName) .WithSku(AvailabilitySetSkuTypes.Managed) .Create(); // create virtual net Console.WriteLine("Creating virtual network..."); var network = azure.Networks.Define(agentConfig.Prefix + rndNum + "VNet") .WithRegion(location) .WithExistingResourceGroup(groupName) .WithAddressSpace("10.0.0.0/16") .WithSubnet(agentConfig.Prefix + rndNum + "Subnet", "10.0.0.0/24") .Create(); // Prepare a batch of Creatable Virtual Machines definitions List <ICreatable <IVirtualMachine> > creatableVirtualMachines = new List <ICreatable <IVirtualMachine> >(); // create vms for (var i = 0; i < agentConfig.VmCount; i++) { // create public ip Console.WriteLine("Creating public IP address..."); var publicIPAddress = azure.PublicIPAddresses.Define(agentConfig.Prefix + rndNum + "PublicIP" + Convert.ToString(i)) .WithRegion(location) .WithExistingResourceGroup(groupName) .WithLeafDomainLabel(agentConfig.Prefix + rndNum + "DNS" + Convert.ToString(i)) .WithDynamicIP() .Create(); // create network security group Console.WriteLine($"Creating network security group..."); var nsg = azure.NetworkSecurityGroups.Define(agentConfig.Prefix + rndNum + "NSG" + Convert.ToString(i)) .WithRegion(location) .WithExistingResourceGroup(groupName) .DefineRule("SSH-PORT") .AllowInbound() .FromAnyAddress() .FromAnyPort() .ToAnyAddress() .ToPort(22) .WithAnyProtocol() .WithPriority(100) .Attach() .DefineRule("NEW-SSH-PORT") .AllowInbound() .FromAnyAddress() .FromAnyPort() .ToAnyAddress() .ToPort(agentConfig.SshPort) .WithAnyProtocol() .WithPriority(101) .Attach() .DefineRule("BENCHMARK-PORT") .AllowInbound() .FromAnyAddress() .FromAnyPort() .ToAnyAddress() .ToPort(agentConfig.OtherPort) .WithAnyProtocol() .WithPriority(102) .Attach() .DefineRule("RPC") .AllowInbound() .FromAnyAddress() .FromAnyPort() .ToAnyAddress() .ToPort(5555) .WithAnyProtocol() .WithPriority(103) .Attach() .Create(); // create network interface Console.WriteLine("Creating network interface..."); var networkInterface = azure.NetworkInterfaces.Define(agentConfig.Prefix + rndNum + "NIC" + Convert.ToString(i)) .WithRegion(location) .WithExistingResourceGroup(groupName) .WithExistingPrimaryNetwork(network) .WithSubnet(agentConfig.Prefix + rndNum + "Subnet") .WithPrimaryPrivateIPAddressDynamic() .WithExistingPrimaryPublicIPAddress(publicIPAddress) .WithExistingNetworkSecurityGroup(nsg) .Create(); Console.WriteLine("Ready to create virtual machine..."); var vm = azure.VirtualMachines.Define(vmNameBase + Convert.ToString(i)) .WithRegion(location) .WithExistingResourceGroup(groupName) .WithExistingPrimaryNetworkInterface(networkInterface) .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts) .WithRootUsername(agentConfig.VmName) .WithRootPassword(agentConfig.VmPassWord) .WithSsh(agentConfig.Ssh) .WithComputerName(vmNameBase + Convert.ToString(i)) .WithExistingAvailabilitySet(availabilitySet) .WithSize(VmSize); creatableVirtualMachines.Add(vm); } sw.Stop(); Console.WriteLine($"prepare for creating vms elapsed time: {sw.Elapsed.TotalMinutes} min"); sw.Restart(); Console.WriteLine($"creating vms"); var virtualMachines = azure.VirtualMachines.Create(creatableVirtualMachines.ToArray()); sw.Stop(); Console.WriteLine($"creating vms elapsed time: {sw.Elapsed.TotalMinutes} min"); var errCode = 0; var res = ""; var cmd = ""; //var rndNum = "1698968314"; // TODO: only for debug var swConfig = new Stopwatch(); swConfig.Start(); // modify limit.conf and change sshd port and restart for (int i = 0; i < agentConfig.VmCount; i++) { Console.WriteLine($"modify limits: {i}th"); var domain = DomainName(agentConfig, rndNum, i); cmd = $"echo '{agentConfig.VmPassWord}' | sudo -S cp /etc/security/limits.conf /etc/security/limits.conf.bak"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, 22, agentConfig.VmPassWord, cmd, handleRes: true); cmd = $"cp /etc/security/limits.conf ~/limits.conf"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, 22, agentConfig.VmPassWord, cmd, handleRes: true); cmd = $"echo 'wanl soft nofile 655350\n' >> ~/limits.conf"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, 22, agentConfig.VmPassWord, cmd, handleRes: true); cmd = $"echo '{agentConfig.VmPassWord}' | sudo -S mv ~/limits.conf /etc/security/limits.conf"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, 22, agentConfig.VmPassWord, cmd, handleRes: true); } // install dotnet for (var i = 0; i < agentConfig.VmCount; i++) { Console.WriteLine($"install dotnet: {i}th"); var port = 22; var domain = DomainName(agentConfig, rndNum, i); cmd = $"wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, port, agentConfig.VmPassWord, cmd, handleRes: true); cmd = $"sudo dpkg -i packages-microsoft-prod.deb"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, port, agentConfig.VmPassWord, cmd, handleRes: true); cmd = $"sudo apt-get -y install apt-transport-https"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, port, agentConfig.VmPassWord, cmd, handleRes: true); cmd = $"sudo apt-get update"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, port, agentConfig.VmPassWord, cmd, handleRes: true); cmd = $"sudo apt-get -y install dotnet-sdk-2.1"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, port, agentConfig.VmPassWord, cmd, handleRes: true); } // modify sshd.conf and restart sshd for (int i = 0; i < agentConfig.VmCount; i++) { Console.WriteLine($"modify sshd_config: {i}th"); var domain = DomainName(agentConfig, rndNum, i); cmd = $"echo '{agentConfig.VmPassWord}' | sudo -S cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, 22, agentConfig.VmPassWord, cmd, handleRes: true); cmd = $"echo '{agentConfig.VmPassWord}' | sudo -S sed -i 's/22/22222/g' /etc/ssh/sshd_config"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, 22, agentConfig.VmPassWord, cmd, handleRes: true); cmd = $"echo '{agentConfig.VmPassWord}' | sudo -S service sshd restart"; (errCode, res) = ShellHelper.RemoteBash(agentConfig.VmName, domain, 22, agentConfig.VmPassWord, cmd, handleRes: true); } swConfig.Stop(); Console.WriteLine($"config elapsed time: {swConfig.Elapsed.TotalMinutes} min"); }
public List <VerificationMessage> ApplyRule(Package package, Models.Manifest manifest) { var r = new List <VerificationMessage>(); //Trace.WriteLine("SQL Scanner - started", "Information"); try { if (manifest.HasSqlScripts) { Stopwatch sw = Stopwatch.StartNew(); DotNetNukeVersionInfo highestAzureCompatableVersion = null; var majorVersion = DotNetNukeMajorVersions.DotNetNukeVersionList(); foreach (var dotNetNukeVersionInfo in majorVersion) { if (DotNetNukeVersions.ConvertNameToRank(package.MinDotNetNukeVersion) <= DotNetNukeVersions.ConvertNameToRank(dotNetNukeVersionInfo.Name)) { var databaseName = Convert.ToBase64String(Guid.NewGuid().ToByteArray()) .Substring(0, 22) .Replace("/", "_") .Replace("+", "-"); try { //create database //Trace.WriteLine("SQL Scanner - creating database: " + databaseName, "Information"); Database.CreateDatabase(databaseName); //create user for the database //Trace.WriteLine("SQL Scanner - creating database user.", "Information"); User.CreateDatabaseAdmin(databaseName); //install the base version script //Trace.WriteLine("SQL Scanner - install base DNN db objects.", "Information"); DNNBase.InstallBaseDNNScripts(databaseName, dotNetNukeVersionInfo); //list and record all DB objects //Trace.WriteLine("SQL Scanner - build list of base db objects.", "Information"); var objectListPre = Objects.GetDatabaseObjects(databaseName); //install extension scripts //Trace.WriteLine("SQL Scanner - install extension.", "Information"); foreach (var sqlScriptFile in manifest.InstallScripts) { if (!File.Exists(sqlScriptFile.TempFilePath) || sqlScriptFile.IsUnInstall) { continue; } try { Script.Execute(databaseName, File.ReadAllText(sqlScriptFile.TempFilePath)); } catch (Exception ex) { Trace.TraceError("SQL Scanner - There was a problem installing: " + sqlScriptFile.Name); var error = ex.InnerException.Message; r.Add(new VerificationMessage { Message = "While testing against " + dotNetNukeVersionInfo.Name + " " + sqlScriptFile.Name + " returned an error: " + Common.CleanError(error), MessageType = MessageTypes.Error, MessageId = new Guid("b25d95e3-06d0-4241-9729-96f85cfddcbf"), Rule = GetType().ToString() }); } } //scan and record all objects created or updated after we installed the extension. //Trace.WriteLine("SQL Scanner - Get new list of db objects post install.", "Information"); var objectListPost = Objects.GetDatabaseObjects(databaseName); //check to see if any core objects were modified. //Trace.WriteLine("SQL Scanner - finding objects that might have been updated.", "Information"); foreach (var o1 in objectListPre) { foreach (var o2 in objectListPost) { if (o1.ObjectId == o2.ObjectId && o1.ModifyDate != o2.ModifyDate) { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " it was detected that a DotNetNuke core SQL object (" + o1.Name + ") was modified by this extension.", MessageType = MessageTypes.Warning, MessageId = new Guid("50072188-f3c3-4ae0-aaed-4f3d22162e9b"), Rule = GetType().ToString() }); } } } var newObjectList = new List <DatabaseObject>(); foreach (var o1 in objectListPost) { var foundMatch = false; foreach (var o2 in objectListPre) { if (o1.ObjectId == o2.ObjectId) { foundMatch = true; } } if (!foundMatch) { newObjectList.Add(o1); } } //check for objects not correctly using {databaseOwner} & {objectQualifier} //Trace.WriteLine("SQL Scanner - Scanning for objects that might not correctly be using dbo & object qualifier tokens.", "Information"); foreach (var databaseObject in newObjectList) { if (databaseObject.SchemaId != 5 && databaseObject.Type.Trim() != "IT") { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a database object using the incorrect schema: " + databaseObject.Name, MessageType = MessageTypes.Error, MessageId = new Guid("bd4ca33b-aa33-4d5f-9468-8d8f7491bb9a"), Rule = GetType().ToString() }); } switch (databaseObject.Type.Trim()) { case "UQ": if (!databaseObject.Name.StartsWith("IX_TestQualifier_") && !databaseObject.Name.StartsWith("IX__TestQual")) { if (databaseObject.Name.Contains("TestQual")) { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a unique constraint using an non-traditional naming convention it should be IX_{objectQualifier}ObjectName: " + databaseObject.Name, MessageType = MessageTypes.Warning, MessageId = new Guid("fc6b8fa6-2863-4bdf-a55c-a84e8165a57a"), Rule = GetType().ToString() }); } else { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a unique constraint which is missing an {objectQualifier} token: " + databaseObject.Name, MessageType = MessageTypes.Warning, MessageId = new Guid("91b12382-7556-42d0-8772-fb91873232a5"), Rule = GetType().ToString() }); } } break; case "PK": if (!databaseObject.Name.StartsWith("PK_TestQualifier_") && !databaseObject.Name.StartsWith("PK__TestQual")) { if (databaseObject.Name.Contains("TestQual")) { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a primary key using an non-traditional naming convention it should be PK_{objectQualifier}ObjectName: " + databaseObject.Name, MessageType = MessageTypes.Warning, MessageId = new Guid("e0bda944-44ab-4338-abb3-8a7bc37c42ee"), Rule = GetType().ToString() }); } else { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a primary key which is missing an {objectQualifier} token: " + databaseObject.Name, MessageType = MessageTypes.Warning, MessageId = new Guid("e25340cb-0e36-4217-a79a-b6ea7faac273"), Rule = GetType().ToString() }); } } break; case "F": if (!databaseObject.Name.StartsWith("FK_TestQualifier_") && !databaseObject.Name.StartsWith("FK__TestQual")) { if (databaseObject.Name.Contains("TestQual")) { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a foreign key using an non-traditional naming convention it should be FK_{objectQualifier}ObjectName: " + databaseObject.Name, MessageType = MessageTypes.Warning, MessageId = new Guid("15c557e1-8a94-4aba-aba3-a952de46f9ba"), Rule = GetType().ToString() }); } else { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a foreign key which is missing an {objectQualifier} token: " + databaseObject.Name, MessageType = MessageTypes.Warning, MessageId = new Guid("a5620d04-16cd-467b-8d86-2f400cd04376"), Rule = GetType().ToString() }); } } break; case "D": if (!databaseObject.Name.StartsWith("DF_TestQualifier_") && !databaseObject.Name.StartsWith("DF__TestQual")) { if (databaseObject.Name.Contains("TestQual")) { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a default constraint using an non-traditional naming convention it should be DF_{objectQualifier}ObjectName: " + databaseObject.Name, MessageType = MessageTypes.Warning, MessageId = new Guid("15c557e1-8a94-4aba-aba3-a952de46f9ba"), Rule = GetType().ToString() }); } else { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a default constraint which is missing an {objectQualifier} token: " + databaseObject.Name, MessageType = MessageTypes.Warning, MessageId = new Guid("a44d52d4-0e7f-438c-9ae1-539f7251f756"), Rule = GetType().ToString() }); } } break; case "IT": break; default: if (!databaseObject.Name.StartsWith("TestQualifier_")) { if (databaseObject.Name.Contains("TestQual")) { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a database object using an non-traditional naming convention it should be {objectQualifier}ObjectName: " + databaseObject.Name, MessageType = MessageTypes.Warning, MessageId = new Guid("15c557e1-8a94-4aba-aba3-a952de46f9ba"), Rule = GetType().ToString() }); } else { r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a database object which is missing an {objectQualifier} token: " + databaseObject.Name, MessageType = MessageTypes.Warning, MessageId = new Guid("ba2855ee-73a5-4787-911b-426b7c0f24ef"), Rule = GetType().ToString() }); } } break; } } //run sp_refreshsqlmodule on all newly created stored procedures to check for errors. //Trace.WriteLine("SQL Scanner - Running sp_refreshsqlmodule on newly created objects.", "Information"); var hasInvalidObjects = false; foreach (var databaseObject in newObjectList) { if (databaseObject.TypeDesc == "SQL_STORED_PROCEDURE" || databaseObject.TypeDesc == "SQL_TRIGGER" || databaseObject.TypeDesc == "SQL_SCALAR_FUNCTION" || databaseObject.TypeDesc == "SQL_TABLE_VALUED_FUNCTION" || //databaseObject.TypeDesc == "SQL_INLINE_TABLE_VALUED_FUNCTION" || databaseObject.TypeDesc == "VIEW") { var result = Objects.CheckDatabaseObject(databaseName, databaseObject); foreach (var error in result) { hasInvalidObjects = true; r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has created a database object: " + databaseObject.Name + " with the following error: " + Common.CleanError(error), MessageType = MessageTypes.Error, MessageId = new Guid("297ccdd3-884f-4cbb-85f0-24c343a9ff2a"), Rule = GetType().ToString() }); } } } //if DNN version > 6.0 and sq_refreshsqlmodule didn't return any errors, run SQL migration wizard //Trace.WriteLine("SQL Scanner - about to run the SQL migration wizard.", "Information"); if (!hasInvalidObjects && DotNetNukeVersions.ConvertNameToRank(dotNetNukeVersionInfo.Name) >= 60000 && newObjectList.Count > 0) { //Trace.WriteLine("SQL Scanner - This extension is new enough and as passed all scans up till now so it's time to run the migration wizard.", "Information"); var hasAzureError = false; var databaseScanner = new DatabaseScanner(); var output = databaseScanner.GenerateScriptFromSourceServer( Common.BuildServerConnection(databaseName), newObjectList); foreach (var error in output) { hasAzureError = true; r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " It appears the extension has an Azure SQL compatibility issue: " + Common.CleanError(error), MessageType = MessageTypes.Error, MessageId = new Guid("98e44676-fde3-419b-bbf8-bcf785cbbf77"), Rule = GetType().ToString() }); } if (!hasAzureError) { highestAzureCompatableVersion = dotNetNukeVersionInfo; } } } catch (Exception ex) { //Trace.WriteLine("SQL Scanner - There was a problem with the migration wizard.", "Information"); var error = ex.Message; r.Add(new VerificationMessage() { Message = "While testing against " + dotNetNukeVersionInfo.Name + " the following error was returned: " + Common.CleanError(error), MessageType = MessageTypes.SystemError, MessageId = new Guid("2b0143ec-1bb6-43b1-9bae-1b114ec24e41"), Rule = GetType().ToString() }); } finally { //drop database Database.DropDatabase(databaseName); User.DropDatabaseAdmin(databaseName); } } } //Trace.WriteLine("SQL Scanner - should we run the Azure install/backup/restore process?", "Information"); if (highestAzureCompatableVersion != null) { //Trace.WriteLine("SQL Scanner - Yes, we should!", "Information"); Stopwatch sw1 = Stopwatch.StartNew(); //if SQL migration wizard passes, do the following: // clear the azure install database try { //Trace.WriteLine("SQL Scanner - clear the Azure database.", "Information"); Azure.ClearInstallAndRestoreDatabases(); } catch (Exception ex) { var error = ex.InnerException.Message; r.Add(new VerificationMessage() { Message = "While testing against SQL Azure database version " + highestAzureCompatableVersion.Name + " the cleanup failed and returned an error: " + Common.CleanError(error), MessageType = MessageTypes.SystemError, MessageId = new Guid("fdde5e65-8a39-41c5-8a9e-9d208a8df609"), Rule = GetType().ToString() }); } sw1.Stop(); r.Add(new VerificationMessage() { Message = "Processed Azure initial clean up in " + (sw1.ElapsedMilliseconds / 1000) + " seconds", MessageType = MessageTypes.Info, MessageId = new Guid("1634b15a-a18b-422c-97f4-12f058c5e03b"), Rule = GetType().ToString() }); Stopwatch sw2 = Stopwatch.StartNew(); //install the base version scripts try { //Trace.WriteLine("SQL Scanner - set up the base DNN tables in Azure.", "Information"); Azure.SetupBaseDNNDatabase(highestAzureCompatableVersion); } catch (Exception ex) { var error = ex.InnerException.Message; r.Add(new VerificationMessage() { Message = "While testing against SQL Azure database version " + highestAzureCompatableVersion.Name + " the base DNN table setup failed and returned an error: " + Common.CleanError(error), MessageType = MessageTypes.SystemError, MessageId = new Guid("898526c6-c8df-4d64-bb35-a04e5bc01044"), Rule = GetType().ToString() }); } sw2.Stop(); r.Add(new VerificationMessage() { Message = "Processed Azure initial setup in " + (sw2.ElapsedMilliseconds / 1000) + " seconds", MessageType = MessageTypes.Info, MessageId = new Guid("f7eeb9b8-6a09-453f-814f-a6a5c126c431"), Rule = GetType().ToString() }); Stopwatch sw3 = Stopwatch.StartNew(); //install extension scripts foreach (var sqlScriptFile in manifest.InstallScripts) { if (!File.Exists(sqlScriptFile.TempFilePath) || sqlScriptFile.IsUnInstall) { continue; } try { //Trace.WriteLine("SQL Scanner - Install the extensions script files in Azure.", "Information"); Azure.InstallScripts(File.ReadAllText(sqlScriptFile.TempFilePath)); } catch (Exception ex) { var error = ex.InnerException.Message; r.Add(new VerificationMessage() { Message = "While testing against SQL Azure database version " + highestAzureCompatableVersion.Name + " " + sqlScriptFile.Name + " returned an error: " + Common.CleanError(error), MessageType = MessageTypes.Error, MessageId = new Guid("84d55196-a64e-4dbe-b848-2c06ba016869"), Rule = GetType().ToString() }); } } sw3.Stop(); r.Add(new VerificationMessage() { Message = "Processed Azure module setup in " + (sw3.ElapsedMilliseconds / 1000) + " seconds", MessageType = MessageTypes.Info, MessageId = new Guid("845b161f-b3c4-4382-8cf5-c105918a891b"), Rule = GetType().ToString() }); Stopwatch sw4 = Stopwatch.StartNew(); //back up database try { //Trace.WriteLine("SQL Scanner - Create a backup of the Azure database.", "Information"); Azure.CreateDatabaseBackup(); } catch (Exception ex) { Trace.TraceError("Error while backing up the azure database."); Trace.TraceError(ex.Message); if (ex.InnerException != null) { Trace.TraceError(ex.InnerException.Message); } var error = ex.InnerException.Message; r.Add(new VerificationMessage() { Message = "While testing against SQL Azure database version " + highestAzureCompatableVersion.Name + " an attempt to backup the database resulted in the following error: " + Common.CleanError(error), MessageType = MessageTypes.Error, MessageId = new Guid("03d76ba9-e2cc-4531-a322-3147d7cd1358"), Rule = GetType().ToString() }); } sw4.Stop(); r.Add(new VerificationMessage() { Message = "Processed Azure back up in " + (sw4.ElapsedMilliseconds / 1000) + " seconds", MessageType = MessageTypes.Info, MessageId = new Guid("382d9b43-a8d0-4a69-8afe-f5f48a82d7dd"), Rule = GetType().ToString() }); Stopwatch sw5 = Stopwatch.StartNew(); //restore database try { //Trace.WriteLine("SQL Scanner - Restore the Azure database.", "Information"); Azure.RestoreDatabaseBackup(); } catch (Exception ex) { var error = ex.InnerException.Message; r.Add(new VerificationMessage() { Message = "While testing against SQL Azure database version " + highestAzureCompatableVersion.Name + " an attempt to restore the database resulted in the following error: " + Common.CleanError(error), MessageType = MessageTypes.Error, MessageId = new Guid("50274d0e-bc59-4b50-8e28-d35183a29428"), Rule = GetType().ToString() }); } sw5.Stop(); r.Add(new VerificationMessage() { Message = "Processed Azure restore in " + (sw5.ElapsedMilliseconds / 1000) + " seconds", MessageType = MessageTypes.Info, MessageId = new Guid("6bbbe53d-4537-4c09-b1b5-c4b4b6fdbdf8"), Rule = GetType().ToString() }); Stopwatch sw6 = Stopwatch.StartNew(); //Clean Up try { //Trace.WriteLine("SQL Scanner - perform the final clean up of the Azure databases and backups.", "Information"); Azure.ClearInstallAndRestoreDatabases(); } catch (Exception ex) { var error = ex.InnerException.Message; r.Add(new VerificationMessage() { Message = "While testing against SQL Azure database version " + highestAzureCompatableVersion.Name + " the cleanup failed and returned an error: " + Common.CleanError(error), MessageType = MessageTypes.SystemError, MessageId = new Guid("fdde5e65-8a39-41c5-8a9e-9d208a8df609"), Rule = GetType().ToString() }); } sw6.Stop(); r.Add(new VerificationMessage() { Message = "Processed Azure final clean up in " + (sw6.ElapsedMilliseconds / 1000) + " seconds", MessageType = MessageTypes.Info, MessageId = new Guid("e7da9c33-3ef4-430e-b8be-44188f09d5c7"), Rule = GetType().ToString() }); } sw.Stop(); r.Add(new VerificationMessage() { Message = "Processed SQL in " + (sw.ElapsedMilliseconds / 1000) + " seconds", MessageType = MessageTypes.Info, MessageId = new Guid("ad338269-f31c-4fe2-b564-4e5b44ac1b6b"), Rule = GetType().ToString() }); } } catch (Exception ex) { if (ex.InnerException != null) { var error = ex.InnerException.Message; r.Add(new VerificationMessage() { Message = "While testing the SQL scripts, the following error was returned: " + Common.CleanError(error), MessageType = MessageTypes.SystemError, MessageId = new Guid("97b6a0e6-1508-438e-a988-319e531c411c"), Rule = GetType().ToString() }); } else { var error = ex.Message; r.Add(new VerificationMessage() { Message = "While testing the SQL scripts, the following error was returned: " + Common.CleanError(error), MessageType = MessageTypes.SystemError, MessageId = new Guid("5b2ee4ba-ebc0-4230-8504-aa3365acfb0c"), Rule = GetType().ToString() }); } } return(r); }
public static void Main(string[] args) { try { //================================================================= // Authenticate var credentials = AzureCredentials.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION")); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC) .Authenticate(credentials) .WithDefaultSubscription(); // Print selected subscription Console.WriteLine("Selected subscription: " + azure.SubscriptionId); try { //============================================================= // Create a virtual network with a frontend and a backend subnets Console.WriteLine("Creating virtual network with a frontend and a backend subnets..."); var network = azure.Networks.Define(vnetName) .WithRegion(Region.US_EAST) .WithNewResourceGroup(rgName) .WithAddressSpace("172.16.0.0/16") .DefineSubnet("Front-end") .WithAddressPrefix("172.16.1.0/24") .Attach() .DefineSubnet("Back-end") .WithAddressPrefix("172.16.3.0/24") .Attach() .Create(); Console.WriteLine("Created a virtual network"); // Print the virtual network details Utilities.PrintVirtualNetwork(network); //============================================================= // Create a public IP address Console.WriteLine("Creating a public IP address..."); var publicIpAddress = azure.PublicIpAddresses.Define(publicIpName1) .WithRegion(Region.US_EAST) .WithExistingResourceGroup(rgName) .WithLeafDomainLabel(publicIpName1) .Create(); Console.WriteLine("Created a public IP address"); // Print the virtual network details Utilities.PrintIpAddress(publicIpAddress); //============================================================= // Create an Internet facing load balancer // Create a frontend IP address // Two backend address pools which contain network interfaces for the virtual // machines to receive HTTP and HTTPS network traffic from the load balancer // Two load balancing rules for HTTP and HTTPS to map public ports on the load // balancer to ports in the backend address pool // Two probes which contain HTTP and HTTPS health probes used to check availability // of virtual machines in the backend address pool // Two inbound NAT rules which contain rules that map a public port on the load // balancer to a port for a specific virtual machine in the backend address pool // - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23 Console.WriteLine("Creating a Internet facing load balancer with ..."); Console.WriteLine("- A frontend IP address"); Console.WriteLine("- Two backend address pools which contain network interfaces for the virtual\n" + " machines to receive HTTP and HTTPS network traffic from the load balancer"); Console.WriteLine("- Two load balancing rules for HTTP and HTTPS to map public ports on the load\n" + " balancer to ports in the backend address pool"); Console.WriteLine("- Two probes which contain HTTP and HTTPS health probes used to check availability\n" + " of virtual machines in the backend address pool"); Console.WriteLine("- Two inbound NAT rules which contain rules that map a public port on the load\n" + " balancer to a port for a specific virtual machine in the backend address pool\n" + " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23"); var loadBalancer1 = azure.LoadBalancers.Define(loadBalancerName1) .WithRegion(Region.US_EAST) .WithExistingResourceGroup(rgName) .DefinePublicFrontend(frontendName) .WithExistingPublicIpAddress(publicIpAddress) .Attach() // Add two backend one per rule .DefineBackend(backendPoolName1) .Attach() .DefineBackend(backendPoolName2) .Attach() // Add two probes one per rule .DefineHttpProbe(httpProbe) .WithRequestPath("/") .WithPort(80) .Attach() .DefineHttpProbe(httpsProbe) .WithRequestPath("/") .WithPort(443) .Attach() // Add two rules that uses above backend and probe .DefineLoadBalancingRule(httpLoadBalancingRule) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(80) .WithProbe(httpProbe) .WithBackend(backendPoolName1) .Attach() .DefineLoadBalancingRule(httpsLoadBalancingRule) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(443) .WithProbe(httpsProbe) .WithBackend(backendPoolName2) .Attach() // Add two nat pools to enable direct VM connectivity for // SSH to port 22 and TELNET to port 23 .DefineInboundNatRule(natRule5000to22forVM1) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(5000) .WithBackendPort(22) .Attach() .DefineInboundNatRule(natRule5001to23forVM1) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(5001) .WithBackendPort(23) .Attach() .DefineInboundNatRule(natRule5002to22forVM2) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(5002) .WithBackendPort(22) .Attach() .DefineInboundNatRule(natRule5003to23forVM2) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(5003) .WithBackendPort(23) .Attach() .Create(); // Print load balancer details Console.WriteLine("Created a load balancer"); Utilities.PrintLoadBalancer(loadBalancer1); //============================================================= // Create two network interfaces in the frontend subnet // associate network interfaces to NAT rules, backend pools Console.WriteLine("Creating two network interfaces in the frontend subnet ..."); Console.WriteLine("- And associating network interfaces to backend pools and NAT rules"); var networkInterfaceCreatables = new List <ICreatable <INetworkInterface> >(); ICreatable <INetworkInterface> networkInterface1Creatable; ICreatable <INetworkInterface> networkInterface2Creatable; networkInterface1Creatable = azure.NetworkInterfaces .Define(networkInterfaceName1) .WithRegion(Region.US_EAST) .WithNewResourceGroup(rgName) .WithExistingPrimaryNetwork(network) .WithSubnet("Front-end") .WithPrimaryPrivateIpAddressDynamic() .WithExistingLoadBalancerBackend(loadBalancer1, backendPoolName1) .WithExistingLoadBalancerBackend(loadBalancer1, backendPoolName2) .WithExistingLoadBalancerInboundNatRule(loadBalancer1, natRule5000to22forVM1) .WithExistingLoadBalancerInboundNatRule(loadBalancer1, natRule5001to23forVM1); networkInterfaceCreatables.Add(networkInterface1Creatable); networkInterface2Creatable = azure.NetworkInterfaces .Define(networkInterfaceName2) .WithRegion(Region.US_EAST) .WithNewResourceGroup(rgName) .WithExistingPrimaryNetwork(network) .WithSubnet("Front-end") .WithPrimaryPrivateIpAddressDynamic() .WithExistingLoadBalancerBackend(loadBalancer1, backendPoolName1) .WithExistingLoadBalancerBackend(loadBalancer1, backendPoolName2) .WithExistingLoadBalancerInboundNatRule(loadBalancer1, natRule5002to22forVM2) .WithExistingLoadBalancerInboundNatRule(loadBalancer1, natRule5003to23forVM2); networkInterfaceCreatables.Add(networkInterface2Creatable); var networkInterfaces1 = azure.NetworkInterfaces.Create(networkInterfaceCreatables.ToArray()); // Print network interface details Console.WriteLine("Created two network interfaces"); Console.WriteLine("Network Interface ONE -"); Utilities.PrintNetworkInterface(networkInterfaces1.ElementAt(0)); Console.WriteLine(); Console.WriteLine("Network Interface TWO -"); Utilities.PrintNetworkInterface(networkInterfaces1.ElementAt(1)); //============================================================= // Create an availability set Console.WriteLine("Creating an availability set ..."); var availSet1 = azure.AvailabilitySets.Define(availSetName) .WithRegion(Region.US_EAST) .WithNewResourceGroup(rgName) .WithFaultDomainCount(2) .WithUpdateDomainCount(4) .Create(); Console.WriteLine("Created first availability set: " + availSet1.Id); Utilities.PrintAvailabilitySet(availSet1); //============================================================= // Create two virtual machines and assign network interfaces Console.WriteLine("Creating two virtual machines in the frontend subnet ..."); Console.WriteLine("- And assigning network interfaces"); var virtualMachineCreatables1 = new List <ICreatable <IVirtualMachine> >(); ICreatable <IVirtualMachine> virtualMachine1Creatable; ICreatable <IVirtualMachine> virtualMachine2Creatable; virtualMachine1Creatable = azure.VirtualMachines .Define(vmName1) .WithRegion(Region.US_EAST) .WithExistingResourceGroup(rgName) .WithExistingPrimaryNetworkInterface(networkInterfaces1.ElementAt(0)) .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS) .WithRootUserName(userName) .WithSsh(sshKey) .WithSize(VirtualMachineSizeTypes.StandardD3V2) .WithExistingAvailabilitySet(availSet1); virtualMachineCreatables1.Add(virtualMachine1Creatable); virtualMachine2Creatable = azure.VirtualMachines .Define(vmName2) .WithRegion(Region.US_EAST) .WithExistingResourceGroup(rgName) .WithExistingPrimaryNetworkInterface(networkInterfaces1.ElementAt(1)) .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS) .WithRootUserName(userName) .WithSsh(sshKey) .WithSize(VirtualMachineSizeTypes.StandardD3V2) .WithExistingAvailabilitySet(availSet1); virtualMachineCreatables1.Add(virtualMachine2Creatable); var t1 = DateTime.UtcNow; var virtualMachines = azure.VirtualMachines.Create(virtualMachineCreatables1.ToArray()); var t2 = DateTime.UtcNow; Console.WriteLine($"Created 2 Linux VMs: (took {(t2 - t1).TotalSeconds} seconds) "); Console.WriteLine(); // Print virtual machine details Console.WriteLine("Virtual Machine ONE -"); Utilities.PrintVirtualMachine(virtualMachines.ElementAt(0)); Console.WriteLine(); Console.WriteLine("Virtual Machine TWO - "); Utilities.PrintVirtualMachine(virtualMachines.ElementAt(1)); //============================================================= // Update a load balancer // configure TCP idle timeout to 15 minutes Console.WriteLine("Updating the load balancer ..."); loadBalancer1.Update() .UpdateLoadBalancingRule(httpLoadBalancingRule) .WithIdleTimeoutInMinutes(15) .Parent() .UpdateLoadBalancingRule(httpsLoadBalancingRule) .WithIdleTimeoutInMinutes(15) .Parent() .Apply(); Console.WriteLine("Update the load balancer with a TCP idle timeout to 15 minutes"); //============================================================= // Create another public IP address Console.WriteLine("Creating another public IP address..."); var publicIpAddress2 = azure.PublicIpAddresses.Define(publicIpName2) .WithRegion(Region.US_EAST) .WithExistingResourceGroup(rgName) .WithLeafDomainLabel(publicIpName2) .Create(); Console.WriteLine("Created another public IP address"); // Print the virtual network details Utilities.PrintIpAddress(publicIpAddress2); //============================================================= // Create another Internet facing load balancer // Create a frontend IP address // Two backend address pools which contain network interfaces for the virtual // machines to receive HTTP and HTTPS network traffic from the load balancer // Two load balancing rules for HTTP and HTTPS to map public ports on the load // balancer to ports in the backend address pool // Two probes which contain HTTP and HTTPS health probes used to check availability // of virtual machines in the backend address pool // Two inbound NAT rules which contain rules that map a public port on the load // balancer to a port for a specific virtual machine in the backend address pool // - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23 Console.WriteLine("Creating another Internet facing load balancer with ..."); Console.WriteLine("- A frontend IP address"); Console.WriteLine("- Two backend address pools which contain network interfaces for the virtual\n" + " machines to receive HTTP and HTTPS network traffic from the load balancer"); Console.WriteLine("- Two load balancing rules for HTTP and HTTPS to map public ports on the load\n" + " balancer to ports in the backend address pool"); Console.WriteLine("- Two probes which contain HTTP and HTTPS health probes used to check availability\n" + " of virtual machines in the backend address pool"); Console.WriteLine("- Two inbound NAT rules which contain rules that map a public port on the load\n" + " balancer to a port for a specific virtual machine in the backend address pool\n" + " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23"); var loadBalancer2 = azure.LoadBalancers.Define(loadBalancerName2) .WithRegion(Region.US_EAST) .WithExistingResourceGroup(rgName) .DefinePublicFrontend(frontendName) .WithExistingPublicIpAddress(publicIpAddress2) .Attach() // Add two backend one per rule .DefineBackend(backendPoolName1) .Attach() .DefineBackend(backendPoolName2) .Attach() // Add two probes one per rule .DefineHttpProbe(httpProbe) .WithRequestPath("/") .WithPort(80) .Attach() .DefineHttpProbe(httpsProbe) .WithRequestPath("/") .WithPort(443) .Attach() // Add two rules that uses above backend and probe .DefineLoadBalancingRule(httpLoadBalancingRule) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(80) .WithProbe(httpProbe) .WithBackend(backendPoolName1) .Attach() .DefineLoadBalancingRule(httpsLoadBalancingRule) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(443) .WithProbe(httpsProbe) .WithBackend(backendPoolName2) .Attach() // Add two nat pools to enable direct VM connectivity for // SSH to port 22 and TELNET to port 23 .DefineInboundNatRule(natRule5000to22forVM1) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(5000) .WithBackendPort(22) .Attach() .DefineInboundNatRule(natRule5001to23forVM1) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(5001) .WithBackendPort(23) .Attach() .DefineInboundNatRule(natRule5002to22forVM2) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(5002) .WithBackendPort(22) .Attach() .DefineInboundNatRule(natRule5003to23forVM2) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(5003) .WithBackendPort(23) .Attach() .Create(); // Print load balancer details Console.WriteLine("Created another load balancer"); Utilities.PrintLoadBalancer(loadBalancer2); //============================================================= // List load balancers var loadBalancers = azure.LoadBalancers.List(); Console.WriteLine("Walking through the list of load balancers"); foreach (var loadBalancer in loadBalancers) { Utilities.PrintLoadBalancer(loadBalancer); } //============================================================= // Remove a load balancer Console.WriteLine("Deleting load balancer " + loadBalancerName2 + "(" + loadBalancer2.Id + ")"); azure.LoadBalancers.Delete(loadBalancer2.Id); Console.WriteLine("Deleted load balancer" + loadBalancerName2); } catch (Exception ex) { Console.WriteLine(ex); } finally { try { Console.WriteLine("Deleting Resource Group: " + rgName); azure.ResourceGroups.Delete(rgName); Console.WriteLine("Deleted Resource Group: " + rgName); } catch (NullReferenceException) { Console.WriteLine("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception ex) { Console.WriteLine(ex); } } } catch (Exception ex) { Console.WriteLine(ex); } }
private IAzure Authenticate() { return(Azure.Authenticate(azureOptions.Credentials).WithDefaultSubscription()); }
static void Main(string[] args) { //Create the management client. This will be used for all the operations //that we will perform in Azure. var credentials = SdkContext.AzureCredentialsFactory .FromFile("./azureauth.properties"); var azure = Azure.Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); //First of all, we need to create a resource group where we will add all //the resources needed for the virtual machine var groupName = "az204-ResourceGroup"; var vmName = "az204VMTesting"; var location = Region.USWest2; var vNetName = "az204VNET"; var vNetAddress = "172.16.0.0/16"; var subnetName = "az204Subnet"; var subnetAddress = "172.16.0.0/24"; var nicName = "az204NIC"; var adminUser = "******"; var adminPassword = "******"; var publicIPName = "az204publicIP"; var nsgName = "az204VNET-NSG"; Console.WriteLine($"Creating resource group {groupName} ..."); var resourceGroup = azure.ResourceGroups.Define(groupName) .WithRegion(location) .Create(); //Every virtual machine needs to be connected to a virtual network. Console.WriteLine($"Creating virtual network {vNetName} ..."); var network = azure.Networks.Define(vNetName) .WithRegion(location) .WithExistingResourceGroup(groupName) .WithAddressSpace(vNetAddress) .WithSubnet(subnetName, subnetAddress) .Create(); //You need a public IP to be able to connect to the VM from the Internet Console.WriteLine($"Creating public IP {publicIPName} ..."); var publicIP = azure.PublicIPAddresses.Define(publicIPName) .WithRegion(location) .WithExistingResourceGroup(groupName) .Create(); //You need a network security group for controlling the access to the VM Console.WriteLine($"Creating Network Security Group {nsgName} ..."); var nsg = azure.NetworkSecurityGroups.Define(nsgName) .WithRegion(location) .WithExistingResourceGroup(groupName) .Create(); //You need a security rule for allowing the access to the VM from the Internet Console.WriteLine($"Creating a Security Rule for allowing the remote access"); nsg.Update() .DefineRule("Allow-RDP") .AllowInbound() .FromAnyAddress() .FromAnyPort() .ToAnyAddress() .ToPort(3389) .WithProtocol(SecurityRuleProtocol.Tcp) .WithPriority(100) .WithDescription("Allow-RDP") .Attach() .Apply(); //Any virtual machine needs a network interface for connecting to the virtual network Console.WriteLine($"Creating network interface {nicName} ..."); var nic = azure.NetworkInterfaces.Define(nicName) .WithRegion(location) .WithExistingResourceGroup(groupName) .WithExistingPrimaryNetwork(network) .WithSubnet(subnetName) .WithPrimaryPrivateIPAddressDynamic() .WithExistingPrimaryPublicIPAddress(publicIP) .WithExistingNetworkSecurityGroup(nsg) .Create(); //Create the virtual machine Console.WriteLine($"Creating virtual machine {vmName} ..."); azure.VirtualMachines.Define(vmName) .WithRegion(location) .WithExistingResourceGroup(groupName) .WithExistingPrimaryNetworkInterface(nic) .WithLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter") .WithAdminUsername(adminUser) .WithAdminPassword(adminPassword) .WithComputerName(vmName) .WithSize(VirtualMachineSizeTypes.StandardDS2V2) .Create(); }
public void Validate(ClusterDefinition clusterDefinition) { Covenant.Requires <ArgumentNullException>(clusterDefinition != null, nameof(clusterDefinition)); switch (Environment) { case HostingEnvironments.Aws: if (Aws == null) { throw new ClusterDefinitionException($"[{nameof(HostingOptions)}.{nameof(Aws)}] must be initialized when cloud provider is [{Environment}]."); } Aws.Validate(clusterDefinition); break; case HostingEnvironments.Azure: if (Azure == null) { throw new ClusterDefinitionException($"[{nameof(HostingOptions)}.{nameof(Azure)}] must be initialized when cloud provider is [{Environment}]."); } Azure.Validate(clusterDefinition); break; case HostingEnvironments.Google: if (Google == null) { throw new ClusterDefinitionException($"[{nameof(HostingOptions)}.{nameof(Google)}] must be initialized when cloud provider is [{Environment}]."); } Google.Validate(clusterDefinition); break; case HostingEnvironments.HyperV: HyperV = HyperV ?? new HyperVOptions(); HyperV.Validate(clusterDefinition); break; case HostingEnvironments.HyperVLocal: HyperVDev = HyperVDev ?? new LocalHyperVOptions(); HyperVDev.Validate(clusterDefinition); break; case HostingEnvironments.Machine: Machine = Machine ?? new MachineOptions(); Machine.Validate(clusterDefinition); break; case HostingEnvironments.XenServer: XenServer = XenServer ?? new XenServerOptions(); XenServer.Validate(clusterDefinition); break; default: throw new NotImplementedException(); } if (!string.IsNullOrWhiteSpace(VmNamePrefix)) { if (!ClusterDefinition.IsValidName(VmNamePrefix)) { throw new ClusterDefinitionException($"[{nameof(HostingOptions)}.{nameof(VmNamePrefix)}={VmNamePrefix}] must include only letters, digits, underscores, or periods."); } } }
public static async Task Run() { string tag_owner = GetConfigItem("tag_owner"); string tag_deallocate = GetConfigItem("tag_deallocate"); int tag_deallocate_days = int.Parse(GetConfigItem("tag_deallocate_days")); string tag_deletevm = GetConfigItem("tag_deletevm"); int tag_deletevm_days = int.Parse(GetConfigItem("tag_deletevm_days")); string tag_deleterg = GetConfigItem("tag_deleterg"); int tag_deleterg_days = int.Parse(GetConfigItem("tag_deleterg_days")); string subscriptionIds = GetConfigItem("subscriptionIds"); string clientId = GetConfigItem("clientId"); string clientSecret = GetConfigItem("clientSecret"); string tenantId = GetConfigItem("tenantId"); AzureCredentialsFactory factory = new AzureCredentialsFactory(); AzureCredentials azureCreds = factory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud); Azure.IAuthenticated azure = Azure.Configure().Authenticate(azureCreds); foreach (var subscriptionId in subscriptionIds.Split(",", StringSplitOptions.RemoveEmptyEntries)) { Console.WriteLine($"Looking for new resources without an owner tag in subscription {subscriptionId}"); var azureSub = azure.WithSubscription(subscriptionId); var insightsClient = new InsightsClient(azureCreds); insightsClient.SubscriptionId = subscriptionId; var resourceGroups = azureSub.ResourceGroups.List(); foreach (var group in resourceGroups) { try { var defaultKeyValuePair = default(KeyValuePair <String, String>); var ownerTag = defaultKeyValuePair; if (group.Tags != null) { ownerTag = group.Tags.Where(tag => tag.Key.Equals(tag_owner, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault(); } String endTime = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); String resourceId = group.Id; if (ownerTag.Equals(defaultKeyValuePair)) { //Console.WriteLine($"Resource group {group.Name} does not contain owner tag...looking in activity log"); String startTime = DateTime.Now.ToUniversalTime().AddHours(-25).ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); string newOwner = UNKNOWN_OWNER; var resourceGroupCreateLogs = await GetCreationLogs(startTime, endTime, resourceId, OPERATION_RESOURCEGROUP_WRITE, insightsClient); if (resourceGroupCreateLogs.Length == 0) { startTime = DateTime.Now.ToUniversalTime().AddDays(-90).ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); resourceGroupCreateLogs = await GetCreationLogs(startTime, endTime, resourceId, OPERATION_RESOURCEGROUP_WRITE, insightsClient); } if (resourceGroupCreateLogs.Length != 0) { newOwner = resourceGroupCreateLogs[0].Caller; } if (!UNKNOWN_OWNER.Equals(newOwner)) { await group.Update().WithTag(tag_owner, newOwner).ApplyAsync(); Console.WriteLine($"Resource group {group.Name} tagged with owner {newOwner}"); } } else if (UNKNOWN_OWNER.Equals(ownerTag.Value)) { bool needsUpdate = false; var updateGroup = group.Update(); if (group.Tags.Where(tag => tag.Key.Equals(tag_deallocate, StringComparison.InvariantCultureIgnoreCase)).Count() == 0) { needsUpdate = true; updateGroup.WithTag(tag_deallocate, DateTime.Now.ToUniversalTime().AddDays(tag_deallocate_days).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")); } if (group.Tags.Where(tag => tag.Key.Equals(tag_deletevm, StringComparison.InvariantCultureIgnoreCase)).Count() == 0) { needsUpdate = true; updateGroup.WithTag(tag_deletevm, DateTime.Now.ToUniversalTime().AddDays(tag_deletevm_days).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")); } if (group.Tags.Where(tag => tag.Key.Equals(tag_deleterg, StringComparison.InvariantCultureIgnoreCase)).Count() == 0) { needsUpdate = true; updateGroup.WithTag(tag_deleterg, DateTime.Now.ToUniversalTime().AddDays(tag_deleterg_days).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")); } if (needsUpdate) { await updateGroup.ApplyAsync(); } } else { //Console.WriteLine($"Resource group {group.Name} is already owned by {ownerTag.Value}"); } } catch (Exception ex) { Console.WriteLine("Exception: " + ex); } } } Console.WriteLine("Done Tagging"); }
public async Task <IActionResult> TestDhmConcurrentVmCreation( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { var parameters = req.GetQueryParameterDictionary(); if (!parameters.ContainsKey(ResourceGroupName) || string.IsNullOrEmpty(parameters[ResourceGroupName])) { return(new BadRequestObjectResult("Resource group name was missing in the query parameters.")); } if (!parameters.ContainsKey(HostGroupName) || string.IsNullOrEmpty(parameters[HostGroupName])) { return(new BadRequestObjectResult("Host group name was missing in the query parameters.")); } if (!parameters.ContainsKey(VmCount) || string.IsNullOrEmpty(parameters[VmCount])) { return(new BadRequestObjectResult("VmCount was missing in the query parameters.")); } if (!parameters.ContainsKey(VmSku) || string.IsNullOrEmpty(parameters[VmSku])) { return(new BadRequestObjectResult("VM SKU was missing in the query parameters.")); } var authEndpoint = _configuration["AuthEndpoint"]; var azureRmEndpoint = _configuration["AzureRmEndpoint"]; var location = _configuration["Location"]; var virtualMachineSize = parameters[VmSku]; var numVirtualMachines = int.Parse(parameters[VmCount]); var tenantId = _configuration["TenantId"]; var clientId = _configuration["ClientId"]; var clientSecret = _configuration["FairfaxClientSecret"]; var subscriptionId = _configuration["SubscriptionId"]; var resourceGroupName = parameters[ResourceGroupName]; var hostGroupName = parameters[HostGroupName]; log.LogInformation($"Generating auth token..."); var token = await TokenHelper.GetToken( authEndpoint, azureRmEndpoint, tenantId, clientId, clientSecret); var customTokenProvider = new AzureCredentials( new TokenCredentials(token), new TokenCredentials(token), tenantId, AzureEnvironment.FromName(_configuration["CloudName"])); var client = RestClient .Configure() .WithEnvironment(AzureEnvironment.FromName(_configuration["CloudName"])) .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .WithCredentials(customTokenProvider) .Build(); var azure = Azure.Authenticate(client, tenantId).WithSubscription(subscriptionId); var computeManagementClient = new ComputeManagementClient(customTokenProvider) { SubscriptionId = subscriptionId, BaseUri = new Uri(_configuration["ResourceManagerUri"]), LongRunningOperationRetryTimeout = 5 }; log.LogInformation($"Creating resource group ({resourceGroupName}), if needed"); var resourceGroup = azure.ResourceGroups.Define(resourceGroupName) .WithRegion(location) .Create(); log.LogInformation($"Creating host group ({hostGroupName}), if needed"); var newDedicatedHostGroup = new DedicatedHostGroup() { Location = location, PlatformFaultDomainCount = 1 }; await computeManagementClient.DedicatedHostGroups.CreateOrUpdateAsync( resourceGroupName, hostGroupName, newDedicatedHostGroup); var taskList = new List <Task <HttpResponseMessage> >(); var inputDictionary = new Dictionary <string, StringContent>(); for (var i = 0; i < numVirtualMachines; i++) { var vmName = $"vm{i}-{new Random().Next(1, 10000)}"; log.LogInformation($"Configuring (not provisioning) VM: {vmName}"); var virtualMachine = CreateVmHelper.CreateVirtualMachine( computeManagementClient, azure, Region.Create(location), resourceGroupName, vmName, virtualMachineSize, null, "pip-" + Guid.NewGuid(), "adh-poc-vnet", "nic-" + Guid.NewGuid()); #if DEBUG var createVmUri = $"http://localhost:7071/api/CreateVm" + $"?token={token}" + $"&cloudName={_configuration["CloudName"]}" + $"&tenantId={tenantId}" + $"&subscriptionId={subscriptionId}" + $"&resourceGroup={resourceGroupName}" + $"&location={location}" + $"&vmSku={virtualMachineSize}" + $"&vmName={vmName}" + $"&dedicatedHostGroupName={hostGroupName}" + $"&platformFaultDomainCount=1"; #else var createVmUri = _configuration["DhmCreateVmnUri"] + $"&token={token}" + $"&cloudName={_configuration["CloudName"]}" + $"&tenantId={tenantId}" + $"&subscriptionId={subscriptionId}" + $"&resourceGroup={resourceGroupName}" + $"&location={location}" + $"&vmSku={virtualMachineSize}" + $"&vmName={vmName}" + $"&dedicatedHostGroupName={hostGroupName}" + $"&platformFaultDomainCount=1"; #endif var httpContent = new StringContent(JsonConvert.SerializeObject(virtualMachine), Encoding.UTF8, "application/json"); inputDictionary[createVmUri] = httpContent; } foreach (var item in inputDictionary) { taskList.Add(_httpClient.PostAsync(item.Key, item.Value)); } await Task.WhenAll(taskList); return(new OkObjectResult($"VM provisioning kicked off successfully for {numVirtualMachines} VMs - exiting.")); }
public async Task <IActionResult> TestPrepareDedicatedHostGroup( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { var parameters = req.GetQueryParameterDictionary(); if (!parameters.ContainsKey(ResourceGroupName) || string.IsNullOrEmpty(parameters[ResourceGroupName])) { return(new BadRequestObjectResult("Resource group name was missing in the query parameters.")); } if (!parameters.ContainsKey(HostGroupName) || string.IsNullOrEmpty(parameters[HostGroupName])) { return(new BadRequestObjectResult("Host group name was missing in the query parameters.")); } if (!parameters.ContainsKey(VmCount) || !Int32.TryParse(parameters[VmCount], out int numVirtualMachines)) { return(new BadRequestObjectResult("VmCount was missing in the query parameters.")); } if (!parameters.ContainsKey(VmSku) || string.IsNullOrEmpty(parameters[VmSku])) { return(new BadRequestObjectResult("VM SKU was missing in the query parameters.")); } var authEndpoint = _configuration["AuthEndpoint"]; var azureRmEndpoint = _configuration["AzureRmEndpoint"]; var location = _configuration["Location"]; var virtualMachineSize = parameters[VmSku]; var tenantId = _configuration["TenantId"]; var clientId = _configuration["ClientId"]; var clientSecret = _configuration["FairfaxClientSecret"]; var subscriptionId = _configuration["SubscriptionId"]; var resourceGroupName = parameters[ResourceGroupName]; var hostGroupName = parameters[HostGroupName]; log.LogInformation($"Generating auth token..."); var token = await TokenHelper.GetToken( authEndpoint, azureRmEndpoint, tenantId, clientId, clientSecret); var customTokenProvider = new AzureCredentials( new TokenCredentials(token), new TokenCredentials(token), tenantId, AzureEnvironment.FromName(_configuration["CloudName"])); var client = RestClient .Configure() .WithEnvironment(AzureEnvironment.FromName(_configuration["CloudName"])) .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .WithCredentials(customTokenProvider) .Build(); var azure = Azure.Authenticate(client, tenantId).WithSubscription(subscriptionId); var computeManagementClient = new ComputeManagementClient(customTokenProvider) { SubscriptionId = subscriptionId, BaseUri = new Uri(_configuration["ResourceManagerUri"]), LongRunningOperationRetryTimeout = 5 }; log.LogInformation($"Creating resource group ({resourceGroupName}), if needed"); var resourceGroup = azure.ResourceGroups.Define(resourceGroupName) .WithRegion(location) .Create(); log.LogInformation($"Creating host group ({hostGroupName}), if needed"); var newDedicatedHostGroup = new DedicatedHostGroup() { Location = location, PlatformFaultDomainCount = 1 }; await computeManagementClient.DedicatedHostGroups.CreateOrUpdateAsync( resourceGroupName, hostGroupName, newDedicatedHostGroup); #if DEBUG var prepareDHGroup = $"http://localhost:7071/api/PrepareDedicatedHostGroup" + $"?token={token}" + $"&cloudName={_configuration["CloudName"]}" + $"&tenantId={tenantId}" + $"&subscriptionId={subscriptionId}" + $"&resourceGroup={resourceGroupName}" + $"&vmSku={virtualMachineSize}" + $"&dedicatedHostGroupName={hostGroupName}" + $"&vmCount={numVirtualMachines}" + $"&platformFaultDomain=0"; #else var prepareDHGroup = _configuration["PrepareDHGroupUri"] + $"&token={token}" + $"&cloudName={_configuration["CloudName"]}" + $"&tenantId={tenantId}" + $"&subscriptionId={subscriptionId}" + $"&resourceGroup={resourceGroupName}" + $"&vmSku={virtualMachineSize}" + $"&dedicatedHostGroupName={hostGroupName}" + $"&vmCount={numVirtualMachines}" + $"&platformFaultDomain=0"; #endif var response = await _httpClient.GetAsync(prepareDHGroup); if (response.StatusCode != HttpStatusCode.OK) { return(new ObjectResult(new { error = $"Exception thrown by {await response.Content.ReadAsStringAsync()}" }) { StatusCode = (int)response.StatusCode }); } var dhCreated = await response.Content.ReadAsAsync <List <DedicatedHost> >(); return(new OkObjectResult($"Prepared Dedicated Host Group completed successfully {string.Join(",", dhCreated.Select(c => c.Name))} VM.")); }
public static void Main(string[] args) { // Subscription authorization file name example: // Azure CLI Demo-2d.azureauth // -2d means that resource groups should be deleted after 2 days. string[] files = Directory.GetFiles(Environment.GetEnvironmentVariable("AZURE_CLEANUP_AUTH_LOCATION"), "*.azureauth"); Console.WriteLine($"Found {files.Length} subscriptions to clean-up..."); foreach (var file in files) { Console.WriteLine($"Cleaning subscription from auth '{file}'"); try { //================================================================= // Authenticate AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(file); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); var client = new Microsoft.Azure.Management.ResourceManager.ResourceManagementClient(credentials); client.SubscriptionId = credentials.DefaultSubscriptionId; Regex r = new Regex(@"-(\d)d", RegexOptions.IgnoreCase); var matches = r.Matches(Path.GetFileName(file)); int dayTTL = -1; if (matches.Count > 0) { dayTTL = Convert.ToInt32(matches[0].Value[1]) - '0'; Console.WriteLine($" - Resource group TTL for this subscription is '{dayTTL}' day(s)."); } try { foreach (var rg in azure.ResourceGroups.List()) { try { if (rg.Name.EndsWith("-permanent", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine($" - Resource Group '{rg.Name}' is marked as 'DO NOT DELETE'. Skipping."); continue; } if ("Deleting".Equals(rg.ProvisioningState, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine($" - Resource Group '{rg.Name}' is already in 'Deleting' state."); continue; } var rgCreationTime = GetRGCreationDateTime(credentials, client, rg); if (dayTTL != -1) { var duration = (DateTime.Now - rgCreationTime).TotalDays; if (duration < dayTTL) { Console.WriteLine($" - Resource Group '{rg.Name}' was created less than {dayTTL} day(s) ago. Skipping."); continue; } } client.ResourceGroups.BeginDeleteWithHttpMessagesAsync(rg.Name) .ConfigureAwait(false) .GetAwaiter() .GetResult(); Console.WriteLine($" - Deleted Resource Group '{rg.Name}'."); } catch (Exception ex) { Console.WriteLine($" [ERROR]: Exception while deleting Resource Group '{rg.Name}'."); Console.WriteLine(ex); } } } catch (Exception ex) { Console.WriteLine($" [ERROR]: Exception while listing Resource Groups."); Console.WriteLine(ex); } } catch (Exception ex) { Console.WriteLine($" [ERROR]: Exception during authentication."); Console.WriteLine(ex); } } Console.WriteLine("Cleanup finished "); }
public static async Task Run([QueueTrigger("create-items")] TrialResource myQueueItem, ILogger log) { log.LogInformation("CreateResource QueueTrigger function processed a request."); var subscriptionId = myQueueItem.SubscriptionId;; AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider(); var tenantId = myQueueItem.TenantId; var tokenCredentials = new TokenCredentials(await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").ConfigureAwait(false)); var azureCredentials = new AzureCredentials( tokenCredentials, tokenCredentials, tenantId, AzureEnvironment.FromName(myQueueItem.AzureEnvironment)); var client = RestClient .Configure() .WithEnvironment(AzureEnvironment.FromName(myQueueItem.AzureEnvironment)) .WithLogLevel(myQueueItem.DeploymentLoggingLevel.ToEnum <HttpLoggingDelegatingHandler.Level>()) .WithCredentials(azureCredentials) .Build(); var azure = Azure .Authenticate(client, tenantId) .WithSubscription(subscriptionId); // var resourceManagementClient = new ResourceManagementClient(client); string rgName = myQueueItem.ResourceGroupName; //SdkContext.RandomResourceName(myQueueItem.ResourceGroupName, 24); string deploymentName = myQueueItem.AppServiceName; try { //var templateJson = File.ReadAllText(System.IO.Path.Combine(context.FunctionDirectory, "..\\FreeFunctionARMTemplate.json")); //============================================================= // Create resource group. Console.WriteLine("Creating a resource group with name: " + rgName); await azure.ResourceGroups.Define(rgName) .WithRegion(myQueueItem.Region.ToEnum <Region>()) .CreateAsync(); Console.WriteLine("Created a resource group with name: " + rgName); //============================================================= // Create a deployment for an Azure App Service via an ARM template. Console.WriteLine("Starting a deployment for an Azure App Service: " + deploymentName); await azure.Deployments.Define(deploymentName) .WithExistingResourceGroup(rgName) .WithTemplateLink("", null) //.WithParameters(new Dictionary<string, Dictionary<string, object>>{ // { "hostingPlanName", new Dictionary<string, object>{{"value",deploymentName}}}, // { "skuName", new Dictionary<string, object>{{"value", "B1" }}}, // { "skuCapacity", new Dictionary<string, object>{{"value",1}}}, // { "webSiteName", new Dictionary<string, object>{{"value", deploymentName } }} // }) //.WithParameters(new Dictionary<string, Dictionary<string, object>>{ // { "msdeployPackageUrl", new Dictionary<string, object>{{"value", "https://tryappservicetemplates.blob.core.windows.net/zipped/Default/Express.zip" } }}, // { "appServiceName", new Dictionary<string, object>{{"value", deploymentName } }} // }) .WithParametersLink("", null) .WithMode(myQueueItem.DeploymentMode.ToEnum <DeploymentMode>()) .CreateAsync(); Console.WriteLine("Started a deployment for an Azure App Service: " + deploymentName); var deployment = await azure.Deployments.GetByResourceGroupAsync(rgName, deploymentName); Console.WriteLine("Current deployment status : " + deployment.ProvisioningState); var tries = 180; while (!(StringComparer.OrdinalIgnoreCase.Equals(deployment.ProvisioningState, "Succeeded") || StringComparer.OrdinalIgnoreCase.Equals(deployment.ProvisioningState, "Failed") || StringComparer.OrdinalIgnoreCase.Equals(deployment.ProvisioningState, "Cancelled")) && tries-- > 0) { SdkContext.DelayProvider.Delay(10000); deployment = await azure.Deployments.GetByResourceGroupAsync(rgName, deploymentName); Console.WriteLine("Current deployment status : " + deployment.ProvisioningState); } if (StringComparer.OrdinalIgnoreCase.Equals(deployment.ProvisioningState, "Succeeded")) { var res = deployment.Outputs; var sitesDeployed = deployment.Dependencies.Where(a => StringComparer.OrdinalIgnoreCase.Equals(a.ResourceType, "Microsoft.Web/sites")); if (sitesDeployed != null) { var siteList = new List <IWebApp>(); foreach (var site in sitesDeployed) { siteList.Add(await azure.WebApps.GetByIdAsync(site.Id)); } } } } catch (Exception ex) { Console.WriteLine(ex); } finally { try { Console.WriteLine("Deleting Resource Group: " + rgName); azure.ResourceGroups.DeleteByName(rgName); Console.WriteLine("Deleted Resource Group: " + rgName); } catch (NullReferenceException) { Console.WriteLine("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception ex) { Console.WriteLine(ex); } } string name = myQueueItem.AppServiceName; }
public static void Main(string[] args) { #if NETCOREAPP3_0 Console.Write("CoFlows CE - NetCoreApp 3.0... "); #endif #if NET461 Console.Write("CoFlows CE - Net Framework 461... "); #endif Console.Write("Python starting... "); PythonEngine.Initialize(); Code.InitializeCodeTypes(new Type[] { typeof(QuantApp.Engine.WorkSpace), typeof(Jint.Native.Array.ArrayConstructor) }); var config_env = Environment.GetEnvironmentVariable("coflows_config"); var config_file = Environment.GetEnvironmentVariable("config_file"); if (string.IsNullOrEmpty(config_file)) { config_file = "quantapp_config.json"; } JObject config = string.IsNullOrEmpty(config_env) ? (JObject)JToken.ReadFrom(new JsonTextReader(File.OpenText(@"mnt/" + config_file))) : (JObject)JToken.Parse(config_env); workspace_name = config["Workspace"].ToString(); hostName = config["Server"]["Host"].ToString(); var secretKey = config["Server"]["SecretKey"].ToString(); ssl_cert = config["Server"]["SSL"]["Cert"].ToString(); ssl_password = config["Server"]["SSL"]["Password"].ToString(); var sslFlag = !string.IsNullOrWhiteSpace(ssl_cert); useJupyter = config["Jupyter"].ToString().ToLower() == "true"; var connectionString = config["Database"].ToString(); var cloudHost = config["Cloud"]["Host"].ToString(); var cloudKey = config["Cloud"]["SecretKey"].ToString(); var cloudSSL = config["Cloud"]["SSL"].ToString(); if (args != null && args.Length > 0 && args[0] == "lab") { Connection.Client.Init(hostName, sslFlag); if (!Connection.Client.Login(secretKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); var pargs = new string[] { "-m", "ipykernel_launcher.py", "-f", args[1] }; Console.Write("Starting lab... "); Python.Runtime.Runtime.Py_Main(pargs.Length, pargs); Console.WriteLine("started lab... "); } //Cloud else if (args != null && args.Length > 1 && args[0] == "cloud" && args[1] == "deploy") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("Starting cloud deployment... "); Code.UpdatePackageFile(workspace_name); var t0 = DateTime.Now; Console.WriteLine("Started: " + t0); var res = Connection.Client.PublishPackage(workspace_name); var t1 = DateTime.Now; Console.WriteLine("Ended: " + t1 + " taking " + (t1 - t0)); Console.Write("Result: " + res); } else if (args != null && args.Length > 1 && args[0] == "cloud" && args[1] == "build") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("CoFlows Cloud build... "); Code.UpdatePackageFile(workspace_name); var t0 = DateTime.Now; Console.WriteLine("Started: " + t0); var res = Connection.Client.BuildPackage(workspace_name); var t1 = DateTime.Now; Console.WriteLine("Ended: " + t1 + " taking " + (t1 - t0)); Console.Write("Result: " + res); } else if (args != null && args.Length > 2 && args[0] == "cloud" && args[1] == "query") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.WriteLine("CoFlows Cloud query... "); var queryID = args[2]; var funcName = args.Length > 3 ? args[3] : null; var parameters = args.Length > 4 ? args.Skip(4).ToArray() : null; var pkg = Code.ProcessPackageFile(workspace_name); Console.WriteLine("Workspace: " + pkg.Name); Console.WriteLine("Query ID: " + queryID); Console.WriteLine("Function Name: " + funcName); if (parameters != null) { for (int i = 0; i < parameters.Length; i++) { Console.WriteLine("Parameter[" + i + "]: " + parameters[i]); } } var(code_name, code) = pkg.Queries.Where(entry => entry.ID == queryID).Select(entry => (entry.Name as string, entry.Content as string)).FirstOrDefault(); var t0 = DateTime.Now; Console.WriteLine("Started: " + t0); var result = Connection.Client.Execute(code, code_name, pkg.ID, queryID, funcName, parameters); var t1 = DateTime.Now; Console.WriteLine("Ended: " + t1 + " taking " + (t1 - t0)); Console.WriteLine("Result: "); Console.WriteLine(result); } else if (args != null && args.Length > 1 && args[0] == "cloud" && args[1] == "log") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("CoFlows Cloud log... "); var res = Connection.Client.RemoteLog(workspace_name); Console.WriteLine("Result: "); Console.WriteLine(res); } else if (args != null && args.Length > 1 && args[0] == "cloud" && args[1] == "remove") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("CoFlows Cloud log... "); var res = Connection.Client.RemoteRemove(workspace_name); Console.WriteLine("Result: "); Console.WriteLine(res); } else if (args != null && args.Length > 1 && args[0] == "cloud" && args[1] == "restart") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("CoFlows Cloud log... "); var res = Connection.Client.RemoteRestart(workspace_name); Console.WriteLine("Result: "); Console.WriteLine(res); } else if (args != null && args.Length > 0 && args[0] == "server") { PythonEngine.BeginAllowThreads(); Databases(connectionString); Console.WriteLine("QuantApp Server " + DateTime.Now); Console.WriteLine("DB Connected"); Console.WriteLine("Local deployment"); if (string.IsNullOrEmpty(config_env)) { var pkg = Code.ProcessPackageFile(workspace_name); Code.ProcessPackageJSON(pkg); SetDefaultWorkSpaces(new string[] { pkg.ID }); Console.WriteLine(pkg.Name + " started"); } else { Console.WriteLine("Empty server..."); } #if NETCOREAPP3_0 if (!sslFlag) { Init(new string[] { "--urls", "http://*:80" }); } else { Init(args); } #endif #if NET461 Init(new string[] { "--urls", "http://*:80" }); #endif Task.Factory.StartNew(() => { while (true) { // Console.WriteLine(DateTime.Now.ToString()); System.Threading.Thread.Sleep(1000); } }); Console.CancelKeyPress += new ConsoleCancelEventHandler(OnExit); _closing.WaitOne(); } //Local else if (args != null && args.Length > 1 && args[0] == "local" && args[1] == "build") { PythonEngine.BeginAllowThreads(); Databases(connectionString); Console.WriteLine("DB Connected"); Console.WriteLine("Local build"); var pkg = Code.ProcessPackageFile(Code.UpdatePackageFile(workspace_name)); var res = Code.BuildRegisterPackage(pkg); if (string.IsNullOrEmpty(res)) { Console.WriteLine("Success!!!"); } else { Console.WriteLine(res); } } else if (args != null && args.Length > 2 && args[0] == "local" && args[1] == "query") { PythonEngine.BeginAllowThreads(); Databases(connectionString); Console.WriteLine("Local Query " + DateTime.Now); Console.WriteLine("DB Connected"); Console.WriteLine("CoFlows Local query... "); var queryID = args[2]; var funcName = args.Length > 3 ? args[3] : null; var parameters = args.Length > 4 ? args.Skip(4).ToArray() : null; Console.WriteLine("QueryID: " + queryID); Console.WriteLine("FuncName: " + funcName); Console.WriteLine("Parameters: " + parameters); var pkg = Code.ProcessPackageFile(Code.UpdatePackageFile(workspace_name)); Code.ProcessPackageJSON(pkg); if (parameters != null) { for (int i = 0; i < parameters.Length; i++) { Console.WriteLine("Parameter[" + i + "]: " + parameters[i]); } } var(code_name, code) = pkg.Queries.Where(entry => entry.ID == queryID).Select(entry => (entry.Name as string, entry.Content as string)).FirstOrDefault(); var t0 = DateTime.Now; Console.WriteLine("Started: " + t0); // var wb = wb_res.FirstOrDefault() as CodeData; var codes = new List <Tuple <string, string> >(); codes.Add(new Tuple <string, string>(code_name, code)); var result = QuantApp.Engine.Utils.ExecuteCodeFunction(false, codes, funcName, parameters); //var result = Connection.Client.Execute(code, code_name, pkg.ID, queryID, funcName, parameters); var t1 = DateTime.Now; Console.WriteLine("Ended: " + t1 + " taking " + (t1 - t0)); Console.WriteLine("Result: "); Console.WriteLine(result); } //Azure Container Instance else if (args != null && args.Length > 1 && args[0] == "azure" && args[1] == "deploy") { PythonEngine.BeginAllowThreads(); Console.WriteLine(); Console.WriteLine("Azure Container Instance start..."); var t0 = DateTime.Now; Console.WriteLine("Started: " + t0); var pkg = Code.ProcessPackageFile(Code.UpdatePackageFile(workspace_name)); var res = Code.BuildRegisterPackage(pkg); if (string.IsNullOrEmpty(res)) { Console.WriteLine("Build Success!!!"); } else { Console.WriteLine(res); } AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(config["AzureContainerInstance"]["AuthFile"].ToString()); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); string rgName = pkg.ID.ToLower() + "-rg"; string aciName = pkg.Name.ToLower(); string containerImageName = "coflows/ce"; Console.WriteLine("Container Name: " + aciName); Console.WriteLine("Resource Group Name: " + rgName); try { Console.WriteLine("Cleaning Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); IResourceGroup resGroup = azure.ResourceGroups.GetByName(rgName); while (resGroup != null) { resGroup = azure.ResourceGroups.GetByName(rgName); Console.Write("."); SdkContext.DelayProvider.Delay(1000); } Console.WriteLine(); Console.WriteLine("Cleaned Resource Group: " + rgName); } catch (Exception e) { Console.WriteLine(); Console.WriteLine("Did not create any resources in Azure. No clean up is necessary"); } Region region = Region.Create(config["AzureContainerInstance"]["Region"].ToString()); Console.WriteLine("Region: " + region); if (config["AzureContainerInstance"]["Gpu"] != null && config["AzureContainerInstance"]["Gpu"]["Cores"].ToString() != "" && config["AzureContainerInstance"]["Gpu"]["Cores"].ToString() != "0" && config["AzureContainerInstance"]["Gpu"]["SKU"].ToString() != "") { Console.WriteLine("Creating a GPU container..."); Task.Run(() => azure.ContainerGroups.Define(aciName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() // .WithNewAzureFileShareVolume(volumeMountName, shareName) .WithoutVolume() .DefineContainerInstance(aciName) .WithImage(containerImageName) .WithExternalTcpPort(sslFlag ? 443 : 80) // .WithVolumeMountSetting(volumeMountName, "/aci/logs/") .WithCpuCoreCount(Int32.Parse(config["AzureContainerInstance"]["Cores"].ToString())) .WithMemorySizeInGB(Int32.Parse(config["AzureContainerInstance"]["Mem"].ToString())) .WithGpuResource( Int32.Parse(config["AzureContainerInstance"]["Gpu"]["Cores"].ToString()), config["AzureContainerInstance"]["Gpu"]["SKU"].ToString().ToLower() == "k80" ? GpuSku.K80 : config["AzureContainerInstance"]["Gpu"]["SKU"].ToString().ToLower() == "p100" ? GpuSku.P100 : GpuSku.V100 ) .WithEnvironmentVariables(new Dictionary <string, string>() { { "coflows_config", File.ReadAllText(@"mnt/" + config_file) }, }) .WithStartingCommandLine("dotnet", "QuantApp.Server.quant.lnx.dll", "server") .Attach() .WithDnsPrefix(config["AzureContainerInstance"]["Dns"].ToString()) .CreateAsync() ); } else { Console.WriteLine("Creating a standard container..."); Task.Run(() => azure.ContainerGroups.Define(aciName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() // .WithNewAzureFileShareVolume(volumeMountName, shareName) .WithoutVolume() .DefineContainerInstance(aciName) .WithImage(containerImageName) .WithExternalTcpPort(sslFlag ? 443 : 80) // .WithVolumeMountSetting(volumeMountName, "/aci/logs/") .WithCpuCoreCount(Int32.Parse(config["AzureContainerInstance"]["Cores"].ToString())) .WithMemorySizeInGB(Int32.Parse(config["AzureContainerInstance"]["Mem"].ToString())) .WithEnvironmentVariables(new Dictionary <string, string>() { { "coflows_config", File.ReadAllText(@"mnt/" + config_file) }, }) .WithStartingCommandLine("dotnet", "QuantApp.Server.quant.lnx.dll", "server") .Attach() .WithDnsPrefix(config["AzureContainerInstance"]["Dns"].ToString()) .CreateAsync() ); } // Poll for the container group IContainerGroup containerGroup = null; while (containerGroup == null) { containerGroup = azure.ContainerGroups.GetByResourceGroup(rgName, aciName); Console.Write("."); SdkContext.DelayProvider.Delay(1000); } var lastContainerGroupState = containerGroup.Refresh().State; Console.WriteLine(); Console.WriteLine($"Container group state: {containerGroup.Refresh().State}"); // Poll until the container group is running while (containerGroup.State != "Running") { var containerGroupState = containerGroup.Refresh().State; if (containerGroupState != lastContainerGroupState) { Console.WriteLine(); Console.WriteLine(containerGroupState); lastContainerGroupState = containerGroupState; } Console.Write("."); System.Threading.Thread.Sleep(1000); } Console.WriteLine(); Console.WriteLine("Container instance IP address: " + containerGroup.IPAddress); Console.WriteLine("Container instance Ports: " + string.Join(",", containerGroup.ExternalTcpPorts)); string serverUrl = config["AzureContainerInstance"]["Dns"].ToString() + "." + config["AzureContainerInstance"]["Region"].ToString().ToLower() + ".azurecontainer.io"; Console.WriteLine("Container instance DNS Prefix: " + serverUrl); SdkContext.DelayProvider.Delay(10000); Connection.Client.Init(serverUrl, sslFlag); if (!Connection.Client.Login(config["Server"]["SecretKey"].ToString())) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("Container connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("Starting azure deployment... "); Code.UpdatePackageFile(workspace_name); var resDeploy = Connection.Client.PublishPackage(workspace_name); var t1 = DateTime.Now; Console.WriteLine("Ended: " + t1 + " taking " + (t1 - t0)); Console.Write("Result: " + resDeploy); } else if (args != null && args.Length > 1 && args[0] == "azure" && args[1] == "remove") { PythonEngine.BeginAllowThreads(); Console.WriteLine("Azure Container Instance remove start"); var pkg = Code.ProcessPackageFile(Code.UpdatePackageFile(workspace_name)); var res = Code.BuildRegisterPackage(pkg); if (!string.IsNullOrEmpty(res)) { Console.WriteLine(res); } AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(config["AzureContainerInstance"]["AuthFile"].ToString()); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); string rgName = pkg.ID.ToLower() + "-rg"; try { Console.WriteLine("Deleting Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); IResourceGroup resGroup = azure.ResourceGroups.GetByName(rgName); while (resGroup != null) { resGroup = azure.ResourceGroups.GetByName(rgName); Console.Write("."); SdkContext.DelayProvider.Delay(1000); } Console.WriteLine(); Console.WriteLine("Deleted Resource Group: " + rgName); } catch (Exception) { Console.WriteLine("Did not create any resources in Azure. No clean up is necessary"); } } else { Console.WriteLine("Wrong argument"); } }
public static void Main(string[] args) { try { var rgName = ResourceNamer.RandomResourceName("rgRSMA", 24); var rgName2 = ResourceNamer.RandomResourceName("rgRSMA", 24); var resourceTagName = ResourceNamer.RandomResourceName("rgRSTN", 24); var resourceTagValue = ResourceNamer.RandomResourceName("rgRSTV", 24); try { //================================================================= // Authenticate AzureCredentials credentials = AzureCredentials.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION")); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC) .Authenticate(credentials) .WithDefaultSubscription(); try { //============================================================= // Create resource group. Console.WriteLine("Creating a resource group with name: " + rgName); var resourceGroup = azure.ResourceGroups .Define(rgName) .WithRegion(Region.US_WEST) .Create(); Console.WriteLine("Created a resource group with name: " + rgName); //============================================================= // Update the resource group. Console.WriteLine("Updating the resource group with name: " + rgName); resourceGroup.Update() .WithTag(resourceTagName, resourceTagValue) .Apply(); Console.WriteLine("Updated the resource group with name: " + rgName); //============================================================= // Create another resource group. Console.WriteLine("Creating another resource group with name: " + rgName2); var resourceGroup2 = azure.ResourceGroups .Define(rgName2) .WithRegion(Region.US_WEST) .Create(); Console.WriteLine("Created another resource group with name: " + rgName2); //============================================================= // List resource groups. Console.WriteLine("Listing all resource groups"); foreach (var rGroup in azure.ResourceGroups.List()) { Console.WriteLine("Resource group: " + rGroup.Name); } //============================================================= // Delete a resource group. Console.WriteLine("Deleting resource group: " + rgName2); azure.ResourceGroups.Delete(rgName2); Console.WriteLine("Deleted resource group: " + rgName2); } catch (Exception ex) { Console.WriteLine(ex); } finally { try { Console.WriteLine("Deleting Resource Group: " + rgName); azure.ResourceGroups.Delete(rgName); Console.WriteLine("Deleted Resource Group: " + rgName); } catch (Exception ex) { Console.WriteLine(ex); } } } catch (Exception ex) { Console.WriteLine(ex); } } catch (Exception ex) { Console.WriteLine(ex); } }
public static void Main(string[] args) { var rgName = ResourceNamer.RandomResourceName("rgRSMR", 24); var resourceName1 = ResourceNamer.RandomResourceName("rn1", 24); var resourceName2 = ResourceNamer.RandomResourceName("rn2", 24); try { //================================================================= // Authenticate AzureCredentials credentials = AzureCredentials.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION")); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC) .Authenticate(credentials) .WithDefaultSubscription(); try { //============================================================= // Create resource group. Console.WriteLine("Creating a resource group with name: " + rgName); azure.ResourceGroups .Define(rgName) .WithRegion(Region.US_WEST) .Create(); //============================================================= // Create storage account. Console.WriteLine("Creating a storage account with name: " + resourceName1); var storageAccount = azure.StorageAccounts .Define(resourceName1) .WithRegion(Region.US_WEST) .WithExistingResourceGroup(rgName) .Create(); Console.WriteLine("Storage account created: " + storageAccount.Id); //============================================================= // Update - set the sku name Console.WriteLine("Updating the storage account with name: " + resourceName1); storageAccount.Update() .WithSku(Microsoft.Azure.Management.Storage.Fluent.Models.SkuName.StandardRAGRS) .Apply(); Console.WriteLine("Updated the storage account with name: " + resourceName1); //============================================================= // Create another storage account. Console.WriteLine("Creating another storage account with name: " + resourceName2); var storageAccount2 = azure.StorageAccounts.Define(resourceName2) .WithRegion(Region.US_WEST) .WithExistingResourceGroup(rgName) .Create(); Console.WriteLine("Storage account created: " + storageAccount2.Id); //============================================================= // List storage accounts. Console.WriteLine("Listing all storage accounts for resource group: " + rgName); foreach (var sAccount in azure.StorageAccounts.List()) { Console.WriteLine("Storage account: " + sAccount.Name); } //============================================================= // Delete a storage accounts. Console.WriteLine("Deleting storage account: " + resourceName2); azure.StorageAccounts.Delete(storageAccount2.Id); Console.WriteLine("Deleted storage account: " + resourceName2); } catch (Exception ex) { Console.WriteLine(ex); } finally { try { Console.WriteLine("Deleting Resource Group: " + rgName); azure.ResourceGroups.Delete(rgName); Console.WriteLine("Deleted Resource Group: " + rgName); } catch (Exception ex) { Console.WriteLine(ex); } } } catch (Exception ex) { Console.WriteLine(ex); } }
public static IAzure GetAzureApi() { var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(Configuration.ClientId(), Configuration.ClientSecret(), Configuration.TenantId(), AzureEnvironment.AzureGlobalCloud); return(Azure.Authenticate(credentials).WithDefaultSubscription()); }
/** * Azure Compute sample for managing virtual machine from Managed Service Identity (MSI) enabled virtual machine - * - Create a virtual machine using MSI credentials from MSI enabled VM * - Delete the virtual machine using MSI credentials from MSI enabled VM. * * Automation testing cannot be enabled for this sample since it can be run only from an Azure virtual machine * with MSI enabled. */ public static void Main(string[] args) { var rgName = "msi-rg-test"; var region = Region.USWestCentral; // This sample required to be run from a MSI enabled virtual machine with role // based contributor access to the resource group with name "msi-rg-test". MSI // enabled VM can be created using service principal credentials as shown below. //var credFile = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"); //IAzure azure = Azure.Authenticate(credFile).WithDefaultSubscription(); //var virtualMachine = azure.VirtualMachines // .Define("<vm-name>") // .WithRegion(region) // .WithNewResourceGroup(rgName) // .WithNewPrimaryNetwork("10.0.0.0/28") // .WithPrimaryPrivateIPAddressDynamic() // .WithNewPrimaryPublicIPAddress(pipName) // .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts) // .WithRootUsername("<user-name>") // .WithRootPassword("<password>") // .WithSize(VirtualMachineSizeTypes.StandardDS2V2) // .WithOSDiskCaching(CachingTypes.ReadWrite) // .WithManagedServiceIdentity() // Enable MSI // .WithRoleBasedAccessToCurrentResourceGroup(BuiltInRole.Contributor) // With MSI Role assignment to current resource group // .Create(); // Specify your subscription Id string subscriptionId = "<subscription-id>"; var linuxVmName = Utilities.CreateRandomName("VM1"); var userName = "******"; var password = "******"; //============================================================= // MSI Authenticate AzureCredentials credentials = new AzureCredentials(new MSILoginInformation { Port = 50342 }, AzureEnvironment.AzureGlobalCloud); IAzure azure = Azure.Authenticate(credentials) .WithSubscription(subscriptionId); //============================================================= // Create a Linux VM using MSI credentials Console.WriteLine("Creating a Linux VM using MSI credentials"); var virtualMachine = azure.VirtualMachines .Define(linuxVmName) .WithRegion(region) .WithExistingResourceGroup(rgName) .WithNewPrimaryNetwork("10.0.0.0/28") .WithPrimaryPrivateIPAddressDynamic() .WithoutPrimaryPublicIPAddress() .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts) .WithRootUsername(userName) .WithRootPassword(password) .WithSize(VirtualMachineSizeTypes.StandardDS2V2) .WithOSDiskCaching(CachingTypes.ReadWrite) .Create(); Console.WriteLine("Created virtual machine using MSI credentials"); Utilities.PrintVirtualMachine(virtualMachine); //============================================================= // Delete the VM using MSI credentials Console.WriteLine("Deleting the virtual machine using MSI credentials"); azure.VirtualMachines.DeleteById(virtualMachine.Id); Console.WriteLine("Deleted virtual machine"); }
public async Task <IActionResult> TestDhmConcurrentVmDeletion( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { var parameters = req.GetQueryParameterDictionary(); if (!parameters.ContainsKey(ResourceGroupName) || string.IsNullOrEmpty(parameters[ResourceGroupName])) { return(new BadRequestObjectResult("ResourceGroupName was missing in the query parameters.")); } if (!parameters.ContainsKey(HostGroupName) || string.IsNullOrEmpty(parameters[HostGroupName])) { return(new BadRequestObjectResult("HostGroupName was missing in the query parameters.")); } if (!parameters.ContainsKey(VmName) || string.IsNullOrEmpty(parameters[VmName])) { return(new BadRequestObjectResult("VmName was missing in the query parameters.")); } var authEndpoint = _configuration["AuthEndpoint"]; var azureRmEndpoint = _configuration["AzureRmEndpoint"]; var location = _configuration["Location"]; var vmName = parameters[VmName]; var tenantId = _configuration["TenantId"]; var clientId = _configuration["ClientId"]; var clientSecret = _configuration["FairfaxClientSecret"]; var subscriptionId = _configuration["SubscriptionId"]; var resourceGroupName = parameters[ResourceGroupName]; var hostGroupName = parameters[HostGroupName]; log.LogInformation($"Generating auth token..."); var token = await TokenHelper.GetToken( authEndpoint, azureRmEndpoint, tenantId, clientId, clientSecret); var customTokenProvider = new AzureCredentials( new TokenCredentials(token), new TokenCredentials(token), tenantId, AzureEnvironment.FromName(_configuration["CloudName"])); var client = RestClient .Configure() .WithEnvironment(AzureEnvironment.FromName(_configuration["CloudName"])) .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .WithCredentials(customTokenProvider) .Build(); var azure = Azure.Authenticate(client, tenantId).WithSubscription(subscriptionId); var computeManagementClient = new ComputeManagementClient(customTokenProvider) { SubscriptionId = subscriptionId, BaseUri = new Uri(_configuration["ResourceManagerUri"]), LongRunningOperationRetryTimeout = 5 }; #if DEBUG var deleteVmUri = $"http://localhost:7071/api/DeleteVm" + $"?token={token}" + $"&cloudName={_configuration["CloudName"]}" + $"&tenantId={tenantId}" + $"&subscriptionId={subscriptionId}" + $"&resourceGroup={resourceGroupName}" + $"&dedicatedHostGroupName={hostGroupName}" + $"&vmName={vmName}"; #else var deleteVmUri = _configuration["DhmDeleteVmnUri"] + $"&token={token}" + $"&cloudName={_configuration["CloudName"]}" + $"&tenantId={tenantId}" + $"&subscriptionId={subscriptionId}" + $"&resourceGroup={resourceGroupName}" + $"&dedicatedHostGroupName={hostGroupName}" + $"&vmName={vmName}"; #endif await _httpClient.GetAsync(deleteVmUri); return(new OkObjectResult($"Deleted {vmName} VM.")); }
public static IAzure GetAzManagementClient(AzureCredentials credentials) { return(Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credentials).WithDefaultSubscription()); }
protected internal override async Task <JobContinuation> Resume() { // Give us another 10 minutes to execute await Extend(TimeSpan.FromMinutes(10)); using (var sql = CloudContext.Clients.CreateSqlManagementClient(Azure.GetCredentials(throwIfMissing: true))) { try { // 2. Check the status of the copy Log.CheckingCopyStatus(CopyOperationId); var ops = await sql.DatabaseOperations.ListByDatabaseAsync(TargetServerName, CopyName); var op = ops.FirstOrDefault(); switch (op.StateId) { case 2: // COMPLETED (http://msdn.microsoft.com/en-us/library/azure/dn720371.aspx) Log.CopyCompleted(); await CompleteCopy(sql); Log.CompletedDatabaseCopyProcess(SourceServerName, SourceDatabaseName, TargetServerName, TargetDatabaseName); return(Complete()); case 3: // FAILED // Copy failed! Fail the whole job throw new JobFailureException(op.Error); default: // Copy is still in progress, check for timeout if (Timeout.HasValue && ((DateTime.UtcNow - Context.Invocation.QueuedAt.UtcDateTime) >= Timeout.Value)) { // Abort the copy await AbortCopy(sql); throw new JobFailureException("Copy operation exceeded timeout and was aborted."); } // Save state and wait for another five minutes Log.CopyInProgress(op.PercentComplete); return(Suspend(TimeSpan.FromMinutes(5), new { SourceServerName, SourceDatabaseName, TargetServerName, TargetDatabaseName, CopyName, CopyOperationId, Timeout, Start })); } } catch (Exception) { // Abort the copy AbortCopy(sql).Wait(); throw; } } }
private async Task <IDeployment> CreateWorkspaceDeploymentAsync(Region region, string workspaceName, Guid subscriptionId, string resourceGroupName, string friendlyName = null, bool createResourceGroup = true, string storageAccountName = null, string keyVaultName = null, string applicationInsightsName = null, string azureContainerRegistryName = null, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(storageAccountName)) { storageAccountName = WorkspaceManagementHelpers.GenerateNameForDependentResource(workspaceName, WorkspaceManagementHelpers.DependentResourceType.StorageAccount); } if (string.IsNullOrEmpty(keyVaultName)) { keyVaultName = WorkspaceManagementHelpers.GenerateNameForDependentResource(workspaceName, WorkspaceManagementHelpers.DependentResourceType.KeyVault); } if (string.IsNullOrEmpty(applicationInsightsName)) { applicationInsightsName = WorkspaceManagementHelpers.GenerateNameForDependentResource(workspaceName, WorkspaceManagementHelpers.DependentResourceType.ApplicationInsights); } if (string.IsNullOrEmpty(azureContainerRegistryName)) { azureContainerRegistryName = WorkspaceManagementHelpers.GenerateNameForDependentResource(workspaceName, WorkspaceManagementHelpers.DependentResourceType.ContainerRegistry); } if (!WorkspaceManagementHelpers.ValidateApplicationInsightName(applicationInsightsName)) { throw new ArgumentException($"Application Insights name name is invalid, does not conform to Azure naming rules. {applicationInsightsName}"); } if (!WorkspaceManagementHelpers.ValidateAzureDNSName(keyVaultName)) { throw new ArgumentException($"Key Vault name is invalid, does not conform to Azure naming rules. {keyVaultName}"); } if (!WorkspaceManagementHelpers.ValidateAzureContainerName(azureContainerRegistryName)) { throw new ArgumentException($"Container Registry name is invalid, does not conform to Azure naming rules. {azureContainerRegistryName}"); } if (!WorkspaceManagementHelpers.ValidateAzureDNSName(storageAccountName)) { throw new ArgumentException($"Storage Account name is invalid, does not conform to Azure naming rules. {storageAccountName}"); } var template = new ArmTemplateBuilder(); var workspace_dependencies = new JArray(); // Build KeyVault JObject kvResource = ArmTemplateBuilder.BuildKVTemplate(keyVaultName, region, this.TenantId); template.AddResource(kvResource); workspace_dependencies.Add($"[resourceId('Microsoft.KeyVault/vaults', '{keyVaultName}')]"); string kvResourceId = GetArmResourceId(subscriptionId.ToString(), resourceGroupName, "Microsoft.KeyVault/vaults", keyVaultName); // Build Storage Account JObject storageResource = ArmTemplateBuilder.BuildStorageAccountTemplate(storageAccountName, region); template.AddResource(storageResource); workspace_dependencies.Add($"[resourceId('Microsoft.Storage/storageAccounts', '{storageAccountName}')]"); string storageResourceId = GetArmResourceId(subscriptionId.ToString(), resourceGroupName, "Microsoft.Storage/storageAccounts", storageAccountName); // Build Azure Container Registry JObject acrResource = ArmTemplateBuilder.BuildAzureContainerRegistryTemplate(azureContainerRegistryName, region); template.AddResource(acrResource); workspace_dependencies.Add($"[resourceId('Microsoft.ContainerRegistry/registries', '{azureContainerRegistryName}')]"); string acrResourceId = GetArmResourceId(subscriptionId.ToString(), resourceGroupName, "Microsoft.ContainerRegistry/registries", azureContainerRegistryName); // Build App Insights Instance JObject appInsightsResource = template.BuildApplicationInsightsTemplate(applicationInsightsName, region); template.AddResource(appInsightsResource); workspace_dependencies.Add($"[resourceId('Microsoft.Insights/components', '{applicationInsightsName}')]"); string appInsightsResourceId = GetArmResourceId(subscriptionId.ToString(), resourceGroupName, "Microsoft.Insights/components", applicationInsightsName); // Build Workspace JObject workspaceResource = ArmTemplateBuilder.BuildWorkspaceResource(workspaceName, region, kvResourceId, acrResourceId, storageResourceId, friendlyName, appInsightsResourceId); workspaceResource.GetValue("dependsOn").Replace(workspace_dependencies); template.AddResource(workspaceResource); // See https://github.com/Azure-Samples/resources-dotnet-deploy-using-arm-template var azure = Azure.Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(new AzureCredentials(this.Credentials, this.Credentials, this.TenantId, AzureEnvironment.AzureGlobalCloud)) .WithSubscription(subscriptionId.ToString()); IDeployment deployment; if (createResourceGroup) { deployment = await azure.Deployments.Define(Guid.NewGuid().ToString()) .WithNewResourceGroup(resourceGroupName, region) .WithTemplate(template.GetTemplate().ToString()) .WithParameters("{}") .WithMode(DeploymentMode.Incremental) .CreateAsync(cancellationToken).ConfigureAwait(false); } else { deployment = await azure.Deployments.Define(Guid.NewGuid().ToString()) .WithExistingResourceGroup(resourceGroupName) .WithTemplate(template.GetTemplate().ToString()) .WithParameters("{}") .WithMode(DeploymentMode.Incremental) .CreateAsync(cancellationToken).ConfigureAwait(false); } return(deployment); }
public static void Main(string[] args) { try { //============================================================= // Authenticate var credentials = AzureCredentials.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION")); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC) .Authenticate(credentials) .WithDefaultSubscription(); // Print selected subscription Console.WriteLine("Selected subscription: " + azure.SubscriptionId); try { //============================================================= // Create a virtual network with a frontend subnet Console.WriteLine("Creating virtual network with a frontend subnet ..."); var network = azure.Networks.Define(vnetName) .WithRegion(Region.US_EAST) .WithNewResourceGroup(rgName) .WithAddressSpace("172.16.0.0/16") .DefineSubnet("Front-end") .WithAddressPrefix("172.16.1.0/24") .Attach() .Create(); Console.WriteLine("Created a virtual network"); // Print the virtual network details Utilities.PrintVirtualNetwork(network); //============================================================= // Create a public IP address Console.WriteLine("Creating a public IP address..."); var publicIpAddress = azure.PublicIpAddresses.Define(publicIpName) .WithRegion(Region.US_EAST) .WithExistingResourceGroup(rgName) .WithLeafDomainLabel(publicIpName) .Create(); Console.WriteLine("Created a public IP address"); // Print the IPAddress details Utilities.PrintIpAddress(publicIpAddress); //============================================================= // Create an Internet facing load balancer with // One frontend IP address // Two backend address pools which contain network interfaces for the virtual // machines to receive HTTP and HTTPS network traffic from the load balancer // Two load balancing rules for HTTP and HTTPS to map public ports on the load // balancer to ports in the backend address pool // Two probes which contain HTTP and HTTPS health probes used to check availability // of virtual machines in the backend address pool // Three inbound NAT rules which contain rules that map a public port on the load // balancer to a port for a specific virtual machine in the backend address pool // - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23 Console.WriteLine("Creating a Internet facing load balancer with ..."); Console.WriteLine("- A frontend IP address"); Console.WriteLine("- Two backend address pools which contain network interfaces for the virtual\n" + " machines to receive HTTP and HTTPS network traffic from the load balancer"); Console.WriteLine("- Two load balancing rules for HTTP and HTTPS to map public ports on the load\n" + " balancer to ports in the backend address pool"); Console.WriteLine("- Two probes which contain HTTP and HTTPS health probes used to check availability\n" + " of virtual machines in the backend address pool"); Console.WriteLine("- Two inbound NAT rules which contain rules that map a public port on the load\n" + " balancer to a port for a specific virtual machine in the backend address pool\n" + " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23"); var loadBalancer1 = azure.LoadBalancers.Define(loadBalancerName1) .WithRegion(Region.US_EAST) .WithExistingResourceGroup(rgName) .DefinePublicFrontend(frontendName) .WithExistingPublicIpAddress(publicIpAddress) .Attach() // Add two backend one per rule .DefineBackend(backendPoolName1) .Attach() .DefineBackend(backendPoolName2) .Attach() // Add two probes one per rule .DefineHttpProbe(httpProbe) .WithRequestPath("/") .WithPort(80) .Attach() .DefineHttpProbe(httpsProbe) .WithRequestPath("/") .WithPort(443) .Attach() // Add two rules that uses above backend and probe .DefineLoadBalancingRule(httpLoadBalancingRule) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(80) .WithProbe(httpProbe) .WithBackend(backendPoolName1) .Attach() .DefineLoadBalancingRule(httpsLoadBalancingRule) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPort(443) .WithProbe(httpsProbe) .WithBackend(backendPoolName2) .Attach() // Add nat pools to enable direct VM connectivity for // SSH to port 22 and TELNET to port 23 .DefineInboundNatPool(natPool50XXto22) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPortRange(5000, 5099) .WithBackendPort(22) .Attach() .DefineInboundNatPool(natPool60XXto23) .WithProtocol(TransportProtocol.Tcp) .WithFrontend(frontendName) .WithFrontendPortRange(6000, 6099) .WithBackendPort(23) .Attach() .Create(); // Print load balancer details Console.WriteLine("Created a load balancer"); Utilities.PrintLoadBalancer(loadBalancer1); //============================================================= // Create a virtual machine scale set with three virtual machines // And, install Apache Web servers on them Console.WriteLine("Creating virtual machine scale set with three virtual machines" + " in the frontend subnet ..."); var t1 = DateTime.UtcNow; var fileUris = new List <string>(); fileUris.Add(apacheInstallScript); var virtualMachineScaleSet = azure.VirtualMachineScaleSets .Define(vmssName) .WithRegion(Region.US_EAST) .WithExistingResourceGroup(rgName) .WithSku(VirtualMachineScaleSetSkuTypes.STANDARD_D3_V2) .WithExistingPrimaryNetworkSubnet(network, "Front-end") .WithPrimaryInternetFacingLoadBalancer(loadBalancer1) .WithPrimaryInternetFacingLoadBalancerBackends(backendPoolName1, backendPoolName2) .WithPrimaryInternetFacingLoadBalancerInboundNatPools(natPool50XXto22, natPool60XXto23) .WithoutPrimaryInternalLoadBalancer() .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS) .WithRootUserName(userName) .WithSsh(sshKey) .WithNewStorageAccount(storageAccountName1) .WithNewStorageAccount(storageAccountName2) .WithNewStorageAccount(storageAccountName3) .WithCapacity(3) // Use a VM extension to install Apache Web servers .DefineNewExtension("CustomScriptForLinux") .WithPublisher("Microsoft.OSTCExtensions") .WithType("CustomScriptForLinux") .WithVersion("1.4") .WithMinorVersionAutoUpgrade() .WithPublicSetting("fileUris", fileUris) .WithPublicSetting("commandToExecute", installCommand) .Attach() .Create(); var t2 = DateTime.UtcNow; Console.WriteLine("Created a virtual machine scale set with " + "3 Linux VMs & Apache Web servers on them: (took " + ((t2 - t1).TotalSeconds) + " seconds) "); Console.WriteLine(); // Print virtual machine scale set details // Utilities.Print(virtualMachineScaleSet); //============================================================= // Stop the virtual machine scale set Console.WriteLine("Stopping virtual machine scale set ..."); virtualMachineScaleSet.PowerOff(); Console.WriteLine("Stopped virtual machine scale set"); //============================================================= // Start the virtual machine scale set Console.WriteLine("Starting virtual machine scale set ..."); virtualMachineScaleSet.Start(); Console.WriteLine("Started virtual machine scale set"); //============================================================= // Update the virtual machine scale set // - double the no. of virtual machines Console.WriteLine("Updating virtual machine scale set " + "- double the no. of virtual machines ..."); virtualMachineScaleSet.Update() .WithCapacity(6) .Apply(); Console.WriteLine("Doubled the no. of virtual machines in " + "the virtual machine scale set"); //============================================================= // re-start virtual machine scale set Console.WriteLine("re-starting virtual machine scale set ..."); virtualMachineScaleSet.Restart(); Console.WriteLine("re-started virtual machine scale set"); } catch (Exception f) { Console.WriteLine(f); } finally { try { Console.WriteLine("Deleting Resource Group: " + rgName); azure.ResourceGroups.Delete(rgName); Console.WriteLine("Deleted Resource Group: " + rgName); } catch (NullReferenceException npe) { Console.WriteLine("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception ex) { Console.WriteLine(ex); } } } catch (Exception ex) { Console.WriteLine(ex); } }