Début configuration pour maj automatique des switchs
This commit is contained in:
parent
6702e2cc41
commit
58eb2bad62
1 changed files with 75 additions and 10 deletions
85
main.py
85
main.py
|
@ -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,17 +181,78 @@ 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):
|
||||||
for arg in sys.argv:
|
"""Met à jour le switch en fonction du constructeur"""
|
||||||
if arg=="--force":
|
if self.switch["model"]:
|
||||||
sw = Switch()
|
constructor = self.switch["model"]["constructor"].lower()
|
||||||
for switch in all_switchs:
|
|
||||||
sw.switch = switch
|
|
||||||
sw.gen_conf_and_write()
|
|
||||||
try:
|
try:
|
||||||
sw.apply_conf()
|
if "hp" in constructor or "aruba" in constructor:
|
||||||
except:
|
self.get_firmware_hp()
|
||||||
print("Erreur dans l'application de la conf pour " + switch["short_name"])
|
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/"):
|
for service in api_client.list("services/regen/"):
|
||||||
if service['hostname'] == client_hostname and \
|
if service['hostname'] == client_hostname and \
|
||||||
|
|
Loading…
Reference in a new issue