|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
#coding:utf-8
|
|
|
|
|
from configparser import ConfigParser
|
|
|
|
|
import socket
|
|
|
|
|
|
|
|
|
@ -13,31 +12,6 @@ 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')
|
|
|
|
|
|
|
|
|
@ -45,7 +19,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=True)
|
|
|
|
|
api_client = Re2oAPIClient(api_hostname, api_username, api_password, use_tls=False)
|
|
|
|
|
|
|
|
|
|
client_hostname = socket.gethostname().split('.', 1)[0]
|
|
|
|
|
|
|
|
|
@ -191,9 +165,8 @@ class Switch:
|
|
|
|
|
if "hp" in constructor or "aruba" in constructor:
|
|
|
|
|
self.gen_conf_hp()
|
|
|
|
|
self.write_conf()
|
|
|
|
|
except RuntimeError as e:
|
|
|
|
|
except RuntimeError:
|
|
|
|
|
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():
|
|
|
|
@ -218,9 +191,8 @@ class Switch:
|
|
|
|
|
if self.primary_firmware < self.switch["firmware"]:
|
|
|
|
|
self.upgrade_hp()
|
|
|
|
|
# self.reboot_hp()
|
|
|
|
|
except RuntimeError as e:
|
|
|
|
|
except RuntimeError:
|
|
|
|
|
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
|
|
|
|
@ -264,32 +236,6 @@ 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()
|
|
|
|
@ -298,10 +244,8 @@ if "--force" in sys.argv:
|
|
|
|
|
sw.gen_conf_and_write()
|
|
|
|
|
try:
|
|
|
|
|
sw.apply_conf()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
except:
|
|
|
|
|
print("Erreur dans l'application de la conf pour " + switch["short_name"])
|
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if "--upgrade" in sys.argv:
|
|
|
|
|
sw = Switch()
|
|
|
|
@ -309,6 +253,7 @@ 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 \
|
|
|
|
@ -323,4 +268,3 @@ for service in api_client.list("services/regen/"):
|
|
|
|
|
except:
|
|
|
|
|
error = True
|
|
|
|
|
api_client.patch(service['api_url'], data={'need_regen': error})
|
|
|
|
|
|
|
|
|
|