better cleaner containers
This commit is contained in:
1 parent
3aa7119bed
commit
d9b984db7f
13 files changed
+550
-320
No files matched your search
@@ -0,0 +1,81 @@
|
||||
{ lib, pkgs, containerLib }:
|
||||
|
||||
rec {
|
||||
# Collect all files from all services in a group.
|
||||
# Returns: { "<svcName>.<filename>" = { target = "..."; content = "..."; }; }
|
||||
getAllFiles = grpCfg:
|
||||
lib.foldAttrs lib.recursiveUpdate { }
|
||||
(lib.mapAttrsToList
|
||||
(svcName: svcCfg:
|
||||
lib.mapAttrs'
|
||||
(target: content:
|
||||
let
|
||||
filename = containerLib.pathToFilename target;
|
||||
in
|
||||
lib.nameValuePair
|
||||
"${svcName}/${filename}"
|
||||
{ inherit target content; }
|
||||
)
|
||||
svcCfg.files
|
||||
)
|
||||
grpCfg.services
|
||||
);
|
||||
|
||||
# Build file volume mounts for a specific service.
|
||||
# Returns: [ "/var/lib/container-services/<group>/files/<filename>:<target>:ro" ]
|
||||
mkFileVolumesForService = groupName: svcCfg:
|
||||
lib.mapAttrsToList
|
||||
(target: _:
|
||||
let
|
||||
filename = containerLib.pathToFilename target;
|
||||
hostPath = "/var/lib/container-services/${groupName}/files/${filename}";
|
||||
in
|
||||
"${hostPath}:${target}:ro"
|
||||
)
|
||||
svcCfg.files;
|
||||
|
||||
# Build file volume mounts for all services in a group.
|
||||
# Returns: { "svcName" = [ "...mount..." ]; }
|
||||
mkFileVolumesForGroup = groupName: grpCfg:
|
||||
lib.mapAttrs
|
||||
(svcName: svcCfg: mkFileVolumesForService groupName svcCfg)
|
||||
grpCfg.services;
|
||||
|
||||
# Generate a shell script that writes all files for a group.
|
||||
mkFilesScript = groupName: grpCfg:
|
||||
let
|
||||
filesMap = getAllFiles grpCfg;
|
||||
writeCommands = lib.mapAttrsToList
|
||||
(path: fileData:
|
||||
let
|
||||
filename = containerLib.pathToFilename fileData.target;
|
||||
hostPath = "/var/lib/container-services/${groupName}/files/${filename}";
|
||||
in
|
||||
''
|
||||
mkdir -p "$(dirname "${hostPath}")"
|
||||
cat > "${hostPath}" <<'CONTENT'
|
||||
${fileData.content}
|
||||
CONTENT
|
||||
chmod 0644 "${hostPath}"
|
||||
''
|
||||
)
|
||||
filesMap;
|
||||
in
|
||||
lib.concatStringsSep "\n" writeCommands;
|
||||
|
||||
# Create a systemd service that writes files for a group.
|
||||
# Only created if the group has files.
|
||||
mkFilesService = groupName: grpCfg: pkgs:
|
||||
let
|
||||
filesMap = getAllFiles grpCfg;
|
||||
in
|
||||
lib.optionalAttrs (filesMap != { }) {
|
||||
"container-services-${groupName}-files" = {
|
||||
description = "Write custom files for container service group '${groupName}'";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
ExecStart = "${pkgs.bash}/bin/bash -c ${lib.escapeShellArg (mkFilesScript groupName grpCfg)}";
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user