From 139662ee08ede477fc5b1236d102bf49b41a6232 Mon Sep 17 00:00:00 2001 From: Sam Tannous Date: Tue, 16 Jun 2015 20:58:15 -0400 Subject: [PATCH] Enhance globbing in ifupdown2 to support breakout cables Ticket: CM-5365 Reviewed By: roopa,dwalton Testing Done: Ran regression suite and hand tested. This patch extends the globbing support in ifupdown2 to handle two levels of square brackets so that breakout cables can be handled (e.g. swp[2-7]s[0-3]). Also modified the user's guide and the man pages. --- docs/source/userguide.rst | 4 ++ ifupdownaddons/modulebase.py | 73 ++++++++++++++++-------- man.rst/ifupdown-addons-interfaces.5.rst | 2 + 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/docs/source/userguide.rst b/docs/source/userguide.rst index c06e9d2..3e86206 100644 --- a/docs/source/userguide.rst +++ b/docs/source/userguide.rst @@ -317,6 +317,10 @@ bridge ports and bond slaves:: iface br1 bridge-ports glob swp7-9.100 swp11.100 glob swp15-18.100 + auto br2 + iface br2 + bridge-ports glob swp[1-6]s[0-3].100 + Using Templates =============== diff --git a/ifupdownaddons/modulebase.py b/ifupdownaddons/modulebase.py index 47df790..e44c0e6 100644 --- a/ifupdownaddons/modulebase.py +++ b/ifupdownaddons/modulebase.py @@ -148,33 +148,58 @@ class moduleBase(object): def parse_glob(self, expr): errmsg = ('error parsing glob expression \'%s\'' %expr + - ' (supported glob syntax: swp1-10 or swp[1-10])') - start_index = 0 - end_index = 0 - try: - regexs = [re.compile(r"([A-Za-z0-9\-]+[A-Za-z])(\d+)\-(\d+)(.*)"), - re.compile(r"([A-Za-z0-9\-]+)\[(\d+)\-(\d+)\](.*)")] - for r in regexs: - m = r.match(expr) - if not m: - continue - mlist = m.groups() - if len(mlist) != 4: - raise Exception(errmsg + '(unexpected len)') - prefix = mlist[0] - suffix = mlist[3] - start_index = int(mlist[1]) - end_index = int(mlist[2]) - except: - self.logger.warn(errmsg) - pass - if not start_index and not end_index: - self.logger.warn(errmsg) - yield expr - else: + ' (supported glob syntax: swp1-10.300 or swp[1-10].300' + + ' or swp[1-10]sub[0-4].300') + + # explanations are shown below in each if clause + regexs = [re.compile(r"([A-Za-z0-9\-]+)\[(\d+)\-(\d+)\]([A-Za-z0-9\-]+)\[(\d+)\-(\d+)\](.*)"), + re.compile(r"([A-Za-z0-9\-]+[A-Za-z])(\d+)\-(\d+)(.*)"), + re.compile(r"([A-Za-z0-9\-]+)\[(\d+)\-(\d+)\](.*)")] + + if regexs[0].match(expr): + # the first regex checks for exactly two levels of ranges defined only with square brackets + # (e.g. swpxyz[10-23]subqwe[0-4].100) to handle naming with two levels of port names. + m = regexs[0].match(expr) + mlist = m.groups() + if len(mlist) < 7: + # we have problems and should not continue + raise Exception('Error: unhandled glob expression %s\n%s' % (expr,errmsg)) + + prefix = mlist[0] + suffix = mlist[6] + start_index = int(mlist[1]) + end_index = int(mlist[2]) + sub_string = mlist[3] + start_sub = int(mlist[4]) + end_sub = int(mlist[5]) + for i in range(start_index, end_index + 1): + for j in range(start_sub, end_sub + 1): + yield prefix + '%d%s%d' % (i,sub_string,j) + suffix + + elif regexs[1].match(expr) or regexs[2].match(expr): + # the second regex for 1 level with a range (e.g. swp10-14.100 + # the third regex checks for 1 level with [] (e.g. swp[10-14].100) + start_index = 0 + end_index = 0 + if regexs[1].match(expr): + m = regexs[1].match(expr) + else: + m = regexs[2].match(expr) + mlist = m.groups() + if len(mlist) != 4: + raise Exception(errmsg + '(unexpected len)') + prefix = mlist[0] + suffix = mlist[3] + start_index = int(mlist[1]) + end_index = int(mlist[2]) for i in range(start_index, end_index + 1): yield prefix + '%d' %i + suffix + else: + # Could not match anything. + self.logger.warn('Warning: could not glob properly with %s\n%s' % (expr,errmsg)) + yield expr + def parse_port_list(self, port_expr, ifacenames=None): """ parse port list containing glob and regex diff --git a/man.rst/ifupdown-addons-interfaces.5.rst b/man.rst/ifupdown-addons-interfaces.5.rst index 82dcfdf..6c0ff78 100644 --- a/man.rst/ifupdown-addons-interfaces.5.rst +++ b/man.rst/ifupdown-addons-interfaces.5.rst @@ -321,6 +321,8 @@ EXAMPLES bridge-ports glob swp1-3.100 + bridge-ports glob swp[1-3]s[0-4].100 + bridge-ports regex (swp[1|2|3].100)