public void DeleteStack(string stackName)
        {
            var variablesFile = Path.GetTempFileName();
            var variables     = new CalamariVariables();

            variables.Set("Octopus.Action.AwsAccount.Variable", "AWSAccount");
            variables.Set("AWSAccount.AccessKey", ExternalVariables.Get(ExternalVariable.AwsAcessKey));
            variables.Set("AWSAccount.SecretKey", ExternalVariables.Get(ExternalVariable.AwsSecretKey));
            variables.Set("Octopus.Action.Aws.Region", region);
            variables.Set(AwsSpecialVariables.CloudFormation.StackName, stackName);
            variables.Save(variablesFile);

            using (new TemporaryFile(variablesFile))
            {
                var log     = new InMemoryLog();
                var command = new DeleteCloudFormationCommand(
                    log,
                    variables
                    );
                var result = command.Execute(new[]
                {
                    "--variables", $"{variablesFile}",
                    "--waitForCompletion", "true"
                });

                result.Should().Be(0);
            }
        }
Exemple #2
0
        protected string UploadEntireCompressedPackage(string packageFilePath, string packageId, string packageVersion, List <S3PackageOptions> propertiesList, VariableDictionary customVariables = null)
        {
            var bucketKeyPrefix = $"calamaritest/{Guid.NewGuid():N}/";
            var variables       = new CalamariVariables();

            propertiesList.ForEach(properties =>
            {
                properties.BucketKeyPrefix = bucketKeyPrefix;
                variables.Set(AwsSpecialVariables.S3.PackageOptions, JsonConvert.SerializeObject(properties, GetEnrichedSerializerSettings()));
                variables.Set(PackageVariables.PackageId, packageId);
                variables.Set(PackageVariables.PackageVersion, packageVersion);
            });

            var variablesFile = Path.GetTempFileName();

            variables.Set("Octopus.Action.AwsAccount.Variable", "AWSAccount");
            variables.Set("AWSAccount.AccessKey", ExternalVariables.Get(ExternalVariable.AwsAcessKey));
            variables.Set("AWSAccount.SecretKey", ExternalVariables.Get(ExternalVariable.AwsSecretKey));
            variables.Set("Octopus.Action.Aws.Region", region);

            if (customVariables != null)
            {
                variables.Merge(customVariables);
            }

            variables.Save(variablesFile);

            using (new TemporaryFile(variablesFile))
            {
                var log        = new InMemoryLog();
                var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

                var command = new UploadAwsS3Command(
                    log,
                    variables,
                    fileSystem,
                    new SubstituteInFiles(log, fileSystem, new FileSubstituter(log, fileSystem), variables),
                    new ExtractPackage(new CombinedPackageExtractor(log, variables, new CommandLineRunner(log, variables)), fileSystem, variables, log),
                    new StructuredConfigVariablesService(new PrioritisedList <IFileFormatVariableReplacer>
                {
                    new JsonFormatVariableReplacer(fileSystem, log),
                    new XmlFormatVariableReplacer(fileSystem, log),
                    new YamlFormatVariableReplacer(fileSystem, log),
                    new PropertiesFormatVariableReplacer(fileSystem, log),
                }, variables, fileSystem, log)
                    );

                var result = command.Execute(new[] {
                    "--package", $"{packageFilePath}",
                    "--variables", $"{variablesFile}",
                    "--bucket", bucketName,
                    "--targetMode", S3TargetMode.EntirePackage.ToString()
                });

                result.Should().Be(0);
            }

            return(bucketKeyPrefix);
        }
Exemple #3
0
        string Upload(string packageName, List <S3FileSelectionProperties> fileSelections)
        {
            var bucketKeyPrefix = $"calamaritest/{Guid.NewGuid():N}/";

            fileSelections.ForEach(properties =>
            {
                if (properties is S3MultiFileSelectionProperties multiFileSelectionProperties)
                {
                    multiFileSelectionProperties.BucketKeyPrefix = bucketKeyPrefix;
                }

                if (properties is S3SingleFileSelectionProperties singleFileSelectionProperties)
                {
                    singleFileSelectionProperties.BucketKeyPrefix = bucketKeyPrefix;
                }
            });

            var variablesFile = Path.GetTempFileName();
            var variables     = new CalamariVariables();

            variables.Set("Octopus.Action.AwsAccount.Variable", "AWSAccount");
            variables.Set("AWSAccount.AccessKey", Environment.GetEnvironmentVariable("AWS_Calamari_Access"));
            variables.Set("AWSAccount.SecretKey", Environment.GetEnvironmentVariable("AWS_Calamari_Secret"));
            variables.Set("Octopus.Action.Aws.Region", RegionEndpoint.APSoutheast1.SystemName);
            variables.Set(AwsSpecialVariables.S3.FileSelections,
                          JsonConvert.SerializeObject(fileSelections, GetEnrichedSerializerSettings()));
            variables.Save(variablesFile);

            var packageDirectory = TestEnvironment.GetTestPath("AWS", "S3", packageName);

            using (var package =
                       new TemporaryFile(PackageBuilder.BuildSimpleZip(packageName, "1.0.0", packageDirectory)))
                using (new TemporaryFile(variablesFile))
                {
                    var log        = new InMemoryLog();
                    var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
                    var command    = new UploadAwsS3Command(
                        log,
                        variables,
                        fileSystem,
                        new SubstituteInFiles(fileSystem, new FileSubstituter(log, fileSystem), variables),
                        new ExtractPackage(new CombinedPackageExtractor(log), fileSystem, variables, log)
                        );
                    var result = command.Execute(new[] {
                        "--package", $"{package.FilePath}",
                        "--variables", $"{variablesFile}",
                        "--bucket", BucketName,
                        "--targetMode", S3TargetMode.FileSelections.ToString()
                    });

                    result.Should().Be(0);
                }

            return(bucketKeyPrefix);
        }
        protected (CalamariResult result, IVariables variables) RunScript(string scriptName,
                                                                          Dictionary <string, string> additionalVariables  = null,
                                                                          Dictionary <string, string> additionalParameters = null,
                                                                          string sensitiveVariablesPassword = null,
                                                                          IEnumerable <string> extensions   = null)
        {
            var variablesFile = Path.GetTempFileName();
            var variables     = new CalamariVariables();

            variables.Set(ScriptVariables.ScriptFileName, scriptName);
            variables.Set(ScriptVariables.ScriptBody, File.ReadAllText(GetFixtureResource("Scripts", scriptName)));
            variables.Set(ScriptVariables.Syntax, scriptName.ToScriptType().ToString());

            additionalVariables?.ToList().ForEach(v => variables[v.Key] = v.Value);

            using (new TemporaryFile(variablesFile))
            {
                var cmdBase = Calamari()
                              .Action("run-script");

                if (sensitiveVariablesPassword == null)
                {
                    variables.Save(variablesFile);
                    cmdBase = cmdBase.Argument("variables", variablesFile);
                }
                else
                {
                    variables.SaveEncrypted(sensitiveVariablesPassword, variablesFile);
                    cmdBase = cmdBase.Argument("sensitiveVariables", variablesFile)
                              .Argument("sensitiveVariablesPassword", sensitiveVariablesPassword);
                }

                if (extensions != null)
                {
                    cmdBase.Argument("extensions", string.Join(",", extensions));
                }

                cmdBase = (additionalParameters ?? new Dictionary <string, string>()).Aggregate(cmdBase, (cmd, param) => cmd.Argument(param.Key, param.Value));

                var output = Invoke(cmdBase, variables);

                return(output, variables);
            }
        }
        public void ShouldRunBashInsteadOfPowerShell()
        {
            var variablesFile = Path.GetTempFileName();

            var variables = new CalamariVariables();

            variables.Set(SpecialVariables.Action.Script.ScriptBodyBySyntax(ScriptSyntax.PowerShell), "Write-Host Hello PowerShell");
            variables.Set(SpecialVariables.Action.Script.ScriptBodyBySyntax(ScriptSyntax.CSharp), "Write-Host Hello CSharp");
            variables.Set(SpecialVariables.Action.Script.ScriptBodyBySyntax(ScriptSyntax.Bash), "echo Hello Bash");
            variables.Save(variablesFile);

            using (new TemporaryFile(variablesFile))
            {
                var output = Invoke(Calamari()
                                    .Action("run-script")
                                    .Argument("variables", variablesFile));

                output.AssertSuccess();
                output.AssertOutput("Hello Bash");
            }
        }
Exemple #6
0
        protected string Upload(string packageName, List <S3TargetPropertiesBase> propertiesList, VariableDictionary customVariables = null, S3TargetMode s3TargetMode = S3TargetMode.FileSelections)
        {
            const string packageVersion  = "1.0.0";
            var          bucketKeyPrefix = $"calamaritest/{Guid.NewGuid():N}/";
            var          variables       = new CalamariVariables();

            propertiesList.ForEach(properties =>
            {
                switch (properties)
                {
                case S3MultiFileSelectionProperties multiFileSelectionProperties:
                    multiFileSelectionProperties.BucketKeyPrefix = bucketKeyPrefix;
                    variables.Set(AwsSpecialVariables.S3.FileSelections, JsonConvert.SerializeObject(propertiesList, GetEnrichedSerializerSettings()));
                    break;

                case S3SingleFileSelectionProperties singleFileSelectionProperties:
                    singleFileSelectionProperties.BucketKeyPrefix = bucketKeyPrefix;
                    variables.Set(AwsSpecialVariables.S3.FileSelections, JsonConvert.SerializeObject(propertiesList, GetEnrichedSerializerSettings()));
                    break;

                case S3PackageOptions packageOptions:
                    packageOptions.BucketKeyPrefix = bucketKeyPrefix;
                    variables.Set(AwsSpecialVariables.S3.PackageOptions, JsonConvert.SerializeObject(packageOptions, GetEnrichedSerializerSettings()));
                    variables.Set(PackageVariables.PackageId, packageName);
                    variables.Set(PackageVariables.PackageVersion, packageVersion);
                    break;
                }
            });

            var variablesFile = Path.GetTempFileName();

            variables.Set("Octopus.Action.AwsAccount.Variable", "AWSAccount");
            variables.Set("AWSAccount.AccessKey", ExternalVariables.Get(ExternalVariable.AwsAcessKey));
            variables.Set("AWSAccount.SecretKey", ExternalVariables.Get(ExternalVariable.AwsSecretKey));
            variables.Set("Octopus.Action.Aws.Region", region);

            if (customVariables != null)
            {
                variables.Merge(customVariables);
            }

            variables.Save(variablesFile);

            var packageDirectory = TestEnvironment.GetTestPath("AWS", "S3", packageName);

            using (var package = new TemporaryFile(PackageBuilder.BuildSimpleZip(packageName, packageVersion, packageDirectory)))
                using (new TemporaryFile(variablesFile))
                {
                    var log        = new InMemoryLog();
                    var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

                    var command = new UploadAwsS3Command(
                        log,
                        variables,
                        fileSystem,
                        new SubstituteInFiles(log, fileSystem, new FileSubstituter(log, fileSystem), variables),
                        new ExtractPackage(new CombinedPackageExtractor(log, variables, new CommandLineRunner(log, variables)), fileSystem, variables, log),
                        new StructuredConfigVariablesService(new PrioritisedList <IFileFormatVariableReplacer>
                    {
                        new JsonFormatVariableReplacer(fileSystem, log),
                        new XmlFormatVariableReplacer(fileSystem, log),
                        new YamlFormatVariableReplacer(fileSystem, log),
                        new PropertiesFormatVariableReplacer(fileSystem, log),
                    }, variables, fileSystem, log)
                        );

                    var result = command.Execute(new[] {
                        "--package", $"{package.FilePath}",
                        "--variables", $"{variablesFile}",
                        "--bucket", bucketName,
                        "--targetMode", s3TargetMode.ToString()
                    });

                    result.Should().Be(0);
                }

            return(bucketKeyPrefix);
        }
Exemple #7
0
 public VariableFile(CalamariVariables variables)
 {
     FilePath = Path.GetTempFileName();
     tempFile = new TemporaryFile(FilePath);
     variables.Save(FilePath);
 }