WIP: working but missing all the container options

This commit is contained in:
pazpi 2024-08-25 15:45:29 +02:00
parent c243852601
commit b659426027
No known key found for this signature in database
GPG key ID: 0942571C4B9966BE

View file

@ -18,19 +18,22 @@ let
description = "List of port mappings (e.g. ['8080:80'])"; description = "List of port mappings (e.g. ['8080:80'])";
}; };
containers = mkOption { containers = mkOption {
type = types.attrsOf (types.submodule { type = types.attrsOf (
options = { types.submodule
image = mkOption { {
type = types.str; options = {
description = "Docker image for the container"; image = mkOption {
}; type = types.str;
extraOptions = mkOption { description = "Docker image for the container";
type = types.listOf types.str; };
default = [ ]; extraOptions = mkOption {
description = "Additional options for the container"; type = types.listOf types.str;
}; default = [ ];
}; description = "Additional options for the container";
}); };
};
}
);
default = { }; default = { };
description = "Containers to run in the pod"; description = "Containers to run in the pod";
}; };
@ -42,13 +45,11 @@ let
podDefinitionString = builtins.toJSON { inherit (podDef) ports; }; podDefinitionString = builtins.toJSON { inherit (podDef) ports; };
in in
pkgs.writeScript "manage-pod-${name}.sh" '' pkgs.writeScript "manage-pod-${name}.sh" ''
#!/usr/bin/env nix-shell #! /bin/sh
#!nix-shell -i bash -p htop curl
set -e set -e
POD_NAME="${name}" POD_NAME="${name}"
POD_DEFINITION='${podDefinitionString}' POD_DEFINITION="${podDefinitionString}"
create_pod() { create_pod() {
podman pod create --name "$POD_NAME" \ podman pod create --name "$POD_NAME" \
@ -56,7 +57,9 @@ let
} }
if podman pod exists "$POD_NAME"; then if podman pod exists "$POD_NAME"; then
CURRENT_CONFIG=$(podman pod inspect "$POD_NAME" | jq -c '.[0] | {ports: [.PortMappings[].HostPort | tostring + ":" + (.ContainerPort | tostring)]}') CURRENT_CONFIG=$(podman pod inspect "$POD_NAME" | jq -c '{ports: .[0].InfraConfig.PortBindings | to_entries | map("\(.value[0].HostPort):\(.key | split("/")[0])") | sort'})
echo "POD_DEFINITION: $POD_DEFINITION"
echo "CURRENT_CONFIG: $CURRENT_CONFIG"
if [ "$CURRENT_CONFIG" != "$POD_DEFINITION" ]; then if [ "$CURRENT_CONFIG" != "$POD_DEFINITION" ]; then
echo "Pod configuration has changed. Recreating pod..." echo "Pod configuration has changed. Recreating pod..."
podman pod rm -f "$POD_NAME" podman pod rm -f "$POD_NAME"
@ -83,46 +86,7 @@ in
environment.systemPackages = [ pkgs.jq ]; environment.systemPackages = [ pkgs.jq ];
systemd.services = mapAttrs' virtualisation.oci-containers.containers = listToAttrs (flatten (mapAttrsToList
(name: podDef:
nameValuePair "podman-pod-${name}" {
description = "Manage Podman pod: ${name}";
serviceConfig = {
Type = "oneshot";
ExecStart = "${createPodScript name podDef}";
};
path = [ pkgs.podman ];
wantedBy = [ "multi-user.target" ];
}
)
cfg //
mapAttrs'
(podName: podDef:
nameValuePair "podman-pod-${podName}" {
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
}
)
cfg //
mapAttrs'
(podName: podDef:
nameValuePair "podman-${podName}" {
after = [ "podman-pod-${podName}.service" ];
requires = [ "podman-pod-${podName}.service" ];
partOf = [ "podman-pod-${podName}.service" ];
}
)
(flattenAttrs (mapAttrsToList
(podName: podDef:
mapAttrs'
(containerName: containerDef:
nameValuePair "${podName}-${containerName}" containerDef
)
podDef.containers
)
cfg));
virtualisation.oci-containers.containers = flatten (mapAttrsToList
(podName: podDef: (podName: podDef:
mapAttrsToList mapAttrsToList
(containerName: containerDef: (containerName: containerDef:
@ -133,7 +97,7 @@ in
) )
podDef.containers podDef.containers
) )
cfg); cfg));
networking.firewall.allowedTCPPorts = flatten (mapAttrsToList networking.firewall.allowedTCPPorts = flatten (mapAttrsToList
(name: podDef: (name: podDef:
@ -141,30 +105,32 @@ in
) )
cfg); cfg);
# systemd.services = mapAttrs' systemd.services =
# (podName: podDef: let
# nameValuePair "podman-pod-${podName}" { podServices = mapAttrs'
# after = [ "network.target" ]; (name: podDef:
# wantedBy = [ "multi-user.target" ]; nameValuePair "podman-pod-${name}" {
# } description = "Manage Podman pod: ${name}";
# ) serviceConfig = {
# cfg // Type = "oneshot";
# mapAttrs' ExecStart = "${createPodScript name podDef}";
# (podName: podDef: };
# nameValuePair "podman-${podName}" { path = [ pkgs.jq pkgs.podman ];
# after = [ "podman-pod-${podName}.service" ]; after = [ "network.target" ];
# requires = [ "podman-pod-${podName}.service" ]; wantedBy = [ "multi-user.target" ];
# partOf = [ "podman-pod-${podName}.service" ]; }
# } )
# ) cfg;
# (flattenAttrs (mapAttrsToList containerServices = mapAttrs'
# (podName: podDef: (name: container:
# mapAttrs' nameValuePair "podman-${name}" {
# (containerName: containerDef: after = [ "podman-pod-${lib.head (lib.splitString "-" name)}.service" ];
# nameValuePair "${podName}-${containerName}" containerDef requires = [ "podman-pod-${lib.head (lib.splitString "-" name)}.service" ];
# ) partOf = [ "podman-pod-${lib.head (lib.splitString "-" name)}.service" ];
# podDef.containers }
# ) )
# cfg)); config.virtualisation.oci-containers.containers;
in
podServices // containerServices;
}; };
} }