Ejemplo n.º 1
0
        public void GetValue_SingleDocument_ArrayPath(string path, string expected)
        {
            var x = @"
instances:
- field: abc
  image:
    tag: develop-2892
- field: def
  image:
    tag: develop-123
- field: xyz
  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();
            var readValue = yamlUtilities.ExtractValueFromDoc(path, doc);

            readValue.Should().Be(expected);
        }
Ejemplo n.º 2
0
        private IEnumerable <DeploymentManifestInstanceInfo> LoadDeploymentManifestInstanceInfos(
            IEnumerable <ApplicationImage> applicationImages,
            string instanceName,
            IEnumerable <FileInfo> files
            )
        {
            var yamlUtilities      = new YamlUtilities();
            var imageToFilenameMap = new Dictionary <ApplicationImage, FileInfo>();

            var result = new List <DeploymentManifestInstanceInfo>();

            var fileInfos = files as FileInfo[] ?? files.ToArray();

            foreach (var file in fileInfos)
            {
                var yaml = new YamlStream();
                yamlUtilities.ReadYamlStream(yaml, file.FullName);

                foreach (var doc in yaml.Documents)
                {
                    foreach (var image in applicationImages)
                    {
                        var tagInManifest = yamlUtilities.ExtractValueFromDoc(image.TagProperty.Path, doc);

                        if (tagInManifest == null)
                        {
                            continue;
                        }

                        if (imageToFilenameMap.ContainsKey(image))
                        {
                            // TODO: handle situation where multiple files define the same image tag (ERROR and warn user)
                        }

                        imageToFilenameMap[image] = file;
                        result.Add(
                            new DeploymentManifestInstanceInfo(
                                instanceName,
                                image,
                                tagInManifest,
                                file,
                                fileInfos,
                                ApplicationImageInstanceType.Primary,
                                new ApplicationImageInstanceSource(ApplicationImageInstanceSourceType.Manual, 0)
                                )
                            );
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public void GetValue_SingleDocument_MapPath(string path, string expected)
        {
            var x = @"
cat:
  image1:
    tag: develop-2892
  image2:
    tag: develop-123";

            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();
            var readValue = yamlUtilities.ExtractValueFromDoc(path, doc);

            readValue.Should().Be(expected);
        }
Ejemplo n.º 5
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);
        }