Début configuration pour maj automatique des switchs

This commit is contained in:
chirac 2019-01-04 11:58:59 +01:00 committed by root
parent 6702e2cc41
commit 58eb2bad62

69
main.py
View file

@ -121,6 +121,10 @@ class Switch:
cookie = get_cookie.json()['cookie'] cookie = get_cookie.json()['cookie']
self.headers = {"Cookie" : cookie} self.headers = {"Cookie" : cookie}
def test(self):
url_info = "http://" + self.switch["ipv4"] + "/rest/v4/system/status/global_info"
get = requests.get(url_info, headers=self.headers)
def apply_conf_hp(self): def apply_conf_hp(self):
"""Apply config restore via rest""" """Apply config restore via rest"""
url_restore = "http://" + self.switch["ipv4"] + "/rest/v4/system/config/cfg_restore" url_restore = "http://" + self.switch["ipv4"] + "/rest/v4/system/config/cfg_restore"
@ -177,9 +181,63 @@ class Switch:
with open("generated/" + self.get_conf_file_name(), 'w+') as f: with open("generated/" + self.get_conf_file_name(), 'w+') as f:
f.write(self.conf) f.write(self.conf)
def upgrade(self):
"""Met à jour le switch en fonction du constructeur"""
if self.switch["model"]:
constructor = self.switch["model"]["constructor"].lower()
try:
if "hp" in constructor or "aruba" in constructor:
self.get_firmware_hp()
if self.primary_firmware < self.switch["firmware"]:
self.upgrade_hp()
# self.reboot_hp()
except RuntimeError:
print("Il y a eu une erreur pour la mise à jour du switch" + self.switch["short_name"])
for arg in sys.argv: def get_firmware_hp(self):
if arg=="--force": # L'URI est positionnée dans une variable
url_cli = "http://" + self.switch["ipv4"] + "/rest/v4/cli"
# On crée le dictionnaire avec la clé "cmd" et la valeur de la commande convertit en châine
data = {
"cmd": "show flash"
}
# On lance la requête avec le dictionnaire convertit en JSON et l'entête de session
post_cmd = requests.post(url_cli, data= json.dumps(data), headers=self.headers)
# On récupère le résultat de la requête, qui est décodé de base64, puis décodé en utf-8
result_decode = base64.b64decode(post_cmd.json()['result_base64_encoded']).decode('utf-8')
self.primary_firmware = result_decode.split('Secondary')[0].split()[-1]
self.secondary_firmware = result_decode.split('Boot')[0].split()[-1]
def upgrade_hp(self):
"""Update switch via rest"""
# On spécifie l'URI pour le transfert de fichier
url_file = "http://" + self.switch["ipv4"] + "/rest/v4/file-transfer"
# On crée le dictionnaire comportant les informations nécessaires (actions / URL / etc...)
data = {
"file_type":"FTT_FIRMWARE",
"url":"http://" + self.settings["switchs_management_interface_ip"] + "/" + self.switch["firmware"] + ".swi",
"action":"FTA_DOWNLOAD",
"boot_image": "BI_PRIMARY_IMAGE"
}
# On lance la requête au format POST
post_file = requests.post(url_file, data=json.dumps(data), headers=self.headers)
def reboot_hp(slef):
url_reboot = "http://" + self.switch["ipv4"] + "/rest/v4/system/reboot"
data = {
"action":"reload"
}
post_reboot = requests.post(url_reboot, data=json.dumps(data), headers=self.headers)
if "--force" in sys.argv:
sw = Switch() sw = Switch()
for switch in all_switchs: for switch in all_switchs:
sw.switch = switch sw.switch = switch
@ -189,6 +247,13 @@ for arg in sys.argv:
except: except:
print("Erreur dans l'application de la conf pour " + switch["short_name"]) print("Erreur dans l'application de la conf pour " + switch["short_name"])
if "--upgrade" in sys.argv:
sw = Switch()
for switch in all_switchs:
sw.switch = switch
sw.upgrade()
for service in api_client.list("services/regen/"): for service in api_client.list("services/regen/"):
if service['hostname'] == client_hostname and \ if service['hostname'] == client_hostname and \
service['service_name'] == 'switchs' and \ service['service_name'] == 'switchs' and \