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 ' )
2018-07-02 10:40:45 +02:00
2018-05-25 00:59:34 +02:00
2018-05-26 23:04:14 +02:00
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/ " )
2018-07-08 19:13:58 +02:00
all_vlans = api_client . list ( " machines/vlan/ " )
all_roles = api_client . list ( " machines/role/ " )
2018-07-02 10:40:45 +02:00
# Création de l'environnement Jinja
ENV = Environment ( loader = FileSystemLoader ( ' . ' ) )
# Import du fichier template dans une variable "template"
2018-07-08 19:13:58 +02:00
template = ENV . get_template ( " templates/hp.tpl " )
2018-07-02 10:40:45 +02:00
# 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:13:58 +02:00
def preprocess ( switch ) :
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 :
if not tagged :
vlans [ vlan [ ' vlan_id ' ] ] [ ' ports_untagged ' ] . append ( str ( port [ ' port ' ] ) )
else :
vlans [ vlan [ ' vlan_id ' ] ] [ ' ports_tagged ' ] . append ( str ( port [ ' port ' ] ) )
ra_guarded = [ ]
loop_protected = [ ]
vlans = dict ( )
for port in switch [ ' ports ' ] :
if port [ ' get_port_profil ' ] [ ' loop_protect ' ] :
loop_protected . append ( str ( port [ ' port ' ] ) )
if port [ ' get_port_profil ' ] [ ' ra_guard ' ] :
ra_guarded . append ( str ( port [ ' port ' ] ) )
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 )
arp_protect_vlans = [ vlan [ " vlan_id " ] for vlan in all_vlans if vlan [ " arp_protect " ] ]
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 ]
2018-07-08 19:26:06 +02:00
dhcp_servers = [ server [ " servers " ] for server in all_roles if server [ " role_type " ] == " dhcp " ] [ 0 ]
2018-07-08 19:13:58 +02:00
2018-07-08 19:26:06 +02:00
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 }
2018-07-08 19:13:58 +02:00
print ( " gen tpl " )
conf = template . render ( switch = all_switchs [ 2 ] , additionals = preprocess ( all_switchs [ 2 ] ) )
for switch in all_switchs :
with open ( " generated/ " + switch [ " short_name " ] + " .conf " , ' w+ ' ) as f :
f . write ( template . render ( switch = switch , additionals = preprocess ( switch ) ) )
2018-07-02 10:40:45 +02:00
print ( conf )