using FluentFTP; using Microsoft.Azure.Management.AppService.Fluent; using Microsoft.Azure.Management.Fluent; using Microsoft.Azure.Management.ResourceManager.Fluent; using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication; using Microsoft.Azure.Storage; using Microsoft.Azure.Storage.Blob; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using Tango.BL; using Tango.BL.Entities; using Tango.Core; using Tango.Core.Helpers; using Microsoft.Azure.Management.AppService.Fluent.DeploymentSlot.Definition; using Microsoft.Azure.Management.AppService.Fluent.WebAppBase.Definition; using Tango.AzureUtils.Environment; namespace Tango.AzureUtils.Deployment { public class DeploymentManager : AzureUtilsComponentBase { #region Constructors public DeploymentManager(IAzure azure) : base(azure) { } #endregion public async Task CreateDeploymentSlot(IWebApp app, IDeploymentSlot sourceSlot, String slotName) { OnProgress(AzureUtilsStage.Initializing, $"Retrieving '{sourceSlot.Name}' settings..."); var sourceSettings = await sourceSlot.GetMachineServiceSettingsAsync(); var targetSettings = EnvironmentSettings.FromSlotName(app.Name, slotName); OnProgress(AzureUtilsStage.Deployment, $"Creating new deployment slot '{targetSettings.SLOT_NAME}'..."); var dictionary = new Dictionary(); dictionary.Add(nameof(MachineServiceSettings.DB_ADDRESS), sourceSettings.DB_ADDRESS); dictionary.Add(nameof(MachineServiceSettings.DB_CATALOG), targetSettings.DB_CATALOG); dictionary.Add(nameof(MachineServiceSettings.DB_PASSWORD), sourceSettings.DB_PASSWORD); dictionary.Add(nameof(MachineServiceSettings.DB_USER_NAME), sourceSettings.DB_USER_NAME); dictionary.Add(nameof(MachineServiceSettings.DEPLOYMENT_SLOT), slotName); dictionary.Add(nameof(MachineServiceSettings.ENFORCE_MACHINE_STUDIO_VERSION), sourceSettings.ENFORCE_MACHINE_STUDIO_VERSION); dictionary.Add(nameof(MachineServiceSettings.ENVIRONMENT_GROUP), targetSettings.ENVIRONMENT_GROUP); dictionary.Add(nameof(MachineServiceSettings.STORAGE_ACCOUNT), sourceSettings.STORAGE_ACCOUNT); dictionary.Add(nameof(MachineServiceSettings.MACHINE_STUDIO_VERSIONS_CONTAINER), targetSettings.MACHINE_STUDIO_VERSIONS_CONTAINER); dictionary.Add(nameof(MachineServiceSettings.TANGO_VERSIONS_CONTAINER), targetSettings.TANGO_VERSIONS_CONTAINER); var definition = app.DeploymentSlots .Define(targetSettings.SLOT_NAME) .WithBrandNewConfiguration() .WithHttpsOnly(sourceSlot.HttpsOnly) .WithWebAppAlwaysOn(sourceSlot.AlwaysOn) .WithWebSocketsEnabled(sourceSlot.WebSocketsEnabled) .WithStickyAppSettings(dictionary) .WithStickyConnectionString(targetSettings.DB_CATALOG, $"Server=tcp:twine.database.windows.net,1433;Initial Catalog={targetSettings.DB_CATALOG};Persist Security Info=False;User ID=BackupUser;Password=Aa123456;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;", Microsoft.Azure.Management.AppService.Fluent.Models.ConnectionStringType.SQLAzure); if (sourceSlot.RemoteDebuggingEnabled) { definition = definition.WithRemoteDebuggingEnabled(sourceSlot.RemoteDebuggingVersion); } var slot = await definition.CreateAsync(); return slot; } public async Task GetDeploymentSlot(IWebApp app, String slotName) { OnProgress(AzureUtilsStage.Deployment, $"Checking '{app.Name}' deployment slots..."); IDeploymentSlot slot = (await app.DeploymentSlots.ListAsync()).ToList().SingleOrDefault(x => x.Name == slotName); return slot; } public async Task RemoveDeploymentSlot(IDeploymentSlot slot) { OnProgress(AzureUtilsStage.Deployment, $"Removing '{slot.Name}' deployment slot..."); var app = slot.Parent; await app.DeploymentSlots.DeleteByIdAsync(slot.Id); } } }