aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Azure/Tango.AzureUtils/Deployment/DeploymentManager.cs
blob: c45068c6c7e1ff6a9282cffae1a58c15984c2164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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<IDeploymentSlot> 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<string, string>();
            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<IDeploymentSlot> 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);
        }
    }
}