Skip to content

Commit

Permalink
quagga-reload.py should be importable
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Walton <dwalton@cumulusnetworks.com>
Reviewed-by:   Don Slice <dslice@cumulusnetworks.com>

Ticket: CM-12686
  • Loading branch information
Daniel Walton committed Aug 31, 2016
1 parent 337299a commit a782e61
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions tools/quagga-reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
from pprint import pformat


log = logging.getLogger(__name__)


class Context(object):

"""
Expand Down Expand Up @@ -80,12 +83,12 @@ def load_from_file(self, filename):
The internal representation has been marked appropriately by passing it
through vtysh with the -m parameter
"""
logger.info('Loading Config object from file %s', filename)
log.info('Loading Config object from file %s', filename)

try:
file_output = subprocess.check_output(['/usr/bin/vtysh', '-m', '-f', filename])
except subprocess.CalledProcessError as e:
logger.error('vtysh marking of config file %s failed with error %s:', filename, str(e))
log.error('vtysh marking of config file %s failed with error %s:', filename, str(e))
print "vtysh marking of file %s failed with error: %s" % (filename, str(e))
sys.exit(1)

Expand All @@ -105,14 +108,14 @@ def load_from_show_running(self):
The internal representation has been marked appropriately by passing it
through vtysh with the -m parameter
"""
logger.info('Loading Config object from vtysh show running')
log.info('Loading Config object from vtysh show running')

try:
config_text = subprocess.check_output(
"/usr/bin/vtysh -c 'show run' | /usr/bin/tail -n +4 | /usr/bin/vtysh -m -f -",
shell=True)
except subprocess.CalledProcessError as e:
logger.error('vtysh marking of running config failed with error %s:', str(e))
log.error('vtysh marking of running config failed with error %s:', str(e))
print "vtysh marking of running config failed with error %s:" % (str(e))
sys.exit(1)

Expand Down Expand Up @@ -259,13 +262,13 @@ def load_contexts(self):
ctx_keys = [line, ]
current_context_lines = []

logger.debug('LINE %-50s: entering new context, %-50s', line, ctx_keys)
log.debug('LINE %-50s: entering new context, %-50s', line, ctx_keys)
self.save_contexts(ctx_keys, current_context_lines)
new_ctx = True

elif line == "end":
self.save_contexts(ctx_keys, current_context_lines)
logger.debug('LINE %-50s: exiting old context, %-50s', line, ctx_keys)
log.debug('LINE %-50s: exiting old context, %-50s', line, ctx_keys)

# Start a new context
new_ctx = True
Expand All @@ -281,7 +284,7 @@ def load_contexts(self):
# Start a new context
ctx_keys = copy.deepcopy(main_ctx_key)
current_context_lines = []
logger.debug('LINE %-50s: popping from subcontext to ctx%-50s', line, ctx_keys)
log.debug('LINE %-50s: popping from subcontext to ctx%-50s', line, ctx_keys)

elif new_ctx is True:
if not main_ctx_key:
Expand All @@ -292,7 +295,7 @@ def load_contexts(self):

current_context_lines = []
new_ctx = False
logger.debug('LINE %-50s: entering new context, %-50s', line, ctx_keys)
log.debug('LINE %-50s: entering new context, %-50s', line, ctx_keys)

elif "address-family " in line:
main_ctx_key = []
Expand All @@ -301,7 +304,7 @@ def load_contexts(self):
self.save_contexts(ctx_keys, current_context_lines)
current_context_lines = []
main_ctx_key = copy.deepcopy(ctx_keys)
logger.debug('LINE %-50s: entering sub-context, append to ctx_keys', line)
log.debug('LINE %-50s: entering sub-context, append to ctx_keys', line)

if line == "address-family ipv6":
ctx_keys.append("address-family ipv6 unicast")
Expand All @@ -313,7 +316,7 @@ def load_contexts(self):
else:
# Continuing in an existing context, add non-commented lines to it
current_context_lines.append(line)
logger.debug('LINE %-50s: append to current_context_lines, %-50s', line, ctx_keys)
log.debug('LINE %-50s: append to current_context_lines, %-50s', line, ctx_keys)

# Save the context of the last one
self.save_contexts(ctx_keys, current_context_lines)
Expand Down Expand Up @@ -699,7 +702,7 @@ def compare_context_objects(newconf, running):
# argparse should prevent this from happening but just to be safe...
else:
raise Exception('Must specify --reload or --test')
logger = logging.getLogger(__name__)
log = logging.getLogger(__name__)

# Verify the new config file is valid
if not os.path.isfile(args.filename):
Expand Down Expand Up @@ -728,9 +731,9 @@ def compare_context_objects(newconf, running):
sys.exit(1)

if args.debug:
logger.setLevel(logging.DEBUG)
log.setLevel(logging.DEBUG)

logger.info('Called via "%s"', str(args))
log.info('Called via "%s"', str(args))

# Create a Config object from the config generated by newconf
newconf = Config()
Expand Down Expand Up @@ -777,7 +780,7 @@ def compare_context_objects(newconf, running):

elif args.reload:

logger.debug('New Quagga Config\n%s', newconf.get_lines())
log.debug('New Quagga Config\n%s', newconf.get_lines())

# This looks a little odd but we have to do this twice...here is why
# If the user had this running bgp config:
Expand All @@ -804,7 +807,7 @@ def compare_context_objects(newconf, running):
for x in range(2):
running = Config()
running.load_from_show_running()
logger.debug('Running Quagga Config (Pass #%d)\n%s', x, running.get_lines())
log.debug('Running Quagga Config (Pass #%d)\n%s', x, running.get_lines())

(lines_to_add, lines_to_del) = compare_context_objects(newconf, running)

Expand Down Expand Up @@ -844,17 +847,17 @@ def compare_context_objects(newconf, running):
# 'no ip ospf authentication message-digest 1.1.1.1' in
# our example above
# - Split that last entry by whitespace and drop the last word
logger.warning('Failed to execute %s', ' '.join(cmd))
log.warning('Failed to execute %s', ' '.join(cmd))
last_arg = cmd[-1].split(' ')

if len(last_arg) <= 2:
logger.error('"%s" we failed to remove this command', original_cmd)
log.error('"%s" we failed to remove this command', original_cmd)
break

new_last_arg = last_arg[0:-1]
cmd[-1] = ' '.join(new_last_arg)
else:
logger.info('Executed "%s"', ' '.join(cmd))
log.info('Executed "%s"', ' '.join(cmd))
break

if lines_to_add:
Expand All @@ -874,7 +877,7 @@ def compare_context_objects(newconf, running):
string.digits) for _ in range(6))

filename = "/var/run/quagga/reload-%s.txt" % random_string
logger.info("%s content\n%s" % (filename, pformat(lines_to_configure)))
log.info("%s content\n%s" % (filename, pformat(lines_to_configure)))

with open(filename, 'w') as fh:
for line in lines_to_configure:
Expand Down

0 comments on commit a782e61

Please sign in to comment.