diff --git a/hardware/fmo-os-rugged-laptop-7330.nix b/hardware/fmo-os-rugged-laptop-7330.nix index ae00091..de8861c 100644 --- a/hardware/fmo-os-rugged-laptop-7330.nix +++ b/hardware/fmo-os-rugged-laptop-7330.nix @@ -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; diff --git a/hardware/fmo-os-rugged-tablet-7230.nix b/hardware/fmo-os-rugged-tablet-7230.nix index a2e0062..7fe2a67 100644 --- a/hardware/fmo-os-rugged-tablet-7230.nix +++ b/hardware/fmo-os-rugged-tablet-7230.nix @@ -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; diff --git a/modules/fmo-dci-passthrough/default.nix b/modules/fmo-dci-passthrough/default.nix index a990a8a..f925a32 100644 --- a/modules/fmo-dci-passthrough/default.nix +++ b/modules/fmo-dci-passthrough/default.nix @@ -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"; @@ -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"; - }; }; }; } diff --git a/modules/fmo-module-list.nix b/modules/fmo-module-list.nix index 4606c8d..2d9a5f5 100644 --- a/modules/fmo-module-list.nix +++ b/modules/fmo-module-list.nix @@ -25,4 +25,5 @@ ./dynamic-device-passthrough-services-host ./fmo-certs-distribution-host ./fmo-monitoring + ./fmo-dci-passthrough ]