Compare commits
3 commits
Author | SHA1 | Date | |
---|---|---|---|
|
3f7450c71a | ||
|
cb97a4f412 | ||
|
eed53de31a |
2 changed files with 44 additions and 17 deletions
|
@ -1,5 +1,8 @@
|
|||
{{ header }}
|
||||
hostname "{{ hostname }}"
|
||||
{%- for module in modules %}
|
||||
module {{ module }}
|
||||
{%- endfor %}
|
||||
console idle-timeout 1800
|
||||
console idle-timeout serial-usb 1800
|
||||
no cdp run
|
||||
|
@ -111,7 +114,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
|
||||
|
@ -121,10 +124,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
|
||||
|
|
|
@ -146,16 +146,24 @@ 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):
|
||||
def gen_conf(master_config, switch_config, header, modules):
|
||||
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"),
|
||||
|
@ -175,11 +183,16 @@ def gen_conf(master_config, switch_config, header):
|
|||
}
|
||||
return conf_from_dict(config_dict)
|
||||
|
||||
def gen_conf_re2o(re2o_config, header):
|
||||
def gen_conf_re2o(re2o_config, header, modules):
|
||||
mgmt_utils = re2o_config.get("switchs_management_utils")
|
||||
ipv4_managers = dict()
|
||||
for m in mgmt_utils.get("subnet"):
|
||||
ipv4_managers[m.get("network")] = { "ip": m.get("network"), "subnet": m.get("netmask")}
|
||||
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")}
|
||||
ipv6_managers = dict()
|
||||
# FUCK YOU ! subnet6 c'est pas une liste de subnets mais un seul subnet
|
||||
m = mgmt_utils.get("subnet6")
|
||||
|
@ -208,12 +221,19 @@ def gen_conf_re2o(re2o_config, header):
|
|||
# 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
|
||||
for i in iface:
|
||||
if i["vlan_id"] == vlan_id:
|
||||
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:
|
||||
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"]
|
||||
vlans[vlan_id]["ip"]["subnet"] = iface["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:
|
||||
|
@ -258,8 +278,11 @@ def gen_conf_re2o(re2o_config, header):
|
|||
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"),
|
||||
|
@ -376,13 +399,14 @@ 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)
|
||||
configuration = gen_conf_re2o(re2o_config, header, modules)
|
||||
else:
|
||||
configuration = gen_conf(master_config, switch_config, header)
|
||||
configuration = gen_conf(master_config, switch_config, header, modules)
|
||||
|
||||
# génération du diff
|
||||
for line in difflib.unified_diff(old_config.split("\n"), configuration.split("\n"), fromfile='origin', tofile='new', lineterm=""):
|
||||
|
|
Loading…
Reference in a new issue