add arguments to the script

aurore-dev
histausse 3 years ago
parent d213b62fe5
commit 72df2665d5

@ -13,6 +13,31 @@ import json
import datetime import datetime
import sys 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 = ConfigParser()
config.read('config.ini') config.read('config.ini')
@ -237,35 +262,42 @@ class Switch:
post_reboot = requests.post(url_reboot, data=json.dumps(data), headers=self.headers) post_reboot = requests.post(url_reboot, data=json.dumps(data), headers=self.headers)
if "--limit" in sys.argv:
if "--force" in sys.argv: list_index = sys.argv.index("--limit") + 1
sw = Switch() if len(sys.argv) >= list_index:
for switch in all_switchs: raise RuntimeError("--limit must be followeb by a comma separated list of switch")
sw.switch = switch
sw.gen_conf_and_write() #if "--force" in sys.argv:
try: # sw = Switch()
sw.apply_conf() # for switch in all_switchs:
except: # sw.switch = switch
print("Erreur dans l'application de la conf pour " + switch["short_name"]) # sw.gen_conf_and_write()
# try:
if "--upgrade" in sys.argv: # sw.apply_conf()
sw = Switch() # except:
for switch in all_switchs: # print("Erreur dans l'application de la conf pour " + switch["short_name"])
sw.switch = switch #
sw.upgrade() #if "--upgrade" in sys.argv:
# sw = Switch()
# for switch in all_switchs:
for service in api_client.list("services/regen/"): # sw.switch = switch
if service['hostname'] == client_hostname and \ # sw.upgrade()
service['service_name'] == 'switchs' and \ #
service['need_regen']: #
error = False #for service in api_client.list("services/regen/"):
sw=Switch() # if service['hostname'] == client_hostname and \
for switch in all_switchs: # service['service_name'] == 'switchs' and \
sw.switch = switch # service['need_regen']:
sw.gen_conf_and_write() # error = False
try: # sw=Switch()
sw.apply_conf() # for switch in all_switchs:
except: # sw.switch = switch
error = True # sw.gen_conf_and_write()
api_client.patch(service['api_url'], data={'need_regen': error}) # try:
# sw.apply_conf()
# except:
# error = True
# api_client.patch(service['api_url'], data={'need_regen': error})
for switch in all_switchs:
print(switch['short_name'])

Loading…
Cancel
Save