3 # Copyright (C) 2014 Cumulus Networks, Inc.
5 # This file is part of Frr.
7 # Frr is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 2, or (at your option) any
12 # Frr is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Frr; see the file COPYING. If not, write to the Free
19 # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 - reads a frr configuration text file
25 - reads frr's current running configuration via "vtysh -c 'show running'"
26 - compares the two configs and determines what commands to execute to
27 synchronize frr's running configuration with the configuation in the
40 from collections
import OrderedDict
41 from ipaddr
import IPv6Address
, IPNetwork
42 from pprint
import pformat
45 log
= logging
.getLogger(__name__
)
48 class VtyshMarkException(Exception):
52 class Context(object):
55 A Context object represents a section of frr configuration such as:
58 description swp3 -> r8's swp1
63 or a single line context object such as this:
69 def __init__(self
, keys
, lines
):
73 # Keep a dictionary of the lines, this is to make it easy to tell if a
74 # line exists in this Context
75 self
.dlines
= OrderedDict()
78 self
.dlines
[ligne
] = True
80 def add_lines(self
, lines
):
82 Add lines to specified context
85 self
.lines
.extend(lines
)
88 self
.dlines
[ligne
] = True
94 A frr configuration is stored in a Config object. A Config object
95 contains a dictionary of Context objects where the Context keys
96 ('router ospf' for example) are our dictionary key.
101 self
.contexts
= OrderedDict()
103 def load_from_file(self
, filename
):
105 Read configuration from specified file and slurp it into internal memory
106 The internal representation has been marked appropriately by passing it
107 through vtysh with the -m parameter
109 log
.info('Loading Config object from file %s', filename
)
112 file_output
= subprocess
.check_output(['/usr/bin/vtysh', '-m', '-f', filename
],
113 stderr
=subprocess
.STDOUT
)
114 except subprocess
.CalledProcessError
as e
:
115 ve
= VtyshMarkException(e
)
119 for line
in file_output
.split('\n'):
122 # Compress duplicate whitespaces
123 line
= ' '.join(line
.split())
126 qv6_line
= get_normalized_ipv6_line(line
)
127 self
.lines
.append(qv6_line
)
129 self
.lines
.append(line
)
133 def load_from_show_running(self
):
135 Read running configuration and slurp it into internal memory
136 The internal representation has been marked appropriately by passing it
137 through vtysh with the -m parameter
139 log
.info('Loading Config object from vtysh show running')
142 config_text
= subprocess
.check_output(
143 "/usr/bin/vtysh -c 'show run' | /usr/bin/tail -n +4 | /usr/bin/vtysh -m -f -",
144 shell
=True, stderr
=subprocess
.STDOUT
)
145 except subprocess
.CalledProcessError
as e
:
146 ve
= VtyshMarkException(e
)
150 for line
in config_text
.split('\n'):
153 if (line
== 'Building configuration...' or
154 line
== 'Current configuration:' or
158 self
.lines
.append(line
)
164 Return the lines read in from the configuration
167 return '\n'.join(self
.lines
)
169 def get_contexts(self
):
171 Return the parsed context as strings for display, log etc.
174 for (_
, ctx
) in sorted(self
.contexts
.iteritems()):
175 print str(ctx
) + '\n'
177 def save_contexts(self
, key
, lines
):
179 Save the provided key and lines as a context
186 IP addresses specified in "network" statements, "ip prefix-lists"
187 etc. can differ in the host part of the specification the user
188 provides and what the running config displays. For example, user
189 can specify 11.1.1.1/24, and the running config displays this as
190 11.1.1.0/24. Ensure we don't do a needless operation for such
191 lines. IS-IS & OSPFv3 have no "network" support.
193 re_key_rt
= re
.match(r
'(ip|ipv6)\s+route\s+([A-Fa-f:.0-9/]+)(.*)$', key
[0])
195 addr
= re_key_rt
.group(2)
198 newaddr
= IPNetwork(addr
)
199 key
[0] = '%s route %s/%s%s' % (re_key_rt
.group(1),
206 re_key_rt
= re
.match(
207 r
'(ip|ipv6)\s+prefix-list(.*)(permit|deny)\s+([A-Fa-f:.0-9/]+)(.*)$',
211 addr
= re_key_rt
.group(4)
214 newaddr
= '%s/%s' % (IPNetwork(addr
).network
,
215 IPNetwork(addr
).prefixlen
)
221 legestr
= re_key_rt
.group(5)
222 re_lege
= re
.search(r
'(.*)le\s+(\d+)\s+ge\s+(\d+)(.*)', legestr
)
224 legestr
= '%sge %s le %s%s' % (re_lege
.group(1),
228 re_lege
= re
.search(r
'(.*)ge\s+(\d+)\s+le\s+(\d+)(.*)', legestr
)
230 if (re_lege
and ((re_key_rt
.group(1) == "ip" and
231 re_lege
.group(3) == "32") or
232 (re_key_rt
.group(1) == "ipv6" and
233 re_lege
.group(3) == "128"))):
234 legestr
= '%sge %s%s' % (re_lege
.group(1),
238 key
[0] = '%s prefix-list%s%s %s%s' % (re_key_rt
.group(1),
244 if lines
and key
[0].startswith('router bgp'):
247 re_net
= re
.match(r
'network\s+([A-Fa-f:.0-9/]+)(.*)$', line
)
249 addr
= re_net
.group(1)
250 if '/' not in addr
and key
[0].startswith('router bgp'):
251 # This is most likely an error because with no
252 # prefixlen, BGP treats the prefixlen as 8
256 newaddr
= IPNetwork(addr
)
257 line
= 'network %s/%s %s' % (newaddr
.network
,
260 newlines
.append(line
)
262 # Really this should be an error. Whats a network
263 # without an IP Address following it ?
264 newlines
.append(line
)
266 newlines
.append(line
)
270 More fixups in user specification and what running config shows.
271 "null0" in routes must be replaced by Null0, and "blackhole" must
272 be replaced by Null0 as well.
274 if (key
[0].startswith('ip route') or key
[0].startswith('ipv6 route') and
275 'null0' in key
[0] or 'blackhole' in key
[0]):
276 key
[0] = re
.sub(r
'\s+null0(\s*$)', ' Null0', key
[0])
277 key
[0] = re
.sub(r
'\s+blackhole(\s*$)', ' Null0', key
[0])
280 if tuple(key
) not in self
.contexts
:
281 ctx
= Context(tuple(key
), lines
)
282 self
.contexts
[tuple(key
)] = ctx
284 ctx
= self
.contexts
[tuple(key
)]
288 if tuple(key
) not in self
.contexts
:
289 ctx
= Context(tuple(key
), [])
290 self
.contexts
[tuple(key
)] = ctx
292 def load_contexts(self
):
294 Parse the configuration and create contexts for each appropriate block
297 current_context_lines
= []
301 The end of a context is flagged via the 'end' keyword:
310 bgp router-id 10.0.0.1
311 bgp log-neighbor-changes
312 no bgp default ipv4-unicast
313 neighbor EBGP peer-group
314 neighbor EBGP advertisement-interval 1
315 neighbor EBGP timers connect 10
316 neighbor 2001:40:1:4::6 remote-as 40
317 neighbor 2001:40:1:8::a remote-as 40
321 neighbor IBGPv6 activate
322 neighbor 2001:10::2 peer-group IBGPv6
323 neighbor 2001:10::3 peer-group IBGPv6
328 neighbor LEAF activate
332 route-target import 10.1.1.1:10100
333 route-target export 10.1.1.1:10100
339 ospf router-id 10.0.0.1
340 log-adjacency-changes detail
341 timers throttle spf 0 50 5000
346 # The code assumes that its working on the output from the "vtysh -m"
347 # command. That provides the appropriate markers to signify end of
348 # a context. This routine uses that to build the contexts for the
351 # There are single line contexts such as "log file /media/node/zebra.log"
352 # and multi-line contexts such as "router ospf" and subcontexts
353 # within a context such as "address-family" within "router bgp"
354 # In each of these cases, the first line of the context becomes the
355 # key of the context. So "router bgp 10" is the key for the non-address
356 # family part of bgp, "router bgp 10, address-family ipv6 unicast" is
357 # the key for the subcontext and so on.
362 # the keywords that we know are single line contexts. bgp in this case
363 # is not the main router bgp block, but enabling multi-instance
364 oneline_ctx_keywords
= ("access-list ",
385 for line
in self
.lines
:
390 if line
.startswith('!') or line
.startswith('#'):
394 if new_ctx
is True and any(line
.startswith(keyword
) for keyword
in oneline_ctx_keywords
):
395 self
.save_contexts(ctx_keys
, current_context_lines
)
397 # Start a new context
400 current_context_lines
= []
402 log
.debug('LINE %-50s: entering new context, %-50s', line
, ctx_keys
)
403 self
.save_contexts(ctx_keys
, current_context_lines
)
407 self
.save_contexts(ctx_keys
, current_context_lines
)
408 log
.debug('LINE %-50s: exiting old context, %-50s', line
, ctx_keys
)
410 # Start a new context
414 current_context_lines
= []
416 elif line
== "exit-address-family" or line
== "exit" or line
== "exit-vni":
417 # if this exit is for address-family ipv4 unicast, ignore the pop
419 self
.save_contexts(ctx_keys
, current_context_lines
)
421 # Start a new context
422 ctx_keys
= copy
.deepcopy(main_ctx_key
)
423 current_context_lines
= []
424 log
.debug('LINE %-50s: popping from subcontext to ctx%-50s', line
, ctx_keys
)
426 elif new_ctx
is True:
430 ctx_keys
= copy
.deepcopy(main_ctx_key
)
433 current_context_lines
= []
435 log
.debug('LINE %-50s: entering new context, %-50s', line
, ctx_keys
)
440 # Save old context first
441 self
.save_contexts(ctx_keys
, current_context_lines
)
442 current_context_lines
= []
443 main_ctx_key
= copy
.deepcopy(ctx_keys
)
444 log
.debug('LINE %-50s: entering sub-context, append to ctx_keys', line
)
446 ctx_keys
.append(line
)
448 elif "address-family " in line
:
451 # Save old context first
452 self
.save_contexts(ctx_keys
, current_context_lines
)
453 current_context_lines
= []
454 main_ctx_key
= copy
.deepcopy(ctx_keys
)
455 log
.debug('LINE %-50s: entering sub-context, append to ctx_keys', line
)
457 if line
== "address-family ipv6":
458 ctx_keys
.append("address-family ipv6 unicast")
459 elif line
== "address-family ipv4":
460 ctx_keys
.append("address-family ipv4 unicast")
461 elif line
== "address-family evpn":
462 ctx_keys
.append("address-family l2vpn evpn")
464 ctx_keys
.append(line
)
467 # Continuing in an existing context, add non-commented lines to it
468 current_context_lines
.append(line
)
469 log
.debug('LINE %-50s: append to current_context_lines, %-50s', line
, ctx_keys
)
471 # Save the context of the last one
472 self
.save_contexts(ctx_keys
, current_context_lines
)
475 def line_to_vtysh_conft(ctx_keys
, line
, delete
):
477 Return the vtysh command for the specified context line
486 for ctx_key
in ctx_keys
:
495 if line
.startswith('no '):
496 cmd
.append('%s' % line
[3:])
498 cmd
.append('no %s' % line
)
504 # If line is None then we are typically deleting an entire
505 # context ('no router ospf' for example)
510 # Only put the 'no' on the last sub-context
511 for ctx_key
in ctx_keys
:
514 if ctx_key
== ctx_keys
[-1]:
515 cmd
.append('no %s' % ctx_key
)
517 cmd
.append('%s' % ctx_key
)
519 for ctx_key
in ctx_keys
:
526 def line_for_vtysh_file(ctx_keys
, line
, delete
):
528 Return the command as it would appear in frr.conf
533 for (i
, ctx_key
) in enumerate(ctx_keys
):
534 cmd
.append(' ' * i
+ ctx_key
)
537 indent
= len(ctx_keys
) * ' '
540 if line
.startswith('no '):
541 cmd
.append('%s%s' % (indent
, line
[3:]))
543 cmd
.append('%sno %s' % (indent
, line
))
546 cmd
.append(indent
+ line
)
548 # If line is None then we are typically deleting an entire
549 # context ('no router ospf' for example)
553 # Only put the 'no' on the last sub-context
554 for ctx_key
in ctx_keys
:
556 if ctx_key
== ctx_keys
[-1]:
557 cmd
.append('no %s' % ctx_key
)
559 cmd
.append('%s' % ctx_key
)
561 for ctx_key
in ctx_keys
:
564 cmd
= '\n' + '\n'.join(cmd
)
566 # There are some commands that are on by default so their "no" form will be
567 # displayed in the config. "no bgp default ipv4-unicast" is one of these.
568 # If we need to remove this line we do so by adding "bgp default ipv4-unicast",
569 # not by doing a "no no bgp default ipv4-unicast"
570 cmd
= cmd
.replace('no no ', '')
575 def get_normalized_ipv6_line(line
):
577 Return a normalized IPv6 line as produced by frr,
578 with all letters in lower case and trailing and leading
579 zeros removed, and only the network portion present if
580 the IPv6 word is a network
583 words
= line
.split(' ')
589 v6word
= IPNetwork(word
)
590 norm_word
= '%s/%s' % (v6word
.network
, v6word
.prefixlen
)
595 norm_word
= '%s' % IPv6Address(word
)
600 norm_line
= norm_line
+ " " + norm_word
602 return norm_line
.strip()
605 def line_exist(lines
, target_ctx_keys
, target_line
, exact_match
=True):
606 for (ctx_keys
, line
) in lines
:
607 if ctx_keys
== target_ctx_keys
:
609 if line
== target_line
:
612 if line
.startswith(target_line
):
617 def ignore_delete_re_add_lines(lines_to_add
, lines_to_del
):
619 # Quite possibly the most confusing (while accurate) variable names in history
620 lines_to_add_to_del
= []
621 lines_to_del_to_del
= []
623 for (ctx_keys
, line
) in lines_to_del
:
626 if ctx_keys
[0].startswith('router bgp') and line
:
628 if line
.startswith('neighbor '):
630 BGP changed how it displays swpX peers that are part of peer-group. Older
631 versions of frr would display these on separate lines:
632 neighbor swp1 interface
633 neighbor swp1 peer-group FOO
635 but today we display via a single line
636 neighbor swp1 interface peer-group FOO
638 This change confuses frr-reload.py so check to see if we are deleting
639 neighbor swp1 interface peer-group FOO
642 neighbor swp1 interface
643 neighbor swp1 peer-group FOO
645 If so then chop the del line and the corresponding add lines
648 re_swpx_int_peergroup
= re
.search('neighbor (\S+) interface peer-group (\S+)', line
)
649 re_swpx_int_v6only_peergroup
= re
.search('neighbor (\S+) interface v6only peer-group (\S+)', line
)
651 if re_swpx_int_peergroup
or re_swpx_int_v6only_peergroup
:
652 swpx_interface
= None
653 swpx_peergroup
= None
655 if re_swpx_int_peergroup
:
656 swpx
= re_swpx_int_peergroup
.group(1)
657 peergroup
= re_swpx_int_peergroup
.group(2)
658 swpx_interface
= "neighbor %s interface" % swpx
659 elif re_swpx_int_v6only_peergroup
:
660 swpx
= re_swpx_int_v6only_peergroup
.group(1)
661 peergroup
= re_swpx_int_v6only_peergroup
.group(2)
662 swpx_interface
= "neighbor %s interface v6only" % swpx
664 swpx_peergroup
= "neighbor %s peer-group %s" % (swpx
, peergroup
)
665 found_add_swpx_interface
= line_exist(lines_to_add
, ctx_keys
, swpx_interface
)
666 found_add_swpx_peergroup
= line_exist(lines_to_add
, ctx_keys
, swpx_peergroup
)
667 tmp_ctx_keys
= tuple(list(ctx_keys
))
669 if not found_add_swpx_peergroup
:
670 tmp_ctx_keys
= list(ctx_keys
)
671 tmp_ctx_keys
.append('address-family ipv4 unicast')
672 tmp_ctx_keys
= tuple(tmp_ctx_keys
)
673 found_add_swpx_peergroup
= line_exist(lines_to_add
, tmp_ctx_keys
, swpx_peergroup
)
675 if not found_add_swpx_peergroup
:
676 tmp_ctx_keys
= list(ctx_keys
)
677 tmp_ctx_keys
.append('address-family ipv6 unicast')
678 tmp_ctx_keys
= tuple(tmp_ctx_keys
)
679 found_add_swpx_peergroup
= line_exist(lines_to_add
, tmp_ctx_keys
, swpx_peergroup
)
681 if found_add_swpx_interface
and found_add_swpx_peergroup
:
683 lines_to_del_to_del
.append((ctx_keys
, line
))
684 lines_to_add_to_del
.append((ctx_keys
, swpx_interface
))
685 lines_to_add_to_del
.append((tmp_ctx_keys
, swpx_peergroup
))
688 We changed how we display the neighbor interface command. Older
689 versions of frr would display the following:
690 neighbor swp1 interface
691 neighbor swp1 remote-as external
692 neighbor swp1 capability extended-nexthop
694 but today we display via a single line
695 neighbor swp1 interface remote-as external
697 and capability extended-nexthop is no longer needed because we
698 automatically enable it when the neighbor is of type interface.
700 This change confuses frr-reload.py so check to see if we are deleting
701 neighbor swp1 interface remote-as (external|internal|ASNUM)
704 neighbor swp1 interface
705 neighbor swp1 remote-as (external|internal|ASNUM)
706 neighbor swp1 capability extended-nexthop
708 If so then chop the del line and the corresponding add lines
710 re_swpx_int_remoteas
= re
.search('neighbor (\S+) interface remote-as (\S+)', line
)
711 re_swpx_int_v6only_remoteas
= re
.search('neighbor (\S+) interface v6only remote-as (\S+)', line
)
713 if re_swpx_int_remoteas
or re_swpx_int_v6only_remoteas
:
714 swpx_interface
= None
717 if re_swpx_int_remoteas
:
718 swpx
= re_swpx_int_remoteas
.group(1)
719 remoteas
= re_swpx_int_remoteas
.group(2)
720 swpx_interface
= "neighbor %s interface" % swpx
721 elif re_swpx_int_v6only_remoteas
:
722 swpx
= re_swpx_int_v6only_remoteas
.group(1)
723 remoteas
= re_swpx_int_v6only_remoteas
.group(2)
724 swpx_interface
= "neighbor %s interface v6only" % swpx
726 swpx_remoteas
= "neighbor %s remote-as %s" % (swpx
, remoteas
)
727 found_add_swpx_interface
= line_exist(lines_to_add
, ctx_keys
, swpx_interface
)
728 found_add_swpx_remoteas
= line_exist(lines_to_add
, ctx_keys
, swpx_remoteas
)
729 tmp_ctx_keys
= tuple(list(ctx_keys
))
731 if found_add_swpx_interface
and found_add_swpx_remoteas
:
733 lines_to_del_to_del
.append((ctx_keys
, line
))
734 lines_to_add_to_del
.append((ctx_keys
, swpx_interface
))
735 lines_to_add_to_del
.append((tmp_ctx_keys
, swpx_remoteas
))
738 We made the 'bgp bestpath as-path multipath-relax' command
739 automatically assume 'no-as-set' since the lack of this option caused
740 weird routing problems. When the running config is shown in
741 releases with this change, the no-as-set keyword is not shown as it
742 is the default. This causes frr-reload to unnecessarily unapply
743 this option only to apply it back again, causing unnecessary session
746 if 'multipath-relax' in line
:
747 re_asrelax_new
= re
.search('^bgp\s+bestpath\s+as-path\s+multipath-relax$', line
)
748 old_asrelax_cmd
= 'bgp bestpath as-path multipath-relax no-as-set'
749 found_asrelax_old
= line_exist(lines_to_add
, ctx_keys
, old_asrelax_cmd
)
751 if re_asrelax_new
and found_asrelax_old
:
753 lines_to_del_to_del
.append((ctx_keys
, line
))
754 lines_to_add_to_del
.append((ctx_keys
, old_asrelax_cmd
))
757 If we are modifying the BGP table-map we need to avoid a del/add and
758 instead modify the table-map in place via an add. This is needed to
759 avoid installing all routes in the RIB the second the 'no table-map'
762 if line
.startswith('table-map'):
763 found_table_map
= line_exist(lines_to_add
, ctx_keys
, 'table-map', False)
766 lines_to_del_to_del
.append((ctx_keys
, line
))
769 More old-to-new config handling. ip import-table no longer accepts
770 distance, but we honor the old syntax. But 'show running' shows only
771 the new syntax. This causes an unnecessary 'no import-table' followed
772 by the same old 'ip import-table' which causes perturbations in
773 announced routes leading to traffic blackholes. Fix this issue.
775 re_importtbl
= re
.search('^ip\s+import-table\s+(\d+)$', ctx_keys
[0])
777 table_num
= re_importtbl
.group(1)
778 for ctx
in lines_to_add
:
779 if ctx
[0][0].startswith('ip import-table %s distance' % table_num
):
780 lines_to_del_to_del
.append((('ip import-table %s' % table_num
,), None))
781 lines_to_add_to_del
.append((ctx
[0], None))
784 ip/ipv6 prefix-list can be specified without a seq number. However,
785 the running config always adds 'seq x', where x is a number incremented
786 by 5 for every element, to the prefix list. So, ignore such lines as
787 well. Sample prefix-list lines:
788 ip prefix-list PR-TABLE-2 seq 5 permit 20.8.2.0/24 le 32
789 ip prefix-list PR-TABLE-2 seq 10 permit 20.8.2.0/24 le 32
790 ipv6 prefix-list vrfdev6-12 permit 2000:9:2::/64 gt 64
792 re_ip_pfxlst
= re
.search('^(ip|ipv6)(\s+prefix-list\s+)(\S+\s+)(seq \d+\s+)(permit|deny)(.*)$',
795 tmpline
= (re_ip_pfxlst
.group(1) + re_ip_pfxlst
.group(2) +
796 re_ip_pfxlst
.group(3) + re_ip_pfxlst
.group(5) +
797 re_ip_pfxlst
.group(6))
798 for ctx
in lines_to_add
:
799 if ctx
[0][0] == tmpline
:
800 lines_to_del_to_del
.append((ctx_keys
, None))
801 lines_to_add_to_del
.append(((tmpline
,), None))
803 if (len(ctx_keys
) == 3 and
804 ctx_keys
[0].startswith('router bgp') and
805 ctx_keys
[1] == 'address-family l2vpn evpn' and
806 ctx_keys
[2].startswith('vni')):
808 re_route_target
= re
.search('^route-target import (.*)$', line
) if line
is not None else False
811 rt
= re_route_target
.group(1).strip()
812 route_target_import_line
= line
813 route_target_export_line
= "route-target export %s" % rt
814 route_target_both_line
= "route-target both %s" % rt
816 found_route_target_export_line
= line_exist(lines_to_del
, ctx_keys
, route_target_export_line
)
817 found_route_target_both_line
= line_exist(lines_to_add
, ctx_keys
, route_target_both_line
)
820 If the running configs has
821 route-target import 1:1
822 route-target export 1:1
824 and the config we are reloading against has
825 route-target both 1:1
827 then we can ignore deleting the import/export and ignore adding the 'both'
829 if found_route_target_export_line
and found_route_target_both_line
:
830 lines_to_del_to_del
.append((ctx_keys
, route_target_import_line
))
831 lines_to_del_to_del
.append((ctx_keys
, route_target_export_line
))
832 lines_to_add_to_del
.append((ctx_keys
, route_target_both_line
))
835 found_add_line
= line_exist(lines_to_add
, ctx_keys
, line
)
838 lines_to_del_to_del
.append((ctx_keys
, line
))
839 lines_to_add_to_del
.append((ctx_keys
, line
))
842 We have commands that used to be displayed in the global part
843 of 'router bgp' that are now displayed under 'address-family ipv4 unicast'
847 neighbor ISL advertisement-interval 0
853 address-family ipv4 unicast
854 neighbor ISL advertisement-interval 0
856 Look to see if we are deleting it in one format just to add it back in the other
858 if ctx_keys
[0].startswith('router bgp') and len(ctx_keys
) > 1 and ctx_keys
[1] == 'address-family ipv4 unicast':
859 tmp_ctx_keys
= list(ctx_keys
)[:-1]
860 tmp_ctx_keys
= tuple(tmp_ctx_keys
)
862 found_add_line
= line_exist(lines_to_add
, tmp_ctx_keys
, line
)
865 lines_to_del_to_del
.append((ctx_keys
, line
))
866 lines_to_add_to_del
.append((tmp_ctx_keys
, line
))
868 for (ctx_keys
, line
) in lines_to_del_to_del
:
869 lines_to_del
.remove((ctx_keys
, line
))
871 for (ctx_keys
, line
) in lines_to_add_to_del
:
872 lines_to_add
.remove((ctx_keys
, line
))
874 return (lines_to_add
, lines_to_del
)
877 def ignore_unconfigurable_lines(lines_to_add
, lines_to_del
):
879 There are certain commands that cannot be removed. Remove
880 those commands from lines_to_del.
882 lines_to_del_to_del
= []
884 for (ctx_keys
, line
) in lines_to_del
:
886 if (ctx_keys
[0].startswith('frr version') or
887 ctx_keys
[0].startswith('frr defaults') or
888 ctx_keys
[0].startswith('password') or
889 ctx_keys
[0].startswith('line vty') or
891 # This is technically "no"able but if we did so frr-reload would
892 # stop working so do not let the user shoot themselves in the foot
894 ctx_keys
[0].startswith('service integrated-vtysh-config')):
896 log
.info("(%s, %s) cannot be removed" % (pformat(ctx_keys
), line
))
897 lines_to_del_to_del
.append((ctx_keys
, line
))
899 for (ctx_keys
, line
) in lines_to_del_to_del
:
900 lines_to_del
.remove((ctx_keys
, line
))
902 return (lines_to_add
, lines_to_del
)
905 def compare_context_objects(newconf
, running
):
907 Create a context diff for the two specified contexts
910 # Compare the two Config objects to find the lines that we need to add/del
915 # Find contexts that are in newconf but not in running
916 # Find contexts that are in running but not in newconf
917 for (running_ctx_keys
, running_ctx
) in running
.contexts
.iteritems():
919 if running_ctx_keys
not in newconf
.contexts
:
921 # We check that the len is 1 here so that we only look at ('router bgp 10')
922 # and not ('router bgp 10', 'address-family ipv4 unicast'). The
923 # latter could cause a false delete_bgpd positive if ipv4 unicast is in
924 # running but not in newconf.
925 if "router bgp" in running_ctx_keys
[0] and len(running_ctx_keys
) == 1:
927 lines_to_del
.append((running_ctx_keys
, None))
929 # We cannot do 'no interface' in FRR, and so deal with it
930 elif running_ctx_keys
[0].startswith('interface'):
931 for line
in running_ctx
.lines
:
932 lines_to_del
.append((running_ctx_keys
, line
))
934 # If this is an address-family under 'router bgp' and we are already deleting the
935 # entire 'router bgp' context then ignore this sub-context
936 elif "router bgp" in running_ctx_keys
[0] and len(running_ctx_keys
) > 1 and delete_bgpd
:
939 # Delete an entire vni sub-context under "address-family l2vpn evpn"
940 elif ("router bgp" in running_ctx_keys
[0] and
941 len(running_ctx_keys
) > 2 and
942 running_ctx_keys
[1].startswith('address-family l2vpn evpn') and
943 running_ctx_keys
[2].startswith('vni ')):
944 lines_to_del
.append((running_ctx_keys
, None))
946 elif ("router bgp" in running_ctx_keys
[0] and
947 len(running_ctx_keys
) > 1 and
948 running_ctx_keys
[1].startswith('address-family')):
949 # There's no 'no address-family' support and so we have to
950 # delete each line individually again
951 for line
in running_ctx
.lines
:
952 lines_to_del
.append((running_ctx_keys
, line
))
955 elif running_ctx_keys
and not any("address-family" in key
for key
in running_ctx_keys
):
956 lines_to_del
.append((running_ctx_keys
, None))
958 elif running_ctx_keys
and not any("vni" in key
for key
in running_ctx_keys
):
959 lines_to_del
.append((running_ctx_keys
, None))
963 for line
in running_ctx
.lines
:
964 lines_to_del
.append((running_ctx_keys
, line
))
966 # Find the lines within each context to add
967 # Find the lines within each context to del
968 for (newconf_ctx_keys
, newconf_ctx
) in newconf
.contexts
.iteritems():
970 if newconf_ctx_keys
in running
.contexts
:
971 running_ctx
= running
.contexts
[newconf_ctx_keys
]
973 for line
in newconf_ctx
.lines
:
974 if line
not in running_ctx
.dlines
:
975 lines_to_add
.append((newconf_ctx_keys
, line
))
977 for line
in running_ctx
.lines
:
978 if line
not in newconf_ctx
.dlines
:
979 lines_to_del
.append((newconf_ctx_keys
, line
))
981 for (newconf_ctx_keys
, newconf_ctx
) in newconf
.contexts
.iteritems():
983 if newconf_ctx_keys
not in running
.contexts
:
984 lines_to_add
.append((newconf_ctx_keys
, None))
986 for line
in newconf_ctx
.lines
:
987 lines_to_add
.append((newconf_ctx_keys
, line
))
989 (lines_to_add
, lines_to_del
) = ignore_delete_re_add_lines(lines_to_add
, lines_to_del
)
990 (lines_to_add
, lines_to_del
) = ignore_unconfigurable_lines(lines_to_add
, lines_to_del
)
992 return (lines_to_add
, lines_to_del
)
996 def vtysh_config_available():
998 Return False if no frr daemon is running or some other vtysh session is
999 in 'configuration terminal' mode which will prevent us from making any
1000 configuration changes.
1004 cmd
= ['/usr/bin/vtysh', '-c', 'conf t']
1005 output
= subprocess
.check_output(cmd
, stderr
=subprocess
.STDOUT
).strip()
1007 if 'VTY configuration is locked by other VTY' in output
:
1009 log
.error("'%s' returned\n%s\n" % (' '.join(cmd
), output
))
1012 except subprocess
.CalledProcessError
as e
:
1013 msg
= "vtysh could not connect with any frr daemons"
1021 if __name__
== '__main__':
1022 # Command line options
1023 parser
= argparse
.ArgumentParser(description
='Dynamically apply diff in frr configs')
1024 parser
.add_argument('--input', help='Read running config from file instead of "show running"')
1025 group
= parser
.add_mutually_exclusive_group(required
=True)
1026 group
.add_argument('--reload', action
='store_true', help='Apply the deltas', default
=False)
1027 group
.add_argument('--test', action
='store_true', help='Show the deltas', default
=False)
1028 parser
.add_argument('--debug', action
='store_true', help='Enable debugs', default
=False)
1029 parser
.add_argument('--stdout', action
='store_true', help='Log to STDOUT', default
=False)
1030 parser
.add_argument('filename', help='Location of new frr config file')
1031 parser
.add_argument('--overwrite', action
='store_true', help='Overwrite frr.conf with running config output', default
=False)
1032 args
= parser
.parse_args()
1035 # For --test log to stdout
1036 # For --reload log to /var/log/frr/frr-reload.log
1037 if args
.test
or args
.stdout
:
1038 logging
.basicConfig(level
=logging
.INFO
,
1039 format
='%(asctime)s %(levelname)5s: %(message)s')
1041 # Color the errors and warnings in red
1042 logging
.addLevelName(logging
.ERROR
, "\033[91m %s\033[0m" % logging
.getLevelName(logging
.ERROR
))
1043 logging
.addLevelName(logging
.WARNING
, "\033[91m%s\033[0m" % logging
.getLevelName(logging
.WARNING
))
1046 if not os
.path
.isdir('/var/log/frr/'):
1047 os
.makedirs('/var/log/frr/')
1049 logging
.basicConfig(filename
='/var/log/frr/frr-reload.log',
1051 format
='%(asctime)s %(levelname)5s: %(message)s')
1053 # argparse should prevent this from happening but just to be safe...
1055 raise Exception('Must specify --reload or --test')
1056 log
= logging
.getLogger(__name__
)
1058 # Verify the new config file is valid
1059 if not os
.path
.isfile(args
.filename
):
1060 msg
= "Filename %s does not exist" % args
.filename
1065 if not os
.path
.getsize(args
.filename
):
1066 msg
= "Filename %s is an empty file" % args
.filename
1071 # Verify that 'service integrated-vtysh-config' is configured
1072 vtysh_filename
= '/etc/frr/vtysh.conf'
1073 service_integrated_vtysh_config
= True
1075 if os
.path
.isfile(vtysh_filename
):
1076 with
open(vtysh_filename
, 'r') as fh
:
1077 for line
in fh
.readlines():
1080 if line
== 'no service integrated-vtysh-config':
1081 service_integrated_vtysh_config
= False
1084 if not service_integrated_vtysh_config
:
1085 msg
= "'service integrated-vtysh-config' is not configured, this is required for 'service frr reload'"
1091 log
.setLevel(logging
.DEBUG
)
1093 log
.info('Called via "%s"', str(args
))
1095 # Create a Config object from the config generated by newconf
1097 newconf
.load_from_file(args
.filename
)
1102 # Create a Config object from the running config
1106 running
.load_from_file(args
.input)
1108 running
.load_from_show_running()
1110 (lines_to_add
, lines_to_del
) = compare_context_objects(newconf
, running
)
1111 lines_to_configure
= []
1114 print "\nLines To Delete"
1115 print "==============="
1117 for (ctx_keys
, line
) in lines_to_del
:
1122 cmd
= line_for_vtysh_file(ctx_keys
, line
, True)
1123 lines_to_configure
.append(cmd
)
1127 print "\nLines To Add"
1128 print "============"
1130 for (ctx_keys
, line
) in lines_to_add
:
1135 cmd
= line_for_vtysh_file(ctx_keys
, line
, False)
1136 lines_to_configure
.append(cmd
)
1141 # We will not be able to do anything, go ahead and exit(1)
1142 if not vtysh_config_available():
1145 log
.debug('New Frr Config\n%s', newconf
.get_lines())
1147 # This looks a little odd but we have to do this twice...here is why
1148 # If the user had this running bgp config:
1151 # neighbor 1.1.1.1 remote-as 50
1152 # neighbor 1.1.1.1 route-map FOO out
1154 # and this config in the newconf config file
1157 # neighbor 1.1.1.1 remote-as 999
1158 # neighbor 1.1.1.1 route-map FOO out
1161 # Then the script will do
1162 # - no neighbor 1.1.1.1 remote-as 50
1163 # - neighbor 1.1.1.1 remote-as 999
1165 # The problem is the "no neighbor 1.1.1.1 remote-as 50" will also remove
1166 # the "neighbor 1.1.1.1 route-map FOO out" line...so we compare the
1167 # configs again to put this line back.
1169 # There are many keywords in FRR that can only appear one time under
1170 # a context, take "bgp router-id" for example. If the config that we are
1171 # reloading against has the following:
1174 # bgp router-id 1.1.1.1
1175 # bgp router-id 2.2.2.2
1177 # The final config needs to contain "bgp router-id 2.2.2.2". On the
1178 # first pass we will add "bgp router-id 2.2.2.2" but then on the second
1179 # pass we will see that "bgp router-id 1.1.1.1" is missing and add that
1180 # back which cancels out the "bgp router-id 2.2.2.2". The fix is for the
1181 # second pass to include all of the "adds" from the first pass.
1182 lines_to_add_first_pass
= []
1186 running
.load_from_show_running()
1187 log
.debug('Running Frr Config (Pass #%d)\n%s', x
, running
.get_lines())
1189 (lines_to_add
, lines_to_del
) = compare_context_objects(newconf
, running
)
1192 lines_to_add_first_pass
= lines_to_add
1194 lines_to_add
.extend(lines_to_add_first_pass
)
1196 # Only do deletes on the first pass. The reason being if we
1197 # configure a bgp neighbor via "neighbor swp1 interface" FRR
1198 # will automatically add:
1201 # ipv6 nd ra-interval 10
1202 # no ipv6 nd suppress-ra
1205 # but those lines aren't in the config we are reloading against so
1206 # on the 2nd pass they will show up in lines_to_del. This could
1207 # apply to other scenarios as well where configuring FOO adds BAR
1209 if lines_to_del
and x
== 0:
1210 for (ctx_keys
, line
) in lines_to_del
:
1215 # 'no' commands are tricky, we can't just put them in a file and
1216 # vtysh -f that file. See the next comment for an explanation
1218 cmd
= line_to_vtysh_conft(ctx_keys
, line
, True)
1221 # Some commands in frr are picky about taking a "no" of the entire line.
1222 # OSPF is bad about this, you can't "no" the entire line, you have to "no"
1223 # only the beginning. If we hit one of these command an exception will be
1224 # thrown. Catch it and remove the last '-c', 'FOO' from cmd and try again.
1227 # frr(config-if)# ip ospf authentication message-digest 1.1.1.1
1228 # frr(config-if)# no ip ospf authentication message-digest 1.1.1.1
1229 # % Unknown command.
1230 # frr(config-if)# no ip ospf authentication message-digest
1231 # % Unknown command.
1232 # frr(config-if)# no ip ospf authentication
1237 _
= subprocess
.check_output(cmd
, stderr
=subprocess
.STDOUT
)
1239 except subprocess
.CalledProcessError
:
1241 # - Pull the last entry from cmd (this would be
1242 # 'no ip ospf authentication message-digest 1.1.1.1' in
1244 # - Split that last entry by whitespace and drop the last word
1245 log
.info('Failed to execute %s', ' '.join(cmd
))
1246 last_arg
= cmd
[-1].split(' ')
1248 if len(last_arg
) <= 2:
1249 log
.error('"%s" we failed to remove this command', original_cmd
)
1252 new_last_arg
= last_arg
[0:-1]
1253 cmd
[-1] = ' '.join(new_last_arg
)
1255 log
.info('Executed "%s"', ' '.join(cmd
))
1259 lines_to_configure
= []
1261 for (ctx_keys
, line
) in lines_to_add
:
1266 cmd
= line_for_vtysh_file(ctx_keys
, line
, False)
1267 lines_to_configure
.append(cmd
)
1269 if lines_to_configure
:
1270 random_string
= ''.join(random
.SystemRandom().choice(
1271 string
.ascii_uppercase
+
1272 string
.digits
) for _
in range(6))
1274 filename
= "/var/run/frr/reload-%s.txt" % random_string
1275 log
.info("%s content\n%s" % (filename
, pformat(lines_to_configure
)))
1277 with
open(filename
, 'w') as fh
:
1278 for line
in lines_to_configure
:
1279 fh
.write(line
+ '\n')
1282 subprocess
.check_output(['/usr/bin/vtysh', '-f', filename
], stderr
=subprocess
.STDOUT
)
1283 except subprocess
.CalledProcessError
as e
:
1284 log
.warning("frr-reload.py failed due to\n%s" % e
.output
)
1288 # Make these changes persistent
1289 if args
.overwrite
or args
.filename
!= '/etc/frr/frr.conf':
1290 subprocess
.call(['/usr/bin/vtysh', '-c', 'write'])