Compare commits

..

No commits in common. "aurore" and "master" have entirely different histories.

2 changed files with 17 additions and 44 deletions

View file

@ -1,8 +1,5 @@
{{ header }}
hostname "{{ hostname }}"
{%- for module in modules %}
module {{ module }}
{%- endfor %}
console idle-timeout 1800
console idle-timeout serial-usb 1800
no cdp run
@ -114,7 +111,7 @@ vlan {{ number }}
{%- endif %}
exit
{%- endfor %}
; allow-unsupported-transceiver
allow-unsupported-transceiver
{%- if loop_protect %}
loop-protect {{ loop_protect.ports }}
loop-protect transmit-interval 3 disable-timer 30
@ -124,10 +121,10 @@ arp-protect
arp-protect validate src-mac dest-mac
arp-protect vlan {{ arp_protect.vlans }}
{%- endif %}
; device-profile name "default-ap-profile"
; cos 0
; exit
; activate software-update disable
; activate provision disable
; password manager
; password operator
device-profile name "default-ap-profile"
cos 0
exit
activate software-update disable
activate provision disable
password manager
password operator

View file

@ -146,24 +146,16 @@ def get_header(old_config):
header = "\n".join(old_config.split("\n")[:2])
return header
def get_modules(old_config):
modules = list()
for line in old_config.split("\n"):
if line.startswith("module"):
modules.append(" ".join(line.split(" ")[1:]))
return modules
def conf_from_dict(config_dict):
with open("configs/config.j2", "r") as template_file:
template = Template(template_file.read())
configuration = template.render(config_dict)
return configuration
def gen_conf(master_config, switch_config, header, modules):
def gen_conf(master_config, switch_config, header):
interfaces, vlans, mac_based_ports, ra_guard_ports, dhcp_snooping_vlans = gen_interfaces(switch_config)
config_dict = {
"header": header,
"modules": modules,
"hostname": switch_config.get("hostname"),
"dhcp_servers": master_config.get("dhcp_servers"),
"dhcpv6_servers": master_config.get("dhcpv6_servers"),
@ -183,16 +175,11 @@ def gen_conf(master_config, switch_config, header, modules):
}
return conf_from_dict(config_dict)
def gen_conf_re2o(re2o_config, header, modules):
def gen_conf_re2o(re2o_config, header):
mgmt_utils = re2o_config.get("switchs_management_utils")
ipv4_managers = dict()
subnets = mgmt_utils.get("subnet")
if isinstance(subnets, list):
for m in mgmt_utils.get("subnet"):
print(m)
ipv4_managers[m.get("network")] = { "ip": m.get("network"), "subnet": m.get("netmask")}
else:
ipv4_managers[subnets.get("network")] = { "ip": subnets.get("network"), "subnet": subnets.get("netmask")}
for m in mgmt_utils.get("subnet"):
ipv4_managers[m.get("network")] = { "ip": m.get("network"), "subnet": m.get("netmask")}
ipv6_managers = dict()
# FUCK YOU ! subnet6 c'est pas une liste de subnets mais un seul subnet
m = mgmt_utils.get("subnet6")
@ -221,19 +208,12 @@ def gen_conf_re2o(re2o_config, header, modules):
# on rajoute les ips sur les vlans où il y en a
for address, iface in re2o_config.get("interfaces_subnet", dict()).items():
# ouais y'a une autre liste là, don't ask
if isinstance(iface, list):
for i in iface:
if i["vlan_id"] == vlan_id:
if vlans[vlan_id].get("ip") is None:
vlans[vlan_id]["ip"] = dict()
vlans[vlan_id]["ip"]["addr"] = address
vlans[vlan_id]["ip"]["subnet"] = i["netmask"]
else:
if iface["vlan_id"] == vlan_id:
for i in iface:
if i["vlan_id"] == vlan_id:
if vlans[vlan_id].get("ip") is None:
vlans[vlan_id]["ip"] = dict()
vlans[vlan_id]["ip"]["addr"] = address
vlans[vlan_id]["ip"]["subnet"] = iface["netmask"]
vlans[vlan_id]["ip"]["subnet"] = i["netmask"]
for address, iface in re2o_config.get("interfaces6_subnet", dict()).items():
if iface["vlan_id"] == vlan_id:
if vlans[vlan_id].get("ip") is None:
@ -278,11 +258,8 @@ def gen_conf_re2o(re2o_config, header, modules):
interfaces.sort(key=lambda x: x["number"])
radius_key = re2o_config.get("get_radius_key_value")
radius_servers = [ {"ip": i, "secret": radius_key } for i in mgmt_utils["radius_servers"]["ipv4"] + mgmt_utils["radius_servers"]["ipv6"]]
if radius_servers == []:
radius_servers = [ {"ip": i, "secret": radius_key } for i in re2o_config["get_radius_servers"]["ipv4"] + mgmt_utils["radius_servers"]["ipv6"]]
config_dict = {
"header": header,
"modules": modules,
"location": re2o_config.get("switchbay").get("name"),
"hostname": re2o_config.get("short_name"),
"dhcp_servers": mgmt_utils.get("dhcp_servers").get("ipv4"),
@ -399,14 +376,13 @@ if __name__ == "__main__":
session = connect_to_switch(switch_address, user="root", key=master_config.get("ssh_private_key")) #TODO: spécifier chemin clef
old_config = sftp_read_file(session, "cfg/running-config").decode("utf-8")
header = get_header(old_config)
modules = get_modules(old_config)
# génération de la conf
logging.info("Generating configuration for {}".format(args.switch_name))
if args.re2o:
configuration = gen_conf_re2o(re2o_config, header, modules)
configuration = gen_conf_re2o(re2o_config, header)
else:
configuration = gen_conf(master_config, switch_config, header, modules)
configuration = gen_conf(master_config, switch_config, header)
# génération du diff
for line in difflib.unified_diff(old_config.split("\n"), configuration.split("\n"), fromfile='origin', tofile='new', lineterm=""):