Skip to content

Commit

Permalink
Looks like it works
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Kuznetsov <[email protected]>
  • Loading branch information
jsvapiav committed Dec 19, 2024
1 parent b68821d commit 4ac7051
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 45 deletions.
3 changes: 3 additions & 0 deletions hardware/fmo-os-rugged-laptop-7330.nix
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@
docker-url = "cr.airoplatform.com";
docker-url-path = "/var/lib/fogdata/cr.url";
}; # services.fmo-dci
fmo-dci-passthrough = {
enable = true;
};
avahi = {
enable = true;
nssmdns = true;
Expand Down
3 changes: 3 additions & 0 deletions hardware/fmo-os-rugged-tablet-7230.nix
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@
docker-url = "cr.airoplatform.com";
docker-url-path = "/var/lib/fogdata/cr.url";
}; # services.fmo-dci
fmo-dci-passthrough = {
enable = true;
};
avahi = {
enable = true;
nssmdns = true;
Expand Down
74 changes: 29 additions & 45 deletions modules/fmo-dci-passthrough/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@
with lib;
let
cfg = config.services.fmo-dci-passthrough;

dockerDevPassScript = pkgs.writeShellScriptBin "docker-dev-pass" ''
CONTAINERNAME=swarm-server-pmc01-swarm-server-1
echo "\n\n\nDevice connection rule has been triggered" >> /tmp/opkey.log
if [ -n "$(${pkgs.docker}/bin/docker ps --quiet --filter name=$CONTAINERNAME)" ] && [ -n "$2" ] && [[ "$5" == 1050/* ]]; then
echo "Container $CONTAINERNAME has been found" >> /tmp/opkey.log
if [ "$1" == "plugged" ]; then
echo "Device plugged $1 $2 $3 $4 $5" >> /tmp/opkey.log
${pkgs.docker}/bin/docker exec --user root $CONTAINERNAME mkdir -p $(dirname $2)
${pkgs.docker}/bin/docker exec --user root $CONTAINERNAME mknod $2 c $3 $4
${pkgs.docker}/bin/docker exec --user root $CONTAINERNAME chmod --recursive 777 $2
${pkgs.docker}/bin/docker exec --user root $CONTAINERNAME service pcscd restart
else
echo "Device unplugged $1 $2 $3 $4 $5" >> /tmp/opkey.log
${pkgs.docker}/bin/docke exec --user root $CONTAINERNAME rm -f $2
fi
else
echo "Container $CONTAINERNAME has not been found" >> /tmp/opkey.log
echo "Unknown error $1 $2 $3 $4 $5" >> /tmp/opkey.log
fi
'';
in {
options.services.fmo-dci-passthrough = {
enable = mkEnableOption "Docker Compose Infrastructure devices passthrough";
Expand All @@ -15,53 +38,14 @@ in {
};

config = mkIf cfg.enable {
environment.systemPackages = with pkgs; [
docker-compose
];

dockerDevPassScript = pkgs.writeShellScriptBin "docker-dev-pass" ''
CONTAINERNAME=swarm-server-pmc01-swarm-server-1 if [ -n "$(docker ps --quiet --filter name=$CONTAINERNAME)" ] && [ -n "$2" ] && [[ "$5" == 1050/* ]]; then
if [ "$1" == "plugged" ]; then
echo "$1 $2 $3 $4 $5" >> /tmp/opkey.log
docker exec --user root $CONTAINERNAME mkdir -p $(dirname $2)
docker exec --user root $CONTAINERNAME mknod $2 c $3 $4
docker exec --user root $CONTAINERNAME chmod --recursive 777 $2
docker exec --user root $CONTAINERNAME service pcscd restart
else
echo "$1 $2 $3 $4 $5" >> /tmp/opkey.log
docker exec --user root $CONTAINERNAME rm -f $2
fi
fi
'';

udev = {

environment.systemPackages = [ dockerDevPassScript ];

services.udev = {
extraRules = ''
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="1050", RUN+="/usr/local/bin/operation-yubikey.sh 'plugged' '%E{DEVNAME}' '%M' '%m' '%E{PRODUCT}'"
ACTION=="remove", SUBSYSTEM=="usb", RUN+="/usr/local/bin/operation-yubikey.sh 'unplugged' '%E{DEVNAME}' '%M' '%m' '%E{PRODUCT}'"
'';
};

systemd.services.fmo-dci-passthrough = {
script = ''
echo "Start docker-compose"
${pkgs.docker-compose}/bin/docker-compose -f $DCPATH up
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="1050", RUN+="${dockerDevPassScript}/bin/docker-dev-pass 'plugged' '%E{DEVNAME}' '%M' '%m' '%E{PRODUCT}'"
ACTION=="remove", SUBSYSTEM=="usb", RUN+="${dockerDevPassScript}/bin/docker-dev-pass 'unplugged' '%E{DEVNAME}' '%M' '%m' '%E{PRODUCT}'"
'';

wantedBy = ["multi-user.target"];
# If you use podman
# after = ["podman.service" "podman.socket"];
# If you use docker
after = [
"docker.service"
"docker.socket"
"network-online.target"
];

# TODO: restart always
serviceConfig = {
Restart = lib.mkForce "always";
RestartSec = "30";
};
};
};
}
1 change: 1 addition & 0 deletions modules/fmo-module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
./dynamic-device-passthrough-services-host
./fmo-certs-distribution-host
./fmo-monitoring
./fmo-dci-passthrough
]

0 comments on commit 4ac7051

Please sign in to comment.