Exemple #1
0
        void ReadAdditionalVariablesFromFile(CalamariVariables variables)
        {
            var path = variables.Get(AdditionalVariablesPathVariable)
                       ?? variables.Get($"env:{AdditionalVariablesPathVariable}");

            string BuildExceptionMessage(string reason)
            => $"Could not read additional variables from JSON file at '{path}'. " +
            $"{reason} Make sure the file can be read or remove the " +
            $"'{AdditionalVariablesPathVariable}' environment variable. " +
            $"See inner exception for details.";

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (!fileSystem.FileExists(path))
            {
                throw new CommandException(BuildExceptionMessage("File does not exist."));
            }

            try
            {
                var readVars = new VariableDictionary(path);
                variables.Merge(readVars);
            }
            catch (Exception e)
            {
                throw new CommandException(BuildExceptionMessage("The file could not be read."), e);
            }
        }
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
        void ReadUnencryptedVariablesFromFile(CommonOptions options, CalamariVariables variables)
        {
            var variablesFile = options.InputVariables.VariablesFile;

            if (string.IsNullOrEmpty(variablesFile))
            {
                return;
            }

            if (!fileSystem.FileExists(variablesFile))
            {
                throw new CommandException("Could not find variables file: " + variablesFile);
            }

            var readVars = new VariableDictionary(variablesFile);

            variables.Merge(readVars);
        }
Exemple #4
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);
        }