chore(fmt): applied nixfmt

This commit is contained in:
catvayor 2024-04-28 11:47:10 +02:00
parent f084f0e1ad
commit 813e4d24a5
3 changed files with 262 additions and 190 deletions

View file

@ -1,4 +1,6 @@
{ pkgs ? (import <nixpkgs> {}) }: {
pkgs ? (import <nixpkgs> { }),
}:
let let
moduleEX2300 = import ./moduleMaker.nix [ moduleEX2300 = import ./moduleMaker.nix [
"ge-0/0/0" "ge-0/0/0"
@ -62,17 +64,21 @@ let
"me0" "me0"
]; ];
evaluator = name: module_inst: evaluator =
let cfg = pkgs.lib.evalModules { name: module_inst:
specialArgs = { inherit pkgs name; }; let
cfg = pkgs.lib.evalModules {
specialArgs = {
inherit pkgs name;
};
modules = [ modules = [
moduleEX2300 moduleEX2300
module_inst module_inst
]; ];
}; };
in "ln -s ${cfg.config.deployement.cmd} $out/${name}"; in
"ln -s ${cfg.config.deployement.cmd} $out/${name}";
hive = import ./netconf-hive.nix; hive = import ./netconf-hive.nix;
cmds = builtins.attrValues (builtins.mapAttrs evaluator hive); cmds = builtins.attrValues (builtins.mapAttrs evaluator hive);
in in
pkgs.runCommand "netconf-deploy" {} pkgs.runCommand "netconf-deploy" { } (builtins.concatStringsSep "\n" ([ "mkdir $out" ] ++ cmds))
(builtins.concatStringsSep "\n" ([ "mkdir $out" ] ++ cmds))

View file

@ -1,27 +1,35 @@
interfaces: interfaces:
{ name, lib, pkgs, config, ... }: {
let cfg = config; name,
in with lib; { lib,
pkgs,
config,
...
}:
let
cfg = config;
in
with lib;
{
options = { options = {
deployement = { deployement = {
targetHost = mkOption { targetHost = mkOption { type = types.str; };
type = types.str;
};
cmd = mkOption { cmd = mkOption {
type = types.package; type = types.package;
readOnly = true; readOnly = true;
}; };
}; };
vlans = let vlans =
let
range_type.options = { range_type.options = {
begin = mkOption { type = types.ints.unsigned; }; begin = mkOption { type = types.ints.unsigned; };
end = mkOption { type = types.ints.unsigned; }; end = mkOption { type = types.ints.unsigned; };
}; };
vlan_type.options = { vlan_type.options = {
ids = mkOption { ids = mkOption {
type = types.either type = types.either types.ints.unsigned (
types.ints.unsigned types.listOf (types.either types.ints.unsigned (types.submodule range_type))
(types.listOf (types.either types.ints.unsigned (types.submodule range_type))); );
default = [ ]; default = [ ];
}; };
management = mkOption { management = mkOption {
@ -34,20 +42,27 @@ in with lib; {
''; '';
}; };
}; };
in mkOption { in
type = types.attrsOf (types.submodule vlan_type); mkOption { type = types.attrsOf (types.submodule vlan_type); };
}; interfaces =
interfaces = let let
template = name: { template = name: {
enable = mkEnableOption "the interface ${name}"; enable = mkEnableOption "the interface ${name}";
interface-mode = mkOption { interface-mode = mkOption {
type = types.nullOr (types.enum [ "trunk" "access" ]); type = types.nullOr (
types.enum [
"trunk"
"access"
]
);
default = null; default = null;
}; };
vlans = mkOption { vlans = mkOption {
type = let type =
let
vlan_type = types.either (types.strMatching "[^\n\r]+") (types.ints.unsigned); vlan_type = types.either (types.strMatching "[^\n\r]+") (types.ints.unsigned);
in types.listOf vlan_type; in
types.listOf vlan_type;
default = [ ]; default = [ ];
}; };
# TODO: use this option # TODO: use this option
@ -61,27 +76,45 @@ in with lib; {
default = null; default = null;
}; };
}; };
in builtins.listToAttrs (map (name: { inherit name; value = template name; }) interfaces); in
builtins.listToAttrs (
map (name: {
inherit name;
value = template name;
}) interfaces
);
}; };
config.deployement.cmd = let config.deployement.cmd =
intf_xmlGen = name: let let
intf_xmlGen =
name:
let
disable_flag = if !cfg.interfaces.${name}.enable then "<disable/>" else ""; disable_flag = if !cfg.interfaces.${name}.enable then "<disable/>" else "";
# FIXME : need to enforce address in reality # FIXME : need to enforce address in reality
mgmt_fam = if !builtins.isNull cfg.interfaces.${name}.management then '' mgmt_fam =
if !builtins.isNull cfg.interfaces.${name}.management then
''
<inet> <inet>
<address> <address>
<name>${cfg.interfaces.${name}.management}</name> <name>${cfg.interfaces.${name}.management}</name>
</address> </address>
</inet>'' else ""; </inet>''
else
"";
members = map (vlan: "<members>${builtins.toString vlan}</members>") cfg.interfaces.${name}.vlans; members = map (vlan: "<members>${builtins.toString vlan}</members>") cfg.interfaces.${name}.vlans;
eth_switch = if builtins.isNull cfg.interfaces.${name}.interface-mode then "" else '' eth_switch =
if builtins.isNull cfg.interfaces.${name}.interface-mode then
""
else
''
<ethernet-switching> <ethernet-switching>
<interface-mode>${cfg.interfaces.${name}.interface-mode}</interface-mode> <interface-mode>${cfg.interfaces.${name}.interface-mode}</interface-mode>
<vlan>${builtins.concatStringsSep "" members}</vlan> <vlan>${builtins.concatStringsSep "" members}</vlan>
<storm-control><profile-name>default</profile-name></storm-control> <storm-control><profile-name>default</profile-name></storm-control>
</ethernet-switching>''; </ethernet-switching>'';
in '' in
''
<interface> <interface>
<name>${name}</name> <name>${name}</name>
${disable_flag} ${disable_flag}
@ -95,49 +128,67 @@ in with lib; {
</interface> </interface>
''; '';
interface_xmls = map intf_xmlGen interfaces; interface_xmls = map intf_xmlGen interfaces;
rstp_gen = name: rstp_gen =
name:
if cfg.interfaces.${name}.enable && !builtins.isNull cfg.interfaces.${name}.interface-mode then if cfg.interfaces.${name}.enable && !builtins.isNull cfg.interfaces.${name}.interface-mode then
"<interface><name>${name}</name></interface>" "<interface><name>${name}</name></interface>"
else ""; else
"";
rstps = map rstp_gen interfaces; rstps = map rstp_gen interfaces;
vlan_trust_table = let vlan_trust_table =
vlan_map = inter: vlan: let
vlan_map =
inter: vlan:
if builtins.isString vlan && cfg.interfaces.${inter}.enable then if builtins.isString vlan && cfg.interfaces.${inter}.enable then
if cfg.interfaces.${inter}.dhcp_trusted then if cfg.interfaces.${inter}.dhcp_trusted then
{ ${vlan}.trust = inter; } { ${vlan}.trust = inter; }
else else
{ ${vlan}.notrust = inter; } { ${vlan}.notrust = inter; }
else else
{}; { };
int_map = inter: map (vlan_map inter) cfg.interfaces.${inter}.vlans; int_map = inter: map (vlan_map inter) cfg.interfaces.${inter}.vlans;
in builtins.zipAttrsWith in
(vlan: values: builtins.zipAttrsWith (_: ints: ints ) values) builtins.zipAttrsWith (vlan: values: builtins.zipAttrsWith (_: ints: ints) values) (
(builtins.concatMap int_map interfaces); builtins.concatMap int_map interfaces
vlans = let );
id_map = id: let vlans =
let
id_map =
id:
let
list = list =
if builtins.isInt id then if builtins.isInt id then
builtins.toString id builtins.toString id
else else
"${builtins.toString id.begin}-${builtins.toString id.end}"; "${builtins.toString id.begin}-${builtins.toString id.end}";
in ''<vlan-id-list>${list}</vlan-id-list>''; in
vlan_map = vlan: let ''<vlan-id-list>${list}</vlan-id-list>'';
ids = if !builtins.isList cfg.vlans.${vlan}.ids then vlan_map =
vlan:
let
ids =
if !builtins.isList cfg.vlans.${vlan}.ids then
[ "<vlan-id>${builtins.toString cfg.vlans.${vlan}.ids}</vlan-id>" ] [ "<vlan-id>${builtins.toString cfg.vlans.${vlan}.ids}</vlan-id>" ]
else map id_map cfg.vlans.${vlan}.ids; else
mgmt_flag = if !builtins.isNull cfg.vlans.${vlan}.management map id_map cfg.vlans.${vlan}.ids;
then "<l3-interface>irb.0</l3-interface>" else ""; mgmt_flag =
in '' if !builtins.isNull cfg.vlans.${vlan}.management then "<l3-interface>irb.0</l3-interface>" else "";
in
''
<vlan> <vlan>
<name>${vlan}</name> <name>${vlan}</name>
${mgmt_flag} ${mgmt_flag}
${builtins.concatStringsSep "\n" ids} ${builtins.concatStringsSep "\n" ids}
</vlan>''; </vlan>'';
in map vlan_map (builtins.attrNames cfg.vlans); in
irb_intf = let map vlan_map (builtins.attrNames cfg.vlans);
irb_intf =
let
addresses = map (vlan: vlan.management) (builtins.attrValues cfg.vlans); addresses = map (vlan: vlan.management) (builtins.attrValues cfg.vlans);
addr = builtins.foldl' (acc: addr: if !builtins.isNull addr then addr else acc) null addresses; addr = builtins.foldl' (acc: addr: if !builtins.isNull addr then addr else acc) null addresses;
in if !builtins.isNull addr then '' in
if !builtins.isNull addr then
''
<interface> <interface>
<name>irb</name> <name>irb</name>
<unit> <unit>
@ -150,7 +201,8 @@ in with lib; {
</unit> </unit>
</interface> </interface>
'' ''
else ""; else
"";
config = '' config = ''
<interfaces operation="replace"> <interfaces operation="replace">
${builtins.concatStringsSep "\n" interface_xmls} ${builtins.concatStringsSep "\n" interface_xmls}
@ -182,8 +234,8 @@ in with lib; {
<commit/> <commit/>
</rpc> </rpc>
''; '';
in pkgs.writeShellScript "deploy-${name}.sh" '' in
pkgs.writeShellScript "deploy-${name}.sh" ''
${pkgs.openssh}/bin/ssh ${cfg.deployement.targetHost} -p 830 -s netconf < ${rpc_requests} ${pkgs.openssh}/bin/ssh ${cfg.deployement.targetHost} -p 830 -s netconf < ${rpc_requests}
'' '';
;
} }

View file

@ -7,21 +7,30 @@ let
management = mgmt; management = mgmt;
}; };
"admin-ap".ids = 3001; "admin-ap".ids = 3001;
"users".ids = [ { begin = 3045; end = 4094; } ]; "users".ids = [
{
begin = 3045;
end = 4094;
}
];
"ap-staging".ids = 2000; "ap-staging".ids = 2000;
}; };
AP = { AP = {
enable = true; enable = true;
interface-mode = "trunk"; interface-mode = "trunk";
vlans = [ "users" "admin-ap" ]; vlans = [
"users"
"admin-ap"
];
}; };
AP-staging = { AP-staging = {
enable = true; enable = true;
interface-mode = "access"; interface-mode = "access";
vlans = [ "ap-staging" ]; vlans = [ "ap-staging" ];
}; };
in { in
{
netcore01 = { netcore01 = {
deployement.targetHost = "jourdan01.dgn"; deployement.targetHost = "jourdan01.dgn";
@ -40,7 +49,8 @@ in {
vlans = [ "admin-core" ]; vlans = [ "admin-core" ];
}; };
"ge-0/0/43" = AP-staging; "ge-0/0/43" = AP-staging;
"ge-0/0/47" = { # ilo "ge-0/0/47" = {
# ilo
enable = true; enable = true;
interface-mode = "access"; interface-mode = "access";
vlans = [ "admin-core" ]; vlans = [ "admin-core" ];
@ -55,7 +65,11 @@ in {
"xe-0/1/1" = { "xe-0/1/1" = {
enable = true; enable = true;
interface-mode = "trunk"; interface-mode = "trunk";
vlans = [ "users" "admin-ap" "admin-core" ]; vlans = [
"users"
"admin-ap"
"admin-core"
];
}; };
"ge-0/1/3" = { "ge-0/1/3" = {
enable = true; enable = true;