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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
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;
namespace Tango.AzureUtils.Deployment
{
public class DeploymentManager : ExtendedObject
{
private AzureUtilsCredentials _credentials;
private IAzure _azure;
private IProgress<FtpProgress> _ftpDownloadProgress;
private IProgress<FtpProgress> _ftpUploadProgress;
//TODO: Embedded TFP injection to current package!
#region Events
public event EventHandler<DeploymentProgressEventArgs> DeploymentProgress;
#endregion
#region Properties
private UpgradeConfiguration _upgradeConfiguration;
public UpgradeConfiguration UpgradeConfiguration
{
get { return _upgradeConfiguration; }
set { _upgradeConfiguration = value; RaisePropertyChangedAuto(); }
}
#endregion
#region Constructors
public DeploymentManager(AzureUtilsCredentials credentials)
{
UpgradeConfiguration = new UpgradeConfiguration();
_credentials = credentials;
_ftpDownloadProgress = new Progress<FtpProgress>((p) =>
{
OnProgress(DeploymentStage.DownloadingFTP, $"Downloading {p.RemotePath}...", p.Progress, 100, false);
});
_ftpUploadProgress = new Progress<FtpProgress>((p) =>
{
OnProgress(DeploymentStage.UploadingFTP, $"Uploading {p.LocalPath}...", p.Progress, 100, false);
});
}
#endregion
#region Authenticate
private IAzure GetOrCreateAzure()
{
if (_azure == null)
{
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
_credentials.ClientID,
_credentials.ClientSecret,
_credentials.TenantID,
AzureEnvironment.AzureGlobalCloud);
_azure = Azure.Authenticate(credentials).WithSubscription(_credentials.SubscriptionID);
}
return _azure;
}
#endregion
#region Helpers
public async Task<List<IWebApp>> GetAllWebAppsAsync()
{
return (await GetOrCreateAzure().WebApps.ListAsync()).ToList();
}
#endregion
#region Init
public void Init()
{
GetOrCreateAzure();
}
#endregion
#region Full Upgrade
public async Task PerformFullUpgrade(IWebAppBase sourceApp, IWebAppBase targetApp)
{
await ValidateUpgrade(sourceApp, targetApp);
await UpgradeStorage(sourceApp, targetApp);
await UpgradeVersions(sourceApp, targetApp);
if (UpgradeConfiguration.UpgradeMachineService)
{
await UpgradeMachineService(sourceApp, targetApp);
}
}
#endregion
#region SQLExaminer
public async Task OpenSQLExaminerSchema(IWebAppBase sourceApp, IWebAppBase targetApp)
{
String projectFile = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "Deployment", "GENERAL_ENV_UPGRADE.seproj");
using (Stream stream = GetFileStream(projectFile))
{
XElement projectXml = XElement.Load(stream);
var sourceSettings = await sourceApp.GetMachineServiceSettingsAsync();
var targetSettings = await targetApp.GetMachineServiceSettingsAsync();
ApplyDatabaseSettingsToProjectXml(projectXml, sourceSettings, targetSettings);
var tempFile = TemporaryManager.CreateImaginaryFile(".seproj");
tempFile.Persist = true;
File.WriteAllText(tempFile, projectXml.ToString());
Process.Start(tempFile);
}
}
public async Task OpenSQLExaminerData(IWebAppBase sourceApp, IWebAppBase targetApp)
{
String projectFile = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "Deployment", "GENERAL_ENV_UPGRADE.sdeproj");
using (Stream stream = GetFileStream(projectFile))
{
XElement projectXml = XElement.Load(stream);
var sourceSettings = await sourceApp.GetMachineServiceSettingsAsync();
var targetSettings = await targetApp.GetMachineServiceSettingsAsync();
ApplyDatabaseSettingsToProjectXml(projectXml, sourceSettings, targetSettings);
var tempFile = TemporaryManager.CreateImaginaryFile(".sdeproj");
tempFile.Persist = true;
File.WriteAllText(tempFile, projectXml.ToString());
Process.Start(tempFile);
}
}
private Stream GetFileStream(String projectFile)
{
byte[] projectBytes = File.ReadAllBytes(projectFile);
MemoryStream stream = new MemoryStream(projectBytes);
return stream;
}
private void ApplyDatabaseSettingsToProjectXml(XElement projectXml, MachineServiceSettings sourceSettings, MachineServiceSettings targetSettings)
{
var sourceElement = projectXml.Elements().SelectMany(x => x.Descendants()).SingleOrDefault(x => x.Name == "Source" && x.Attributes().SingleOrDefault(y => y.Name == "id").Value == "1");
var targetElement = projectXml.Elements().SelectMany(x => x.Descendants()).SingleOrDefault(x => x.Name == "Source" && x.Attributes().SingleOrDefault(y => y.Name == "id").Value == "2");
ApplyDatabaseSettingsToSourceElement(sourceElement, sourceSettings);
ApplyDatabaseSettingsToSourceElement(targetElement, targetSettings);
}
private void ApplyDatabaseSettingsToSourceElement(XElement sourceElement, MachineServiceSettings settings)
{
sourceElement.Element("ServerName").SetValue(settings.DB_ADDRESS);
sourceElement.Element("Database").SetValue(settings.DB_CATALOG);
sourceElement.Element("Login").SetValue(settings.DB_USER_NAME);
sourceElement.Element("Password").SetValue(settings.DB_PASSWORD);
}
#endregion
#region FTP
private async Task<List<FtpResult>> DownloadWebAppFiles(IWebAppBase app, String targetFolder)
{
var profile = await app.GetPublishingProfileAsync();
using (var ftp = new FtpClient(profile.FtpUrl, profile.FtpUsername, profile.FtpPassword))
{
var downloadResults = await ftp.DownloadDirectoryAsync(targetFolder, "/site/wwwroot", progress: _ftpDownloadProgress);
foreach (var downloadResult in downloadResults)
{
if (downloadResult.IsFailed)
{
throw downloadResult.Exception;
}
}
return downloadResults;
}
}
private async Task<List<FtpResult>> UploadWebAppFiles(IWebAppBase app, String sourceFolder)
{
var profile = await app.GetPublishingProfileAsync();
using (var ftp = new FtpClient(profile.FtpUrl, profile.FtpUsername, profile.FtpPassword))
{
var uploadResults = await ftp.UploadDirectoryAsync(sourceFolder, "/site/wwwroot", existsMode: FtpRemoteExists.Overwrite, progress: _ftpUploadProgress);
foreach (var uploadResult in uploadResults)
{
if (uploadResult.IsFailed)
{
throw uploadResult.Exception;
}
}
return uploadResults;
}
}
#endregion
#region Machine Service
public async Task UpgradeMachineService(IWebAppBase sourceApp, IWebAppBase targetApp)
{
var webAppFilesTempFolder = TemporaryManager.CreateFolder();
var downloadResults = await DownloadWebAppFiles(sourceApp, webAppFilesTempFolder);
var uploadResults = await UploadWebAppFiles(targetApp, webAppFilesTempFolder + "\\wwwroot");
}
#endregion
#region Applications Versions & Storage Blobs
public async Task<MachineStudioVersion> GetLatestMachineStudioVersion(IWebAppBase app)
{
MachineServiceSettings settings = null;
try
{
settings = await app.GetMachineServiceSettingsAsync();
}
catch (Exception ex)
{
throw new ArgumentException("Could not fetch machine service settings. Please check that all settings are available.");
}
try
{
DataSource dataSource = settings.ToDataSource();
using (var db = ObservablesContext.CreateDefault(dataSource))
{
var versions = await db.MachineStudioVersions.ToListAsync();
var latest_machine_version = versions.OrderByDescending(x => Version.Parse(x.Version)).FirstOrDefault();
return latest_machine_version;
}
}
catch (Exception ex)
{
throw new InvalidDataException("Could not retrieve latest Machine Studio version from database.", ex);
}
}
public async Task<TangoVersion> GetLatestPPCVersion(IWebAppBase app)
{
MachineServiceSettings settings = null;
try
{
settings = await app.GetMachineServiceSettingsAsync();
}
catch (Exception ex)
{
throw new ArgumentException("Could not fetch machine service settings. Please check that all settings are available.", ex);
}
try
{
DataSource dataSource = settings.ToDataSource();
using (var db = ObservablesContext.CreateDefault(dataSource))
{
var versions = await db.TangoVersions.ToListAsync();
var latest_machine_version = versions.OrderByDescending(x => Version.Parse(x.Version)).FirstOrDefault();
return latest_machine_version;
}
}
catch (Exception ex)
{
throw new InvalidDataException("Could not retrieve latest PPC version from database.", ex);
}
}
public async Task UpgradeStorage(IWebAppBase sourceApp, IWebAppBase targetApp)
{
await ValidateUpgrade(sourceApp, targetApp);
var latestMachineStudioVersion = await GetLatestMachineStudioVersion(sourceApp);
var latestPPCVersion = await GetLatestPPCVersion(sourceApp);
var sourceSettings = await sourceApp.GetMachineServiceSettingsAsync();
var targetSettings = await targetApp.GetMachineServiceSettingsAsync();
var sourceAccount = CloudStorageAccount.Parse(sourceSettings.STORAGE_ACCOUNT);
var sourceClient = sourceAccount.CreateCloudBlobClient();
var targetAccount = CloudStorageAccount.Parse(targetSettings.STORAGE_ACCOUNT);
var targetClient = targetAccount.CreateCloudBlobClient();
var sourceMachineStudioContainer = sourceClient.GetContainerReference(sourceSettings.MACHINE_STUDIO_VERSIONS_CONTAINER);
var targetMachineStudioContainer = targetClient.GetContainerReference(targetSettings.MACHINE_STUDIO_VERSIONS_CONTAINER);
var sourcePPCContainer = sourceClient.GetContainerReference(sourceSettings.TANGO_VERSIONS_CONTAINER);
var targetPPCContainer = targetClient.GetContainerReference(targetSettings.TANGO_VERSIONS_CONTAINER);
var sourceMachineStudioBlob = sourceMachineStudioContainer.GetBlockBlobReference(latestMachineStudioVersion.BlobName);
var sourceMachineStudioInstallerBlob = sourceMachineStudioContainer.GetBlockBlobReference(latestMachineStudioVersion.InstallerBlobName);
var targetMachineStudioBlob = CreateEmptyBlob(targetMachineStudioContainer, sourceMachineStudioBlob.Name);
var targetMachineStudioInstallerBlob = CreateEmptyBlob(targetMachineStudioContainer, sourceMachineStudioInstallerBlob.Name);
await Task.Factory.StartNew(() =>
{
targetMachineStudioBlob.StartCopy(sourceMachineStudioBlob);
targetMachineStudioInstallerBlob.StartCopy(sourceMachineStudioInstallerBlob);
});
}
public async Task UpgradeVersions(IWebAppBase sourceApp, IWebAppBase targetApp)
{
await ValidateUpgrade(sourceApp, targetApp);
if (UpgradeConfiguration.UpgradeMachineStudio)
{
await UpgradeMachineStudioVersion(sourceApp, targetApp);
}
if (UpgradeConfiguration.UpgradePPC)
{
await UpgradePPCVersion(sourceApp, targetApp);
}
}
private async Task UpgradeMachineStudioVersion(IWebAppBase sourceApp, IWebAppBase targetApp)
{
var latestMachineStudioVersion = await GetLatestMachineStudioVersion(sourceApp);
var targetDataSource = (await targetApp.GetMachineServiceSettingsAsync()).ToDataSource();
using (var db = ObservablesContext.CreateDefault(targetDataSource))
{
db.MachineStudioVersions.Add(latestMachineStudioVersion);
await db.SaveChangesAsync();
}
}
private async Task UpgradePPCVersion(IWebAppBase sourceApp, IWebAppBase targetApp)
{
var latestPPCVersion = await GetLatestPPCVersion(sourceApp);
var targetDataSource = (await targetApp.GetMachineServiceSettingsAsync()).ToDataSource();
using (var db = ObservablesContext.CreateDefault(targetDataSource))
{
db.TangoVersions.Add(latestPPCVersion);
await db.SaveChangesAsync();
}
}
private CloudBlockBlob CreateEmptyBlob(CloudBlobContainer container, String name)
{
CloudBlockBlob targetBlob = container.GetBlockBlobReference(name);
using (MemoryStream ms = new MemoryStream())
{
targetBlob.UploadFromStream(ms);//Empty memory stream. Will create an empty blob.
}
return targetBlob;
}
#endregion
#region Validation
public async Task ValidateUpgrade(IWebAppBase sourceApp, IWebAppBase targetApp)
{
if (UpgradeConfiguration.UpgradeMachineStudio)
{
await ValidateMachineStudioUpgrade(sourceApp, targetApp);
}
if (UpgradeConfiguration.UpgradePPC)
{
await ValidatePPCUpgrade(sourceApp, targetApp);
}
}
private async Task ValidateMachineStudioUpgrade(IWebAppBase sourceApp, IWebAppBase targetApp)
{
var sourceSettings = await sourceApp.GetMachineServiceSettingsAsync();
var targetSettings = await targetApp.GetMachineServiceSettingsAsync();
var latestSourceMachineStudioVersion = await GetLatestMachineStudioVersion(sourceApp);
//Check if there is any source machine studio version.
if (latestSourceMachineStudioVersion == null)
{
throw new ValidationException("Could not locate a Machine Studio version entry on the source database.");
}
var latestTargetMachineStudioVersion = await GetLatestMachineStudioVersion(targetApp);
//Check target latest machine studio version is older if there is any.
if (latestTargetMachineStudioVersion != null && Version.Parse(latestSourceMachineStudioVersion.Version) <= Version.Parse(latestTargetMachineStudioVersion.Version))
{
throw new ValidationException($"Machine Studio source version is '{latestSourceMachineStudioVersion.Version}' while target version is '{latestTargetMachineStudioVersion.Version}'.");
}
var targetAccount = CloudStorageAccount.Parse(targetSettings.STORAGE_ACCOUNT);
var targetClient = targetAccount.CreateCloudBlobClient();
var targetMachineStudioContainer = targetClient.GetContainerReference(targetSettings.MACHINE_STUDIO_VERSIONS_CONTAINER);
//Check machine studio binaries blob not exists on the target.
var targetMachineStudioBlob = targetMachineStudioContainer.GetBlockBlobReference(latestSourceMachineStudioVersion.BlobName);
if (await targetMachineStudioBlob.ExistsAsync())
{
throw new ValidationException($"Machine Studio Block blob '{latestSourceMachineStudioVersion.BlobName}' already exists on the target storage.");
}
//Check machine studio installer blob not exists on the target.
var targetMachineStudioInstallerBlob = targetMachineStudioContainer.GetBlockBlobReference(latestSourceMachineStudioVersion.InstallerBlobName);
if (await targetMachineStudioInstallerBlob.ExistsAsync())
{
throw new ValidationException($"Machine Studio Block blob '{latestSourceMachineStudioVersion.InstallerBlobName}' already exists on the target storage.");
}
}
private async Task ValidatePPCUpgrade(IWebAppBase sourceApp, IWebAppBase targetApp)
{
var sourceSettings = await sourceApp.GetMachineServiceSettingsAsync();
var targetSettings = await targetApp.GetMachineServiceSettingsAsync();
var latestSourcePPCVersion = await GetLatestPPCVersion(sourceApp);
//Check if there is any source PPC version.
if (latestSourcePPCVersion == null)
{
throw new ValidationException("Could not locate a PPC version entry on the source database.");
}
var latestTargetPPCVersion = await GetLatestPPCVersion(targetApp);
//Check target latest PPC version is older if there is any.
if (latestTargetPPCVersion != null && Version.Parse(latestSourcePPCVersion.Version) <= Version.Parse(latestTargetPPCVersion.Version))
{
throw new ValidationException($"PPC source version is '{latestSourcePPCVersion.Version}' while target version is '{latestTargetPPCVersion.Version}'.");
}
var targetAccount = CloudStorageAccount.Parse(targetSettings.STORAGE_ACCOUNT);
var targetClient = targetAccount.CreateCloudBlobClient();
var targetPPCContainer = targetClient.GetContainerReference(targetSettings.TANGO_VERSIONS_CONTAINER);
//Check PPC binaries blob not exists on the target.
var targetPPCBlob = targetPPCContainer.GetBlockBlobReference(latestSourcePPCVersion.BlobName);
if (await targetPPCBlob.ExistsAsync())
{
throw new ValidationException($"PPC Block blob '{latestSourcePPCVersion.BlobName}' already exists on the target storage.");
}
//Check PPC installer blob not exists on the target.
var targetPPCInstallerBlob = targetPPCContainer.GetBlockBlobReference(latestSourcePPCVersion.InstallerBlobName);
if (await targetPPCInstallerBlob.ExistsAsync())
{
throw new ValidationException($"PPC Block blob '{latestSourcePPCVersion.InstallerBlobName}' already exists on the target storage.");
}
}
#endregion
#region Virtual Methods
protected virtual void OnProgress(DeploymentStage stage, String message = null, double progress = 0, double maximum = 100, bool indeterminate = true)
{
DeploymentProgress?.Invoke(this, new DeploymentProgressEventArgs()
{
Stage = stage,
Message = message,
Progress = progress,
Maximum = maximum,
IsIndeterminate = indeterminate,
});
}
#endregion
}
}
|