diff --git a/main.py b/main.py index 3b8d68b..0c324aa 100644 --- a/main.py +++ b/main.py @@ -13,6 +13,31 @@ import json import datetime import sys +HELP_MESSAGE = """ +Provisionning script from re2o. + +python3 main.py [--help] + [--force] [--upgrade] [--limit ] + + --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 + 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') @@ -237,35 +262,42 @@ class Switch: 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 \ - service['service_name'] == 'switchs' and \ - service['need_regen']: - error = False - sw=Switch() - for switch in all_switchs: - sw.switch = switch - sw.gen_conf_and_write() - try: - sw.apply_conf() - except: - error = True - api_client.patch(service['api_url'], data={'need_regen': error}) +if "--limit" in sys.argv: + list_index = sys.argv.index("--limit") + 1 + if len(sys.argv) >= list_index: + raise RuntimeError("--limit must be followeb by a comma separated list of switch") + +#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 \ +# service['service_name'] == 'switchs' and \ +# service['need_regen']: +# error = False +# sw=Switch() +# for switch in all_switchs: +# sw.switch = switch +# sw.gen_conf_and_write() +# 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'])