Ejemplo n.º 1
0
 public DeploymentBundleHandler(
     OrchestratorSession session,
     ICommandLineWrapper commandLineWrapper,
     IAWSResourceQueryer awsResourceQueryer,
     IOrchestratorInteractiveService interactiveService,
     IDirectoryManager directoryManager,
     IZipFileManager zipFileManager)
 {
     _session            = session;
     _commandLineWrapper = commandLineWrapper;
     _awsResourceQueryer = awsResourceQueryer;
     _interactiveService = interactiveService;
     _directoryManager   = directoryManager;
     _zipFileManager     = zipFileManager;
 }
 public Orchestrator(
     OrchestratorSession session,
     IOrchestratorInteractiveService interactiveService,
     ICdkProjectHandler cdkProjectHandler,
     IAWSResourceQueryer awsResourceQueryer,
     IDeploymentBundleHandler deploymentBundleHandler,
     IList <string> recipeDefinitionPaths)
 {
     _session                 = session;
     _interactiveService      = interactiveService;
     _cdkProjectHandler       = cdkProjectHandler;
     _deploymentBundleHandler = deploymentBundleHandler;
     _recipeDefinitionPaths   = recipeDefinitionPaths;
     _awsResourceQueryer      = awsResourceQueryer;
     _awsClientFactory        = new DefaultAWSClientFactory();
 }
Ejemplo n.º 3
0
        public async Task CreateCdkDeployment(OrchestratorSession session, CloudApplication cloudApplication, Recommendation recommendation)
        {
            // Create a new temporary CDK project for a new deployment
            _interactiveService.LogMessageLine($"Generating a {recommendation.Recipe.Name} CDK Project");
            var cdkProjectPath = await CreateCdkProjectForDeployment(recommendation, session);

            // Write required configuration in appsettings.json
            var appSettingsBody     = _appSettingsBuilder.Build(cloudApplication, recommendation);
            var appSettingsFilePath = Path.Combine(cdkProjectPath, "appsettings.json");

            using (var appSettingsFile = new StreamWriter(appSettingsFilePath))
            {
                await appSettingsFile.WriteAsync(appSettingsBody);
            }

            _interactiveService.LogMessageLine("Starting deployment of CDK Project");

            // Ensure region is bootstrapped
            await _commandLineWrapper.Run($"npx cdk bootstrap aws://{session.AWSAccountId}/{session.AWSRegion}");

            // Handover to CDK command line tool
            // Use a CDK Context parameter to specify the settings file that has been serialized.
            await _commandLineWrapper.Run($"npx cdk deploy --require-approval never -c {CloudFormationIdentifierConstants.SETTINGS_PATH_CDK_CONTEXT_PARAMETER}=\"{appSettingsFilePath}\"", cdkProjectPath);
        }
Ejemplo n.º 4
0
        private async Task <string> CreateCdkProjectForDeployment(Recommendation recommendation, OrchestratorSession session)
        {
            var tempDirectoryPath =
                Path.Combine(
                    CDKConstants.ProjectsDirectory,
                    Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));

            Directory.CreateDirectory(tempDirectoryPath);

            var templateEngine = new TemplateEngine();
            await templateEngine.GenerateCDKProjectFromTemplate(recommendation, session, tempDirectoryPath);

            return(tempDirectoryPath);
        }
        public async Task GenerateCDKProjectFromTemplate(Recommendation recommendation, OrchestratorSession session, string outputDirectory)
        {
            //The location of the base template that will be installed into the templating engine
            var cdkProjectTemplateDirectory = Path.Combine(Path.GetDirectoryName(recommendation.Recipe.RecipePath), recommendation.Recipe.CdkProjectTemplate);

            //Installing the base template into the templating engine to make it available for generation
            InstallTemplates(cdkProjectTemplateDirectory);

            //Looking up the installed template in the templating engine
            var template =
                _bootstrapper
                .ListTemplates(
                    true,
                    WellKnownSearchFilters.NameFilter(recommendation.Recipe.CdkProjectTemplateId))
                .FirstOrDefault()
                ?.Info;

            //If the template is not found, throw an exception
            if (template == null)
            {
                throw new Exception($"Failed to find a Template for [{recommendation.Recipe.CdkProjectTemplateId}]");
            }

            var templateParameters = new Dictionary <string, string> {
                { "AWSAccountID", session.AWSAccountId },
                { "AWSRegion", session.AWSRegion },

                // CDK Template projects can parameterize the version number of the AWS.Deploy.Recipes.CDK.Common package. This avoid
                // projects having to be modified every time the package version is bumped.
                { "AWSDeployRecipesCDKCommonVersion", FileVersionInfo.GetVersionInfo(typeof(CloudFormationIdentifierConstants).Assembly.Location).ProductVersion }
            };

            foreach (var option in recommendation.Recipe.OptionSettings)
            {
                var currentValue = recommendation.GetOptionSettingValue(option);
                if (currentValue != null)
                {
                    templateParameters[option.Id] = currentValue.ToString();
                }
            }

            try
            {
                //Generate the CDK project using the installed template into the output directory
                await _bootstrapper.CreateAsync(template, recommendation.ProjectDefinition.AssemblyName, outputDirectory, templateParameters, false, "");
            }
            catch
            {
                throw new TemplateGenerationFailedException();
            }
        }