requirements + clean prints + colored diff

This commit is contained in:
Michaël Paulon 2020-08-14 14:53:17 +02:00
parent 9894c0f240
commit e5b989b367
2 changed files with 15 additions and 6 deletions

View file

@ -5,6 +5,7 @@ import logging
from pprint import pprint
import colorlog
import termcolor
import yaml
from jinja2 import Template
@ -21,7 +22,6 @@ def syntax_from_range(value, vlan_syntax=False):
syntax = ""
in_range = False
for i in value:
print(i, type(i))
if prec is None or prec != i - 1:
if syntax == "":
syntax = str(i)
@ -105,7 +105,6 @@ def gen_interfaces(switch_config):
if_profile = yaml.load(open("configs/profiles/{}.yml".format(if_config.get("profile"))), yaml.Loader)
iface = range_from_syntax(iface)
for iface_number in iface:
print("if_num", iface_number)
if not if_profile.get("ignore_port", False):
interface = {
"number": iface_number,
@ -146,9 +145,6 @@ def gen_interfaces(switch_config):
def gen_conf(master_config, switch_config, old_config):
header = "\n".join(old_config.split("\n")[:2])
interfaces, vlans, mac_based_ports, ra_guard_ports, dhcp_snooping_vlans = gen_interfaces(switch_config)
print("mac", set(mac_based_ports))
print("ra", set(ra_guard_ports))
print("dhcp", set(dhcp_snooping_vlans))
config_dict = {
"header": header,
"hostname": switch_config.get("hostname"),
@ -211,6 +207,13 @@ if __name__ == "__main__":
logging.info("Generating configuration for {}".format(args.switch_name))
configuration = gen_conf(master_config, switch_config, old_config)
for line in difflib.unified_diff(old_config.split("\n"), configuration.split("\n"), fromfile='origin', tofile='new', lineterm=""):
if line.startswith("-"):
termcolor.cprint(line, "red")
elif line.startswith("+"):
termcolor.cprint(line, "green")
elif line.startswith("@"):
termcolor.cprint(line, "yellow")
else:
print(line)
if args.dry_run or input("Voulez-vous déployer la configuration sur le switch ? y/[n]").lower() not in ["o","y"]:
logger.info("Aborting deployement")

View file

@ -1 +1,7 @@
colorlog==4.2.1
Jinja2==2.11.2
MarkupSafe==1.1.1
pkg-resources==0.0.0
PyYAML==5.3.1
ssh2-python==0.18.0.post1
termcolor==1.1.0