liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

client.nix (2174B)


      1 {
      2   liminix,
      3   writeAshScript,
      4   serviceFns,
      5   writeFennel,
      6   anoia,
      7   linotify,
      8   lualinux,
      9   s6-rc-up-tree,
     10   lib,
     11 }:
     12 { interface }:
     13 let
     14   inherit (liminix.services) longrun;
     15   name = "${interface.name}.dhcpc";
     16   script = writeAshScript "dhcp-notify" { } ''
     17     . ${serviceFns}
     18     exec 2>&1
     19     action=$1
     20     unset_address() {
     21       ip -4 address flush $interface
     22       ip link set up dev $interface
     23     }
     24     set_address() {
     25         ip address replace $ip/$mask dev $interface
     26         (in_outputs ${name}
     27          touch .lock
     28          test -f state && rm state
     29          for i in lease mask ip router siaddr dns serverid subnet opt53 interface ; do
     30             (printenv $i || true) > $i
     31          done
     32          rm .lock
     33          touch state)
     34     }
     35     case $action in
     36       deconfig)
     37         unset_address
     38         ;;
     39       bound)
     40         # this doesn't actually replace, it adds a new address.
     41         set_address
     42         ;;
     43       renew)
     44         # renewal mut be for an address we already have, so do nothing
     45         ;;
     46       nak)
     47         echo "received NAK on $interface"
     48         unset_address
     49         ;;
     50     esac
     51   '';
     52   service = longrun {
     53     inherit name;
     54     run = "exec /bin/udhcpc -n -A 15 -f -i $(output ${interface} ifname) -x hostname:$(cat /proc/sys/kernel/hostname) -s ${script}";
     55     dependencies = [ interface ];
     56   };
     57   controlled-name = "${name}-lease-acquired";
     58   watcher = longrun {
     59     name = "${name}-watcher";
     60     dependencies = [ service ];
     61     run =
     62       let
     63         script = writeFennel "dhcp-lease-watcher" {
     64           packages = [
     65             anoia
     66             linotify
     67             lualinux
     68           ];
     69           mainFunction = "run";
     70         } ./lease-watcher.fnl;
     71       in
     72       ''
     73         export PATH=${s6-rc-up-tree}/bin/:$PATH
     74         ${script} ${service} ${controlled-name}
     75       '';
     76   };
     77 in
     78 longrun {
     79   name = controlled-name;
     80   run = ''
     81     set -e
     82     echo dhcp lease acquired $(output ${service} ip)
     83     (in_outputs ${controlled-name}
     84      touch .lock
     85      cp $(output_path ${service})/* .
     86      rm .lock
     87      )
     88     echo > /proc/self/fd/10
     89     while sleep 86400 ; do true ; done
     90   '';
     91   controller = watcher;
     92   notification-fd = 10;
     93 }