aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Azure/Tango.AzureUtils/Environment/EnvironmentManager.cs
blob: c7e48fab1ecd69bca3ec6533c7066f841c84bfc8 (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
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure;
using Microsoft.Azure.Management.AppService.Fluent;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.RecoveryServices.Backup;
using Microsoft.Azure.Management.Sql.Fluent.Models;
using Microsoft.WindowsAzure.Storage;
using Tango.AzureUtils.ActiveDirectory;
using Tango.AzureUtils.Deployment;
using Tango.Core.DB;

namespace Tango.AzureUtils.Environment
{
    public class EnvironmentManager : AzureUtilsComponentBase
    {
        private ActiveDirectoryManager _adManager;

        public EnvironmentManager(IAzure azure, ActiveDirectoryManager activeDirectoryManager) : base(azure)
        {
            _adManager = activeDirectoryManager;
        }

        #region Deployment Slots

        public async Task<IDeploymentSlot> CreateDeploymentSlot(IWebApp app, IDeploymentSlot existingSlot, String name, String adEmail, String adPassword)
        {
            var settings = await existingSlot.GetMachineServiceSettingsAsync();

            String dbCatalog = $"Tango_{name}";
            String machineStudioContainerName = $"machine-studio-versions-{name.ToLower()}";
            String ppcContainerName = $"tango-versions-{name.ToLower()}";
            String machineServiceBackupsContainerName = $"machine-service-backups-{name.ToLower()}";
            String machineServiceLogsContainerName = $"machine-service-logs-{name.ToLower()}";
            String environmentGroupName = $"Tango {name}";

            await _adManager.Authenticate(adEmail, adPassword);

            if (!await _adManager.IsGroupExists(environmentGroupName))
            {
                await _adManager.AddGroup(environmentGroupName);
                await _adManager.AddUserToGroup(environmentGroupName, adEmail);
            }

            return new object() as IDeploymentSlot;

            var dictionary = new Dictionary<string, string>();
            dictionary.Add(nameof(MachineServiceSettings.DB_ADDRESS), settings.DB_ADDRESS);
            dictionary.Add(nameof(MachineServiceSettings.DB_CATALOG), dbCatalog);
            dictionary.Add(nameof(MachineServiceSettings.DB_PASSWORD), settings.DB_PASSWORD);
            dictionary.Add(nameof(MachineServiceSettings.DB_USER_NAME), settings.DB_USER_NAME);
            dictionary.Add(nameof(MachineServiceSettings.DEPLOYMENT_SLOT), name);
            dictionary.Add(nameof(MachineServiceSettings.ENFORCE_MACHINE_STUDIO_VERSION), settings.ENFORCE_MACHINE_STUDIO_VERSION);
            dictionary.Add(nameof(MachineServiceSettings.ENVIRONMENT_GROUP), environmentGroupName);
            dictionary.Add(nameof(MachineServiceSettings.STORAGE_ACCOUNT), settings.STORAGE_ACCOUNT);
            dictionary.Add(nameof(MachineServiceSettings.MACHINE_STUDIO_VERSIONS_CONTAINER), machineStudioContainerName);
            dictionary.Add(nameof(MachineServiceSettings.TANGO_VERSIONS_CONTAINER), ppcContainerName);

            //Add Slot
            var slot = await app.DeploymentSlots
                .Define(app.Name + "-" + name)
                .WithBrandNewConfiguration()
                .WithWebAppAlwaysOn(true)
                .WithWebSocketsEnabled(true)
                .WithStickyAppSettings(dictionary)
                .WithStickyConnectionString(dbCatalog, $"Server=tcp:twine.database.windows.net,1433;Initial Catalog={dbCatalog};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)
                .CreateAsync();

            //Add Database
            var sqlServer = (await Azure.SqlServers.ListAsync()).SingleOrDefault(x => x.Name == "twine");
            var database = await sqlServer.Databases.Define(dbCatalog).WithEdition(DatabaseEdition.Standard).CreateAsync();

            using (DbManager db = DbManager.FromCredentials(settings.DB_ADDRESS, dbCatalog, settings.DB_USER_NAME, settings.DB_PASSWORD))
            {
                await db.ExecuteCommandAsync("CREATE USER [BackupUser] FOR LOGIN [BackupUser] WITH DEFAULT_SCHEMA=[dbo]");
                await db.ExecuteCommandAsync("EXEC sp_addrolemember N'db_owner', N'BackupUser'");
                await db.ExecuteCommandAsync("EXEC sp_addrolemember N'db_accessadmin', N'BackupUser'");
                await db.ExecuteCommandAsync("EXEC sp_addrolemember N'db_securityadmin', N'BackupUser'");
                await db.ExecuteCommandAsync("EXEC sp_addrolemember N'db_backupoperator', N'BackupUser'");
                await db.ExecuteCommandAsync("EXEC sp_addrolemember N'db_datareader', N'BackupUser'");
                await db.ExecuteCommandAsync("EXEC sp_addrolemember N'db_datawriter', N'BackupUser'");
            }

            //Add Storage Containers
            var targetAccount = CloudStorageAccount.Parse(settings.STORAGE_ACCOUNT);
            var targetClient = targetAccount.CreateCloudBlobClient();

            var machineStudioContainer = targetClient.GetContainerReference(machineStudioContainerName);
            await machineStudioContainer.CreateIfNotExistsAsync();

            var ppcContainer = targetClient.GetContainerReference(ppcContainerName);
            await ppcContainer.CreateIfNotExistsAsync();

            var machineServiceBackupsContainer = targetClient.GetContainerReference(machineServiceBackupsContainerName);
            await machineServiceBackupsContainer.CreateIfNotExistsAsync();

            var machineServiceLogsContainer = targetClient.GetContainerReference(machineServiceLogsContainerName);
            await machineServiceLogsContainer.CreateIfNotExistsAsync();

            return slot;
        }

        #endregion
    }
}