Exemplo n.º 1
0
        public void SetValueInDoc_MultiDocument()
        {
            var x = @"cat:
  image:
    tag: develop-2892
dog:
  image:
    tag: develop-2892
---
sheep:
  image:
    tag: develop-10";

            var expected = @"cat:
  image:
    tag: develop-2892
dog:
  image:
    tag: develop-999
---
sheep:
  image:
    tag: develop-10
";

            var byteArray = Encoding.UTF8.GetBytes(x);
            var inStream  = new MemoryStream(byteArray);

            var yamlUtilities = new YamlUtilities();
            var yaml          = new YamlStream();

            yamlUtilities.ReadYamlStream(yaml, inStream);

            var doc = yaml.Documents.First();

            yamlUtilities.SetValueInDoc("dog.image.tag", doc, "develop-999");

            var outStream = new MemoryStream();

            yamlUtilities.WriteYamlStream(yaml, outStream);

            outStream.Position = 0;
            var reader = new StreamReader(outStream);
            var text   = reader.ReadToEnd();

            text.Should().Be(expected);
        }
Exemplo n.º 2
0
        private async Task <List <FileInfo> > InternalCreateRelease(RawDeploymentManifest manifest, string instance)
        {
            // the Raw Deployment Manifest type builds instances by cloning the original files and executes modifications on them.
            var copyOperations = new List <(FileInfo source, FileInfo dest)>();
            var deploymentManifestSourcePath = manifest.GetFullPath().FullName;

            foreach (var file in manifest.Files)
            {
                var sourceFilePath = Path.Combine(deploymentManifestSourcePath, file);
                var sourceFileInfo = new FileInfo(sourceFilePath);

                var targetFilename =
                    $"{Path.GetFileNameWithoutExtension(sourceFileInfo.Name)}-{instance}{sourceFileInfo.Extension}";
                var targetFilePath = Path.Combine(deploymentManifestSourcePath, targetFilename);
                var targetFileInfo = new FileInfo(targetFilePath);

                copyOperations.Add((sourceFileInfo, targetFileInfo));
            }

            copyOperations.ForEach(x => x.source.CopyTo(x.dest.FullName));

            // prepare to apply required modifications

            var yamlUtilities = new YamlUtilities();

            // TODO: load replacement entires from configuration
            var replacementEntries = new Dictionary <string, string>()
            {
                { "ingress.hosts[0].host", "{instanceid}.{original}" }
            };

            var modifiedFiles = new List <FileInfo>();

            foreach (var file in copyOperations.Select(x => x.dest))
            {
                var modified = false;

                var yamlStream = new YamlStream();

                {
                    await using var stream = file.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
                    yamlUtilities.ReadYamlStream(yamlStream, stream);

                    foreach (var item in replacementEntries)
                    {
                        var currentValue = yamlUtilities.ExtractValueFromDoc(item.Key, yamlStream.Documents.First());
                        if (currentValue == null)
                        {
                            continue;
                        }

                        var replacementValue = item.Value;
                        replacementValue = replacementValue.Replace("{original}", currentValue);
                        replacementValue = replacementValue.Replace("{instanceid}", instance);
                        yamlUtilities.SetValueInDoc(item.Key, yamlStream.Documents.First(), replacementValue);
                        modified = true;
                    }
                }

                if (modified)
                {
                    await using var stream = file.Open(FileMode.Create, FileAccess.Write, FileShare.None);
                    yamlUtilities.WriteYamlStream(yamlStream, stream);

                    modifiedFiles.Add(file);
                }
            }

            return(modifiedFiles);
        }