-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathOrchestrator.cs
More file actions
417 lines (375 loc) · 24.2 KB
/
Orchestrator.cs
File metadata and controls
417 lines (375 loc) · 24.2 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Amazon.EC2.Model;
using AWS.Deploy.Common;
using AWS.Deploy.Common.Data;
using AWS.Deploy.Common.Extensions;
using AWS.Deploy.Common.IO;
using AWS.Deploy.Common.Recipes;
using AWS.Deploy.Common.Utilities;
using AWS.Deploy.Orchestration.CDK;
using AWS.Deploy.Orchestration.DeploymentCommands;
using AWS.Deploy.Orchestration.Docker;
using AWS.Deploy.Orchestration.LocalUserSettings;
using AWS.Deploy.Orchestration.ServiceHandlers;
using AWS.Deploy.Recipes;
namespace AWS.Deploy.Orchestration
{
/// <summary>
/// The Orchestrator holds all the metadata that the CLI and the AWS toolkit for Visual studio interact with to perform a deployment.
/// It is responsible for generating deployment recommendations, creating deployment bundles and also acts as a mediator
/// between the client UI and the CDK.
/// </summary>
public class Orchestrator
{
internal readonly ICdkProjectHandler? _cdkProjectHandler;
internal readonly ICDKManager? _cdkManager;
internal readonly ICDKVersionDetector? _cdkVersionDetector;
internal readonly IOrchestratorInteractiveService? _interactiveService;
internal readonly IAWSResourceQueryer? _awsResourceQueryer;
internal readonly IDeploymentBundleHandler? _deploymentBundleHandler;
internal readonly ILocalUserSettingsEngine? _localUserSettingsEngine;
internal readonly IDockerEngine? _dockerEngine;
internal readonly IRecipeHandler? _recipeHandler;
internal readonly IFileManager? _fileManager;
internal readonly IDirectoryManager? _directoryManager;
internal readonly OrchestratorSession? _session;
internal readonly IAWSServiceHandler? _awsServiceHandler;
private readonly IOptionSettingHandler? _optionSettingHandler;
internal readonly IDeployToolWorkspaceMetadata? _workspaceMetadata;
internal readonly ISystemCapabilityEvaluator? _systemCapabilityEvaluator;
public Orchestrator(
OrchestratorSession session,
IOrchestratorInteractiveService interactiveService,
ICdkProjectHandler cdkProjectHandler,
ICDKManager cdkManager,
ICDKVersionDetector cdkVersionDetector,
IAWSResourceQueryer awsResourceQueryer,
IDeploymentBundleHandler deploymentBundleHandler,
ILocalUserSettingsEngine localUserSettingsEngine,
IDockerEngine dockerEngine,
IRecipeHandler recipeHandler,
IFileManager fileManager,
IDirectoryManager directoryManager,
IAWSServiceHandler awsServiceHandler,
IOptionSettingHandler optionSettingHandler,
IDeployToolWorkspaceMetadata deployToolWorkspaceMetadata,
ISystemCapabilityEvaluator systemCapabilityEvaluator)
{
_session = session;
_interactiveService = interactiveService;
_cdkProjectHandler = cdkProjectHandler;
_cdkManager = cdkManager;
_cdkVersionDetector = cdkVersionDetector;
_awsResourceQueryer = awsResourceQueryer;
_deploymentBundleHandler = deploymentBundleHandler;
_dockerEngine = dockerEngine;
_recipeHandler = recipeHandler;
_localUserSettingsEngine = localUserSettingsEngine;
_fileManager = fileManager;
_directoryManager = directoryManager;
_awsServiceHandler = awsServiceHandler;
_optionSettingHandler = optionSettingHandler;
_workspaceMetadata = deployToolWorkspaceMetadata;
_systemCapabilityEvaluator = systemCapabilityEvaluator;
}
public Orchestrator(OrchestratorSession session, IRecipeHandler recipeHandler)
{
_session = session;
_recipeHandler = recipeHandler;
}
/// <summary>
/// Method that generates the list of recommendations to deploy with.
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public async Task<List<Recommendation>> GenerateDeploymentRecommendations()
{
if (_session == null)
throw new InvalidOperationException($"{nameof(_session)} is null as part of the orchestartor object");
if (_recipeHandler == null)
throw new InvalidOperationException($"{nameof(_recipeHandler)} is null as part of the orchestartor object");
var engine = new RecommendationEngine.RecommendationEngine(_session, _recipeHandler);
var recipePaths = new HashSet<string> { RecipeLocator.FindRecipeDefinitionsPath() };
var customRecipePaths = await _recipeHandler.LocateCustomRecipePaths(_session.ProjectDefinition);
return await engine.ComputeRecommendations(recipeDefinitionPaths: recipePaths.Union(customRecipePaths).ToList());
}
/// <summary>
/// Method to generate the list of recommendations to create deployment projects for.
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public async Task<List<Recommendation>> GenerateRecommendationsToSaveDeploymentProject()
{
if (_session == null)
throw new InvalidOperationException($"{nameof(_session)} is null as part of the orchestartor object");
if (_recipeHandler == null)
throw new InvalidOperationException($"{nameof(_recipeHandler)} is null as part of the orchestartor object");
var engine = new RecommendationEngine.RecommendationEngine(_session, _recipeHandler);
var compatibleRecommendations = await engine.ComputeRecommendations();
var cdkRecommendations = compatibleRecommendations.Where(x => x.Recipe.DeploymentType == DeploymentTypes.CdkProject).ToList();
return cdkRecommendations;
}
/// <summary>
/// Include in the list of recommendations the recipe the deploymentProjectPath implements.
/// </summary>
/// <param name="deploymentProjectPath"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="InvalidCliArgumentException"></exception>
public async Task<List<Recommendation>> GenerateRecommendationsFromSavedDeploymentProject(string deploymentProjectPath)
{
if (_session == null)
throw new InvalidOperationException($"{nameof(_session)} is null as part of the orchestartor object");
if (_recipeHandler == null)
throw new InvalidOperationException($"{nameof(_recipeHandler)} is null as part of the orchestartor object");
if (_directoryManager == null)
throw new InvalidOperationException($"{nameof(_directoryManager)} is null as part of the orchestartor object");
if (!_directoryManager.Exists(deploymentProjectPath))
throw new InvalidCliArgumentException(DeployToolErrorCode.DeploymentProjectPathNotFound, $"The path '{deploymentProjectPath}' does not exists on the file system. Please provide a valid deployment project path and try again.");
var engine = new RecommendationEngine.RecommendationEngine(_session, _recipeHandler);
return await engine.ComputeRecommendations(recipeDefinitionPaths: new List<string> { deploymentProjectPath });
}
/// <summary>
/// Creates a deep copy of the recommendation object and applies the previous settings to that recommendation.
/// </summary>
public async Task<Recommendation> ApplyRecommendationPreviousSettings(Recommendation recommendation, IDictionary<string, object> previousSettings)
{
if (_optionSettingHandler == null)
throw new InvalidOperationException($"{nameof(_optionSettingHandler)} is null as part of the orchestartor object");
if (_interactiveService == null)
throw new InvalidOperationException($"{nameof(_interactiveService)} is null as part of the orchestrator object");
var recommendationCopy = recommendation.DeepCopy();
recommendationCopy.IsExistingCloudApplication = true;
foreach (var optionSetting in recommendationCopy.Recipe.OptionSettings)
{
if (previousSettings.TryGetValue(optionSetting.Id, out var value))
{
try
{
await _optionSettingHandler.SetOptionSettingValue(recommendationCopy, optionSetting, value, skipValidation: true);
}
catch (UnsupportedOptionSettingType ex)
{
_interactiveService.LogErrorMessage($"Unable to retrieve value of '{optionSetting.Name}' from previous deployment. Make sure to set it again prior to redeployment.");
_interactiveService.LogDebugMessage(ex.Message);
}
}
}
return recommendationCopy;
}
public async Task ApplyAllReplacementTokens(Recommendation recommendation, string cloudApplicationName)
{
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_LATEST_DOTNET_BEANSTALK_PLATFORM_ARN))
{
if (_awsResourceQueryer == null)
throw new InvalidOperationException($"{nameof(_awsResourceQueryer)} is null as part of the Orchestrator object");
var latestPlatform = await _awsResourceQueryer.GetLatestElasticBeanstalkPlatformArn(recommendation.ProjectDefinition.TargetFramework, BeanstalkPlatformType.Linux);
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_LATEST_DOTNET_BEANSTALK_PLATFORM_ARN, latestPlatform.PlatformArn);
}
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_LATEST_DOTNET_WINDOWS_BEANSTALK_PLATFORM_ARN))
{
if (_awsResourceQueryer == null)
throw new InvalidOperationException($"{nameof(_awsResourceQueryer)} is null as part of the Orchestrator object");
var latestPlatform = await _awsResourceQueryer.GetLatestElasticBeanstalkPlatformArn(recommendation.ProjectDefinition.TargetFramework, BeanstalkPlatformType.Windows);
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_LATEST_DOTNET_WINDOWS_BEANSTALK_PLATFORM_ARN, latestPlatform.PlatformArn);
}
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_STACK_NAME))
{
// Apply the user entered stack name to the recommendation so that any default settings based on stack name are applied.
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_STACK_NAME, cloudApplicationName);
}
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_ECR_REPOSITORY_NAME))
{
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_ECR_REPOSITORY_NAME, cloudApplicationName.ToLower());
}
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_ECR_IMAGE_TAG))
{
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_ECR_IMAGE_TAG, DateTime.UtcNow.Ticks.ToString());
}
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_DOCKERFILE_PATH))
{
if (_deploymentBundleHandler != null && DockerUtilities.TryGetDefaultDockerfile(recommendation, _fileManager, out var defaultDockerfilePath))
{
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_DOCKERFILE_PATH, defaultDockerfilePath);
}
}
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_DEFAULT_VPC_ID))
{
if (_awsResourceQueryer == null)
throw new InvalidOperationException($"{nameof(_awsResourceQueryer)} is null as part of the Orchestrator object");
var defaultVPC = await _awsResourceQueryer.GetDefaultVpc();
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_DEFAULT_VPC_ID, defaultVPC?.VpcId ?? string.Empty);
}
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_HAS_DEFAULT_VPC))
{
if (_awsResourceQueryer == null)
throw new InvalidOperationException($"{nameof(_awsResourceQueryer)} is null as part of the Orchestrator object");
var defaultVPC = await _awsResourceQueryer.GetDefaultVpc();
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_HAS_DEFAULT_VPC, defaultVPC != null);
}
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_HAS_NOT_VPCS))
{
if (_awsResourceQueryer == null)
throw new InvalidOperationException($"{nameof(_awsResourceQueryer)} is null as part of the Orchestrator object");
var vpcs = await _awsResourceQueryer.GetListOfVpcs() ?? new List<Vpc>();
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_HAS_NOT_VPCS, vpcs.Any());
}
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_DEFAULT_CONTAINER_PORT))
{
if (_dockerEngine == null)
throw new InvalidOperationException($"{nameof(_dockerEngine)} is null as part of the Orchestrator object");
var defaultPort = _dockerEngine.DetermineDefaultDockerPort(recommendation);
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_DEFAULT_CONTAINER_PORT, defaultPort);
recommendation.DeploymentBundle.DockerfileHttpPort = defaultPort;
}
if (recommendation.ReplacementTokens.ContainsKey(Constants.RecipeIdentifier.REPLACE_TOKEN_DEFAULT_ENVIRONMENT_ARCHITECTURE))
{
recommendation.AddReplacementToken(Constants.RecipeIdentifier.REPLACE_TOKEN_DEFAULT_ENVIRONMENT_ARCHITECTURE, Constants.Recipe.DefaultSupportedArchitecture);
}
}
public async Task DeployRecommendation(CloudApplication cloudApplication, Recommendation recommendation)
{
var deploymentCommand = DeploymentCommandFactory.BuildDeploymentCommand(recommendation.Recipe.DeploymentType);
await deploymentCommand.ExecuteAsync(this, cloudApplication, recommendation);
}
public async Task CreateDeploymentBundle(CloudApplication cloudApplication, Recommendation recommendation)
{
if (_interactiveService == null)
throw new InvalidOperationException($"{nameof(_interactiveService)} is null as part of the orchestrator object");
if (_systemCapabilityEvaluator == null)
throw new InvalidOperationException($"{nameof(_systemCapabilityEvaluator)} is null as part of the orchestrator object");
if (recommendation.Recipe.DeploymentBundle == DeploymentBundleTypes.Container)
{
var installedContainerAppInfo = await _systemCapabilityEvaluator.GetInstalledContainerAppInfo(recommendation);
var commandName = installedContainerAppInfo?.AppName?.ToLower();
if (string.IsNullOrEmpty(commandName))
throw new ContainerBuildFailedException(DeployToolErrorCode.ContainerBuildFailed, "No container app (Docker or Podman) is currently installed/running on your system.", -1);
_interactiveService.LogSectionStart("Creating deployment image",
$"Using the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(commandName)} CLI to create a container image.");
try
{
await CreateContainerDeploymentBundle(cloudApplication, recommendation);
}
catch (DeployToolException ex)
{
throw new FailedToCreateDeploymentBundleException(ex.ErrorCode, ex.Message, ex.ProcessExitCode, ex);
}
}
else if (recommendation.Recipe.DeploymentBundle == DeploymentBundleTypes.DotnetPublishZipFile)
{
_interactiveService.LogSectionStart("Creating deployment zip bundle",
"Using the dotnet CLI build the project and zip the publish artifacts.");
try
{
await CreateDotnetPublishDeploymentBundle(recommendation);
}
catch (DeployToolException ex)
{
throw new FailedToCreateDeploymentBundleException(ex.ErrorCode, ex.Message, ex.ProcessExitCode, ex);
}
}
}
private async Task CreateContainerDeploymentBundle(CloudApplication cloudApplication, Recommendation recommendation)
{
if (_interactiveService == null)
throw new InvalidOperationException($"{nameof(_interactiveService)} is null as part of the orchestartor object");
if (_dockerEngine == null)
throw new InvalidOperationException($"{nameof(_dockerEngine)} is null as part of the orchestartor object");
if (_deploymentBundleHandler == null)
throw new InvalidOperationException($"{nameof(_deploymentBundleHandler)} is null as part of the orchestrator object");
if (_optionSettingHandler == null)
throw new InvalidOperationException($"{nameof(_optionSettingHandler)} is null as part of the orchestrator object");
if (_fileManager == null)
throw new InvalidOperationException($"{nameof(_fileManager)} is null as part of the orchestrator object");
if (!DockerUtilities.TryGetDockerfile(recommendation, _fileManager, out _))
{
_interactiveService.LogInfoMessage("Generating Dockerfile...");
try
{
_dockerEngine.GenerateDockerFile(recommendation);
}
catch (DockerEngineExceptionBase ex)
{
var errorMessage = "Failed to generate a docker file due to the following error:" + Environment.NewLine + ex.Message;
throw new FailedToGenerateDockerFileException(DeployToolErrorCode.FailedToGenerateDockerFile, errorMessage, ex);
}
}
_dockerEngine.DetermineDockerExecutionDirectory(recommendation);
// Read this from the OptionSetting instead of recommendation.DeploymentBundle.
// When its value comes from a replacement token, it wouldn't have been set back to the DeploymentBundle
var respositoryName = _optionSettingHandler.GetOptionSettingValue<string>(recommendation, _optionSettingHandler.GetOptionSetting(recommendation, Constants.Docker.ECRRepositoryNameOptionId));
if (respositoryName == null)
throw new InvalidECRRepositoryNameException(DeployToolErrorCode.ECRRepositoryNameIsNull, "The ECR Repository Name is null.");
string imageTag;
try
{
var tagSuffix = _optionSettingHandler.GetOptionSettingValue<string>(recommendation, _optionSettingHandler.GetOptionSetting(recommendation, Constants.Docker.ImageTagOptionId));
imageTag = $"{respositoryName}:{tagSuffix}";
}
catch (OptionSettingItemDoesNotExistException)
{
imageTag = $"{respositoryName}:{DateTime.UtcNow.Ticks}";
}
await _deploymentBundleHandler.BuildContainerImage(cloudApplication, recommendation, imageTag);
// These option settings need to be persisted back as they are not always provided by the user and we have custom logic to determine their values
await _optionSettingHandler.SetOptionSettingValue(recommendation, Constants.Docker.DockerExecutionDirectoryOptionId, recommendation.DeploymentBundle.DockerExecutionDirectory);
await _optionSettingHandler.SetOptionSettingValue(recommendation, Constants.Docker.DockerfileOptionId, recommendation.DeploymentBundle.DockerfilePath);
// Try to inspect the container environment variables to provide better insights on the HTTP port to use for the container.
// If we run into issues doing so, we can proceed without throwing a terminating exception.
try
{
var environmentVariables = await _deploymentBundleHandler.InspectContainerImageEnvironmentVariables(recommendation, imageTag);
if (environmentVariables.ContainsKey(Constants.Docker.DotnetHttpPortEnvironmentVariable))
{
var httpPort = environmentVariables[Constants.Docker.DotnetHttpPortEnvironmentVariable];
// Assuming a single value can be specified
if (int.TryParse(httpPort, out var httpPortInt))
{
if (recommendation.DeploymentBundle.DockerfileHttpPort != httpPortInt)
{
_interactiveService.LogInfoMessage($"The HTTP port you have chosen in your deployment settings is different than the .NET HTTP port exposed in the container. " +
$"The container has the environment variable {Constants.Docker.DotnetHttpPortEnvironmentVariable}={httpPortInt}, " +
$"whereas the port you chose in the deployment settings is {recommendation.DeploymentBundle.DockerfileHttpPort}." +
$"The deployment may fail the health check if these 2 ports are misaligned.");
}
}
}
}
catch (ContainerInspectFailedException ex)
{
_interactiveService.LogDebugMessage($"Unable to inspect the docker container to retrieve the HTTP port used by .NET due to the following error: {ex.Message}");
}
_interactiveService.LogSectionStart("Pushing container image to Elastic Container Registry (ECR)", "Using the docker CLI to log on to ECR and push the local image to ECR.");
await _deploymentBundleHandler.PushContainerImageToECR(recommendation, respositoryName, imageTag);
}
private async Task CreateDotnetPublishDeploymentBundle(Recommendation recommendation)
{
if (_deploymentBundleHandler == null)
throw new InvalidOperationException($"{nameof(_deploymentBundleHandler)} is null as part of the orchestartor object");
if (_interactiveService == null)
throw new InvalidOperationException($"{nameof(_interactiveService)} is null as part of the orchestartor object");
await _deploymentBundleHandler.CreateDotnetPublishZip(recommendation);
}
public CloudApplicationResourceType GetCloudApplicationResourceType(DeploymentTypes deploymentType)
{
switch (deploymentType)
{
case DeploymentTypes.CdkProject:
return CloudApplicationResourceType.CloudFormationStack;
case DeploymentTypes.BeanstalkEnvironment:
return CloudApplicationResourceType.BeanstalkEnvironment;
case DeploymentTypes.ElasticContainerRegistryImage:
return CloudApplicationResourceType.ElasticContainerRegistryImage;
default:
var errorMessage = $"Failed to find ${nameof(CloudApplicationResourceType)} from {nameof(DeploymentTypes)} {deploymentType}";
throw new FailedToFindCloudApplicationResourceType(DeployToolErrorCode.FailedToFindCloudApplicationResourceType, errorMessage);
}
}
}
}