|
| 1 | +#!/usr/sbin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import click |
| 5 | +import json |
| 6 | +import subprocess |
| 7 | + |
| 8 | +SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' |
| 9 | +MINIGRAPH_PATH = '/etc/sonic/minigraph.xml' |
| 10 | +MINIGRAPH_BGP_ASN_KEY = 'minigraph_bgp_asn' |
| 11 | +MINIGRAPH_BGP_SESSIONS = 'minigraph_bgp' |
| 12 | + |
| 13 | +# |
| 14 | +# Helper functions |
| 15 | +# |
| 16 | + |
| 17 | +# Returns BGP ASN as a string |
| 18 | +def _get_bgp_asn_from_minigraph(): |
| 19 | + # Get BGP ASN from minigraph |
| 20 | + proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-m', MINIGRAPH_PATH, '--var-json', MINIGRAPH_BGP_ASN_KEY], |
| 21 | + stdout=subprocess.PIPE, |
| 22 | + shell=False, |
| 23 | + stderr=subprocess.STDOUT) |
| 24 | + stdout = proc.communicate()[0] |
| 25 | + proc.wait() |
| 26 | + return json.loads(stdout.rstrip('\n')) |
| 27 | + |
| 28 | +# Returns True if a neighbor has the IP address <ipaddress>, False if not |
| 29 | +def _is_neighbor_ipaddress(ipaddress): |
| 30 | + # Get BGP ASN from minigraph |
| 31 | + proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-m', MINIGRAPH_PATH, '--var-json', MINIGRAPH_BGP_SESSIONS], |
| 32 | + stdout=subprocess.PIPE, |
| 33 | + shell=False, |
| 34 | + stderr=subprocess.STDOUT) |
| 35 | + stdout = proc.communicate()[0] |
| 36 | + proc.wait() |
| 37 | + bgp_session_list = json.loads(stdout.rstrip('\n')) |
| 38 | + |
| 39 | + for session in bgp_session_list: |
| 40 | + if session['addr'] == ipaddress: |
| 41 | + return True |
| 42 | + |
| 43 | + return False |
| 44 | + |
| 45 | +# Returns string containing IP address of neighbor with hostname <hostname> or None if <hostname> not a neighbor |
| 46 | +def _get_neighbor_ipaddress_by_hostname(hostname): |
| 47 | + # Get BGP ASN from minigraph |
| 48 | + proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-m', MINIGRAPH_PATH, '--var-json', MINIGRAPH_BGP_SESSIONS], |
| 49 | + stdout=subprocess.PIPE, |
| 50 | + shell=False, |
| 51 | + stderr=subprocess.STDOUT) |
| 52 | + stdout = proc.communicate()[0] |
| 53 | + proc.wait() |
| 54 | + bgp_session_list = json.loads(stdout.rstrip('\n')) |
| 55 | + |
| 56 | + for session in bgp_session_list: |
| 57 | + if session['name'] == hostname: |
| 58 | + return session['addr']; |
| 59 | + |
| 60 | + return None |
| 61 | + |
| 62 | + |
| 63 | +# Run bash command and print output to stdout |
| 64 | +def run_command(command, pager=False): |
| 65 | + click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green')) |
| 66 | + p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) |
| 67 | + if pager is True: |
| 68 | + click.echo_via_pager(p.stdout.read()) |
| 69 | + else: |
| 70 | + click.echo(p.stdout.read()) |
| 71 | + p.wait() |
| 72 | + if p.returncode != 0: |
| 73 | + sys.exit(p.returncode) |
| 74 | + |
| 75 | + |
| 76 | +# This is our main entrypoint - the main 'config' command |
| 77 | +@click.group() |
| 78 | +def cli(): |
| 79 | + """SONiC command line - 'config' command""" |
| 80 | + pass |
| 81 | + |
| 82 | + |
| 83 | +# |
| 84 | +# 'bgp' group |
| 85 | +# |
| 86 | + |
| 87 | +@cli.group() |
| 88 | +def bgp(): |
| 89 | + """BGP-related tasks""" |
| 90 | + pass |
| 91 | + |
| 92 | +# |
| 93 | +# 'shutdown' subgroup |
| 94 | +# |
| 95 | + |
| 96 | +@bgp.group() |
| 97 | +def shutdown(): |
| 98 | + """Shut down BGP session(s)""" |
| 99 | + pass |
| 100 | + |
| 101 | + |
| 102 | +# 'neighbor' subcommand |
| 103 | +@shutdown.command() |
| 104 | +@click.argument('ipaddr_or_hostname', required=True) |
| 105 | +def neighbor(ipaddr_or_hostname): |
| 106 | + """Shut down BGP session by neighbor IP address or hostname""" |
| 107 | + bgp_asn = _get_bgp_asn_from_minigraph() |
| 108 | + |
| 109 | + if _is_neighbor_ipaddress(ipaddr_or_hostname): |
| 110 | + ipaddress = ipaddr_or_hostname |
| 111 | + else: |
| 112 | + # If <ipaddr_or_hostname> is not the IP address of a neighbor, check to see if it's a hostname |
| 113 | + ipaddress = _get_neighbor_ipaddress_by_hostname(ipaddr_or_hostname) |
| 114 | + |
| 115 | + if ipaddress == None: |
| 116 | + print "Error: could not locate neighbor '{}'".format(ipaddr_or_hostname) |
| 117 | + raise click.Abort |
| 118 | + |
| 119 | + command = "vtysh -c 'configure terminal' -c 'router bgp {}' -c 'neighbor {} shutdown'".format(bgp_asn, ipaddress) |
| 120 | + run_command(command) |
| 121 | + |
| 122 | + |
| 123 | +@bgp.group() |
| 124 | +def startup(): |
| 125 | + """Start up BGP session(s)""" |
| 126 | + pass |
| 127 | + |
| 128 | + |
| 129 | +# 'neighbor' subcommand |
| 130 | +@startup.command() |
| 131 | +@click.argument('ipaddr_or_hostname', required=True) |
| 132 | +def neighbor(ipaddr_or_hostname): |
| 133 | + """Start up BGP session by neighbor IP address or hostname""" |
| 134 | + bgp_asn = _get_bgp_asn_from_minigraph() |
| 135 | + |
| 136 | + if _is_neighbor_ipaddress(ipaddr_or_hostname): |
| 137 | + ipaddress = ipaddr_or_hostname |
| 138 | + else: |
| 139 | + # If <ipaddr_or_hostname> is not the IP address of a neighbor, check to see if it's a hostname |
| 140 | + ipaddress = _get_neighbor_ipaddress_by_hostname(ipaddr_or_hostname) |
| 141 | + |
| 142 | + if ipaddress == None: |
| 143 | + print "Error: could not locate neighbor '{}'".format(ipaddr_or_hostname) |
| 144 | + raise click.Abort |
| 145 | + |
| 146 | + command = "vtysh -c 'configure terminal' -c 'router bgp {}' -c 'no neighbor {} shutdown'".format(bgp_asn, ipaddress) |
| 147 | + run_command(command) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + cli() |
| 152 | + |
0 commit comments