From d1b4f569134bb070d54a7193d7a6f8d2eda1c575 Mon Sep 17 00:00:00 2001 From: chirac Date: Sun, 8 Jul 2018 19:13:58 +0200 Subject: [PATCH 01/31] Generateur de confi de switch HP --- .gitignore | 1 + generated/__init__.py | 0 main.py | 53 ++++++++++++++++++++-- templates/{hp_test.tpl => hp.tpl} | 74 +++++++++++++++++++++++++++++-- 4 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 generated/__init__.py rename templates/{hp_test.tpl => hp.tpl} (52%) diff --git a/.gitignore b/.gitignore index 641c4cc..9e2266d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ config.ini **/__pycache__/** **.list +generated/* diff --git a/generated/__init__.py b/generated/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py index 46e6de9..697aaf3 100755 --- a/main.py +++ b/main.py @@ -19,17 +19,62 @@ api_client = Re2oAPIClient(api_hostname, api_username, api_password) client_hostname = socket.gethostname().split('.', 1)[0] +print("get switchs conf") 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 ENV = Environment(loader=FileSystemLoader('.')) # Import du fichier template dans une variable "template" -template = ENV.get_template("templates/hp_test.tpl") +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. -conf = template.render(switch=all_switchs[2]) -print(all_switchs[2]) +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] + + 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} + +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))) + + print(conf) diff --git a/templates/hp_test.tpl b/templates/hp.tpl similarity index 52% rename from templates/hp_test.tpl rename to templates/hp.tpl index 1ef99a8..90c9f20 100644 --- a/templates/hp_test.tpl +++ b/templates/hp.tpl @@ -12,12 +12,58 @@ snmpv3 restricted-access snmpv3 user "crans" snmpv3 group ManagerPriv user "crans" sec-model ver3 snmp-server community "public" Operator +;--- Heure/date +time timezone 60 +time daylight-time-rule Western-Europe +{%- for server in additionals.ntp_servers %} +{%- for interface in server.interface %} +{%- if switch.subnet.0.vlan_id == interface.vlan_id %} +sntp server priority {{ loop.index }} {{ interface.ipv4 }} 4 +{%- if interface.ipv6 %} +sntp server priority {{ loop.index + 1 }} {{ interface.ipv6.0.ipv6 }} 4 +{%- endif %} +{%- endif %} +{%- endfor %} +{%- endfor %} timesync sntp sntp unicast ;--- Misc --- console inactivity-timer 30 +;--- Logs --- +{%- for server in additionals.log_servers %} +{%- for interface in server.interface %} +{%- if switch.subnet.0.vlan_id == interface.vlan_id %} +logging {{ interface.ipv4 }} +{%- if interface.ipv6 %} +logging {{ interface.ipv6.0.ipv6 }} +{%- endif %} +{%- endif %} +{%- endfor %} +{%- endfor %} ;--- IP du switch --- no ip default-gateway +max-vlans 256 +{%- for id, vlan in additionals.vlans.items() %} +vlan {{ id }} + name "{{ vlan["name"]|capitalize }}" + {%- if vlan["ports_tagged"] %} + tagged {{ vlan["ports_tagged"]|join(' ') }} + {%- endif %} + {%- if vlan["ports_untagged"] %} + untagged {{ vlan["ports_untagged"]|join(' ') }} + {%- endif %} + {%- if switch.subnet.0.vlan_id == id %} + ip address {{ switch.ipv4 }} {{ switch.subnet.0.netmask }} + {%- else %} + no ip address + {%- endif %} + {%- if switch.subnet.0.vlan_id == id %} + ipv6 address {{ switch.ipv6 }} {{ switch.subnet6.netmask }} + {%- else %} + no ipv6 enable + {%- endif %} +exit +{%- endfor %} ;--- Accès d'administration --- no telnet-server no web-management @@ -25,19 +71,41 @@ aaa authentication ssh login public-key none aaa authentication ssh enable public-key none ip ssh ip ssh filetransfer -ip authorized-managers {{ switch.subnet.0.network }} {{switch.subnet.0.netmask }} access manager +ip authorized-managers {{ switch.subnet.0.network }} {{ switch.subnet.0.netmask }} access manager {%- if switch.subnet6 %} -ipv6 authorized-managers {{ switch.subnet6.network }} {{switch.subnet6.netmask }} access manager +ipv6 authorized-managers {{ switch.subnet6.network }} {{ switch.subnet6.netmask }} access manager {%- endif %} +{%- if additionals.loop_protected %} ;--- Protection contre les boucles --- loop-protect disable-timer 30 loop-protect transmit-interval 3 +loop-protect {{ additionals.loop_protected|join(' ') }} +{%- endif %} radius-server dyn-autz-port 3799 ;--- Filtrage mac --- aaa port-access mac-based addr-format multi-colon ;--- Bricoles --- no cdp run +{%- if additionals.dhcp_snooping_vlans %} +;--- DHCP Snooping --- +dhcp-snooping vlan {{ additionals.dhcp_snooping_vlans|join(' ') }} dhcp-snooping +{%- endif %} +{%- if additionals.arp_protect_vlans %} +;--- ARP Protect --- +arp-protect +arp-protect vlan {{ additionals.arp_protect_vlans|join(' ') }} +arp-protect validate src-mac dest-mac +{%- endif %} +{%- if additionals.dhcpv6_snooping_vlans %} +;--- DHCPv6 Snooping --- +dhcpv6-snooping vlan {{ additionals.dhcpv6_snooping_vlans|join(' ') }} +dhcpv6-snooping +{%- endif %} +{%- if additionals.ra_guarded %} +;--- RA guards --- +ipv6 ra-guard ports {{ additionals.ra_guarded|join(' ')}} +{%- endif %} ;--- Config des prises --- {%- for port in switch.ports %} {%- if port.get_port_profil.radius_type == "802.1X" %} @@ -61,7 +129,7 @@ interface {{ port.port }} {%- else %} disable {%- endif %} - name "{{ port.port }}" + name "{{ port.pretty_name }}" {%- if port.get_port_profil.flow_control %} flow control {%- endif %} From adf81b8b3a5ac5f2cab4d7381265f01dba2c4968 Mon Sep 17 00:00:00 2001 From: chirac Date: Sun, 8 Jul 2018 19:26:06 +0200 Subject: [PATCH 02/31] =?UTF-8?q?Authorisation=20des=20ip=20des=20dhcp=20l?= =?UTF-8?q?=C3=A9gitimes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 3 ++- templates/hp.tpl | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 697aaf3..03bcb72 100755 --- a/main.py +++ b/main.py @@ -66,8 +66,9 @@ def preprocess(switch): 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} + 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} print("gen tpl") conf = template.render(switch=all_switchs[2], additionals=preprocess(all_switchs[2])) diff --git a/templates/hp.tpl b/templates/hp.tpl index 90c9f20..fc825b9 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -88,6 +88,11 @@ aaa port-access mac-based addr-format multi-colon no cdp run {%- if additionals.dhcp_snooping_vlans %} ;--- DHCP Snooping --- +{%- for server in additionals.dhcp_servers %} +{%- for interface in server.interface %} +dhcp-snooping authorized-server {{ interface.ipv4 }} +{%- endfor %} +{%- endfor %} dhcp-snooping vlan {{ additionals.dhcp_snooping_vlans|join(' ') }} dhcp-snooping {%- endif %} From 610a43c919192e79bc62124763cefe8a7446be13 Mon Sep 17 00:00:00 2001 From: chirac Date: Sun, 8 Jul 2018 19:59:13 +0200 Subject: [PATCH 03/31] =?UTF-8?q?Remaniement,=20syst=C3=A8me=20de=20class?= =?UTF-8?q?=20pour=20g=C3=A9rer=20les=20constructeurs=20differents?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 100 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 42 deletions(-) diff --git a/main.py b/main.py index 03bcb72..b9f9d5a 100755 --- a/main.py +++ b/main.py @@ -13,69 +13,85 @@ 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] print("get switchs conf") 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 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. -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'])) + +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']} else: - vlans[vlan['vlan_id']]['ports_tagged'].append(str(port['port'])) + 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() + 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'])) + 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) - 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 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"]] + 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] + 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"]] - 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] + 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} - 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") -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))) +sw = Switch() +for switch in all_switchs: + sw.switch = switch + sw.gen_conf_hp() + sw.write_conf() -print(conf) From a477b2a889ecf730c8a93c7f4663fd70c40c1d9f Mon Sep 17 00:00:00 2001 From: chirac Date: Sun, 8 Jul 2018 20:31:04 +0200 Subject: [PATCH 04/31] Gestion igmp et mld par vlans --- main.py | 5 ++++- templates/hp.tpl | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index b9f9d5a..1d34073 100755 --- a/main.py +++ b/main.py @@ -68,13 +68,16 @@ class Switch: 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"]] + 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"]] 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] + radius_servers = [server["servers"] for server in self.all_roles if server["role_type"] == "radius-server"][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']] - 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} + 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} def gen_conf_hp(self): diff --git a/templates/hp.tpl b/templates/hp.tpl index fc825b9..9e35d54 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -62,6 +62,15 @@ vlan {{ id }} {%- else %} no ipv6 enable {%- endif %} + {%- if id in additionals.igmp_vlans %} + ip igmp + no ip igmp querier + {%- endif %} + {%- if id in additionals.mld_vlans %} + no ipv6 mld querier + ipv6 mld version 1 + ipv6 mld enable + {%- endif %} exit {%- endfor %} ;--- Accès d'administration --- @@ -81,6 +90,16 @@ loop-protect disable-timer 30 loop-protect transmit-interval 3 loop-protect {{ additionals.loop_protected|join(' ') }} {%- endif %} +;--- Serveurs Radius +radius-server dead-time 2 +{%- for server in additionals.radius_servers %} +{%- for interface in server.interface %} +{%- if switch.subnet.0.vlan_id == interface.vlan_id %} +radius-server host {{ interface.ipv4 }} key "plop" +radius-server host {{ interface.ipv4 }} dyn-authorization +{%- endif %} +{%- endfor %} +{%- endfor %} radius-server dyn-autz-port 3799 ;--- Filtrage mac --- aaa port-access mac-based addr-format multi-colon From 465c96492e0f8630e0e046582a751616c0f3d174 Mon Sep 17 00:00:00 2001 From: chirac Date: Sun, 8 Jul 2018 22:47:03 +0200 Subject: [PATCH 05/31] Affichage de la version de la conf du switch --- templates/hp.tpl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/templates/hp.tpl b/templates/hp.tpl index 9e35d54..091afda 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -1,16 +1,14 @@ -; {{ switch.model.reference }}A Configuration Editor; Created on release #{{ firmware }} +; {{ switch.model.reference }}A Configuration Editor; Created on release #{{ switch.model.firmware }} hostname "{{ switch.short_name }}" ; Generated on {{ date_gen }} by re2o ;--- Snmp --- -snmp-server contact "root@crans.org" snmp-server location "{{ switch.switchbay.name }}" ;A faire à la main snmpv3 enable snmpv3 restricted-access -;snmpv3 user "initial" -snmpv3 user "crans" -snmpv3 group ManagerPriv user "crans" sec-model ver3 +snmpv3 user "re2o" +snmpv3 group ManagerPriv user "re2o" sec-model ver3 snmp-server community "public" Operator ;--- Heure/date time timezone 60 From 9566a0d6705fb0550ad47588c56d6e794d643a0b Mon Sep 17 00:00:00 2001 From: chirac Date: Mon, 9 Jul 2018 00:12:38 +0200 Subject: [PATCH 06/31] Formatage de certains options non correct --- templates/hp.tpl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/templates/hp.tpl b/templates/hp.tpl index 091afda..bfc72b7 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -3,7 +3,9 @@ hostname "{{ switch.short_name }}" ; Generated on {{ date_gen }} by re2o ;--- Snmp --- +{%- if switch.switchbay.name %} snmp-server location "{{ switch.switchbay.name }}" +{%- endif %} ;A faire à la main snmpv3 enable snmpv3 restricted-access @@ -45,10 +47,10 @@ max-vlans 256 vlan {{ id }} name "{{ vlan["name"]|capitalize }}" {%- if vlan["ports_tagged"] %} - tagged {{ vlan["ports_tagged"]|join(' ') }} + tagged {{ vlan["ports_tagged"]|join(',') }} {%- endif %} {%- if vlan["ports_untagged"] %} - untagged {{ vlan["ports_untagged"]|join(' ') }} + untagged {{ vlan["ports_untagged"]|join(',') }} {%- endif %} {%- if switch.subnet.0.vlan_id == id %} ip address {{ switch.ipv4 }} {{ switch.subnet.0.netmask }} @@ -86,7 +88,7 @@ ipv6 authorized-managers {{ switch.subnet6.network }} {{ switch.subnet6.netmask ;--- Protection contre les boucles --- loop-protect disable-timer 30 loop-protect transmit-interval 3 -loop-protect {{ additionals.loop_protected|join(' ') }} +loop-protect {{ additionals.loop_protected|join(',') }} {%- endif %} ;--- Serveurs Radius radius-server dead-time 2 @@ -126,7 +128,7 @@ dhcpv6-snooping {%- endif %} {%- if additionals.ra_guarded %} ;--- RA guards --- -ipv6 ra-guard ports {{ additionals.ra_guarded|join(' ')}} +ipv6 ra-guard ports {{ additionals.ra_guarded|join(',')}} {%- endif %} ;--- Config des prises --- {%- for port in switch.ports %} @@ -153,7 +155,7 @@ interface {{ port.port }} {%- endif %} name "{{ port.pretty_name }}" {%- if port.get_port_profil.flow_control %} - flow control + flow-control {%- endif %} {%- if not port.get_port_profil.dhcp_snooping %} dhcp-snooping trust From a4922017e8975a3b08348ece5d98ac136128882f Mon Sep 17 00:00:00 2001 From: chirac Date: Mon, 9 Jul 2018 00:36:55 +0200 Subject: [PATCH 07/31] Notation cidr pour les ip des switchs --- templates/hp.tpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/hp.tpl b/templates/hp.tpl index bfc72b7..87a22e7 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -53,12 +53,12 @@ vlan {{ id }} untagged {{ vlan["ports_untagged"]|join(',') }} {%- endif %} {%- if switch.subnet.0.vlan_id == id %} - ip address {{ switch.ipv4 }} {{ switch.subnet.0.netmask }} + ip address {{ switch.ipv4 }}/{{ switch.subnet.0.netmask_cidr }} {%- else %} no ip address {%- endif %} {%- if switch.subnet.0.vlan_id == id %} - ipv6 address {{ switch.ipv6 }} {{ switch.subnet6.netmask }} + ipv6 address {{ switch.ipv6 }}/{{ switch.subnet6.netmask_cidr }} {%- else %} no ipv6 enable {%- endif %} From b1319bfcc219fe071ad56bfbfa02dc9931456ba2 Mon Sep 17 00:00:00 2001 From: chirac Date: Mon, 9 Jul 2018 00:43:21 +0200 Subject: [PATCH 08/31] Fonction aiguillage suivant constructeur --- main.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/main.py b/main.py index 1d34073..e66d155 100755 --- a/main.py +++ b/main.py @@ -85,7 +85,17 @@ class Switch: self.preprocess_hp() self.conf = self.hp_tpl.render(switch=self.switch, additionals=self.additionals) + + def gen_conf_and_write(self): + """Génère la conf suivant le bon constructeur et l'écrit""" + if self.switch["model"]: + constructor = self.switch["model"]["constructor"].lower() + if "hp" in constructor or "aruba" in constructor: + self.gen_conf_hp() + self.write_conf() + def write_conf(self): + """Ecriture de la conf du switch dans le fichier qui va bien""" with open("generated/" + self.get_conf_file_name(), 'w+') as f: f.write(self.conf) @@ -95,6 +105,7 @@ print("gen tpl") sw = Switch() for switch in all_switchs: sw.switch = switch +# sw.gen_conf_and_write() sw.gen_conf_hp() sw.write_conf() From 64aa4ad521b6201be8209ff00a8dca70eda99617 Mon Sep 17 00:00:00 2001 From: chirac Date: Mon, 9 Jul 2018 23:42:26 +0200 Subject: [PATCH 09/31] Gestion de l'interface rest dans le template --- templates/hp.tpl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/templates/hp.tpl b/templates/hp.tpl index 87a22e7..f67b0cf 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -75,7 +75,15 @@ exit {%- endfor %} ;--- Accès d'administration --- no telnet-server +{%- if switch.web_management_enabled %} +web-management ssl +web-management plaintext +{%- else %} no web-management +{%- endif %} +{%- if switch.rest_enabled %} +rest-interface +{%- endif %} aaa authentication ssh login public-key none aaa authentication ssh enable public-key none ip ssh From aca526940556a796977727e54779b62d99c8bb75 Mon Sep 17 00:00:00 2001 From: chirac Date: Tue, 10 Jul 2018 00:04:11 +0200 Subject: [PATCH 10/31] =?UTF-8?q?Web=20management=20ssl=20d=C3=A9sactiv?= =?UTF-8?q?=C3=A9=20par=20defaut=20(putain=20de=20clef=20hp=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/hp.tpl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/templates/hp.tpl b/templates/hp.tpl index f67b0cf..310f8c2 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -76,8 +76,12 @@ exit ;--- Accès d'administration --- no telnet-server {%- if switch.web_management_enabled %} -web-management ssl +{%- if switch.web_management_enabled != "ssl" %} web-management plaintext +{%- endif %} +{%- if switch.web_management_enabled == "ssl" %} +web-management ssl +{%- endif %} {%- else %} no web-management {%- endif %} From 1490d59ebacaa39b27a3f047d46dbdde095f3ecd Mon Sep 17 00:00:00 2001 From: chirac Date: Wed, 11 Jul 2018 01:20:55 +0200 Subject: [PATCH 11/31] Gestion de la clef radius dans la conf des switchs --- templates/hp.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/hp.tpl b/templates/hp.tpl index 310f8c2..72b1463 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -107,7 +107,7 @@ radius-server dead-time 2 {%- for server in additionals.radius_servers %} {%- for interface in server.interface %} {%- if switch.subnet.0.vlan_id == interface.vlan_id %} -radius-server host {{ interface.ipv4 }} key "plop" +radius-server host {{ interface.ipv4 }} key "{{ switch.get_radius_key_value }}" radius-server host {{ interface.ipv4 }} dyn-authorization {%- endif %} {%- endfor %} From 21b25f1cb229e2058610e361627b5d278857497a Mon Sep 17 00:00:00 2001 From: chirac Date: Wed, 11 Jul 2018 02:35:13 +0200 Subject: [PATCH 12/31] Generation et application automatique de la conf sur les switchs cibles via rest --- main.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index e66d155..df88191 100755 --- a/main.py +++ b/main.py @@ -6,6 +6,10 @@ from re2oapi import Re2oAPIClient from jinja2 import Environment, FileSystemLoader +import requests +import base64 +import json + config = ConfigParser() config.read('config.ini') @@ -37,6 +41,8 @@ class Switch: self.conf = None self.name = None self.switch = None + self.headers = None + self.creds_dict = None def get_conf_file_name(self): return self.switch["short_name"] + ".conf" @@ -85,6 +91,39 @@ class Switch: self.preprocess_hp() self.conf = self.hp_tpl.render(switch=self.switch, additionals=self.additionals) + def check_and_get_login(self): + """Récupère les login/mdp du switch, renvoie false si ils sont indisponibles""" + self.creds_dict = self.switch["get_management_cred_value"] + if self.creds_dict: + return True + else: + return False + + def login_hp(self): + """Login into rest interface of this switch""" + url_login = "http://" + self.switch["ipv4"] + "/rest/v3/login-sessions" + + payload_login = { + "userName" : self.creds_dict["id"], + "password" : self.creds_dict["pass"] + } + get_cookie = requests.post(url_login, data=json.dumps(payload_login)) + cookie = get_cookie.json()['cookie'] + self.headers = {"Cookie" : cookie} + + def apply_conf_hp(self): + """Apply config restore via rest""" + url_restore = "http://" + self.switch["ipv4"] + "/rest/v4/system/config/cfg_restore" + data = { + "server_type": "ST_TFTP", + "file_name": self.get_conf_file_name(), + "tftp_server_address": {"server_address": + {"ip_address": + {"version":"IAV_IP_V4", + "octets":"10.231.100.249"}}}, + } + # Nous lançons la requête de type POST. + post_restore = requests.post(url_restore, data=json.dumps(data), headers=self.headers) def gen_conf_and_write(self): """Génère la conf suivant le bon constructeur et l'écrit""" @@ -94,6 +133,14 @@ class Switch: self.gen_conf_hp() self.write_conf() + def apply_conf(self): + if self.check_and_get_login(): + if self.switch["model"] and self.switch["automatic_provision"] == True: + constructor = self.switch["model"]["constructor"].lower() + if "hp" in constructor or "aruba" in constructor: + self.login_hp() + self.apply_conf_hp() + def write_conf(self): """Ecriture de la conf du switch dans le fichier qui va bien""" with open("generated/" + self.get_conf_file_name(), 'w+') as f: @@ -105,7 +152,8 @@ print("gen tpl") sw = Switch() for switch in all_switchs: sw.switch = switch -# sw.gen_conf_and_write() - sw.gen_conf_hp() - sw.write_conf() + sw.gen_conf_and_write() + sw.apply_conf() +# sw.gen_conf_hp() +# sw.write_conf() From 7d7c7f2f13ba998ce24ebd43c62a54ebc43b1900 Mon Sep 17 00:00:00 2001 From: chirac Date: Wed, 11 Jul 2018 02:49:43 +0200 Subject: [PATCH 13/31] Rattrappage de l'erreur si l'apply conf marche pas --- main.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index df88191..63a5416 100755 --- a/main.py +++ b/main.py @@ -21,7 +21,6 @@ api_client = Re2oAPIClient(api_hostname, api_username, api_password) client_hostname = socket.gethostname().split('.', 1)[0] -print("get switchs conf") all_switchs = api_client.list("switchs/ports-config/") # Création de l'environnement Jinja @@ -146,14 +145,13 @@ class Switch: with open("generated/" + self.get_conf_file_name(), 'w+') as f: f.write(self.conf) -print("gen tpl") sw = Switch() for switch in all_switchs: sw.switch = switch sw.gen_conf_and_write() - sw.apply_conf() -# sw.gen_conf_hp() -# sw.write_conf() - + try: + sw.apply_conf() + except: + print("Erreur dans l'application de la conf pour " + switch["short_name"]) From 3d7c9d98be772fa52e10d867e6817fb67a0857a4 Mon Sep 17 00:00:00 2001 From: chirac Date: Wed, 11 Jul 2018 23:58:16 +0200 Subject: [PATCH 14/31] =?UTF-8?q?Utilise=20les=20r=C3=A9glages=20roles=20e?= =?UTF-8?q?t=20topologie=20option=20pour=20reconfig=20les=20switchs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 12 ++++-------- templates/hp.tpl | 48 ++++++++++++++++++------------------------------ 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/main.py b/main.py index 63a5416..4d71553 100755 --- a/main.py +++ b/main.py @@ -34,7 +34,7 @@ class Switch: def __init__(self): self.additionnal = None self.all_vlans = api_client.list("machines/vlan/") - self.all_roles = api_client.list("machines/role/") + self.settings = api_client.view("preferences/optionaltopologie/") # Import du fichier template dans une variable "template" self.hp_tpl = ENV.get_template("templates/hp.tpl") self.conf = None @@ -75,20 +75,16 @@ class Switch: dhcpv6_snooping_vlans = [vlan["vlan_id"] for vlan in self.all_vlans if vlan["dhcpv6_snooping"]] 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"]] - 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] - radius_servers = [server["servers"] for server in self.all_roles if server["role_type"] == "radius-server"][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']] - 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} + 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, 'igmp_vlans' : igmp_vlans, 'mld_vlans': mld_vlans} 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) + self.conf = self.hp_tpl.render(switch=self.switch, settings=self.settings, additionals=self.additionals) def check_and_get_login(self): """Récupère les login/mdp du switch, renvoie false si ils sont indisponibles""" @@ -119,7 +115,7 @@ class Switch: "tftp_server_address": {"server_address": {"ip_address": {"version":"IAV_IP_V4", - "octets":"10.231.100.249"}}}, + "octets":self.settings["switchs_management_interface_ip"]}}}, } # Nous lançons la requête de type POST. post_restore = requests.post(url_restore, data=json.dumps(data), headers=self.headers) diff --git a/templates/hp.tpl b/templates/hp.tpl index 72b1463..98150cc 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -15,30 +15,22 @@ snmp-server community "public" Operator ;--- Heure/date time timezone 60 time daylight-time-rule Western-Europe -{%- for server in additionals.ntp_servers %} -{%- for interface in server.interface %} -{%- if switch.subnet.0.vlan_id == interface.vlan_id %} -sntp server priority {{ loop.index }} {{ interface.ipv4 }} 4 -{%- if interface.ipv6 %} -sntp server priority {{ loop.index + 1 }} {{ interface.ipv6.0.ipv6 }} 4 -{%- endif %} -{%- endif %} +{%- for ipv4 in settings.switchs_management_utils.ntp_servers.ipv4 %} +sntp server priority {{ loop.index }} {{ ipv4 }} 4 {%- endfor %} +{%- for ipv6 in settings.switchs_management_utils.ntp_servers.ipv6 %} +sntp server priority {{ loop.index + settings.switchs_management_utils.ntp_servers.ipv4|length }} {{ ipv6 }} 4 {%- endfor %} timesync sntp sntp unicast ;--- Misc --- console inactivity-timer 30 ;--- Logs --- -{%- for server in additionals.log_servers %} -{%- for interface in server.interface %} -{%- if switch.subnet.0.vlan_id == interface.vlan_id %} -logging {{ interface.ipv4 }} -{%- if interface.ipv6 %} -logging {{ interface.ipv6.0.ipv6 }} -{%- endif %} -{%- endif %} +{%- for ipv4 in settings.switchs_management_utils.log_servers.ipv4 %} +logging {{ ipv4 }} {%- endfor %} +{%- for ipv6 in settings.switchs_management_utils.log_servers.ipv6 %} +logging {{ ipv6 }} {%- endfor %} ;--- IP du switch --- no ip default-gateway @@ -92,9 +84,11 @@ aaa authentication ssh login public-key none aaa authentication ssh enable public-key none ip ssh ip ssh filetransfer -ip authorized-managers {{ switch.subnet.0.network }} {{ switch.subnet.0.netmask }} access manager -{%- if switch.subnet6 %} -ipv6 authorized-managers {{ switch.subnet6.network }} {{ switch.subnet6.netmask }} access manager +{%- if settings.switchs_management_utils.subnet %} +ip authorized-managers {{ settings.switchs_management_utils.subnet.0.network }} {{ settings.switchs_management_utils.subnet.0.netmask }} access manager +{%- endif %} +{%- if settings.switchs_management_utils.subnet6 %} +ipv6 authorized-managers {{ settings.switchs_management_utils.subnet6.network }} {{ settings.switchs_management_utils.subnet6.netmask }} access manager {%- endif %} {%- if additionals.loop_protected %} ;--- Protection contre les boucles --- @@ -104,13 +98,9 @@ loop-protect {{ additionals.loop_protected|join(',') }} {%- endif %} ;--- Serveurs Radius radius-server dead-time 2 -{%- for server in additionals.radius_servers %} -{%- for interface in server.interface %} -{%- if switch.subnet.0.vlan_id == interface.vlan_id %} -radius-server host {{ interface.ipv4 }} key "{{ switch.get_radius_key_value }}" -radius-server host {{ interface.ipv4 }} dyn-authorization -{%- endif %} -{%- endfor %} +{%- for ipv4 in settings.switchs_management_utils.radius_servers.ipv4 %} +radius-server host {{ ipv4 }} key "{{ switch.get_radius_key_value }}" +radius-server host {{ ipv4 }} dyn-authorization {%- endfor %} radius-server dyn-autz-port 3799 ;--- Filtrage mac --- @@ -119,10 +109,8 @@ aaa port-access mac-based addr-format multi-colon no cdp run {%- if additionals.dhcp_snooping_vlans %} ;--- DHCP Snooping --- -{%- for server in additionals.dhcp_servers %} -{%- for interface in server.interface %} -dhcp-snooping authorized-server {{ interface.ipv4 }} -{%- endfor %} +{%- for ipv4 in settings.switchs_management_utils.dhcp_servers.ipv4 %} +dhcp-snooping authorized-server {{ ipv4 }} {%- endfor %} dhcp-snooping vlan {{ additionals.dhcp_snooping_vlans|join(' ') }} dhcp-snooping From 8a4212c101b38093790249426202f7b5e28e1209 Mon Sep 17 00:00:00 2001 From: chirac Date: Thu, 12 Jul 2018 00:02:07 +0200 Subject: [PATCH 15/31] =?UTF-8?q?Check=20si=20le=20provisioning=20est=20ac?= =?UTF-8?q?tiv=C3=A9...=20avant=20de=20l'appliquer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 4d71553..508cdb3 100755 --- a/main.py +++ b/main.py @@ -130,7 +130,7 @@ class Switch: def apply_conf(self): if self.check_and_get_login(): - if self.switch["model"] and self.switch["automatic_provision"] == True: + if self.switch["model"] and self.switch["automatic_provision"] == True and self.settings["provision_switchs_enabled"]: constructor = self.switch["model"]["constructor"].lower() if "hp" in constructor or "aruba" in constructor: self.login_hp() From ab37b30b3cd0139c339388c07779816d5e6888af Mon Sep 17 00:00:00 2001 From: chirac Date: Thu, 12 Jul 2018 01:20:11 +0200 Subject: [PATCH 16/31] Configure les ip sur tous les vlans --- templates/hp.tpl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/templates/hp.tpl b/templates/hp.tpl index 98150cc..eebed11 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -43,17 +43,17 @@ vlan {{ id }} {%- endif %} {%- if vlan["ports_untagged"] %} untagged {{ vlan["ports_untagged"]|join(',') }} + {%- endif %} + {%- for ipv4, subnet in switch.interfaces_subnet.items() %} + {%- if subnet.0.vlan_id == id %} + ip address {{ ipv4 }}/{{ subnet.0.netmask_cidr }} {%- endif %} - {%- if switch.subnet.0.vlan_id == id %} - ip address {{ switch.ipv4 }}/{{ switch.subnet.0.netmask_cidr }} - {%- else %} - no ip address - {%- endif %} - {%- if switch.subnet.0.vlan_id == id %} - ipv6 address {{ switch.ipv6 }}/{{ switch.subnet6.netmask_cidr }} - {%- else %} - no ipv6 enable + {%- endfor %} + {%- for ipv6, subnet6 in switch.interfaces6_subnet.items() %} + {%- if subnet6.vlan_id == id %} + ipv6 address {{ ipv6 }}/{{ subnet6.netmask_cidr }} {%- endif %} + {%- endfor %} {%- if id in additionals.igmp_vlans %} ip igmp no ip igmp querier From 4bac7c64ddfa2a5b8386580d4d4780529972083a Mon Sep 17 00:00:00 2001 From: chirac Date: Thu, 12 Jul 2018 15:30:05 +0200 Subject: [PATCH 17/31] Ajoute l'option force reboot, reboot le switch pour appliquer les options reboot --- main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/main.py b/main.py index 508cdb3..286caf9 100755 --- a/main.py +++ b/main.py @@ -116,6 +116,7 @@ class Switch: {"ip_address": {"version":"IAV_IP_V4", "octets":self.settings["switchs_management_interface_ip"]}}}, + "is_forced_reboot_enabled": True, } # Nous lançons la requête de type POST. post_restore = requests.post(url_restore, data=json.dumps(data), headers=self.headers) From 827220a50ad5ef3b7035d424730343ded78d4420 Mon Sep 17 00:00:00 2001 From: chirac Date: Thu, 12 Jul 2018 16:53:35 +0200 Subject: [PATCH 18/31] Formatage , et gestion des igmp querier --- main.py | 16 ++++++++++++---- templates/hp.tpl | 30 +++++++++++++++++++----------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 286caf9..60e9f5e 100755 --- a/main.py +++ b/main.py @@ -70,6 +70,13 @@ class Switch: for vlan in port['get_port_profil']['vlan_tagged']: add_to_vlans(vlans, vlan, port) + for ip, subnet in self.switch["interfaces_subnet"].items(): + vlans[subnet[0]["vlan_id"]].setdefault("ipv4", {}) + vlans[subnet[0]["vlan_id"]]["ipv4"][ip] = subnet + for ipv6, subnet in self.switch["interfaces6_subnet"].items(): + vlans[subnet["vlan_id"]].setdefault("ipv6", {}) + vlans[subnet["vlan_id"]]["ipv6"][ipv6] = subnet + 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"]] @@ -112,10 +119,11 @@ class Switch: data = { "server_type": "ST_TFTP", "file_name": self.get_conf_file_name(), - "tftp_server_address": {"server_address": - {"ip_address": - {"version":"IAV_IP_V4", - "octets":self.settings["switchs_management_interface_ip"]}}}, + "tftp_server_address": { + "server_address": { + "ip_address": { + "version":"IAV_IP_V4", + "octets":self.settings["switchs_management_interface_ip"]}}}, "is_forced_reboot_enabled": True, } # Nous lançons la requête de type POST. diff --git a/templates/hp.tpl b/templates/hp.tpl index eebed11..ce50cf5 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -44,25 +44,33 @@ vlan {{ id }} {%- if vlan["ports_untagged"] %} untagged {{ vlan["ports_untagged"]|join(',') }} {%- endif %} - {%- for ipv4, subnet in switch.interfaces_subnet.items() %} - {%- if subnet.0.vlan_id == id %} - ip address {{ ipv4 }}/{{ subnet.0.netmask_cidr }} + {%- if id in additionals.igmp_vlans %} + ip igmp + {%- endif %} + {%- if id in additionals.mld_vlans %} + ipv6 mld version 1 + ipv6 mld enable {%- endif %} + {%- if vlan.ipv4 %} + {%- for ipv4, subnet in vlan.ipv4.items() %} + ip address {{ ipv4 }}/{{ subnet.0.netmask_cidr }} {%- endfor %} - {%- for ipv6, subnet6 in switch.interfaces6_subnet.items() %} - {%- if subnet6.vlan_id == id %} + {%- else %} + no ip address + {%- endif %} + {%- if vlan.ipv6 %} + {%- for ipv6, subnet6 in vlan.ipv6.items() %} ipv6 address {{ ipv6 }}/{{ subnet6.netmask_cidr }} - {%- endif %} {%- endfor %} + {%- else %} + no ipv6 address {%- if id in additionals.igmp_vlans %} - ip igmp no ip igmp querier - {%- endif %} + {%- endif %} {%- if id in additionals.mld_vlans %} no ipv6 mld querier - ipv6 mld version 1 - ipv6 mld enable - {%- endif %} + {%- endif %} + {%- endif %} exit {%- endfor %} ;--- Accès d'administration --- From fae56e6fa66383117dd229e4fad1a62ad3eaee06 Mon Sep 17 00:00:00 2001 From: chirac Date: Thu, 12 Jul 2018 16:55:15 +0200 Subject: [PATCH 19/31] Commentaire --- main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.py b/main.py index 60e9f5e..78d2799 100755 --- a/main.py +++ b/main.py @@ -70,6 +70,7 @@ class Switch: for vlan in port['get_port_profil']['vlan_tagged']: add_to_vlans(vlans, vlan, port) + #Trie les ip par vlan, et les ajoute ainsi que les subnet for ip, subnet in self.switch["interfaces_subnet"].items(): vlans[subnet[0]["vlan_id"]].setdefault("ipv4", {}) vlans[subnet[0]["vlan_id"]]["ipv4"][ip] = subnet @@ -77,6 +78,7 @@ class Switch: vlans[subnet["vlan_id"]].setdefault("ipv6", {}) vlans[subnet["vlan_id"]]["ipv6"][ipv6] = subnet + #Regroupement des options par vlans : dhcp_soop,arp, et dhcpv6, ainsi que igmp, mld , ra-guard et loop_protect 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"]] From 6dd130a9627aa4f01f617923a76c3474ea0316f5 Mon Sep 17 00:00:00 2001 From: chirac Date: Thu, 12 Jul 2018 17:37:48 +0200 Subject: [PATCH 20/31] Gestion du mode SFTP de provision --- main.py | 36 ++++++++++++++++++++++++++---------- templates/hp.tpl | 2 -- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 78d2799..d26f237 100755 --- a/main.py +++ b/main.py @@ -118,16 +118,32 @@ class Switch: def apply_conf_hp(self): """Apply config restore via rest""" url_restore = "http://" + self.switch["ipv4"] + "/rest/v4/system/config/cfg_restore" - data = { - "server_type": "ST_TFTP", - "file_name": self.get_conf_file_name(), - "tftp_server_address": { - "server_address": { - "ip_address": { - "version":"IAV_IP_V4", - "octets":self.settings["switchs_management_interface_ip"]}}}, - "is_forced_reboot_enabled": True, - } + provision_mode = self.settings["switchs_provision"] + if provision_mode == "tftp": + data = { + "server_type": "ST_TFTP", + "file_name": self.get_conf_file_name(), + "tftp_server_address": { + "server_address": { + "ip_address": { + "version":"IAV_IP_V4", + "octets":self.settings["switchs_management_interface_ip"]}}}, + "is_forced_reboot_enabled": True, + } + elif provision_mode == "sftp": + data = { + "server_type": "ST_SFTP", + "file_name": self.get_conf_file_name(), + "tftp_server_address": { + "server_address": { + "ip_address": { + "version":"IAV_IP_V4", + "octets":self.settings["switchs_management_interface_ip"]}}, + "user_name": self.settings["switchs_management_sftp_creds"]["login"], + "password": self.settings["switchs_management_sftp_creds"]["pass"], + }, + "is_forced_reboot_enabled": True, + } # Nous lançons la requête de type POST. post_restore = requests.post(url_restore, data=json.dumps(data), headers=self.headers) diff --git a/templates/hp.tpl b/templates/hp.tpl index ce50cf5..cff6914 100644 --- a/templates/hp.tpl +++ b/templates/hp.tpl @@ -62,8 +62,6 @@ vlan {{ id }} {%- for ipv6, subnet6 in vlan.ipv6.items() %} ipv6 address {{ ipv6 }}/{{ subnet6.netmask_cidr }} {%- endfor %} - {%- else %} - no ipv6 address {%- if id in additionals.igmp_vlans %} no ip igmp querier {%- endif %} From d2e1b92eb7401a02bc67f2742ff9f55074ad5c30 Mon Sep 17 00:00:00 2001 From: chirac Date: Thu, 12 Jul 2018 17:48:32 +0200 Subject: [PATCH 21/31] README --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 375b681..b39a2ff 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ -## Re2o - DHCP +## Re2o - Switchs config -This service uses Re2o API to generate DHCP leases files +This service uses Re2o API to generate Swicths config files +More info on https://gitlab.federez.net/re2o/switchs/wikis/home ## Requirements * python3 +* python3-jinja2 +* python3-json * requirements in https://gitlab.federez.net/re2o/re2oapi From 3ade345c95d24a6487fc33439ce817f5841692a3 Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Tue, 17 Jul 2018 23:51:20 +0200 Subject: [PATCH 22/31] recuperalier des info alias mail et generation du fichier aliases de postfix --- generated/__init__.py | 0 main.py | 164 ++++--------------------------------- templates/hp.tpl | 184 ------------------------------------------ templates/list | 10 +++ 4 files changed, 24 insertions(+), 334 deletions(-) delete mode 100644 generated/__init__.py delete mode 100644 templates/hp.tpl create mode 100644 templates/list diff --git a/generated/__init__.py b/generated/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/main.py b/main.py index d26f237..11bf6e0 100755 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ from jinja2 import Environment, FileSystemLoader import requests import base64 import json +import os.path config = ConfigParser() config.read('config.ini') @@ -21,160 +22,23 @@ api_client = Re2oAPIClient(api_hostname, api_username, api_password) client_hostname = socket.gethostname().split('.', 1)[0] -all_switchs = api_client.list("switchs/ports-config/") +all_users = api_client.list("mail/alias") # Création de l'environnement Jinja -ENV = Environment(loader=FileSystemLoader('.')) +env = Environment(loader=FileSystemLoader('.')) +template = env.get_template('templates/list') +aliases_rendered = template.render(data=all_users) +fichier = open('generated/aliases','w') -# 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. +if os.path.isfile('aliases_local'): # if a local aliases file exist, add it's content at the beginning + local = open('aliases_local','r') + for line in local.readlines(): + fichier.write(line) + local.close() +fichier.write(aliases_rendered) +fichier.close() -class Switch: - def __init__(self): - self.additionnal = None - self.all_vlans = api_client.list("machines/vlan/") - self.settings = api_client.view("preferences/optionaltopologie/") - # 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 - self.headers = None - self.creds_dict = None +print('done') - 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: - if not tagged: - vlans[vlan['vlan_id']]['ports_untagged'].append(str(port['port'])) - else: - vlans[vlan['vlan_id']]['ports_tagged'].append(str(port['port'])) - - vlans = dict() - - 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) - - #Trie les ip par vlan, et les ajoute ainsi que les subnet - for ip, subnet in self.switch["interfaces_subnet"].items(): - vlans[subnet[0]["vlan_id"]].setdefault("ipv4", {}) - vlans[subnet[0]["vlan_id"]]["ipv4"][ip] = subnet - for ipv6, subnet in self.switch["interfaces6_subnet"].items(): - vlans[subnet["vlan_id"]].setdefault("ipv6", {}) - vlans[subnet["vlan_id"]]["ipv6"][ipv6] = subnet - - #Regroupement des options par vlans : dhcp_soop,arp, et dhcpv6, ainsi que igmp, mld , ra-guard et loop_protect - 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"]] - 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"]] - 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']] - - 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, 'igmp_vlans' : igmp_vlans, 'mld_vlans': mld_vlans} - - - 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, settings=self.settings, additionals=self.additionals) - - def check_and_get_login(self): - """Récupère les login/mdp du switch, renvoie false si ils sont indisponibles""" - self.creds_dict = self.switch["get_management_cred_value"] - if self.creds_dict: - return True - else: - return False - - def login_hp(self): - """Login into rest interface of this switch""" - url_login = "http://" + self.switch["ipv4"] + "/rest/v3/login-sessions" - - payload_login = { - "userName" : self.creds_dict["id"], - "password" : self.creds_dict["pass"] - } - get_cookie = requests.post(url_login, data=json.dumps(payload_login)) - cookie = get_cookie.json()['cookie'] - self.headers = {"Cookie" : cookie} - - def apply_conf_hp(self): - """Apply config restore via rest""" - url_restore = "http://" + self.switch["ipv4"] + "/rest/v4/system/config/cfg_restore" - provision_mode = self.settings["switchs_provision"] - if provision_mode == "tftp": - data = { - "server_type": "ST_TFTP", - "file_name": self.get_conf_file_name(), - "tftp_server_address": { - "server_address": { - "ip_address": { - "version":"IAV_IP_V4", - "octets":self.settings["switchs_management_interface_ip"]}}}, - "is_forced_reboot_enabled": True, - } - elif provision_mode == "sftp": - data = { - "server_type": "ST_SFTP", - "file_name": self.get_conf_file_name(), - "tftp_server_address": { - "server_address": { - "ip_address": { - "version":"IAV_IP_V4", - "octets":self.settings["switchs_management_interface_ip"]}}, - "user_name": self.settings["switchs_management_sftp_creds"]["login"], - "password": self.settings["switchs_management_sftp_creds"]["pass"], - }, - "is_forced_reboot_enabled": True, - } - # Nous lançons la requête de type POST. - post_restore = requests.post(url_restore, data=json.dumps(data), headers=self.headers) - - def gen_conf_and_write(self): - """Génère la conf suivant le bon constructeur et l'écrit""" - if self.switch["model"]: - constructor = self.switch["model"]["constructor"].lower() - if "hp" in constructor or "aruba" in constructor: - self.gen_conf_hp() - self.write_conf() - - def apply_conf(self): - if self.check_and_get_login(): - if self.switch["model"] and self.switch["automatic_provision"] == True and self.settings["provision_switchs_enabled"]: - constructor = self.switch["model"]["constructor"].lower() - if "hp" in constructor or "aruba" in constructor: - self.login_hp() - self.apply_conf_hp() - - def write_conf(self): - """Ecriture de la conf du switch dans le fichier qui va bien""" - with open("generated/" + self.get_conf_file_name(), 'w+') as f: - f.write(self.conf) - - - -sw = Switch() -for switch in all_switchs: - sw.switch = switch - sw.gen_conf_and_write() - try: - sw.apply_conf() - except: - print("Erreur dans l'application de la conf pour " + switch["short_name"]) diff --git a/templates/hp.tpl b/templates/hp.tpl deleted file mode 100644 index cff6914..0000000 --- a/templates/hp.tpl +++ /dev/null @@ -1,184 +0,0 @@ -; {{ switch.model.reference }}A Configuration Editor; Created on release #{{ switch.model.firmware }} - -hostname "{{ switch.short_name }}" -; Generated on {{ date_gen }} by re2o -;--- Snmp --- -{%- if switch.switchbay.name %} -snmp-server location "{{ switch.switchbay.name }}" -{%- endif %} -;A faire à la main -snmpv3 enable -snmpv3 restricted-access -snmpv3 user "re2o" -snmpv3 group ManagerPriv user "re2o" sec-model ver3 -snmp-server community "public" Operator -;--- Heure/date -time timezone 60 -time daylight-time-rule Western-Europe -{%- for ipv4 in settings.switchs_management_utils.ntp_servers.ipv4 %} -sntp server priority {{ loop.index }} {{ ipv4 }} 4 -{%- endfor %} -{%- for ipv6 in settings.switchs_management_utils.ntp_servers.ipv6 %} -sntp server priority {{ loop.index + settings.switchs_management_utils.ntp_servers.ipv4|length }} {{ ipv6 }} 4 -{%- endfor %} -timesync sntp -sntp unicast -;--- Misc --- -console inactivity-timer 30 -;--- Logs --- -{%- for ipv4 in settings.switchs_management_utils.log_servers.ipv4 %} -logging {{ ipv4 }} -{%- endfor %} -{%- for ipv6 in settings.switchs_management_utils.log_servers.ipv6 %} -logging {{ ipv6 }} -{%- endfor %} -;--- IP du switch --- -no ip default-gateway -max-vlans 256 -{%- for id, vlan in additionals.vlans.items() %} -vlan {{ id }} - name "{{ vlan["name"]|capitalize }}" - {%- if vlan["ports_tagged"] %} - tagged {{ vlan["ports_tagged"]|join(',') }} - {%- endif %} - {%- if vlan["ports_untagged"] %} - untagged {{ vlan["ports_untagged"]|join(',') }} - {%- endif %} - {%- if id in additionals.igmp_vlans %} - ip igmp - {%- endif %} - {%- if id in additionals.mld_vlans %} - ipv6 mld version 1 - ipv6 mld enable - {%- endif %} - {%- if vlan.ipv4 %} - {%- for ipv4, subnet in vlan.ipv4.items() %} - ip address {{ ipv4 }}/{{ subnet.0.netmask_cidr }} - {%- endfor %} - {%- else %} - no ip address - {%- endif %} - {%- if vlan.ipv6 %} - {%- for ipv6, subnet6 in vlan.ipv6.items() %} - ipv6 address {{ ipv6 }}/{{ subnet6.netmask_cidr }} - {%- endfor %} - {%- if id in additionals.igmp_vlans %} - no ip igmp querier - {%- endif %} - {%- if id in additionals.mld_vlans %} - no ipv6 mld querier - {%- endif %} - {%- endif %} -exit -{%- endfor %} -;--- Accès d'administration --- -no telnet-server -{%- if switch.web_management_enabled %} -{%- if switch.web_management_enabled != "ssl" %} -web-management plaintext -{%- endif %} -{%- if switch.web_management_enabled == "ssl" %} -web-management ssl -{%- endif %} -{%- else %} -no web-management -{%- endif %} -{%- if switch.rest_enabled %} -rest-interface -{%- endif %} -aaa authentication ssh login public-key none -aaa authentication ssh enable public-key none -ip ssh -ip ssh filetransfer -{%- if settings.switchs_management_utils.subnet %} -ip authorized-managers {{ settings.switchs_management_utils.subnet.0.network }} {{ settings.switchs_management_utils.subnet.0.netmask }} access manager -{%- endif %} -{%- if settings.switchs_management_utils.subnet6 %} -ipv6 authorized-managers {{ settings.switchs_management_utils.subnet6.network }} {{ settings.switchs_management_utils.subnet6.netmask }} access manager -{%- endif %} -{%- if additionals.loop_protected %} -;--- Protection contre les boucles --- -loop-protect disable-timer 30 -loop-protect transmit-interval 3 -loop-protect {{ additionals.loop_protected|join(',') }} -{%- endif %} -;--- Serveurs Radius -radius-server dead-time 2 -{%- for ipv4 in settings.switchs_management_utils.radius_servers.ipv4 %} -radius-server host {{ ipv4 }} key "{{ switch.get_radius_key_value }}" -radius-server host {{ ipv4 }} dyn-authorization -{%- endfor %} -radius-server dyn-autz-port 3799 -;--- Filtrage mac --- -aaa port-access mac-based addr-format multi-colon -;--- Bricoles --- -no cdp run -{%- if additionals.dhcp_snooping_vlans %} -;--- DHCP Snooping --- -{%- for ipv4 in settings.switchs_management_utils.dhcp_servers.ipv4 %} -dhcp-snooping authorized-server {{ ipv4 }} -{%- endfor %} -dhcp-snooping vlan {{ additionals.dhcp_snooping_vlans|join(' ') }} -dhcp-snooping -{%- endif %} -{%- if additionals.arp_protect_vlans %} -;--- ARP Protect --- -arp-protect -arp-protect vlan {{ additionals.arp_protect_vlans|join(' ') }} -arp-protect validate src-mac dest-mac -{%- endif %} -{%- if additionals.dhcpv6_snooping_vlans %} -;--- DHCPv6 Snooping --- -dhcpv6-snooping vlan {{ additionals.dhcpv6_snooping_vlans|join(' ') }} -dhcpv6-snooping -{%- endif %} -{%- if additionals.ra_guarded %} -;--- RA guards --- -ipv6 ra-guard ports {{ additionals.ra_guarded|join(',')}} -{%- endif %} -;--- Config des prises --- -{%- for port in switch.ports %} -{%- if port.get_port_profil.radius_type == "802.1X" %} -aaa port-access authenticator {{ port.port }} -{%- if port.get_port_profil.mac_limit %} -aaa port-access authenticator {{ port.port }} client-limit {{ port.get_port_profil.mac_limit }} -{%- endif %} -aaa port-access authenticator {{ port.port }} logoff-period 3600 -{%- endif %} -{%- if port.get_port_profil.radius_type == "MAC-radius" %} -aaa port-access mac-based {{ port.port }} -{%- if port.get_port_profil.mac_limit %} -aaa port-access mac-based {{ port.port }} addr-limit {{ port.get_port_profil.mac_limit }} -{%- endif %} -aaa port-access mac-based {{ port.port }} logoff-period 3600 -aaa port-access mac-based {{ port.port }} unauth-vid 1 -{%- endif %} -interface {{ port.port }} - {%- if port.state %} - enable - {%- else %} - disable - {%- endif %} - name "{{ port.pretty_name }}" - {%- if port.get_port_profil.flow_control %} - flow-control - {%- endif %} - {%- if not port.get_port_profil.dhcp_snooping %} - dhcp-snooping trust - {%- endif %} - {%- if not port.get_port_profil.arp_protect %} - arp-protect trust - {%- endif %} - {%- if not port.get_port_profil.dhcpv6_snooping %} - dhcpv6-snooping trust - {%- endif %} - no lacp -exit -{%- endfor %} -;--- Configuration comptabilisation RADIUS --- -aaa accounting network start-stop radius -aaa accounting session-id unique -aaa accounting update periodic 240 -;--- Filtre de protocole --- -filter multicast 01005e0000fb drop all -filter multicast 3333000000fb drop all diff --git a/templates/list b/templates/list new file mode 100644 index 0000000..ff577f3 --- /dev/null +++ b/templates/list @@ -0,0 +1,10 @@ +# Liste d'association alias:addresse des users +{%- for user in data -%} +{%- for alias in user.get_mail_aliases -%} +{%- if user.redirection -%} +{{alias.valeur}}:{{ user.get_mail }} +{%- else -%} +{{alias.valeur}}:{{ user.pseudo }} +{% endif %} +{% endfor %} +{% endfor %} From dac9cca99455168f9083fd5ff8f0242afaafd81a Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Thu, 19 Jul 2018 12:53:35 +0200 Subject: [PATCH 23/31] update et reload de postfix --- main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 11bf6e0..38b2207 100755 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ from jinja2 import Environment, FileSystemLoader import requests import base64 import json +from subprocess import call import os.path config = ConfigParser() @@ -40,5 +41,6 @@ if os.path.isfile('aliases_local'): # if a local aliases file exist, add it's c fichier.write(aliases_rendered) fichier.close() -print('done') +call(["newalias", "generated/aliases"]) # Update the aliases config file +call(["postfix", "reload"]) # force the reloading now From 102816dedcd0db8817ab56763307316baeccdba6 Mon Sep 17 00:00:00 2001 From: Charlie Jacomme Date: Tue, 7 Aug 2018 17:30:58 +0200 Subject: [PATCH 24/31] gitignore aliases_local --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9e2266d..4b61a7f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ config.ini **/__pycache__/** **.list generated/* +aliases_local From 60c3f1032f96af7ec6284826538798455e771b8d Mon Sep 17 00:00:00 2001 From: Charlie Jacomme Date: Tue, 7 Aug 2018 17:32:20 +0200 Subject: [PATCH 25/31] gitmodule on crans.adm --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 361b0a1..f418c65 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "re2oapi"] path = re2oapi - url = https://gitlab.federez.net/re2o/re2oapi.git + url = https://gitlab.adm.crans.org/nounous/re2o-re2oapi.git From 570dc611d4fa84e76803811ad61538e7735f8b4a Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Tue, 7 Aug 2018 17:38:12 +0200 Subject: [PATCH 26/31] =?UTF-8?q?verifie=20si=20il=20y=20a=20besoin=20de?= =?UTF-8?q?=20reg=C3=A9n=C3=A9rer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) mode change 100755 => 100644 main.py diff --git a/main.py b/main.py old mode 100755 new mode 100644 index 38b2207..28152d4 --- a/main.py +++ b/main.py @@ -25,22 +25,30 @@ client_hostname = socket.gethostname().split('.', 1)[0] all_users = api_client.list("mail/alias") -# Création de l'environnement Jinja -env = Environment(loader=FileSystemLoader('.')) -template = env.get_template('templates/list') -aliases_rendered = template.render(data=all_users) -fichier = open('generated/aliases','w') +def generate(api_client): + # Création de l'environnement Jinja + env = Environment(loader=FileSystemLoader('.')) + template = env.get_template('templates/list') + aliases_rendered = template.render(data=all_users) -if os.path.isfile('aliases_local'): # if a local aliases file exist, add it's content at the beginning - local = open('aliases_local','r') - for line in local.readlines(): - fichier.write(line) - local.close() + fichier = open('generated/aliases','w') -fichier.write(aliases_rendered) -fichier.close() + if os.path.isfile('aliases_local'): # if a local aliases file exist, add it's content at the beginning + local = open('aliases_local','r') + for line in local.readlines(): + fichier.write(line) + local.close() + fichier.write(aliases_rendered) + fichier.close() -call(["newalias", "generated/aliases"]) # Update the aliases config file -call(["postfix", "reload"]) # force the reloading now + call(["newalias", "generated/aliases"]) # Update the aliases config file + call(["postfix", "reload"]) # force the reloading now + +for service in api_client.list("services/regen/"): + if service['hostname'] == client_hostname and \ + service['service_name'] == 'mail-server' and \ + service['need_regen']: + generate(api_client) + api_client.patch(service['api_url'], data={'need_regen': False}) From 51dcb049cf9b2854be3d7b985daf0a2a98313db5 Mon Sep 17 00:00:00 2001 From: Charlie Jacomme Date: Tue, 7 Aug 2018 19:58:19 +0200 Subject: [PATCH 27/31] update templates for new re2o names --- main.py | 2 +- templates/list | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 11bf6e0..f61f773 100755 --- a/main.py +++ b/main.py @@ -22,7 +22,7 @@ api_client = Re2oAPIClient(api_hostname, api_username, api_password) client_hostname = socket.gethostname().split('.', 1)[0] -all_users = api_client.list("mail/alias") +all_users = api_client.list("localemail/users") # Création de l'environnement Jinja env = Environment(loader=FileSystemLoader('.')) diff --git a/templates/list b/templates/list index ff577f3..b88a55a 100644 --- a/templates/list +++ b/templates/list @@ -1,10 +1,10 @@ # Liste d'association alias:addresse des users {%- for user in data -%} -{%- for alias in user.get_mail_aliases -%} -{%- if user.redirection -%} -{{alias.valeur}}:{{ user.get_mail }} +{%- for alias in user.email_address -%} +{%- if user.local_email_redirect -%} +{{alias.complete_email_address}}:{{ user.email }} {%- else -%} -{{alias.valeur}}:{{ user.pseudo }} +{{alias.complete_email_address}}:{{ alias.user }} {% endif %} {% endfor %} {% endfor %} From c04f1170af978e256bba1e9a75e0058b366f9d41 Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Tue, 7 Aug 2018 20:21:58 +0200 Subject: [PATCH 28/31] juste pour le pusher --- main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/main.py b/main.py index 28152d4..491acfa 100644 --- a/main.py +++ b/main.py @@ -25,7 +25,6 @@ client_hostname = socket.gethostname().split('.', 1)[0] all_users = api_client.list("mail/alias") - def generate(api_client): # Création de l'environnement Jinja env = Environment(loader=FileSystemLoader('.')) From d9101e710ffb0f77dd149ae0bc29722e2b5d3420 Mon Sep 17 00:00:00 2001 From: Charlie Jacomme Date: Wed, 8 Aug 2018 17:21:46 +0200 Subject: [PATCH 29/31] template update, no spaces, no @ --- templates/list | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/templates/list b/templates/list index b88a55a..3c2229a 100644 --- a/templates/list +++ b/templates/list @@ -1,10 +1,11 @@ # Liste d'association alias:addresse des users + {%- for user in data -%} {%- for alias in user.email_address -%} {%- if user.local_email_redirect -%} -{{alias.complete_email_address}}:{{ user.email }} +{{ alias.local_part }}:{{ user.email }} {%- else -%} -{{alias.complete_email_address}}:{{ alias.user }} +{{ alias.local_part }}:{{ alias.user }} {% endif %} -{% endfor %} -{% endfor %} +{%- endfor -%} +{%- endfor -%} From 5b88781fe043522a69854896e90dce7b0003a96e Mon Sep 17 00:00:00 2001 From: Charlie Jacomme Date: Wed, 8 Aug 2018 17:22:01 +0200 Subject: [PATCH 30/31] small fixes --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) mode change 100644 => 100755 main.py diff --git a/main.py b/main.py old mode 100644 new mode 100755 index de1a219..7533d8e --- a/main.py +++ b/main.py @@ -23,9 +23,9 @@ api_client = Re2oAPIClient(api_hostname, api_username, api_password) client_hostname = socket.gethostname().split('.', 1)[0] -all_users = api_client.list("localemail/users") def generate(api_client): + all_users = api_client.list("localemail/users") # Création de l'environnement Jinja env = Environment(loader=FileSystemLoader('.')) template = env.get_template('templates/list') @@ -42,7 +42,7 @@ def generate(api_client): fichier.write(aliases_rendered) fichier.close() - call(["newalias", "generated/aliases"]) # Update the aliases config file + call(["/usr/bin/newaliases"]) # Update the aliases config file call(["postfix", "reload"]) # force the reloading now for service in api_client.list("services/regen/"): From 9a4b0d5e3f74e1a9cfeee1a3a1f03d97da422c94 Mon Sep 17 00:00:00 2001 From: Charlie Jacomme Date: Wed, 8 Aug 2018 17:55:41 +0200 Subject: [PATCH 31/31] template update bug fix --- templates/list | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/list b/templates/list index 3c2229a..356f9a6 100644 --- a/templates/list +++ b/templates/list @@ -1,6 +1,7 @@ # Liste d'association alias:addresse des users - -{%- for user in data -%} + + +{% for user in data -%} {%- for alias in user.email_address -%} {%- if user.local_email_redirect -%} {{ alias.local_part }}:{{ user.email }}