diff --git a/main.py b/main.py index 03f86b4..1b5be8a 100644 --- a/main.py +++ b/main.py @@ -121,6 +121,10 @@ class Switch: cookie = get_cookie.json()['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): """Apply config restore via rest""" url_restore = "http://" + self.switch["ipv4"] + "/rest/v4/system/config/cfg_restore" @@ -177,17 +181,78 @@ class Switch: with open("generated/" + self.get_conf_file_name(), 'w+') as f: f.write(self.conf) - -for arg in sys.argv: - if arg=="--force": - sw = Switch() - for switch in all_switchs: - sw.switch = switch - sw.gen_conf_and_write() + def upgrade(self): + """Met à jour le switch en fonction du constructeur""" + if self.switch["model"]: + constructor = self.switch["model"]["constructor"].lower() try: - sw.apply_conf() - except: - print("Erreur dans l'application de la conf pour " + switch["short_name"]) + 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"]) + + def get_firmware_hp(self): + # 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() + 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"]) + +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/"): if service['hostname'] == client_hostname and \