From 504b1b90e809934a01357450ccdf35c4e4afc74d Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 23 Jul 2015 00:37:21 +0200 Subject: [PATCH] * tv identity: define type harder --- 1systems/tv/cd.nix | 7 +-- 1systems/tv/mkdir.nix | 7 +-- 1systems/tv/nomic.nix | 7 +-- 1systems/tv/rmdir.nix | 7 +-- 1systems/tv/wu.nix | 7 +-- 2configs/tv/charybdis.nix | 16 +++-- 2configs/tv/identity.nix | 91 +++++++++++++++++++++++++++++ 3modules/tv/consul.nix | 15 ++--- 3modules/tv/identity.nix | 119 +++++++++++++++++--------------------- 4lib/tv/default.nix | 64 ++++++++++++++++++-- 10 files changed, 230 insertions(+), 110 deletions(-) create mode 100644 2configs/tv/identity.nix diff --git a/1systems/tv/cd.nix b/1systems/tv/cd.nix index 42e4ecf1a..d3cae6f4a 100644 --- a/1systems/tv/cd.nix +++ b/1systems/tv/cd.nix @@ -35,11 +35,8 @@ in singleton config.tv.github-hosts-sync.port; } { - imports = [ ../../3modules/tv/identity.nix ]; - tv.identity = { - enable = true; - self = config.tv.identity.hosts.cd; - }; + imports = [ ../../2configs/tv/identity.nix ]; + tv.identity.self = config.tv.identity.hosts.cd; } { imports = [ ../../3modules/tv/iptables.nix ]; diff --git a/1systems/tv/mkdir.nix b/1systems/tv/mkdir.nix index e4e89872e..2e90c8acd 100644 --- a/1systems/tv/mkdir.nix +++ b/1systems/tv/mkdir.nix @@ -11,11 +11,8 @@ with lib; ../../2configs/tv/exim-smarthost.nix ../../2configs/tv/git-public.nix { - imports = [ ../../3modules/tv/identity.nix ]; - tv.identity = { - enable = true; - self = config.tv.identity.hosts.mkdir; - }; + imports = [ ../../2configs/tv/identity.nix ]; + tv.identity.self = config.tv.identity.hosts.mkdir; } { imports = [ ../../3modules/tv/iptables.nix ]; diff --git a/1systems/tv/nomic.nix b/1systems/tv/nomic.nix index b6235297f..6f984c44d 100644 --- a/1systems/tv/nomic.nix +++ b/1systems/tv/nomic.nix @@ -10,11 +10,8 @@ with lib; ../../2configs/tv/exim-retiolum.nix ../../2configs/tv/git-public.nix { - imports = [ ../../3modules/tv/identity.nix ]; - tv.identity = { - enable = true; - self = config.tv.identity.hosts.nomic; - }; + imports = [ ../../2configs/tv/identity.nix ]; + tv.identity.self = config.tv.identity.hosts.nomic; } { imports = [ ../../3modules/tv/iptables.nix ]; diff --git a/1systems/tv/rmdir.nix b/1systems/tv/rmdir.nix index 14817c9bc..b77a1c39e 100644 --- a/1systems/tv/rmdir.nix +++ b/1systems/tv/rmdir.nix @@ -11,11 +11,8 @@ with lib; ../../2configs/tv/exim-smarthost.nix ../../2configs/tv/git-public.nix { - imports = [ ../../3modules/tv/identity.nix ]; - tv.identity = { - enable = true; - self = config.tv.identity.hosts.rmdir; - }; + imports = [ ../../2configs/tv/identity.nix ]; + tv.identity.self = config.tv.identity.hosts.rmdir; } { imports = [ ../../3modules/tv/iptables.nix ]; diff --git a/1systems/tv/wu.nix b/1systems/tv/wu.nix index 866386698..400005cb7 100644 --- a/1systems/tv/wu.nix +++ b/1systems/tv/wu.nix @@ -18,11 +18,8 @@ in ../../2configs/tv/xserver.nix ../../2configs/tv/synaptics.nix # TODO w110er if xserver is enabled { - imports = [ ../../3modules/tv/identity.nix ]; - tv.identity = { - enable = true; - self = config.tv.identity.hosts.wu; - }; + imports = [ ../../2configs/tv/identity.nix ]; + tv.identity.self = config.tv.identity.hosts.wu; } { environment.systemPackages = with pkgs; [ diff --git a/2configs/tv/charybdis.nix b/2configs/tv/charybdis.nix index 014844e8b..a45c7aebe 100644 --- a/2configs/tv/charybdis.nix +++ b/2configs/tv/charybdis.nix @@ -123,7 +123,7 @@ let #loadmodule "extensions/ip_cloaking.so"; serverinfo { - name = ${toJSON config.tv.identity.self.fqdn}; + name = ${toJSON (head config.tv.identity.self.nets.retiolum.aliases)}; sid = "4z3"; description = "miep!"; network_name = "irc.retiolum"; @@ -133,9 +133,9 @@ let /* On multi-homed hosts you may need the following. These define * the addresses we connect from to other servers. */ /* for IPv4 */ - vhost = ${toJSON config.tv.identity.self.addr}; + vhost = ${concatMapStringsSep ", " toJSON config.tv.identity.self.nets.retiolum.addrs4}; /* for IPv6 */ - vhost6 = ${toJSON config.tv.identity.self.addr6}; + vhost6 = ${concatMapStringsSep ", " toJSON config.tv.identity.self.nets.retiolum.addrs6}; /* ssl_private_key: our ssl private key */ ssl_private_key = "/tmp/ssl.key"; @@ -238,12 +238,10 @@ let /* If you want to listen on a specific IP only, specify host. * host definitions apply only to the following port line. */ - host = ${toJSON config.tv.identity.self.addr}; - port = 6667; - sslport = 6697; - - /* Listen on IPv6 (if you used host= above). */ - host = ${toJSON config.tv.identity.self.addr6}; + # XXX This is stupid because only one host is allowed[?] + #host = ''${concatMapStringsSep ", " toJSON ( + # config.tv.identity.self.nets.retiolum.addrs + #)}; port = 6667; sslport = 6697; }; diff --git a/2configs/tv/identity.nix b/2configs/tv/identity.nix new file mode 100644 index 000000000..312c59d8d --- /dev/null +++ b/2configs/tv/identity.nix @@ -0,0 +1,91 @@ +{ config, ... }: + +{ + imports = [ ../../3modules/tv/identity.nix ]; + tv.identity = { + enable = true; + hosts = { + cd = { + cores = 2; + dc = "tv"; #dc = "cac"; + nets = { + internet = { + addrs4 = ["162.219.7.216"]; + aliases = [ + "cd.internet" + "cd.viljetic.de" + "cgit.cd.viljetic.de" + "cd.krebsco.de" + ]; + }; + retiolum = { + addrs4 = ["10.243.113.222"]; + addrs6 = ["42:4522:25f8:36bb:8ccb:0150:231a:2af3"]; + aliases = [ + "cd.retiolum" + "cgit.cd.retiolum" + ]; + }; + }; + search = "retiolum"; + }; + mkdir = { + cores = 1; + dc = "tv"; #dc = "cac"; + nets = { + retiolum = { + addrs4 = ["10.243.113.223"]; + aliases = [ + "mkdir.retiolum" + "cgit.mkdir.retiolum" + ]; + }; + }; + search = "retiolum"; + }; + nomic = { + cores = 2; + dc = "tv"; #dc = "gg23"; + nets = { + retiolum = { + addrs4 = ["10.243.0.110"]; + aliases = [ + "nomic.retiolum" + "cgit.nomic.retiolum" + ]; + }; + }; + search = "retiolum"; + }; + rmdir = { + cores = 1; + dc = "tv"; #dc = "cac"; + nets = { + retiolum = { + addrs4 = ["10.243.113.224"]; + addrs6 = ["42:4522:25f8:36bb:8ccb:0150:231a:2af5"]; + aliases = [ + "rmdir.retiolum" + "cgit.rmdir.retiolum" + ]; + }; + }; + search = "retiolum"; + }; + wu = { + cores = 4; + # TODO wu is mobile, so dc means "home data center" + dc = "tv"; #dc = "gg23"; + nets = { + retiolum = { + addrs4 = ["10.243.13.37"]; + aliases = [ + "wu.retiolum" + ]; + }; + }; + search = "retiolum"; + }; + }; + }; +} diff --git a/3modules/tv/consul.nix b/3modules/tv/consul.nix index db0cd7a9e..312faa02f 100644 --- a/3modules/tv/consul.nix +++ b/3modules/tv/consul.nix @@ -5,8 +5,7 @@ # TODO consul-bootstrap HOST that actually does is # TODO tools to inspect state of a cluster in outage state -with builtins; -with lib; +with import ../../4lib/tv { inherit lib pkgs; }; let cfg = config.tv.consul; @@ -24,10 +23,10 @@ let enable = mkEnableOption "tv.consul"; dc = mkOption { - type = types.unspecified; + type = types.label; }; hosts = mkOption { - type = with types; listOf unspecified; + type = with types; listOf host; }; encrypt-file = mkOption { type = types.str; # TODO path (but not just into store) @@ -38,7 +37,7 @@ let default = "/var/lib/consul"; }; self = mkOption { - type = types.unspecified; + type = types.host; }; server = mkOption { type = types.bool; @@ -56,9 +55,11 @@ let log_level = "INFO"; #node_name = server = cfg.server; - bind_addr = cfg.self.addr; # TODO cfg.addr enable_syslog = true; - retry_join = map (getAttr "addr") (filter (host: host.fqdn != cfg.self.fqdn) cfg.hosts); + retry_join = + # TODO allow consul in other nets than retiolum [maybe] + concatMap (host: host.nets.retiolum.addrs) + (filter (host: host.name != cfg.self.name) cfg.hosts); leave_on_terminate = true; } // optionalAttrs cfg.server { bootstrap_expect = length cfg.hosts; diff --git a/3modules/tv/identity.nix b/3modules/tv/identity.nix index d3ac91393..0b94dff85 100644 --- a/3modules/tv/identity.nix +++ b/3modules/tv/identity.nix @@ -1,6 +1,6 @@ -{ config, lib, ... }: +{ config, lib, pkgs, ... }: -with lib; +with import ../../4lib/tv { inherit lib pkgs; }; let cfg = config.tv.identity; @@ -13,77 +13,66 @@ let enable = mkEnableOption "tv.identity"; self = mkOption { - type = types.unspecified; + type = types.host; }; + + others = mkOption { + type = types.host; + default = filterAttrs (name: _host: name != cfg.self.name) cfg.hosts; + }; + hosts = mkOption { - type = with types; attrsOf unspecified; - default = { - cd = { - #dc = "cac"; - dc = "tv"; - fqdn = "cd.retiolum"; - subdomains = [ - "cgit" - ]; - addr = "10.243.113.222"; - addr6 = "42:4522:25f8:36bb:8ccb:0150:231a:2af3"; - #internet-addr = "162.219.5.183"; - cores = 2; - }; - mkdir = { - #dc = "cac"; - dc = "tv"; - fqdn = "mkdir.retiolum"; - subdomains = [ - "cgit" - ]; - addr = "10.243.113.223"; - cores = 1; - }; - nomic = { - #dc = "gg"; - dc = "tv"; - fqdn = "nomic.retiolum"; - subdomains = [ - "cgit" - ]; - addr = "10.243.0.110"; - cores = 2; - }; - rmdir = { - #dc = "cac"; - dc = "tv"; - fqdn = "rmdir.retiolum"; - subdomains = [ - "cgit" - ]; - addr = "10.243.113.224"; - #addr = "42:4522:25f8:36bb:8ccb:0150:231a:2af5"; - cores = 1; - }; - wu = { - #dc = "gg"; - dc = "tv"; - fqdn = "wu.retiolum"; - subdomains = [ - "cgit" - ]; - addr = "10.243.13.37"; - cores = 8; - }; - }; + type = with types; attrsOf host; + apply = mapAttrs (name: value: value // { inherit name; }); }; }; imp = { networking.extraHosts = - let - f = name: { addr, fqdn, subdomains, ... }: '' - ${addr} ${toString (map (s: "${s}.${name} ${s}.${fqdn}") subdomains)} - ''; - in - concatStringsSep "\n" (mapAttrsToList f cfg.hosts); + concatStringsSep "\n" (flatten ( + # TODO deepMap ["hosts" "nets"] (hostname: host: netname: net: + mapAttrsToList (hostname: host: + mapAttrsToList (netname: net: + let + aliases = toString (unique (longs ++ shorts)); + longs = (splitByProvider net.aliases).hosts; + shorts = map (removeSuffix ".${cfg.self.search}") longs; + in + map (addr: "${addr} ${aliases}") net.addrs + ) host.nets + ) cfg.hosts + )); }; + # TODO move domain name providers to a dedicated module + # providers : tree label providername + providers = { + internet = "hosts"; + retiolum = "hosts"; + de.viljetic = "regfish"; + de.krebsco = "ovh"; + }; + + # splitByProvider : [alias] -> set providername [alias] + splitByProvider = foldl (acc: alias: insert (providerOf alias) alias acc) {}; + + # providerOf : alias -> providername + providerOf = alias: + tree-get (splitString "." alias) providers; + + # insert : k -> v -> set k [v] -> set k [v] + insert = name: value: set: + set // { ${name} = set.${name} or [] ++ [value]; }; + + # tree k v = set k (either v (tree k v)) + + # tree-get : [k] -> tree k v -> v + tree-get = path: x: + let + y = x.${last path}; + in + if typeOf y != "set" + then y + else tree-get (init path) y; in out diff --git a/4lib/tv/default.nix b/4lib/tv/default.nix index 164a6a1aa..6985baa90 100644 --- a/4lib/tv/default.nix +++ b/4lib/tv/default.nix @@ -1,12 +1,10 @@ { lib, pkgs, ... }: with builtins; +with lib; -let - inherit (lib) mapAttrs stringAsChars; -in +builtins // lib // rec { -rec { git = import ./git.nix { lib = lib // { inherit addNames; @@ -59,4 +57,62 @@ rec { else if c == "\n" then "'\n'" else "\\${c}"); + types = lib.types // (with lib.types; rec { + + host = submodule { + options = { + name = mkOption { + type = label; + }; + dc = mkOption { + type = label; + }; + cores = mkOption { + type = positive; + }; + nets = mkOption { + type = attrsOf net; + apply = x: assert hasAttr "retiolum" x; x; + }; + search = mkOption { + type = hostname; + }; + }; + }; + + net = submodule ({ config, ... }: { + options = { + addrs = mkOption { + type = listOf addr; + apply = _: config.addrs4 ++ config.addrs6; + }; + addrs4 = mkOption { + type = listOf addr4; + default = []; + }; + addrs6 = mkOption { + type = listOf addr6; + default = []; + }; + aliases = mkOption { + # TODO nonEmptyListOf hostname + type = listOf hostname; + }; + }; + }); + + positive = mkOptionType { + name = "positive integer"; + check = x: isInt x && x > 0; + merge = mergeOneOption; + }; + + # TODO + addr = str; + addr4 = str; + addr6 = str; + hostname = str; + label = str; + }); + }