public NPMPackageInitializerTests()
        {
            _fileManager            = new TestFileManager();
            _directoryManager       = new TestDirectoryManager();
            _testCommandLineWrapper = new TestCommandLineWrapper();
            var packageJsonGenerator = new PackageJsonGenerator(_packageJsonTemplate);

            _npmPackageInitializer = new NPMPackageInitializer(_testCommandLineWrapper, packageJsonGenerator, _fileManager, _directoryManager);
        }
Ejemplo n.º 2
0
        private static async Task <int> Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            SetExecutionEnvironment();

            var preambleWriter = new ConsoleInteractiveServiceImpl(diagnosticLoggingEnabled: false);

            preambleWriter.WriteLine("AWS .NET deployment tool for deploying .NET Core applications to AWS.");
            preambleWriter.WriteLine("Project Home: https://github.com/aws/aws-dotnet-deploy");
            preambleWriter.WriteLine(string.Empty);

            // Name is important to set here to show correctly in the CLI usage help.
            // Either dotnet-aws or dotnet aws works from the CLI. System.Commandline's help system does not like having a space with dotnet aws.
            var rootCommand = new RootCommand {
                Name = "dotnet-aws", Description = "The AWS .NET deployment tool for deploying .NET applications on AWS."
            };

            var deployCommand = new Command(
                "deploy",
                "Inspect, build, and deploy the .NET project to AWS using the recommended AWS service.")
            {
                _optionProfile,
                _optionRegion,
                _optionProjectPath,
                _optionDiagnosticLogging
            };

            deployCommand.Handler = CommandHandler.Create <string, string, string, bool, bool>(async(profile, region, projectPath, saveCdkProject, diagnostics) =>
            {
                var toolInteractiveService = new ConsoleInteractiveServiceImpl(diagnostics);

                try
                {
                    var orchestratorInteractiveService = new ConsoleOrchestratorLogger(toolInteractiveService);

                    var previousSettings = PreviousDeploymentSettings.ReadSettings(projectPath, null);

                    var awsUtilities   = new AWSUtilities(toolInteractiveService);
                    var awsCredentials = await awsUtilities.ResolveAWSCredentials(profile, previousSettings.Profile);
                    var awsRegion      = awsUtilities.ResolveAWSRegion(region, previousSettings.Region);

                    var commandLineWrapper =
                        new CommandLineWrapper(
                            orchestratorInteractiveService,
                            awsCredentials,
                            awsRegion);

                    var directoryManager     = new DirectoryManager();
                    var fileManager          = new FileManager();
                    var packageJsonGenerator = new PackageJsonGenerator(
                        typeof(PackageJsonGenerator).Assembly
                        .ReadEmbeddedFile(PackageJsonGenerator.TemplateIdentifier));
                    var npmPackageInitializer = new NPMPackageInitializer(commandLineWrapper, packageJsonGenerator, fileManager, directoryManager);
                    var cdkInstaller          = new CDKInstaller(commandLineWrapper);
                    var cdkManager            = new CDKManager(cdkInstaller, npmPackageInitializer);

                    var systemCapabilityEvaluator = new SystemCapabilityEvaluator(commandLineWrapper, cdkManager);
                    var systemCapabilities        = systemCapabilityEvaluator.Evaluate();

                    var stsClient      = new AmazonSecurityTokenServiceClient(awsCredentials);
                    var callerIdentity = await stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest());

                    var session = new OrchestratorSession
                    {
                        AWSProfileName     = profile,
                        AWSCredentials     = awsCredentials,
                        AWSRegion          = awsRegion,
                        AWSAccountId       = callerIdentity.Account,
                        ProjectPath        = projectPath,
                        ProjectDirectory   = projectPath,
                        SystemCapabilities = systemCapabilities,
                        CdkManager         = cdkManager
                    };

                    var awsResourceQueryer = new AWSResourceQueryer(new DefaultAWSClientFactory());
                    var zipFileManager     = new ZipFileManager(commandLineWrapper);

                    var deploy = new DeployCommand(
                        toolInteractiveService,
                        orchestratorInteractiveService,
                        new CdkProjectHandler(orchestratorInteractiveService, commandLineWrapper),
                        new DeploymentBundleHandler(session, commandLineWrapper, awsResourceQueryer, orchestratorInteractiveService, directoryManager, zipFileManager),
                        awsResourceQueryer,
                        session);

                    await deploy.ExecuteAsync(saveCdkProject);

                    return(CommandReturnCodes.SUCCESS);
                }
                catch (Exception e) when(e.IsAWSDeploymentExpectedException())
                {
                    // helpful error message should have already been presented to the user,
                    // bail out with an non-zero return code.
                    return(CommandReturnCodes.USER_ERROR);
                }
                catch (Exception e)
                {
                    // This is a bug
                    toolInteractiveService.WriteErrorLine(
                        "Unhandled exception.  This is a bug.  Please copy the stack trace below and file a bug at https://github.com/aws/aws-dotnet-deploy. " +
                        e.PrettyPrint());

                    return(CommandReturnCodes.UNHANDLED_EXCEPTION);
                }
            });
            rootCommand.Add(deployCommand);

            var listCommand = new Command("list-deployments", "List existing deployments.")
            {
                _optionProfile,
                _optionRegion,
                _optionProjectPath,
                _optionDiagnosticLogging
            };

            listCommand.Handler = CommandHandler.Create <string, string, string, bool>(async(profile, region, projectPath, diagnostics) =>
            {
                var toolInteractiveService         = new ConsoleInteractiveServiceImpl(diagnostics);
                var orchestratorInteractiveService = new ConsoleOrchestratorLogger(toolInteractiveService);

                var awsUtilities = new AWSUtilities(toolInteractiveService);

                var previousSettings = PreviousDeploymentSettings.ReadSettings(projectPath, null);

                var awsCredentials = await awsUtilities.ResolveAWSCredentials(profile, previousSettings.Profile);
                var awsRegion      = awsUtilities.ResolveAWSRegion(region, previousSettings.Region);

                var stsClient      = new AmazonSecurityTokenServiceClient(awsCredentials);
                var callerIdentity = await stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest());

                var session = new OrchestratorSession
                {
                    AWSProfileName   = profile,
                    AWSCredentials   = awsCredentials,
                    AWSRegion        = awsRegion,
                    AWSAccountId     = callerIdentity.Account,
                    ProjectPath      = projectPath,
                    ProjectDirectory = projectPath
                };

                var commandLineWrapper =
                    new CommandLineWrapper(
                        orchestratorInteractiveService,
                        awsCredentials,
                        awsRegion);

                var awsResourceQueryer = new AWSResourceQueryer(new DefaultAWSClientFactory());
                var directoryManager   = new DirectoryManager();
                var zipFileManager     = new ZipFileManager(commandLineWrapper);

                await new ListDeploymentsCommand(toolInteractiveService,
                                                 new ConsoleOrchestratorLogger(toolInteractiveService),
                                                 new CdkProjectHandler(orchestratorInteractiveService, commandLineWrapper),
                                                 new DeploymentBundleHandler(session, commandLineWrapper, awsResourceQueryer, orchestratorInteractiveService, directoryManager, zipFileManager),
                                                 awsResourceQueryer,
                                                 session).ExecuteAsync();
            });
            rootCommand.Add(listCommand);

            var deleteCommand = new Command("delete-deployment", "Delete an existing deployment.")
            {
                _optionProfile,
                _optionRegion,
                _optionProjectPath,
                _optionDiagnosticLogging,
                new Argument("deployment-name")
            };

            deleteCommand.Handler = CommandHandler.Create <string, string, string, string, bool>(async(profile, region, projectPath, deploymentName, diagnostics) =>
            {
                var toolInteractiveService = new ConsoleInteractiveServiceImpl(diagnostics);
                var awsUtilities           = new AWSUtilities(toolInteractiveService);

                var previousSettings = PreviousDeploymentSettings.ReadSettings(projectPath, null);

                var awsCredentials = await awsUtilities.ResolveAWSCredentials(profile, previousSettings.Profile);
                var awsRegion      = awsUtilities.ResolveAWSRegion(region, previousSettings.Region);

                var session = new OrchestratorSession
                {
                    AWSProfileName = profile,
                    AWSCredentials = awsCredentials,
                    AWSRegion      = awsRegion,
                };

                await new DeleteDeploymentCommand(new DefaultAWSClientFactory(), toolInteractiveService, session).ExecuteAsync(deploymentName);

                return(CommandReturnCodes.SUCCESS);
            });
            rootCommand.Add(deleteCommand);

            // if user didn't specify a command, default to help
            if (args.Length == 0)
            {
                args = new string[] { "-h" };
            }

            return(await rootCommand.InvokeAsync(args));
        }