Compare commits

...

5 Commits

@ -1,4 +1,5 @@
#!/usr/bin/env python3
#coding:utf-8
from configparser import ConfigParser
import socket
@ -12,6 +13,31 @@ import json
import datetime
import sys
HELP_MESSAGE = """
Provisionning script from re2o.
python3 main.py [--help]
[--force] [--upgrade] [--limit <switchs>]
--help
Print this message
--force
Force rewrite the switch config. If not applied, the script only reconfigure the switchs that
re2o indicate as needing regeneration.
--upgrade
Upgrade the switch config
--limit <switchs>
Limit the scripts to the given list of switchs.
the list of switchs is a coma separated list of switch short name, whitout spaces.
exp: python3 --force --limit sw-ga-1,sw-ga-2
"""
# Yes, it's uggly, but we don't need to load all the mess bellow for just an help message.
if "--help" in sys.argv:
print(HELP_MESSAGE)
exit()
config = ConfigParser()
config.read('config.ini')
@ -19,7 +45,7 @@ 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, use_tls=False)
api_client = Re2oAPIClient(api_hostname, api_username, api_password, use_tls=True)
client_hostname = socket.gethostname().split('.', 1)[0]
@ -165,8 +191,9 @@ class Switch:
if "hp" in constructor or "aruba" in constructor:
self.gen_conf_hp()
self.write_conf()
except RuntimeError:
except RuntimeError as e:
print("Il y a eu une erreur pour le switch %s, la config proposée n'est pas intègre" % self.switch["short_name"])
print(e)
def apply_conf(self):
if self.check_and_get_login():
@ -191,8 +218,9 @@ class Switch:
if self.primary_firmware < self.switch["firmware"]:
self.upgrade_hp()
# self.reboot_hp()
except RuntimeError:
except RuntimeError as e:
print("Il y a eu une erreur pour la mise à jour du switch" + self.switch["short_name"])
print(e)
def get_firmware_hp(self):
# L'URI est positionnée dans une variable
@ -236,6 +264,32 @@ class Switch:
post_reboot = requests.post(url_reboot, data=json.dumps(data), headers=self.headers)
limit = False
if "--limit" in sys.argv:
limit = True
limit_list_index = sys.argv.index("--limit") + 1
if len(sys.argv) <= limit_list_index:
raise RuntimeError("--limit must be followeb by a comma separated list of switch")
limit_switchs = set(
filter(
bool,
map(
lambda x: x.strip(),
sys.argv[limit_list_index].split(',')
)
)
)
found_switchs = { x['short_name'] for x in all_switchs }
all_switchs = list(
filter(
lambda x: x['short_name'] in limit_switchs,
all_switchs
)
)
if not limit_switchs.issubset(found_switchs):
print("WARNING: some switchs in the list have not been found in the re2o API and will not be processed")
for sw_name in limit_switchs - found_switchs:
print("{name} has not been found".format(name=sw_name))
if "--force" in sys.argv:
sw = Switch()
@ -244,8 +298,10 @@ if "--force" in sys.argv:
sw.gen_conf_and_write()
try:
sw.apply_conf()
except:
except Exception as e:
print("Erreur dans l'application de la conf pour " + switch["short_name"])
print(e)
if "--upgrade" in sys.argv:
sw = Switch()
@ -253,7 +309,6 @@ if "--upgrade" in sys.argv:
sw.switch = switch
sw.upgrade()
for service in api_client.list("services/regen/"):
if service['hostname'] == client_hostname and \
service['service_name'] == 'switchs' and \
@ -268,3 +323,4 @@ for service in api_client.list("services/regen/"):
except:
error = True
api_client.patch(service['api_url'], data={'need_regen': error})

@ -118,6 +118,7 @@ radius-server host {{ ipv4 }} dyn-authorization
radius-server dyn-autz-port 3799
;--- Filtrage mac ---
aaa port-access mac-based addr-format multi-colon
aaa port-access mac-based unauth-redirect "http://re2o.auro.re/users/initial_register"
;--- Bricoles ---
no cdp run
{%- if additionals.dhcp_snooping_vlans %}

Loading…
Cancel
Save