commit 3899daee561f329d6bbd84f46ecae70486bcf6aa
parent b17f623d03546cf6fbcf79d06d79042a8d8bf5d1
Author: Daniel Barlow <dan@telent.net>
Date: Mon, 15 Jul 2024 22:37:37 +0100
create a module for round-robin
Diffstat:
4 files changed, 69 insertions(+), 28 deletions(-)
diff --git a/examples/l2tp.nix b/examples/l2tp.nix
@@ -47,6 +47,7 @@ in rec {
../modules/watchdog
../modules/mount
../modules/ppp
+ ../modules/round-robin
];
hostname = "thing";
@@ -59,17 +60,7 @@ in rec {
services.wan =
let
- controller = longrun rec {
- name = "wan-switcher";
- run = ''
- in_outputs ${name}
- exec ${pkgs.s6-rc-round-robin}/bin/s6-rc-round-robin \
- -p ${proxy.name} \
- ${lib.concatStringsSep " "
- (builtins.map (f: f.name) [pppoe l2tp])}
- '';
- };
- pppoe = (svc.pppoe.build {
+ pppoe = svc.pppoe.build {
interface = config.hardware.networkInterfaces.wan;
ppp-options = [
@@ -77,7 +68,7 @@ in rec {
"name" rsecrets.l2tp.name
"password" rsecrets.l2tp.password
];
- }).overrideAttrs(o: { inherit controller; });
+ };
l2tp =
let
@@ -91,7 +82,7 @@ in rec {
target = lns.address;
dependencies = [services.dhcpc check-address];
};
- in (svc.l2tp.build {
+ in svc.l2tp.build {
lns = lns.address;
ppp-options = [
"debug" "+ipv6" "noauth"
@@ -99,20 +90,11 @@ in rec {
"password" rsecrets.l2tp.password
];
dependencies = [config.services.lns-address route check-address];
- }).overrideAttrs(o: { inherit controller; });
- proxy = oneshot rec {
- name = "wan-proxy";
- inherit controller;
- buildInputs = [ pppoe l2tp];
- up = ''
- echo start proxy ${name}
- set -x
- (in_outputs ${name}
- cp -rv $(output_path ${controller} active)/* .
- )
- '';
- };
- in proxy;
+ };
+ in svc.round-robin.build {
+ name = "wan";
+ services = [ l2tp pppoe ];
+ };
services.sshd = svc.ssh.build { };
diff --git a/modules/round-robin/default.nix b/modules/round-robin/default.nix
@@ -0,0 +1,27 @@
+## Round Robin
+##
+## Given a list of services, run each in turn until it exits, then
+## runs the next.
+
+
+{ lib, pkgs, config, ...}:
+let
+ inherit (lib) mkOption types;
+ inherit (pkgs) liminix;
+ inherit (pkgs.liminix.services) longrun;
+in {
+ options = {
+ system.service.round-robin = mkOption {
+ description = "run services one at a time and failover to next";
+ type = liminix.lib.types.serviceDefn;
+ };
+ };
+ config.system.service.round-robin = config.system.callService ./service.nix {
+ services = mkOption {
+ type = types.listOf liminix.lib.types.service;
+ };
+ name = mkOption {
+ type = types.str;
+ };
+ };
+}
diff --git a/modules/round-robin/service.nix b/modules/round-robin/service.nix
@@ -0,0 +1,32 @@
+{
+ liminix, lib, s6-rc-round-robin
+}:
+{ services, name} :
+let
+ inherit (liminix.services) oneshot longrun;
+ controlled-services = builtins.map
+ (s: s.overrideAttrs(o: { inherit controller; }))
+ services;
+ controller = let name' = "control-${name}"; in longrun {
+ name = name';
+ run = ''
+ in_outputs ${name'}
+ exec ${s6-rc-round-robin}/bin/s6-rc-round-robin \
+ -p ${proxy.name} \
+ ${lib.concatStringsSep " "
+ (builtins.map (f: f.name) controlled-services)}
+ '';
+ };
+ proxy = oneshot rec {
+ inherit name;
+ inherit controller;
+ buildInputs = controlled-services;
+ up = ''
+ echo start proxy ${name}
+ set -x
+ (in_outputs ${name}
+ cp -rv $(output_path ${controller} active)/* .
+ )
+ '';
+ };
+in proxy
diff --git a/pkgs/s6-rc-round-robin/default.nix b/pkgs/s6-rc-round-robin/default.nix
@@ -13,7 +13,7 @@ stdenv.mkDerivation {
propagatedBuildInputs = [ s6-rc-up-tree ];
installPhase = ''
mkdir -p $out/bin
- cp -p ${writeFennel "uevent-watch" {
+ cp -p ${writeFennel "s6-rc-round-robin" {
packages = [fennel anoia linotify lualinux s6-rc-up-tree] ;
mainFunction = "run";
} ./robin.fnl} $out/bin/s6-rc-round-robin