2018-06-27 19:00:53 +02:00
#!/usr/bin/env python3
2018-05-24 15:27:42 +02:00
from configparser import ConfigParser
2018-05-26 23:04:14 +02:00
import socket
2018-05-24 15:27:42 +02:00
from re2oapi import Re2oAPIClient
2018-07-02 10:40:45 +02:00
from jinja2 import Environment , FileSystemLoader
2018-05-24 15:27:42 +02:00
config = ConfigParser ( )
config . read ( ' config.ini ' )
2018-05-26 23:04:14 +02:00
api_hostname = config . get ( ' Re2o ' , ' hostname ' )
api_password = config . get ( ' Re2o ' , ' password ' )
api_username = config . get ( ' Re2o ' , ' username ' )
api_client = Re2oAPIClient ( api_hostname , api_username , api_password )
client_hostname = socket . gethostname ( ) . split ( ' . ' , 1 ) [ 0 ]
2018-07-08 19:13:58 +02:00
print ( " get switchs conf " )
2018-07-02 10:40:45 +02:00
all_switchs = api_client . list ( " switchs/ports-config/ " )
# Création de l'environnement Jinja
ENV = Environment ( loader = FileSystemLoader ( ' . ' ) )
# 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.
2018-07-08 19:59:13 +02:00
class Switch :
def __init__ ( self ) :
self . additionnal = None
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 ' ] }
2018-07-08 19:13:58 +02:00
else :
2018-07-08 19:59:13 +02:00
if not tagged :
vlans [ vlan [ ' vlan_id ' ] ] [ ' ports_untagged ' ] . append ( str ( port [ ' port ' ] ) )
else :
vlans [ vlan [ ' vlan_id ' ] ] [ ' ports_tagged ' ] . append ( str ( port [ ' port ' ] ) )
2018-07-08 19:13:58 +02:00
2018-07-08 19:59:13 +02:00
vlans = dict ( )
2018-07-08 19:13:58 +02:00
2018-07-08 19:59:13 +02:00
for port in self . switch [ ' ports ' ] :
if port [ ' get_port_profil ' ] [ ' vlan_untagged ' ] :
add_to_vlans ( vlans , port [ ' get_port_profil ' ] [ ' vlan_untagged ' ] , port , tagged = False )
if port [ ' get_port_profil ' ] [ ' vlan_tagged ' ] :
for vlan in port [ ' get_port_profil ' ] [ ' vlan_tagged ' ] :
add_to_vlans ( vlans , vlan , port )
2018-07-08 19:13:58 +02:00
2018-07-08 19:59:13 +02:00
arp_protect_vlans = [ vlan [ " vlan_id " ] for vlan in self . all_vlans if vlan [ " arp_protect " ] ]
dhcp_snooping_vlans = [ vlan [ " vlan_id " ] for vlan in self . all_vlans if vlan [ " dhcp_snooping " ] ]
dhcpv6_snooping_vlans = [ vlan [ " vlan_id " ] for vlan in self . all_vlans if vlan [ " dhcpv6_snooping " ] ]
2018-07-08 20:31:04 +02:00
igmp_vlans = [ vlan [ " vlan_id " ] for vlan in self . all_vlans if vlan [ " igmp " ] ]
mld_vlans = [ vlan [ " vlan_id " ] for vlan in self . all_vlans if vlan [ " mld " ] ]
2018-07-08 19:59:13 +02:00
ntp_servers = [ server [ " servers " ] for server in self . all_roles if server [ " role_type " ] == " ntp-server " ] [ 0 ]
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 ]
2018-07-08 20:31:04 +02:00
radius_servers = [ server [ " servers " ] for server in self . all_roles if server [ " role_type " ] == " radius-server " ] [ 0 ]
2018-07-08 19:59:13 +02:00
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 ' ] ]
2018-07-08 19:13:58 +02:00
2018-07-08 20:31:04 +02:00
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 , ' radius_servers ' : radius_servers , ' igmp_vlans ' : igmp_vlans , ' mld_vlans ' : mld_vlans }
2018-07-08 19:13:58 +02:00
2018-07-08 19:59:13 +02:00
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 )
2018-07-08 19:13:58 +02:00
print ( " gen tpl " )
2018-07-08 19:59:13 +02:00
sw = Switch ( )
for switch in all_switchs :
sw . switch = switch
sw . gen_conf_hp ( )
sw . write_conf ( )
2018-07-08 19:13:58 +02:00