Use volumes to share sshkey and authorized keys instead of copying and add build time variables to docker-compose
This commit is contained in:
52
docker-compose-generator/src/BuildTimeVariableVisitor.cs
Normal file
52
docker-compose-generator/src/BuildTimeVariableVisitor.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using YamlDotNet.RepresentationModel;
|
||||
|
||||
namespace DockerGenerator
|
||||
{
|
||||
// Replace built time variable ( $<variable_name>? ) in the docker generator
|
||||
class BuildTimeVariableVisitor : YamlVisitorBase
|
||||
{
|
||||
class Context
|
||||
{
|
||||
public List<YamlScalarNode> ToRemove = new List<YamlScalarNode>();
|
||||
}
|
||||
Stack<Context> _Contexts = new Stack<Context>();
|
||||
Context CurrentContext
|
||||
{
|
||||
get
|
||||
{
|
||||
return _Contexts.TryPeek(out var ctx) ? ctx : null;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void VisitChildren(YamlSequenceNode sequence)
|
||||
{
|
||||
_Contexts.Push(new Context());
|
||||
base.VisitChildren(sequence);
|
||||
var ctx = _Contexts.Pop();
|
||||
foreach (var child in ctx.ToRemove)
|
||||
{
|
||||
sequence.Children.Remove(child);
|
||||
}
|
||||
}
|
||||
public override void Visit(YamlScalarNode scalar)
|
||||
{
|
||||
bool removeNode = false;
|
||||
scalar.Value = Regex.Replace(scalar.Value, "\\$<(.*?)>\\?", (match) =>
|
||||
{
|
||||
var replacedBy = Environment.GetEnvironmentVariable(match.Groups[1].Value);
|
||||
if (string.IsNullOrEmpty(replacedBy))
|
||||
{
|
||||
removeNode = true;
|
||||
}
|
||||
return replacedBy;
|
||||
});
|
||||
if (removeNode)
|
||||
CurrentContext?.ToRemove.Add(scalar);
|
||||
base.Visit(scalar);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,7 +87,7 @@ namespace DockerGenerator
|
||||
output.Add("services", new YamlMappingNode(Merge(services)));
|
||||
output.Add("volumes", new YamlMappingNode(volumes));
|
||||
output.Add("networks", new YamlMappingNode(networks));
|
||||
|
||||
PostProcess(output);
|
||||
|
||||
var dockerImages = ((YamlMappingNode)output["services"]).Children.Select(kv => kv.Value["image"].ToString()).ToList();
|
||||
dockerImages.Add("btcpayserver/docker-compose-builder:1.24.1");
|
||||
@@ -119,6 +119,11 @@ namespace DockerGenerator
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
private void PostProcess(YamlMappingNode output)
|
||||
{
|
||||
new BuildTimeVariableVisitor().Visit(output);
|
||||
}
|
||||
|
||||
private KeyValuePair<YamlNode, YamlNode>[] Merge(List<KeyValuePair<YamlNode, YamlNode>> services)
|
||||
{
|
||||
return services
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "pregen",
|
||||
"environmentVariables": {
|
||||
"BTCPAY_HOST_SSHKEYFILE": "test.rsa",
|
||||
"BTCPAYGEN_LIGHTNING": "clightning",
|
||||
"BTCPAYGEN_CRYPTO4": "ftc",
|
||||
"BTCPAYGEN_CRYPTO3": "btg",
|
||||
@@ -13,4 +14,4 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user