Esempio n. 1
0
        /// <summary>
        /// This method takes a user specified directory path and generates the CDK deployment project at this location.
        /// If the provided directory path is an empty string, then a default directory is created to save the CDK deployment project.
        /// </summary>
        /// <param name="saveCdkDirectoryPath">An absolute or a relative path provided by the user.</param>
        /// <param name="projectDisplayName">The name of the deployment project that will be displayed in the list of available deployment options.</param>
        /// <returns></returns>
        public async Task ExecuteAsync(string saveCdkDirectoryPath, string projectDisplayName)
        {
            var orchestrator    = new Orchestrator(_session, new[] { RecipeLocator.FindRecipeDefinitionsPath() });
            var recommendations = await GenerateRecommendationsToSaveDeploymentProject(orchestrator);

            var selectedRecommendation = _consoleUtilities.AskToChooseRecommendation(recommendations);

            if (string.IsNullOrEmpty(saveCdkDirectoryPath))
            {
                saveCdkDirectoryPath = GenerateDefaultSaveDirectoryPath();
            }

            var newDirectoryCreated = CreateSaveCdkDirectory(saveCdkDirectoryPath);

            var(isValid, errorMessage) = ValidateSaveCdkDirectory(saveCdkDirectoryPath);
            if (!isValid)
            {
                if (newDirectoryCreated)
                {
                    _directoryManager.Delete(saveCdkDirectoryPath);
                }
                errorMessage = $"Failed to generate deployment project.{Environment.NewLine}{errorMessage}";
                throw new InvalidSaveDirectoryForCdkProject(errorMessage.Trim());
            }

            var directoryUnderSourceControl = await IsDirectoryUnderSourceControl(saveCdkDirectoryPath);

            if (!directoryUnderSourceControl)
            {
                var userPrompt = "Warning: The target directory is not being tracked by source control. If the saved deployment " +
                                 "project is used for deployment it is important that the deployment project is retained to allow " +
                                 "future redeployments to previously deployed applications. " + Environment.NewLine + Environment.NewLine +
                                 "Do you still want to continue?";

                _toolInteractiveService.WriteLine();
                var yesNoResult = _consoleUtilities.AskYesNoQuestion(userPrompt, YesNo.Yes);

                if (yesNoResult == YesNo.No)
                {
                    if (newDirectoryCreated)
                    {
                        _directoryManager.Delete(saveCdkDirectoryPath);
                    }
                    return;
                }
            }

            _cdkProjectHandler.CreateCdkProject(selectedRecommendation, _session, saveCdkDirectoryPath);
            await GenerateDeploymentRecipeSnapShot(selectedRecommendation, saveCdkDirectoryPath, projectDisplayName);

            var saveCdkDirectoryFullPath = _directoryManager.GetDirectoryInfo(saveCdkDirectoryPath).FullName;

            _toolInteractiveService.WriteLine();
            _toolInteractiveService.WriteLine($"The CDK deployment project is saved at: {saveCdkDirectoryFullPath}");

            await _deploymentManifestEngine.UpdateDeploymentManifestFile(saveCdkDirectoryFullPath, _targetApplicationFullPath);
        }
Esempio n. 2
0
        public async Task UpdateDeploymentManifestFile()
        {
            // Arrange
            var saveCdkDirectoryFullPath  = Path.Combine(_targetApplicationDirectoryFullPath, "DeploymentProjects", "MyCdkApp");
            var saveCdkDirectoryFullPath2 = Path.Combine(_targetApplicationDirectoryFullPath, "DeploymentProjects", "MyCdkApp2");
            var saveCdkDirectoryFullPath3 = Path.Combine(_targetApplicationDirectoryFullPath, "DeploymentProjects", "MyCdkApp3");

            _testDirectoryManager.CreateDirectory(saveCdkDirectoryFullPath);
            _testDirectoryManager.CreateDirectory(saveCdkDirectoryFullPath2);
            _testDirectoryManager.CreateDirectory(saveCdkDirectoryFullPath3);

            var saveCdkDirectoryRelativePath  = Path.GetRelativePath(_targetApplicationDirectoryFullPath, saveCdkDirectoryFullPath);
            var saveCdkDirectoryRelativePath2 = Path.GetRelativePath(_targetApplicationDirectoryFullPath, saveCdkDirectoryFullPath2);
            var saveCdkDirectoryRelativePath3 = Path.GetRelativePath(_targetApplicationDirectoryFullPath, saveCdkDirectoryFullPath3);

            var deploymentManifestFilePath = Path.Combine(_targetApplicationDirectoryFullPath, "aws-deployments.json");

            // Act
            await _deploymentManifestEngine.UpdateDeploymentManifestFile(saveCdkDirectoryFullPath, _targetApplicationFullPath);

            // Assert
            Assert.True(_fileManager.Exists(deploymentManifestFilePath));
            var deploymentProjectPaths = await GetDeploymentManifestEntries(deploymentManifestFilePath);

            Assert.Single(deploymentProjectPaths);
            deploymentProjectPaths.ShouldContain(saveCdkDirectoryRelativePath);

            // Update deployment-manifest file
            await _deploymentManifestEngine.UpdateDeploymentManifestFile(saveCdkDirectoryFullPath2, _targetApplicationFullPath);

            await _deploymentManifestEngine.UpdateDeploymentManifestFile(saveCdkDirectoryFullPath3, _targetApplicationFullPath);

            // Assert
            Assert.True(_fileManager.Exists(deploymentManifestFilePath));
            deploymentProjectPaths = await GetDeploymentManifestEntries(deploymentManifestFilePath);

            Assert.Equal(3, deploymentProjectPaths.Count);
            deploymentProjectPaths.ShouldContain(saveCdkDirectoryRelativePath);
            deploymentProjectPaths.ShouldContain(saveCdkDirectoryRelativePath2);
            deploymentProjectPaths.ShouldContain(saveCdkDirectoryRelativePath3);

            // cleanup
            File.Delete(deploymentManifestFilePath);
            Assert.False(_fileManager.Exists(deploymentManifestFilePath));
        }