Remaniement, système de class pour gérer les constructeurs differents
This commit is contained in:
parent
adf81b8b3a
commit
610a43c919
1 changed files with 58 additions and 42 deletions
100
main.py
100
main.py
|
@ -13,69 +13,85 @@ api_hostname = config.get('Re2o', 'hostname')
|
||||||
api_password = config.get('Re2o', 'password')
|
api_password = config.get('Re2o', 'password')
|
||||||
api_username = config.get('Re2o', 'username')
|
api_username = config.get('Re2o', 'username')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
api_client = Re2oAPIClient(api_hostname, api_username, api_password)
|
api_client = Re2oAPIClient(api_hostname, api_username, api_password)
|
||||||
|
|
||||||
client_hostname = socket.gethostname().split('.', 1)[0]
|
client_hostname = socket.gethostname().split('.', 1)[0]
|
||||||
|
|
||||||
print("get switchs conf")
|
print("get switchs conf")
|
||||||
all_switchs = api_client.list("switchs/ports-config/")
|
all_switchs = api_client.list("switchs/ports-config/")
|
||||||
all_vlans = api_client.list("machines/vlan/")
|
|
||||||
all_roles = api_client.list("machines/role/")
|
|
||||||
|
|
||||||
# Création de l'environnement Jinja
|
# Création de l'environnement Jinja
|
||||||
ENV = Environment(loader=FileSystemLoader('.'))
|
ENV = Environment(loader=FileSystemLoader('.'))
|
||||||
|
|
||||||
# Import du fichier template dans une variable "template"
|
|
||||||
template = ENV.get_template("templates/hp.tpl")
|
|
||||||
|
|
||||||
# Création du template final avec les valeurs contenues dans le dictionnaire "valeurs" - Ces valeurs sont positionnées dans un objet "temp", qui sera utilisé par le moteur, et que l'on retrouve dans le template.
|
# Création du template final avec les valeurs contenues dans le dictionnaire "valeurs" - Ces valeurs sont positionnées dans un objet "temp", qui sera utilisé par le moteur, et que l'on retrouve dans le template.
|
||||||
|
|
||||||
def preprocess(switch):
|
|
||||||
def add_to_vlans(vlans, vlan, port, tagged=True):
|
class Switch:
|
||||||
if not vlan['vlan_id'] in vlans:
|
def __init__(self):
|
||||||
if not tagged:
|
self.additionnal = None
|
||||||
vlans[vlan['vlan_id']] = {'ports_untagged' : [str(port['port'])], 'ports_tagged' : [], 'name' : vlan['name']}
|
self.all_vlans = api_client.list("machines/vlan/")
|
||||||
|
self.all_roles = api_client.list("machines/role/")
|
||||||
|
# Import du fichier template dans une variable "template"
|
||||||
|
self.hp_tpl = ENV.get_template("templates/hp.tpl")
|
||||||
|
self.conf = None
|
||||||
|
self.name = None
|
||||||
|
self.switch = None
|
||||||
|
|
||||||
|
def get_conf_file_name(self):
|
||||||
|
return self.switch["short_name"] + ".conf"
|
||||||
|
|
||||||
|
def preprocess_hp(self):
|
||||||
|
"""Prérempli certains valeurs renvoyées directement à jinja, pour plus de simplicité"""
|
||||||
|
|
||||||
|
def add_to_vlans(vlans, vlan, port, tagged=True):
|
||||||
|
if not vlan['vlan_id'] in vlans:
|
||||||
|
if not tagged:
|
||||||
|
vlans[vlan['vlan_id']] = {'ports_untagged' : [str(port['port'])], 'ports_tagged' : [], 'name' : vlan['name']}
|
||||||
|
else:
|
||||||
|
vlans[vlan['vlan_id']] = {'ports_tagged' : [str(port['port'])], 'ports_untagged' : [], 'name' : vlan['name']}
|
||||||
else:
|
else:
|
||||||
vlans[vlan['vlan_id']] = {'ports_tagged' : [str(port['port'])], 'ports_untagged' : [], 'name' : vlan['name']}
|
if not tagged:
|
||||||
else:
|
vlans[vlan['vlan_id']]['ports_untagged'].append(str(port['port']))
|
||||||
if not tagged:
|
else:
|
||||||
vlans[vlan['vlan_id']]['ports_untagged'].append(str(port['port']))
|
vlans[vlan['vlan_id']]['ports_tagged'].append(str(port['port']))
|
||||||
else:
|
|
||||||
vlans[vlan['vlan_id']]['ports_tagged'].append(str(port['port']))
|
|
||||||
|
|
||||||
ra_guarded = []
|
vlans = dict()
|
||||||
loop_protected = []
|
|
||||||
vlans = dict()
|
|
||||||
|
|
||||||
for port in switch['ports']:
|
for port in self.switch['ports']:
|
||||||
if port['get_port_profil']['loop_protect']:
|
if port['get_port_profil']['vlan_untagged']:
|
||||||
loop_protected.append(str(port['port']))
|
add_to_vlans(vlans, port['get_port_profil']['vlan_untagged'], port, tagged=False)
|
||||||
if port['get_port_profil']['ra_guard']:
|
if port['get_port_profil']['vlan_tagged']:
|
||||||
ra_guarded.append(str(port['port']))
|
for vlan in port['get_port_profil']['vlan_tagged']:
|
||||||
|
add_to_vlans(vlans, vlan, port)
|
||||||
|
|
||||||
if port['get_port_profil']['vlan_untagged']:
|
arp_protect_vlans = [vlan["vlan_id"] for vlan in self.all_vlans if vlan["arp_protect"]]
|
||||||
add_to_vlans(vlans, port['get_port_profil']['vlan_untagged'], port, tagged=False)
|
dhcp_snooping_vlans = [vlan["vlan_id"] for vlan in self.all_vlans if vlan["dhcp_snooping"]]
|
||||||
if port['get_port_profil']['vlan_tagged']:
|
dhcpv6_snooping_vlans = [vlan["vlan_id"] for vlan in self.all_vlans if vlan["dhcpv6_snooping"]]
|
||||||
for vlan in port['get_port_profil']['vlan_tagged']:
|
ntp_servers = [server["servers"] for server in self.all_roles if server["role_type"] == "ntp-server"][0]
|
||||||
add_to_vlans(vlans, vlan, port)
|
log_servers = [server["servers"] for server in self.all_roles if server["role_type"] == "log-server"][0]
|
||||||
|
dhcp_servers = [server["servers"] for server in self.all_roles if server["role_type"] == "dhcp"][0]
|
||||||
|
ra_guarded = [str(port['port']) for port in self.switch['ports'] if port['get_port_profil']['ra_guard']]
|
||||||
|
loop_protected = [str(port['port']) for port in self.switch['ports'] if port['get_port_profil']['loop_protect']]
|
||||||
|
|
||||||
arp_protect_vlans = [vlan["vlan_id"] for vlan in all_vlans if vlan["arp_protect"]]
|
self.additionals = {'ra_guarded' : ra_guarded, 'loop_protected' : loop_protected, 'vlans' : vlans, 'arp_protect_vlans' : arp_protect_vlans, 'dhcp_snooping_vlans' : dhcp_snooping_vlans, 'dhcpv6_snooping_vlans' : dhcpv6_snooping_vlans, 'ntp_servers': ntp_servers, 'log_servers': log_servers, 'dhcp_servers' : dhcp_servers}
|
||||||
dhcp_snooping_vlans = [vlan["vlan_id"] for vlan in all_vlans if vlan["dhcp_snooping"]]
|
|
||||||
dhcpv6_snooping_vlans = [vlan["vlan_id"] for vlan in all_vlans if vlan["dhcpv6_snooping"]]
|
|
||||||
ntp_servers = [server["servers"] for server in all_roles if server["role_type"] == "ntp-server"][0]
|
|
||||||
log_servers = [server["servers"] for server in all_roles if server["role_type"] == "log-server"][0]
|
|
||||||
dhcp_servers = [server["servers"] for server in all_roles if server["role_type"] == "dhcp"][0]
|
|
||||||
|
|
||||||
return {'ra_guarded' : ra_guarded, 'loop_protected' : loop_protected, 'vlans' : vlans, 'arp_protect_vlans' : arp_protect_vlans, 'dhcp_snooping_vlans' : dhcp_snooping_vlans, 'dhcpv6_snooping_vlans' : dhcpv6_snooping_vlans, 'ntp_servers': ntp_servers, 'log_servers': log_servers, 'dhcp_servers' : dhcp_servers}
|
|
||||||
|
def gen_conf_hp(self):
|
||||||
|
"""Génère la config pour ce switch hp"""
|
||||||
|
self.preprocess_hp()
|
||||||
|
self.conf = self.hp_tpl.render(switch=self.switch, additionals=self.additionals)
|
||||||
|
|
||||||
|
def write_conf(self):
|
||||||
|
with open("generated/" + self.get_conf_file_name(), 'w+') as f:
|
||||||
|
f.write(self.conf)
|
||||||
|
|
||||||
print("gen tpl")
|
print("gen tpl")
|
||||||
conf = template.render(switch=all_switchs[2], additionals=preprocess(all_switchs[2]))
|
|
||||||
|
|
||||||
|
|
||||||
|
sw = Switch()
|
||||||
for switch in all_switchs:
|
for switch in all_switchs:
|
||||||
with open("generated/" + switch["short_name"] + ".conf", 'w+') as f:
|
sw.switch = switch
|
||||||
f.write(template.render(switch=switch, additionals=preprocess(switch)))
|
sw.gen_conf_hp()
|
||||||
|
sw.write_conf()
|
||||||
|
|
||||||
|
|
||||||
print(conf)
|
|
||||||
|
|
Loading…
Reference in a new issue