-
Notifications
You must be signed in to change notification settings - Fork 9
/
ComposeFile.cs
161 lines (131 loc) · 5.72 KB
/
ComposeFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System.Text;
using Serilog;
namespace CreateMatrix;
public class ComposeFile
{
public static void Create(string makePath)
{
// Create local Compose file
var composeFile = CreateComposefile(null);
var filePath = Path.Combine(makePath, "Test.yml");
Log.Logger.Information("Writing Compose file to {Path}", filePath);
File.WriteAllText(filePath, composeFile, Encoding.UTF8);
// Create develop Compose file
composeFile = CreateComposefile("develop");
filePath = Path.Combine(makePath, "Test-develop.yml");
Log.Logger.Information("Writing Compose file to {Path}", filePath);
File.WriteAllText(filePath, composeFile, Encoding.UTF8);
// Create latest Compose file
composeFile = CreateComposefile("latest");
filePath = Path.Combine(makePath, "Test-latest.yml");
Log.Logger.Information("Writing Compose file to {Path}", filePath);
File.WriteAllText(filePath, composeFile, Encoding.UTF8);
}
private static string CreateComposefile(string? label)
{
// TODO: Switch to volume sub-paths on Moby v26+
// https://github.com/moby/moby/pull/45687
// Compose file header
var stringBuilder = new StringBuilder();
stringBuilder.Append("""
# Compose file created by CreateMatrix, do not modify by hand
""");
stringBuilder.AppendLine();
// Create volumes
stringBuilder.Append(CreateVolumes());
stringBuilder.AppendLine();
// Create services
stringBuilder.Append(CreateServices(label));
stringBuilder.AppendLine();
return stringBuilder.ToString().Replace("\r\n", "\n").Trim();
}
private static string CreateVolumes()
{
var stringBuilder = new StringBuilder();
stringBuilder.Append("""
volumes:
""");
stringBuilder.AppendLine();
// Create a volume for every product
foreach (var productType in ProductInfo.GetProductTypes())
{
// Standard
stringBuilder.Append(CreateVolume(productType, false));
stringBuilder.AppendLine();
// LSIO
stringBuilder.Append(CreateVolume(productType, true));
stringBuilder.AppendLine();
}
return stringBuilder.ToString();
}
private static string CreateVolume(ProductInfo.ProductType productType, bool lsio)
{
if (lsio)
return $$"""
# Dockerfile : {{ProductInfo.GetDocker(productType, lsio)}}
test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_config:
test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_media:
""";
else
return $$"""
# Dockerfile : {{ProductInfo.GetDocker(productType, lsio)}}
test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_etc:
test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_ini:
test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_var:
test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_media:
"""
;
}
private static string CreateServices(string? label)
{
var stringBuilder = new StringBuilder();
stringBuilder.Append("""
services:
""");
stringBuilder.AppendLine();
// Create a service for every product
int standardPort = 7101, lsioPort = 7201;
foreach (var productType in ProductInfo.GetProductTypes())
{
// Standard
stringBuilder.Append(CreateService(productType, false, standardPort ++, label));
stringBuilder.AppendLine();
// LSIO
stringBuilder.Append(CreateService(productType, true, lsioPort ++, label));
stringBuilder.AppendLine();
}
return stringBuilder.ToString();
}
private static string CreateService(ProductInfo.ProductType productType, bool lsio, int port, string? label)
{
var image = string.IsNullOrEmpty(label) ? $"test_{ProductInfo.GetDocker(productType, lsio).ToLower()}" : $"docker.io/ptr727/{ProductInfo.GetDocker(productType, lsio).ToLower()}:{label}";
var service = $$"""
# Dockerfile : {{ProductInfo.GetDocker(productType, lsio)}}
# Port : {{port}}
{{ProductInfo.GetDocker(productType, lsio).ToLower()}}:
image: {{image}}
container_name: {{ProductInfo.GetDocker(productType, lsio).ToLower()}}-container
restart: unless-stopped
environment:
- TZ=Americas/Los_Angeles
network_mode: bridge
ports:
- {{port}}:7001
""";
if (lsio)
service += $$"""
volumes:
- test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_config:/config
- test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_media:/media
""";
else
service += $$"""
volumes:
- test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_etc:/opt/{{ProductInfo.GetCompany(productType).ToLower()}}/mediaserver/etc
- test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_ini:/home/{{ProductInfo.GetCompany(productType).ToLower()}}/.config/nx_ini
- test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_var:/opt/{{ProductInfo.GetCompany(productType).ToLower()}}/mediaserver/var
- test_{{ProductInfo.GetDocker(productType, lsio).ToLower()}}_media:/media
""";
return service;
}
}