-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDockerfile.cs
More file actions
376 lines (326 loc) · 15.1 KB
/
Dockerfile.cs
File metadata and controls
376 lines (326 loc) · 15.1 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
namespace CreateMatrix;
internal static class DockerFile
{
private const string UbuntuBaseTag = "noble";
private const string BaseImage = "docker.io/ptr727/nx-base:ubuntu-" + UbuntuBaseTag;
private const string BaseImageLsio = "docker.io/ptr727/nx-base-lsio:ubuntu-" + UbuntuBaseTag;
public static void Create(
List<ProductInfo> productList,
string dockerPath,
VersionInfo.LabelType label
)
{
CreateBaseDockerfiles(dockerPath);
// Create a Docker file for each product type
foreach (ProductInfo.ProductType productType in ProductInfo.GetProductTypes())
{
// Find the matching product
ProductInfo? productInfo = productList.Find(item => item.Product == productType);
ArgumentNullException.ThrowIfNull(productInfo);
// Get the version for the label, not all releases include Beta and RC labels
VersionInfo? versionInfo = productInfo.Versions.Find(item =>
item.Labels.Contains(label)
);
// If the specific label is not found, use the latest version
if (versionInfo == null)
{
Log.Logger.Warning(
"Label {Label} not found for {Product}, using latest",
label,
productType
);
versionInfo = productInfo.Versions.Find(item =>
item.Labels.Contains(VersionInfo.LabelType.Latest)
);
}
ArgumentNullException.ThrowIfNull(versionInfo);
// Create the standard Docker file
string dockerFile = CreateDockerfile(productType, versionInfo, false);
string filePath = Path.Combine(
dockerPath,
$"{ProductInfo.GetDocker(productType, false)}.Dockerfile"
);
Log.Logger.Information("Writing Dockerfile to {Path}", filePath);
File.WriteAllText(filePath, dockerFile);
// Create the LSIO Docker file
dockerFile = CreateDockerfile(productType, versionInfo, true);
filePath = Path.Combine(
dockerPath,
$"{ProductInfo.GetDocker(productType, true)}.Dockerfile"
);
Log.Logger.Information("Writing Dockerfile to {Path}", filePath);
File.WriteAllText(filePath, dockerFile);
}
}
private static string CreateDockerfile(
ProductInfo.ProductType productType,
VersionInfo versionInfo,
bool lsio
)
{
// From
StringBuilder stringBuilder = new();
_ = stringBuilder.AppendLineCrlf(CreateFrom(productType, lsio));
// Args
_ = stringBuilder.AppendLineCrlf(CreateArgs(productType, versionInfo, lsio));
// Install
_ = stringBuilder.AppendLineCrlf(CreateInstall(lsio));
// Entrypoint
_ = stringBuilder.AppendLineCrlf(CreateEntrypoint(productType, lsio));
return stringBuilder.ToString();
}
private static string CreateFrom(ProductInfo.ProductType productType, bool lsio)
{
string from = $$"""
# Dockerfile created by CreateMatrix, do not modify by hand
# Product: {{productType}}
# Description: {{ProductInfo.GetDescription(productType)}}
# Company: {{ProductInfo.GetCompany(productType)}}
# Release: {{ProductInfo.GetRelease(productType)}}
# LSIO: {{lsio}}
# https://support.networkoptix.com/hc/en-us/articles/205313168-Nx-Witness-Operating-System-Support
# Latest Ubuntu supported for v6 is Noble
# Base images are built in this repo, see Docker/NxBase*.Dockerfile
""";
if (lsio)
{
from += $$"""
FROM {{BaseImageLsio}}
""";
}
else
{
from += $$"""
FROM {{BaseImage}}
""";
}
return from;
}
private static void CreateBaseDockerfiles(string dockerPath)
{
string baseDockerfile = $$"""
# Base Dockerfile for Nx Witness images
# Built from ubuntu:{{UbuntuBaseTag}}
FROM ubuntu:{{UbuntuBaseTag}}
# Prevent EULA and confirmation prompts in installers
ARG DEBIAN_FRONTEND=noninteractive
# Common packages used by all product images
# https://github.com/ptr727/NxWitness/issues/282
RUN apt-get update \
&& apt-get upgrade --yes \
&& apt-get install --no-install-recommends --yes \
ca-certificates \
gdb \
libdrm2 \
sudo \
unzip \
wget \
&& apt-get clean \
&& apt-get autoremove --purge \
&& rm -rf /var/lib/apt/lists/*
""";
string baseFilePath = Path.Combine(dockerPath, "NxBase.Dockerfile");
Log.Logger.Information("Writing Dockerfile to {Path}", baseFilePath);
File.WriteAllText(baseFilePath, baseDockerfile);
string baseLsioDockerfile = $$"""
# Base Dockerfile for Nx Witness LSIO images
# Built from lsiobase/ubuntu:{{UbuntuBaseTag}}
FROM lsiobase/ubuntu:{{UbuntuBaseTag}}
# Prevent EULA and confirmation prompts in installers
ARG DEBIAN_FRONTEND=noninteractive
# Common packages used by all product images
# https://github.com/ptr727/NxWitness/issues/282
RUN apt-get update \
&& apt-get upgrade --yes \
&& apt-get install --no-install-recommends --yes \
ca-certificates \
gdb \
libdrm2 \
unzip \
wget \
&& apt-get clean \
&& apt-get autoremove --purge \
&& rm -rf /var/lib/apt/lists/*
""";
string baseLsioFilePath = Path.Combine(dockerPath, "NxBase-LSIO.Dockerfile");
Log.Logger.Information("Writing Dockerfile to {Path}", baseLsioFilePath);
File.WriteAllText(baseLsioFilePath, baseLsioDockerfile);
}
private static string CreateArgs(
ProductInfo.ProductType productType,
VersionInfo versionInfo,
bool lsio
) =>
// Args
$$"""
# Labels
ARG LABEL_NAME="{{ProductInfo.GetDocker(productType, lsio)}}"
ARG LABEL_DESCRIPTION="{{ProductInfo.GetDescription(productType)}}"
ARG LABEL_VERSION="{{versionInfo.Version}}"
# Download URL and version
# Current values are defined by the build pipeline
ARG DOWNLOAD_X64_URL="{{versionInfo.UriX64}}"
ARG DOWNLOAD_ARM64_URL="{{versionInfo.UriArm64}}"
ARG DOWNLOAD_VERSION="{{versionInfo.Version}}"
# Used for ${COMPANY_NAME} setting the server user and install directory
ARG RUNTIME_NAME="{{ProductInfo.GetCompany(productType)}}"
# Global builder variables
# https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope
ARG \
# Platform of the build result. Eg linux/amd64, linux/arm/v7, windows/amd64
TARGETPLATFORM \
# Architecture component of TARGETPLATFORM
TARGETARCH \
# Platform of the node performing the build
BUILDPLATFORM
# Prevent EULA and confirmation prompts in installers
ARG DEBIAN_FRONTEND=noninteractive
# Media server user and directory name
ENV COMPANY_NAME=${RUNTIME_NAME}
# Labels
LABEL name=${LABEL_NAME}-${DOWNLOAD_VERSION} \
description=${LABEL_DESCRIPTION} \
version=${LABEL_VERSION} \
maintainer="Pieter Viljoen <ptr727@users.noreply.github.com>"
""";
private static string CreateInstall(bool lsio)
{
// Install
string install = """
# Base image includes required tools and utilities.
# Download the installer file
WORKDIR /temp
RUN /bin/bash -euo pipefail -c '\
DEB_FILE="./vms_server.deb"; \
TARGET_PLATFORM="${TARGETPLATFORM:-}"; \
DOWNLOAD_URL="${DOWNLOAD_X64_URL:?DOWNLOAD_X64_URL is required}"; \
if [ "${TARGET_PLATFORM}" = "linux/arm64" ]; then \
DOWNLOAD_URL="${DOWNLOAD_ARM64_URL:?DOWNLOAD_ARM64_URL is required}"; \
fi; \
echo "Download URL: ${DOWNLOAD_URL}"; \
DOWNLOAD_FILENAME="$(basename -- "${DOWNLOAD_URL}")"; \
echo "Download Filename: ${DOWNLOAD_FILENAME}"; \
wget --no-verbose --tries=5 --timeout=30 --retry-connrefused "${DOWNLOAD_URL}"; \
case "${DOWNLOAD_FILENAME}" in \
*.zip) \
echo "Downloaded ZIP: ${DOWNLOAD_FILENAME}"; \
DOWNLOAD_DIR="./download_zip"; \
rm -rf "${DOWNLOAD_DIR}"; \
mkdir -p "${DOWNLOAD_DIR}"; \
unzip -q -d "${DOWNLOAD_DIR}" "${DOWNLOAD_FILENAME}"; \
DEB_ZIP_FILE="$(find "${DOWNLOAD_DIR}" -maxdepth 1 -type f -name "*.deb" -print -quit)"; \
if [ -z "${DEB_ZIP_FILE}" ]; then \
echo "No .deb found in ${DOWNLOAD_DIR}" >&2; \
exit 1; \
fi; \
echo "DEB in ZIP: ${DEB_ZIP_FILE}"; \
mv "${DEB_ZIP_FILE}" "${DEB_FILE}"; \
rm -rf "${DOWNLOAD_DIR}" "${DOWNLOAD_FILENAME}"; \
;; \
*.deb) \
echo "Downloaded DEB: ${DOWNLOAD_FILENAME}"; \
mv "${DOWNLOAD_FILENAME}" "${DEB_FILE}"; \
;; \
*) \
echo "Unsupported download type: ${DOWNLOAD_FILENAME}" >&2; \
exit 1; \
;; \
esac; \
echo "DEB File: ${DEB_FILE}"'
""";
if (lsio)
{
install += """
# LSIO maps the host PUID and PGID environment variables to "abc" in the container.
# https://docs.linuxserver.io/misc/non-root/
# LSIO does not officially support changing the "abc" username
# https://discourse.linuxserver.io/t/changing-abc-container-user/3208
# https://github.com/linuxserver/docker-baseimage-ubuntu/blob/noble/root/etc/s6-overlay/s6-rc.d/init-adduser/run
# The mediaserver calls "chown ${COMPANY_NAME}" at runtime
# Change LSIO user "abc" to ${COMPANY_NAME}
RUN usermod -l ${COMPANY_NAME} abc \
# Change group "abc" to ${COMPANY_NAME}
&& groupmod -n ${COMPANY_NAME} abc \
# Replace "abc" with ${COMPANY_NAME}
&& sed -i "s/abc/\${COMPANY_NAME}/g" /etc/s6-overlay/s6-rc.d/init-adduser/run
""";
}
install += """
# Install the mediaserver
RUN apt-get update \
&& apt-get install --no-install-recommends --yes \
./vms_server.deb \
# Cleanup
&& apt-get clean \
&& apt-get autoremove --purge \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /temp
""";
if (lsio)
{
install += """
# Set ownership permissions
RUN chown --verbose ${COMPANY_NAME}:${COMPANY_NAME} /opt/${COMPANY_NAME}/mediaserver/bin \
&& chown --verbose ${COMPANY_NAME}:${COMPANY_NAME} /opt/${COMPANY_NAME}/mediaserver/bin/external.dat
""";
}
else
{
install += """
# Add the mediaserver ${COMPANY_NAME} user to the sudoers group
# Only allow sudo no password access to the root-tool
RUN echo "${COMPANY_NAME} ALL = NOPASSWD: /opt/${COMPANY_NAME}/mediaserver/bin/root-tool" > /etc/sudoers.d/${COMPANY_NAME}
""";
}
return install;
}
private static string CreateEntrypoint(ProductInfo.ProductType productType, bool lsio) =>
// Entrypoint
lsio
? """
# Copy etc init and services files
# https://github.com/just-containers/s6-overlay#container-environment
# https://www.linuxserver.io/blog/how-is-container-formed
COPY s6-overlay /etc/s6-overlay
# Expose port 7001
EXPOSE 7001
# Create mount points
# Config links will be created at runtime, see LSIO/etc/s6-overlay/s6-rc.d/init-nx-relocate/run
# /opt/${COMPANY_NAME}/mediaserver/etc -> /config/etc
# /opt/${COMPANY_NAME}/mediaserver/var -> /config/var
# /root/.config/nx_ini links -> /config/ini
# /config is for configuration
# /media is for recordings
# /backup is for backups
# /analytics is for analytics
VOLUME /config /media /backup /analytics
"""
: $$"""
# Copy the entrypoint.sh launch script
# entrypoint.sh will run the mediaserver and root-tool
COPY entrypoint.sh /opt/entrypoint.sh
RUN chmod +x /opt/entrypoint.sh
# Run the entrypoint as the mediaserver ${COMPANY_NAME} user
# Note that this user exists in the container and does not directly map to a user on the host
USER ${COMPANY_NAME}
# Runs entrypoint.sh on container start
ENTRYPOINT ["/opt/entrypoint.sh"]
# Expose port 7001
EXPOSE 7001
# Create mount points
# Link config directly to internal paths
# /mnt/config/etc:opt/{{ProductInfo.GetCompany(
productType
).ToLowerInvariant()}}/mediaserver/etc
# /mnt/config/nx_ini:/home/{{ProductInfo.GetCompany(
productType
).ToLowerInvariant()}}/.config/nx_ini
# /mnt/config/var:/opt/{{ProductInfo.GetCompany(
productType
).ToLowerInvariant()}}/mediaserver/var
# /media is for recordings
# /backup is for backups
# /analytics is for analytics
VOLUME /media /backup /analytics
""";
}