|
| 1 | +import click |
| 2 | +from hier_config import Platform, WorkflowRemediation, get_hconfig |
| 3 | +from hier_config.utils import read_text_from_file |
| 4 | + |
| 5 | +# Mapping for driver platforms |
| 6 | +PLATFORM_MAP = { |
| 7 | + "ios": Platform.CISCO_IOS, |
| 8 | + "nxos": Platform.CISCO_NXOS, |
| 9 | + "iosxr": Platform.CISCO_XR, |
| 10 | + "eos": Platform.ARISTA_EOS, |
| 11 | + "junos": Platform.JUNIPER_JUNOS, |
| 12 | + "vyos": Platform.VYOS, |
| 13 | + "generic": Platform.GENERIC, |
| 14 | + "hp_comware5": Platform.HP_COMWARE5, |
| 15 | + "hp_procurve": Platform.HP_PROCURVE, |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +@click.group() |
| 20 | +def cli(): |
| 21 | + """Hier Config CLI Tool""" |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +def common_options(func): |
| 26 | + """Reusable options for platform, running config, and generated config.""" |
| 27 | + func = click.option( |
| 28 | + "--platform", |
| 29 | + type=click.Choice(PLATFORM_MAP.keys(), case_sensitive=False), |
| 30 | + required=True, |
| 31 | + help="Platform driver to use (e.g., ios, nxos, iosxr, eos, junos, vyos, generic).", |
| 32 | + )(func) |
| 33 | + func = click.option( |
| 34 | + "--running-config", |
| 35 | + type=click.Path(exists=True, readable=True), |
| 36 | + required=True, |
| 37 | + help="Path to the running configuration file.", |
| 38 | + )(func) |
| 39 | + func = click.option( |
| 40 | + "--generated-config", |
| 41 | + type=click.Path(exists=True, readable=True), |
| 42 | + required=True, |
| 43 | + help="Path to the generated (intended) configuration file.", |
| 44 | + )(func) |
| 45 | + return func |
| 46 | + |
| 47 | + |
| 48 | +@cli.command() |
| 49 | +@common_options |
| 50 | +def remediation(platform, running_config, generated_config): |
| 51 | + """ |
| 52 | + Generate the remediation configuration. |
| 53 | + """ |
| 54 | + platform_enum = PLATFORM_MAP[platform.lower()] |
| 55 | + running_config_text = read_text_from_file(running_config) |
| 56 | + generated_config_text = read_text_from_file(generated_config) |
| 57 | + |
| 58 | + running_hconfig = get_hconfig(platform_enum, running_config_text) |
| 59 | + generated_hconfig = get_hconfig(platform_enum, generated_config_text) |
| 60 | + |
| 61 | + workflow = WorkflowRemediation(running_hconfig, generated_hconfig) |
| 62 | + |
| 63 | + click.echo("\n=== Remediation Configuration ===") |
| 64 | + for line in workflow.remediation_config.all_children_sorted(): |
| 65 | + click.echo(line.cisco_style_text()) |
| 66 | + |
| 67 | + |
| 68 | +@cli.command() |
| 69 | +@common_options |
| 70 | +def rollback(platform, running_config, generated_config): |
| 71 | + """ |
| 72 | + Generate the rollback configuration. |
| 73 | + """ |
| 74 | + platform_enum = PLATFORM_MAP[platform.lower()] |
| 75 | + running_config_text = read_text_from_file(running_config) |
| 76 | + generated_config_text = read_text_from_file(generated_config) |
| 77 | + |
| 78 | + running_hconfig = get_hconfig(platform_enum, running_config_text) |
| 79 | + generated_hconfig = get_hconfig(platform_enum, generated_config_text) |
| 80 | + |
| 81 | + workflow = WorkflowRemediation(running_hconfig, generated_hconfig) |
| 82 | + |
| 83 | + click.echo("\n=== Rollback Configuration ===") |
| 84 | + for line in workflow.rollback_config.all_children_sorted(): |
| 85 | + click.echo(line.cisco_style_text()) |
| 86 | + |
| 87 | + |
| 88 | +@cli.command() |
| 89 | +@common_options |
| 90 | +def future(platform, running_config, generated_config): |
| 91 | + """ |
| 92 | + Generate the future configuration. |
| 93 | + """ |
| 94 | + platform_enum = PLATFORM_MAP[platform.lower()] |
| 95 | + running_config_text = read_text_from_file(running_config) |
| 96 | + generated_config_text = read_text_from_file(generated_config) |
| 97 | + |
| 98 | + running_hconfig = get_hconfig(platform_enum, running_config_text) |
| 99 | + generated_hconfig = get_hconfig(platform_enum, generated_config_text) |
| 100 | + |
| 101 | + future_config = running_hconfig.future(generated_hconfig) |
| 102 | + |
| 103 | + click.echo("\n=== Future Configuration ===") |
| 104 | + for line in future_config.all_children_sorted(): |
| 105 | + click.echo(line.cisco_style_text()) |
| 106 | + |
| 107 | + |
| 108 | +@cli.command() |
| 109 | +def list_platforms(): |
| 110 | + """ |
| 111 | + List all available platforms. |
| 112 | + """ |
| 113 | + click.echo("\n=== Available Platforms ===") |
| 114 | + for platform in PLATFORM_MAP.keys(): |
| 115 | + click.echo(f"- {platform}") |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + cli() |
0 commit comments