1
0
mirror of https://github.com/CumulusNetworks/ifupdown2.git synced 2024-05-06 15:54:50 +00:00

addons: batman_adv: import and IO api refactoring

The new code base supports installation via pypi so we need to update the
imports statement.
It's also good practice to use the existing IO apis to read/write and execute
sub-commands, those API will do error handling and logging.

Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
This commit is contained in:
Julien Fortin
2018-12-19 07:14:15 +01:00
parent b1a2d2417b
commit 8d8cd4f438

View File

@ -4,11 +4,21 @@
# Author: Maximilian Wilhelm, max@sdn.clinic # Author: Maximilian Wilhelm, max@sdn.clinic
# #
from ifupdown.iface import * try:
from ifupdownaddons.modulebase import moduleBase from ifupdown2.ifupdown.iface import *
from ifupdownaddons.iproute2 import iproute2 from ifupdown2.ifupdown.utils import utils
from ifupdown.netlink import netlink from ifupdown2.ifupdownaddons.modulebase import moduleBase
import ifupdown.ifupdownflags as ifupdownflags from ifupdown2.ifupdownaddons.LinkUtils import LinkUtils
from ifupdown2.ifupdown.netlink import netlink
import ifupdown2.ifupdown.ifupdownflags as ifupdownflags
except:
from ifupdown.iface import *
from ifupdown.utils import utils
from ifupdownaddons.modulebase import moduleBase
from ifupdownaddons.LinkUtils import LinkUtils
from ifupdown.netlink import netlink
import ifupdown.ifupdownflags as ifupdownflags
import logging import logging
import re import re
import subprocess import subprocess
@ -123,8 +133,7 @@ class batman_adv (moduleBase):
attr_file_name = self._batman_attrs[attr]['filename'] attr_file_name = self._batman_attrs[attr]['filename']
attr_file_path = "/sys/class/net/%s/mesh/%s" % (ifaceobj.name, attr_file_name) attr_file_path = "/sys/class/net/%s/mesh/%s" % (ifaceobj.name, attr_file_name)
try: try:
with open (attr_file_path, "r") as fh: return self.read_file_oneline(attr_file_path)
return fh.readline ().strip ()
except IOError as i: except IOError as i:
raise Exception ("_read_current_batman_attr (%s) %s" % (attr, i)) raise Exception ("_read_current_batman_attr (%s) %s" % (attr, i))
except ValueError: except ValueError:
@ -138,8 +147,7 @@ class batman_adv (moduleBase):
attr_file_name = self._batman_attrs[attr]['filename'] attr_file_name = self._batman_attrs[attr]['filename']
attr_file_path = "/sys/class/net/%s/mesh/%s" % (ifaceobj.name, attr_file_name) attr_file_path = "/sys/class/net/%s/mesh/%s" % (ifaceobj.name, attr_file_name)
try: try:
with open (attr_file_path, "w") as fh: self.write_file(attr_file_path, "%s\n" % value)
fh.write ("%s\n" % value)
except IOError as i: except IOError as i:
raise Exception ("_set_batman_attr (%s): %s" % (attr, i)) raise Exception ("_set_batman_attr (%s): %s" % (attr, i))
@ -150,7 +158,7 @@ class batman_adv (moduleBase):
try: try:
self.logger.debug ("Running batctl -m %s if %s %s" % (bat_iface, op, mesh_iface)) self.logger.debug ("Running batctl -m %s if %s %s" % (bat_iface, op, mesh_iface))
batctl_output = subprocess.check_output (["batctl", "-m", bat_iface, "if", op, mesh_iface], stderr = subprocess.STDOUT) utils.exec_commandl(["batctl", "-m", bat_iface, "if", op, mesh_iface])
except subprocess.CalledProcessError as c: except subprocess.CalledProcessError as c:
raise Exception ("Command \"batctl -m %s if %s %s\" failed: %s" % (bat_iface, op, mesh_iface, c.output)) raise Exception ("Command \"batctl -m %s if %s %s\" failed: %s" % (bat_iface, op, mesh_iface, c.output))
except Exception as e: except Exception as e:
@ -160,6 +168,7 @@ class batman_adv (moduleBase):
def _find_member_ifaces (self, ifaceobj, ignore = True): def _find_member_ifaces (self, ifaceobj, ignore = True):
members = [] members = []
iface_ignore_re = self._get_batman_ifaces_ignore_regex (ifaceobj) iface_ignore_re = self._get_batman_ifaces_ignore_regex (ifaceobj)
self.logger.info("batman: executing: %s" % " ".join(["batctl", "-m", ifaceobj.name, "if"]))
batctl_fh = subprocess.Popen (["batctl", "-m", ifaceobj.name, "if"], bufsize = 4194304, stdout = subprocess.PIPE).stdout batctl_fh = subprocess.Popen (["batctl", "-m", ifaceobj.name, "if"], bufsize = 4194304, stdout = subprocess.PIPE).stdout
for line in batctl_fh.readlines (): for line in batctl_fh.readlines ():
iface = line.split (':')[0] iface = line.split (':')[0]
@ -310,7 +319,7 @@ class batman_adv (moduleBase):
def _init_command_handlers (self): def _init_command_handlers (self):
if not self.ipcmd: if not self.ipcmd:
self.ipcmd = iproute2() self.ipcmd = LinkUtils()
def run (self, ifaceobj, operation, query_ifaceobj = None, **extra_args): def run (self, ifaceobj, operation, query_ifaceobj = None, **extra_args):