feat(rpc): Finished refactor

This commit is contained in:
catvayor 2024-04-30 20:08:07 +02:00
parent 5018ab847c
commit 986d814c65
5 changed files with 233 additions and 354 deletions

View file

@ -2,82 +2,59 @@
pkgs ? (import <nixpkgs> { }), pkgs ? (import <nixpkgs> { }),
}: }:
let let
moduleEX2300 = import ./moduleMaker.nix [ lib = pkgs.lib;
"ge-0/0/0" hive_mod = { lib, config, name, ... }: with lib; {
"ge-0/0/1" options.deployment = {
"ge-0/0/2" targetHost = mkOption { type = types.str; };
"ge-0/0/3" rpc = mkOption {
"ge-0/0/4" type = types.package;
"ge-0/0/5" readOnly = true;
"ge-0/0/6" };
"ge-0/0/7" cmd = mkOption {
"ge-0/0/8" type = types.package;
"ge-0/0/9" readOnly = true;
"ge-0/0/10" };
"ge-0/0/11" };
"ge-0/0/12" config.deployment = rec {
"ge-0/0/13" rpc =
"ge-0/0/14" pkgs.writeText "config-${name}_rpc.xml" ''
"ge-0/0/15" <rpc>
"ge-0/0/16" <edit-config>
"ge-0/0/17" <config>
"ge-0/0/18" ${config.netconf.xmls.configuration}
"ge-0/0/19" </config>
"ge-0/0/20" <target>
"ge-0/0/21" <candidate/>
"ge-0/0/22" </target>
"ge-0/0/23" </edit-config>
"ge-0/0/24" </rpc>
"ge-0/0/25" <rpc>
"ge-0/0/26" <commit/>
"ge-0/0/27" </rpc>
"ge-0/0/28" '';
"ge-0/0/29" cmd = pkgs.writeShellApplication {
"ge-0/0/30" name = "deploy-${name}.sh";
"ge-0/0/31" runtimeInputs = with pkgs; [ openssh ];
"ge-0/0/32" text = ''ssh "${config.deployment.targetHost}" -p 830 -s netconf < ${rpc}'';
"ge-0/0/33" };
"ge-0/0/34" };
"ge-0/0/35" };
"ge-0/0/36"
"ge-0/0/37"
"ge-0/0/38"
"ge-0/0/39"
"ge-0/0/40"
"ge-0/0/41"
"ge-0/0/42"
"ge-0/0/43"
"ge-0/0/44"
"ge-0/0/45"
"ge-0/0/46"
"ge-0/0/47"
"ge-0/1/0"
"ge-0/1/1"
"ge-0/1/2"
"ge-0/1/3"
"xe-0/1/0"
"xe-0/1/1"
"xe-0/1/2"
"xe-0/1/3"
"me0"
];
evaluator = evaluator =
name: module_inst: name: module_inst:
let let
cfg = pkgs.lib.evalModules { cfg = pkgs.lib.evalModules {
specialArgs = { specialArgs = {
inherit pkgs name; inherit name;
}; };
modules = [ modules = [
moduleEX2300 ./junos
./ex2300.nix
hive_mod
module_inst module_inst
]; ];
}; };
in in
"ln -s ${cfg.config.deployement.cmd} $out/${name}"; "ln -s ${lib.getExe cfg.config.deployment.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

62
ex2300.nix Normal file
View file

@ -0,0 +1,62 @@
{
netconf.mandatoryInterfaces = [
"ge-0/0/0"
"ge-0/0/1"
"ge-0/0/2"
"ge-0/0/3"
"ge-0/0/4"
"ge-0/0/5"
"ge-0/0/6"
"ge-0/0/7"
"ge-0/0/8"
"ge-0/0/9"
"ge-0/0/10"
"ge-0/0/11"
"ge-0/0/12"
"ge-0/0/13"
"ge-0/0/14"
"ge-0/0/15"
"ge-0/0/16"
"ge-0/0/17"
"ge-0/0/18"
"ge-0/0/19"
"ge-0/0/20"
"ge-0/0/21"
"ge-0/0/22"
"ge-0/0/23"
"ge-0/0/24"
"ge-0/0/25"
"ge-0/0/26"
"ge-0/0/27"
"ge-0/0/28"
"ge-0/0/29"
"ge-0/0/30"
"ge-0/0/31"
"ge-0/0/32"
"ge-0/0/33"
"ge-0/0/34"
"ge-0/0/35"
"ge-0/0/36"
"ge-0/0/37"
"ge-0/0/38"
"ge-0/0/39"
"ge-0/0/40"
"ge-0/0/41"
"ge-0/0/42"
"ge-0/0/43"
"ge-0/0/44"
"ge-0/0/45"
"ge-0/0/46"
"ge-0/0/47"
"ge-0/1/0"
"ge-0/1/1"
"ge-0/1/2"
"ge-0/1/3"
"xe-0/1/0"
"xe-0/1/1"
"xe-0/1/2"
"xe-0/1/3"
];
}

36
junos/default.nix Normal file
View file

@ -0,0 +1,36 @@
{
name,
lib,
config,
...
}:
with lib;
{
imports = [
./protocols.nix
./interfaces.nix
./vlans.nix
];
options = {
netconf.xmls.configuration = mkOption {
type = types.str;
readOnly = true;
};
netconf.mandatoryInterfaces = mkOption { type = types.listOf types.str; };
};
config.interfaces =
let
mkIntf = name: {
inherit name;
value.enable = mkDefault false;
};
in
listToAttrs (map mkIntf config.netconf.mandatoryInterfaces);
config.netconf.xmls.configuration = ''
<configuration>
${config.netconf.xmls.interfaces}
${config.netconf.xmls.protocols}
${config.netconf.xmls.vlans}
</configuration>
'';
}

View file

@ -1,241 +0,0 @@
interfaces:
{
name,
lib,
pkgs,
config,
...
}:
let
cfg = config;
in
with lib;
{
options = {
deployement = {
targetHost = mkOption { type = types.str; };
cmd = mkOption {
type = types.package;
readOnly = true;
};
};
vlans =
let
range_type.options = {
begin = mkOption { type = types.ints.unsigned; };
end = mkOption { type = types.ints.unsigned; };
};
vlan_type.options = {
ids = mkOption {
type = types.either types.ints.unsigned (
types.listOf (types.either types.ints.unsigned (types.submodule range_type))
);
default = [ ];
};
management = mkOption {
# FIXME : support ipv4, either static or dhcp (with the coffee)
type = types.nullOr types.str;
default = null;
description = ''
IP address with wich to permit management on this vlan.
Only one vlan can set an IP (this module limitation, not switch).
'';
};
};
in
mkOption { type = types.attrsOf (types.submodule vlan_type); };
interfaces =
let
template = name: {
enable = mkEnableOption "the interface ${name}";
interface-mode = mkOption {
type = types.nullOr (
types.enum [
"trunk"
"access"
]
);
default = null;
};
vlans = mkOption {
type =
let
vlan_type = types.either (types.strMatching "[^\n\r]+") (types.ints.unsigned);
in
types.listOf vlan_type;
default = [ ];
};
# TODO: use this option
dhcp_trusted = mkOption {
type = types.bool;
default = false;
};
management = mkOption {
# FIXME : support ipv6, either static or dhcp (with the coffee)
type = types.nullOr types.str;
default = null;
};
};
in
builtins.listToAttrs (
map (name: {
inherit name;
value = template name;
}) interfaces
);
};
config.deployement.cmd =
let
intf_xmlGen =
name:
let
disable_flag = if !cfg.interfaces.${name}.enable then "<disable/>" else "";
# FIXME : need to enforce address in reality
mgmt_fam =
if !builtins.isNull cfg.interfaces.${name}.management then
''
<inet>
<address>
<name>${cfg.interfaces.${name}.management}</name>
</address>
</inet>''
else
"";
members = map (vlan: "<members>${builtins.toString vlan}</members>") cfg.interfaces.${name}.vlans;
eth_switch =
if builtins.isNull cfg.interfaces.${name}.interface-mode then
""
else
''
<ethernet-switching>
<interface-mode>${cfg.interfaces.${name}.interface-mode}</interface-mode>
<vlan>${builtins.concatStringsSep "" members}</vlan>
<storm-control><profile-name>default</profile-name></storm-control>
</ethernet-switching>'';
in
''
<interface>
<name>${name}</name>
${disable_flag}
<unit>
<name>0</name>
<family>
${mgmt_fam}
${eth_switch}
</family>
</unit>
</interface>
'';
interface_xmls = map intf_xmlGen interfaces;
rstp_gen =
name:
if cfg.interfaces.${name}.enable && !builtins.isNull cfg.interfaces.${name}.interface-mode then
"<interface><name>${name}</name></interface>"
else
"";
rstps = map rstp_gen interfaces;
vlan_trust_table =
let
vlan_map =
inter: vlan:
if builtins.isString vlan && cfg.interfaces.${inter}.enable then
if cfg.interfaces.${inter}.dhcp_trusted then
{ ${vlan}.trust = inter; }
else
{ ${vlan}.notrust = inter; }
else
{ };
int_map = inter: map (vlan_map inter) cfg.interfaces.${inter}.vlans;
in
builtins.zipAttrsWith (vlan: values: builtins.zipAttrsWith (_: ints: ints) values) (
builtins.concatMap int_map interfaces
);
vlans =
let
id_map =
id:
let
list =
if builtins.isInt id then
builtins.toString id
else
"${builtins.toString id.begin}-${builtins.toString id.end}";
in
''<vlan-id-list>${list}</vlan-id-list>'';
vlan_map =
vlan:
let
ids =
if !builtins.isList cfg.vlans.${vlan}.ids then
[ "<vlan-id>${builtins.toString cfg.vlans.${vlan}.ids}</vlan-id>" ]
else
map id_map cfg.vlans.${vlan}.ids;
mgmt_flag =
if !builtins.isNull cfg.vlans.${vlan}.management then "<l3-interface>irb.0</l3-interface>" else "";
in
''
<vlan>
<name>${vlan}</name>
${mgmt_flag}
${builtins.concatStringsSep "\n" ids}
</vlan>'';
in
map vlan_map (builtins.attrNames cfg.vlans);
irb_intf =
let
addresses = map (vlan: vlan.management) (builtins.attrValues cfg.vlans);
addr = builtins.foldl' (acc: addr: if !builtins.isNull addr then addr else acc) null addresses;
in
if !builtins.isNull addr then
''
<interface>
<name>irb</name>
<unit>
<name>0</name>
<family>
<inet6>
<address><name>${addr}</name></address>
</inet6>
</family>
</unit>
</interface>
''
else
"";
config = ''
<interfaces operation="replace">
${builtins.concatStringsSep "\n" interface_xmls}
${irb_intf}
</interfaces>
<protocols>
<rstp operation="replace">
${builtins.concatStringsSep "\n" rstps}
</rstp>
</protocols>
<vlans operation="replace">
${builtins.concatStringsSep "\n" vlans}
</vlans>
'';
rpc_requests = pkgs.writeText "config-${name}_rpc.xml" ''
<rpc>
<edit-config>
<config>
<configuration>
${config}
</configuration>
</config>
<target>
<candidate/>
</target>
</edit-config>
</rpc>
<rpc>
<commit/>
</rpc>
'';
in
pkgs.writeShellScript "deploy-${name}.sh" ''
${pkgs.openssh}/bin/ssh ${cfg.deployement.targetHost} -p 830 -s netconf < ${rpc_requests}
'';
}

View file

@ -1,22 +1,24 @@
let let
vlansPlan = mgmt: { vlansPlan = {
"uplink-cri".ids = 223; "uplink-cri".id = 223;
"admin-core" = { "admin-core" = {
ids = 3000; id = 3000;
management = mgmt; l3-interface = "irb.0";
}; };
"admin-ap".ids = 3001; "admin-ap".id = 3001;
"users".ids = [ "users".id-list = [
{ {
begin = 3045; begin = 3045;
end = 4094; end = 4094;
} }
]; ];
"ap-staging".ids = 2000; "ap-staging".id = 2000;
}; };
AP = { AP = {
enable = true;
unit."0".family.ethernet-switching = {
enable = true; enable = true;
interface-mode = "trunk"; interface-mode = "trunk";
vlans = [ vlans = [
@ -24,17 +26,35 @@ let
"admin-ap" "admin-ap"
]; ];
}; };
};
AP-staging = { AP-staging = {
enable = true;
unit."0".family.ethernet-switching = {
enable = true; enable = true;
interface-mode = "access"; interface-mode = "access";
vlans = [ "ap-staging" ]; vlans = [ "ap-staging" ];
}; };
};
in in
{ {
netcore01 = { netcore01 = {
deployement.targetHost = "jourdan01.dgn"; deployment.targetHost = "jourdan01.dgn";
vlans = vlansPlan;
protocols.rstp = [
"ge-0/0/12"
"ge-0/0/13"
"ge-0/0/14"
"ge-0/0/15"
"ge-0/0/16"
"ge-0/0/17"
"ge-0/0/42"
"ge-0/0/43"
"ge-0/0/47"
vlans = vlansPlan "fd26:baf9:d250:8000::1001/64"; "xe-0/1/0"
"xe-0/1/1"
"ge-0/1/3"
];
interfaces = { interfaces = {
"ge-0/0/12" = AP; "ge-0/0/12" = AP;
"ge-0/0/13" = AP; "ge-0/0/13" = AP;
@ -44,25 +64,35 @@ in
"ge-0/0/17" = AP; "ge-0/0/17" = AP;
"ge-0/0/42" = { "ge-0/0/42" = {
enable = true;
unit."0".family.ethernet-switching = {
enable = true; enable = true;
interface-mode = "access"; interface-mode = "access";
vlans = [ "admin-core" ]; vlans = [ "admin-core" ];
}; };
};
"ge-0/0/43" = AP-staging; "ge-0/0/43" = AP-staging;
"ge-0/0/47" = { "ge-0/0/47" = {
# ilo # ilo
enable = true;
unit."0".family.ethernet-switching = {
enable = true; enable = true;
interface-mode = "access"; interface-mode = "access";
vlans = [ "admin-core" ]; vlans = [ "admin-core" ];
}; };
};
"xe-0/1/0" = { "xe-0/1/0" = {
enable = true;
unit."0".family.ethernet-switching = {
enable = true; enable = true;
interface-mode = "trunk"; interface-mode = "trunk";
vlans = [ "all" ]; vlans = [ "all" ];
dhcp_trusted = true; };
}; };
"xe-0/1/1" = { "xe-0/1/1" = {
enable = true;
unit."0".family.ethernet-switching = {
enable = true; enable = true;
interface-mode = "trunk"; interface-mode = "trunk";
vlans = [ vlans = [
@ -71,34 +101,49 @@ in
"admin-core" "admin-core"
]; ];
}; };
};
"ge-0/1/3" = { "ge-0/1/3" = {
enable = true;
unit."0".family.ethernet-switching = {
enable = true; enable = true;
interface-mode = "trunk"; interface-mode = "trunk";
vlans = [ "uplink-cri" ]; vlans = [ "uplink-cri" ];
}; };
"me0" = {
enable = true;
management = "192.168.42.6/24";
};
};
};
netaccess01 = {
deployement.targetHost = "root@192.168.42.6";
vlans = vlansPlan "fd26:baf9:d250:8000::2001/64";
interfaces = {
"xe-0/1/0" = {
enable = true;
interface-mode = "trunk";
vlans = [ "all" ];
dhcp_trusted = true;
}; };
"me0" = { "me0" = {
enable = true; enable = true;
management = "192.168.42.6/24"; unit."0".family.inet = {
enable = true;
address = [ "192.168.42.6/24" ];
};
};
"irb" = {
enable = true;
unit."0".family.inet6 = {
enable = true;
address = [ "fd26:baf9:d250:8000::1001/64" ];
}; };
}; };
}; };
};
# netaccess01 = {
# deployement.targetHost = "root@192.168.42.6";
# vlans = vlansPlan "fd26:baf9:d250:8000::2001/64";
# interfaces = {
# "xe-0/1/0" = {
# enable = true;
# interface-mode = "trunk";
# vlans = [ "all" ];
# dhcp_trusted = true;
# };
# "me0" = {
# enable = true;
# management = "192.168.42.6/24";
# };
# };
# };
} }