further removal and refactoring of methods used within misc/utils.py

This commit is contained in:
Blake J. Harnden 2018-03-02 09:15:52 -08:00
parent 6211b09585
commit 00b3c97448
23 changed files with 181 additions and 293 deletions

View file

@ -86,4 +86,4 @@ def ngloadkernelmodule(name):
:param str name: module name :param str name: module name
:return: nothing :return: nothing
""" """
utils.mutecall(["kldload", name]) utils.check_cmd(["kldload", name])

View file

@ -263,7 +263,7 @@ class SimpleJailNode(PyCoreNode):
self.attachnet(ifindex, net) self.attachnet(ifindex, net)
if hwaddr: if hwaddr:
self.sethwaddr(ifindex, hwaddr) self.sethwaddr(ifindex, hwaddr)
for addr in utils.maketuple(addrlist): for addr in utils.make_tuple(addrlist):
self.addaddr(ifindex, addr) self.addaddr(ifindex, addr)
self.ifup(ifindex) self.ifup(ifindex)
return ifindex return ifindex

View file

@ -93,7 +93,7 @@ class CoreRequestHandler(SocketServer.BaseRequestHandler):
self.master = False self.master = False
self.session = None self.session = None
utils.closeonexec(request.fileno()) utils.close_onexec(request.fileno())
SocketServer.BaseRequestHandler.__init__(self, request, client_address, server) SocketServer.BaseRequestHandler.__init__(self, request, client_address, server)
def setup(self): def setup(self):
@ -371,7 +371,7 @@ class CoreRequestHandler(SocketServer.BaseRequestHandler):
try: try:
header = self.request.recv(coreapi.CoreMessage.header_len) header = self.request.recv(coreapi.CoreMessage.header_len)
if len(header) > 0: if len(header) > 0:
logger.debug("received message header: %s", utils.hexdump(header)) logger.debug("received message header: %s", utils.hex_dump(header))
except IOError as e: except IOError as e:
raise IOError("error receiving header (%s)" % e) raise IOError("error receiving header (%s)" % e)
@ -388,7 +388,7 @@ class CoreRequestHandler(SocketServer.BaseRequestHandler):
data = "" data = ""
while len(data) < message_len: while len(data) < message_len:
data += self.request.recv(message_len - len(data)) data += self.request.recv(message_len - len(data))
logger.debug("received message data: %s" % utils.hexdump(data)) logger.debug("received message data: %s" % utils.hex_dump(data))
if len(data) > message_len: if len(data) > message_len:
error_message = "received message length does not match received data (%s != %s)" % ( error_message = "received message length does not match received data (%s != %s)" % (
len(data), message_len) len(data), message_len)
@ -1110,8 +1110,7 @@ class CoreRequestHandler(SocketServer.BaseRequestHandler):
# execute the command with no response # execute the command with no response
else: else:
if message.flags & MessageFlags.LOCAL.value: if message.flags & MessageFlags.LOCAL.value:
# TODO: get this consolidated into utils utils.mute_detach(command)
utils.mutedetach(shlex.split(command))
else: else:
node.cmd(command, wait=False) node.cmd(command, wait=False)
except KeyError: except KeyError:

View file

@ -182,7 +182,7 @@ class EmaneManager(ConfigurableManager):
if self.service: if self.service:
for f in self.service._readFd, self.service._writeFd, self.service._socket, self.service._socketOTA: for f in self.service._readFd, self.service._writeFd, self.service._socket, self.service._socketOTA:
if f: if f:
utils.closeonexec(f) utils.close_onexec(f)
if filename is not None: if filename is not None:
os.environ.pop(EmaneManager.EVENTCFGVAR) os.environ.pop(EmaneManager.EVENTCFGVAR)

View file

@ -190,7 +190,7 @@ class EmaneModel(WirelessModel):
multiple values. multiple values.
""" """
try: try:
values = utils.maketuplefromstr(value, str) values = utils.make_tuple_fromstr(value, str)
except SyntaxError: except SyntaxError:
logger.exception("error in value string to param list") logger.exception("error in value string to param list")
return None return None

View file

@ -136,7 +136,7 @@ router ospf6
:param routerid: router id :param routerid: router id
:param str redistribute: redistribute value :param str redistribute: redistribute value
""" """
ospf6ifs = utils.maketuple(ospf6ifs) ospf6ifs = utils.make_tuple(ospf6ifs)
interfaces = "\n!\n".join(map(str, ospf6ifs)) interfaces = "\n!\n".join(map(str, ospf6ifs))
ospfifs = "\n ".join(map(lambda x: "interface %s area %s" % (x.name(), area), ospf6ifs)) ospfifs = "\n ".join(map(lambda x: "interface %s area %s" % (x.name(), area), ospf6ifs))
Conf.__init__(self, interfaces=interfaces, routerid=routerid, ospfifs=ospfifs, redistribute=redistribute) Conf.__init__(self, interfaces=interfaces, routerid=routerid, ospfifs=ospfifs, redistribute=redistribute)
@ -163,9 +163,9 @@ $forwarding
:param str logfile: log file name :param str logfile: log file name
:param debugs: debug options :param debugs: debug options
""" """
routers = "\n!\n".join(map(str, utils.maketuple(routers))) routers = "\n!\n".join(map(str, utils.make_tuple(routers)))
if debugs: if debugs:
debugs = "\n".join(utils.maketuple(debugs)) debugs = "\n".join(utils.make_tuple(debugs))
else: else:
debugs = "! no debugs" debugs = "! no debugs"
forwarding = "ip forwarding\nipv6 forwarding" forwarding = "ip forwarding\nipv6 forwarding"

View file

@ -17,7 +17,74 @@ from core import logger
DEVNULL = open(os.devnull, "wb") DEVNULL = open(os.devnull, "wb")
def closeonexec(fd): def _detach_init():
"""
Fork a child process and exit.
:return: nothing
"""
if os.fork():
# parent exits
os._exit(0)
os.setsid()
def _valid_module(path, file_name):
"""
Check if file is a valid python module.
:param str path: path to file
:param str file_name: file name to check
:return: True if a valid python module file, False otherwise
:rtype: bool
"""
file_path = os.path.join(path, file_name)
if not os.path.isfile(file_path):
return False
if file_name.startswith("_"):
return False
if not file_name.endswith(".py"):
return False
return True
def _is_class(module, member, clazz):
"""
Validates if a module member is a class and an instance of a CoreService.
:param module: module to validate for service
:param member: member to validate for service
:param clazz: clazz type to check for validation
:return: True if a valid service, False otherwise
:rtype: bool
"""
if not inspect.isclass(member):
return False
if not issubclass(member, clazz):
return False
if member.__module__ != module.__name__:
return False
return True
def _is_exe(file_path):
"""
Check if a given file path exists and is an executable file.
:param str file_path: file path to check
:return: True if the file is considered and executable file, False otherwise
:rtype: bool
"""
return os.path.isfile(file_path) and os.access(file_path, os.X_OK)
def close_onexec(fd):
""" """
Close on execution of a shell process. Close on execution of a shell process.
@ -37,56 +104,11 @@ def check_executables(executables):
:raises EnvironmentError: when an executable doesn't exist or is not executable :raises EnvironmentError: when an executable doesn't exist or is not executable
""" """
for executable in executables: for executable in executables:
if not is_exe(executable): if not _is_exe(executable):
raise EnvironmentError("executable not found: %s" % executable) raise EnvironmentError("executable not found: %s" % executable)
def is_exe(file_path): def make_tuple(obj):
"""
Check if a given file path exists and is an executable file.
:param str file_path: file path to check
:return: True if the file is considered and executable file, False otherwise
:rtype: bool
"""
return os.path.isfile(file_path) and os.access(file_path, os.X_OK)
def which(program):
"""
From: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
:param str program: program to check for
:return: path if it exists, none otherwise
"""
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip("\"")
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def ensurepath(pathlist):
"""
Checks a list of paths are contained within the environment path, if not add it to the path.
:param list[str] pathlist: list of paths to check
:return: nothing
"""
searchpath = os.environ["PATH"].split(":")
for p in set(pathlist):
if p not in searchpath:
os.environ["PATH"] += ":" + p
def maketuple(obj):
""" """
Create a tuple from an object, or return the object itself. Create a tuple from an object, or return the object itself.
@ -100,7 +122,7 @@ def maketuple(obj):
return obj, return obj,
def maketuplefromstr(s, value_type): def make_tuple_fromstr(s, value_type):
""" """
Create a tuple from a string. Create a tuple from a string.
@ -122,101 +144,22 @@ def split_args(args):
:return: shell-like syntax list :return: shell-like syntax list
:rtype: list :rtype: list
""" """
# split shell string to shell array for convenience
if type(args) == str: if type(args) == str:
args = shlex.split(args) args = shlex.split(args)
return args return args
def mutecall(*args, **kwargs): def mute_detach(args, **kwargs):
"""
Run a muted call command.
:param list args: arguments for the command
:param dict kwargs: keyword arguments for the command
:return: command result
:rtype: int
"""
kwargs["stdout"] = DEVNULL
kwargs["stderr"] = subprocess.STDOUT
return subprocess.call(*args, **kwargs)
def mutecheck_call(*args, **kwargs):
"""
Run a muted check call command.
:param list args: arguments for the command
:param dict kwargs: keyword arguments for the command
:return: command result
:rtype: int
"""
kwargs["stdout"] = DEVNULL
kwargs["stderr"] = subprocess.STDOUT
return subprocess.check_call(*args, **kwargs)
def spawn(*args, **kwargs):
"""
Wrapper for running a spawn command and returning the process id.
:param list args: arguments for the command
:param dict kwargs: keyword arguments for the command
:return: process id of the command
:rtype: int
"""
return subprocess.Popen(*args, **kwargs).pid
def mutespawn(*args, **kwargs):
"""
Wrapper for running a muted spawned command.
:param list args: arguments for the command
:param dict kwargs: keyword arguments for the command
:return: process id of the command
:rtype: int
"""
kwargs["stdout"] = DEVNULL
kwargs["stderr"] = subprocess.STDOUT
return subprocess.Popen(*args, **kwargs).pid
def detachinit():
"""
Fork a child process and exit.
:return: nothing
"""
if os.fork():
# parent exits
os._exit(0)
os.setsid()
def detach(*args, **kwargs):
"""
Run a detached process by forking it.
:param list args: arguments for the command
:param dict kwargs: keyword arguments for the command
:return: process id of the command
:rtype: int
"""
kwargs["preexec_fn"] = detachinit
return subprocess.Popen(*args, **kwargs).pid
def mutedetach(*args, **kwargs):
""" """
Run a muted detached process by forking it. Run a muted detached process by forking it.
:param list args: arguments for the command :param list[str]|str args: arguments for the command
:param dict kwargs: keyword arguments for the command :param dict kwargs: keyword arguments for the command
:return: process id of the command :return: process id of the command
:rtype: int :rtype: int
""" """
kwargs["preexec_fn"] = detachinit args = split_args(args)
kwargs["preexec_fn"] = _detach_init
kwargs["stdout"] = DEVNULL kwargs["stdout"] = DEVNULL
kwargs["stderr"] = subprocess.STDOUT kwargs["stderr"] = subprocess.STDOUT
return subprocess.Popen(*args, **kwargs).pid return subprocess.Popen(*args, **kwargs).pid
@ -286,7 +229,7 @@ def check_cmd(args, **kwargs):
raise subprocess.CalledProcessError(-1, args) raise subprocess.CalledProcessError(-1, args)
def hexdump(s, bytes_per_word=2, words_per_line=8): def hex_dump(s, bytes_per_word=2, words_per_line=8):
""" """
Hex dump of a string. Hex dump of a string.
@ -297,10 +240,11 @@ def hexdump(s, bytes_per_word=2, words_per_line=8):
""" """
dump = "" dump = ""
count = 0 count = 0
bytes = bytes_per_word * words_per_line total_bytes = bytes_per_word * words_per_line
while s: while s:
line = s[:bytes] line = s[:total_bytes]
s = s[bytes:] s = s[total_bytes:]
tmp = map(lambda x: ("%02x" * bytes_per_word) % x, tmp = map(lambda x: ("%02x" * bytes_per_word) % x,
zip(*[iter(map(ord, line))] * bytes_per_word)) zip(*[iter(map(ord, line))] * bytes_per_word))
if len(line) % 2: if len(line) % 2:
@ -310,7 +254,7 @@ def hexdump(s, bytes_per_word=2, words_per_line=8):
return dump[:-1] return dump[:-1]
def filemunge(pathname, header, text): def file_munge(pathname, header, text):
""" """
Insert text at the end of a file, surrounded by header comments. Insert text at the end of a file, surrounded by header comments.
@ -320,15 +264,15 @@ def filemunge(pathname, header, text):
:return: nothing :return: nothing
""" """
# prevent duplicates # prevent duplicates
filedemunge(pathname, header) file_demunge(pathname, header)
f = open(pathname, "a")
f.write("# BEGIN %s\n" % header) with open(pathname, "a") as append_file:
f.write(text) append_file.write("# BEGIN %s\n" % header)
f.write("# END %s\n" % header) append_file.write(text)
f.close() append_file.write("# END %s\n" % header)
def filedemunge(pathname, header): def file_demunge(pathname, header):
""" """
Remove text that was inserted in a file surrounded by header comments. Remove text that was inserted in a file surrounded by header comments.
@ -336,25 +280,27 @@ def filedemunge(pathname, header):
:param str header: header text to target for removal :param str header: header text to target for removal
:return: nothing :return: nothing
""" """
f = open(pathname, "r") with open(pathname, "r") as read_file:
lines = f.readlines() lines = read_file.readlines()
f.close()
start = None start = None
end = None end = None
for i in range(len(lines)): for i in range(len(lines)):
if lines[i] == "# BEGIN %s\n" % header: if lines[i] == "# BEGIN %s\n" % header:
start = i start = i
elif lines[i] == "# END %s\n" % header: elif lines[i] == "# END %s\n" % header:
end = i + 1 end = i + 1
if start is None or end is None: if start is None or end is None:
return return
f = open(pathname, "w")
lines = lines[:start] + lines[end:] with open(pathname, "w") as write_file:
f.write("".join(lines)) lines = lines[:start] + lines[end:]
f.close() write_file.write("".join(lines))
def expandcorepath(pathname, session=None, node=None): def expand_corepath(pathname, session=None, node=None):
""" """
Expand a file path given session information. Expand a file path given session information.
@ -369,13 +315,15 @@ def expandcorepath(pathname, session=None, node=None):
pathname = pathname.replace("%SESSION%", str(session.session_id)) pathname = pathname.replace("%SESSION%", str(session.session_id))
pathname = pathname.replace("%SESSION_DIR%", session.session_dir) pathname = pathname.replace("%SESSION_DIR%", session.session_dir)
pathname = pathname.replace("%SESSION_USER%", session.user) pathname = pathname.replace("%SESSION_USER%", session.user)
if node is not None: if node is not None:
pathname = pathname.replace("%NODE%", str(node.objid)) pathname = pathname.replace("%NODE%", str(node.objid))
pathname = pathname.replace("%NODENAME%", node.name) pathname = pathname.replace("%NODENAME%", node.name)
return pathname return pathname
def sysctldevname(devname): def sysctl_devname(devname):
""" """
Translate a device name to the name used with sysctl. Translate a device name to the name used with sysctl.
@ -477,7 +425,7 @@ def daemonize(rootdir="/", umask=0, close_fds=False, dontclose=(),
logger.exception("error closing file descriptor") logger.exception("error closing file descriptor")
def readfileintodict(filename, d): def load_config(filename, d):
""" """
Read key=value pairs from a file, into a dict. Skip comments; strip newline characters and spacing. Read key=value pairs from a file, into a dict. Skip comments; strip newline characters and spacing.
@ -487,78 +435,18 @@ def readfileintodict(filename, d):
""" """
with open(filename, "r") as f: with open(filename, "r") as f:
lines = f.readlines() lines = f.readlines()
for l in lines:
if l[:1] == "#": for line in lines:
if line[:1] == "#":
continue continue
try: try:
key, value = l.split("=", 1) key, value = line.split("=", 1)
d[key] = value.strip() d[key] = value.strip()
except ValueError: except ValueError:
logger.exception("error reading file to dict: %s", filename) logger.exception("error reading file to dict: %s", filename)
def checkforkernelmodule(name):
"""
Return a string if a Linux kernel module is loaded, None otherwise.
The string is the line from /proc/modules containing the module name,
memory size (bytes), number of loaded instances, dependencies, state,
and kernel memory offset.
:param str name: name of kernel module to check for
:return: kernel module line, None otherwise
:rtype: str
"""
with open("/proc/modules", "r") as f:
for line in f:
if line.startswith(name + " "):
return line.rstrip()
return None
def _valid_module(path, file_name):
"""
Check if file is a valid python module.
:param str path: path to file
:param str file_name: file name to check
:return: True if a valid python module file, False otherwise
:rtype: bool
"""
file_path = os.path.join(path, file_name)
if not os.path.isfile(file_path):
return False
if file_name.startswith("_"):
return False
if not file_name.endswith(".py"):
return False
return True
def _is_class(module, member, clazz):
"""
Validates if a module member is a class and an instance of a CoreService.
:param module: module to validate for service
:param member: member to validate for service
:param clazz: clazz type to check for validation
:return: True if a valid service, False otherwise
:rtype: bool
"""
if not inspect.isclass(member):
return False
if not issubclass(member, clazz):
return False
if member.__module__ != module.__name__:
return False
return True
def load_classes(path, clazz): def load_classes(path, clazz):
""" """
Dynamically load classes for use within CORE. Dynamically load classes for use within CORE.

View file

@ -487,7 +487,7 @@ class RJ45Node(PyCoreNode, PyCoreNetIf):
try: try:
utils.check_cmd([constants.IP_BIN, "link", "set", self.localname, "down"]) utils.check_cmd([constants.IP_BIN, "link", "set", self.localname, "down"])
utils.check_cmd([constants.IP_BIN, "addr", "flush", "dev", self.localname]) utils.check_cmd([constants.IP_BIN, "addr", "flush", "dev", self.localname])
utils.mutecall([constants.TC_BIN, "qdisc", "del", "dev", self.localname, "root"]) utils.check_cmd([constants.TC_BIN, "qdisc", "del", "dev", self.localname, "root"])
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
logger.exception("error shutting down: %s", e.output) logger.exception("error shutting down: %s", e.output)
@ -541,7 +541,7 @@ class RJ45Node(PyCoreNode, PyCoreNetIf):
self.attachnet(net) self.attachnet(net)
if addrlist: if addrlist:
for addr in utils.maketuple(addrlist): for addr in utils.make_tuple(addrlist):
self.addaddr(addr) self.addaddr(addr)
return ifindex return ifindex

View file

@ -36,12 +36,9 @@ utils.check_executables([
def ebtables_commands(call, commands): def ebtables_commands(call, commands):
ebtables_lock.acquire() with ebtables_lock:
try:
for command in commands: for command in commands:
call(command) call(command)
finally:
ebtables_lock.release()
class OvsNet(PyCoreNet): class OvsNet(PyCoreNet):
@ -109,16 +106,18 @@ class OvsNet(PyCoreNet):
ebtables_queue.stopupdateloop(self) ebtables_queue.stopupdateloop(self)
utils.mutecall([constants.IP_BIN, "link", "set", self.bridge_name, "down"]) try:
utils.mutecall([constants.OVS_BIN, "del-br", self.bridge_name]) utils.check_cmd([constants.IP_BIN, "link", "set", self.bridge_name, "down"])
utils.check_cmd([constants.OVS_BIN, "del-br", self.bridge_name])
ebtables_commands(utils.mutecall, [ ebtables_commands(utils.check_cmd, [
[constants.EBTABLES_BIN, "-D", "FORWARD", "--logical-in", self.bridge_name, "-j", self.bridge_name], [constants.EBTABLES_BIN, "-D", "FORWARD", "--logical-in", self.bridge_name, "-j", self.bridge_name],
[constants.EBTABLES_BIN, "-X", self.bridge_name] [constants.EBTABLES_BIN, "-X", self.bridge_name]
]) ])
except subprocess.CalledProcessError as e:
logger.exception("error bringing bridge down and removing it: %s", e.output)
# removes veth pairs used for bridge-to-bridge connections
for interface in self.netifs(): for interface in self.netifs():
# removes veth pairs used for bridge-to-bridge connections
interface.shutdown() interface.shutdown()
self._netif.clear() self._netif.clear()

View file

@ -67,7 +67,7 @@ class VEth(PyCoreNetIf):
logger.exception("error shutting down interface") logger.exception("error shutting down interface")
if self.localname: if self.localname:
utils.mutedetach([constants.IP_BIN, "link", "delete", self.localname]) utils.mute_detach([constants.IP_BIN, "link", "delete", self.localname])
self.up = False self.up = False
@ -164,8 +164,8 @@ class TunTap(PyCoreNetIf):
""" """
def localdevexists(): def localdevexists():
args = (constants.IP_BIN, "link", "show", self.localname) args = [constants.IP_BIN, "link", "show", self.localname]
return utils.mutecall(args) return utils.cmd(args)
self.waitfor(localdevexists) self.waitfor(localdevexists)
@ -269,7 +269,7 @@ class GreTap(PyCoreNetIf):
if remoteip is None: if remoteip is None:
raise ValueError, "missing remote IP required for GRE TAP device" raise ValueError, "missing remote IP required for GRE TAP device"
args = ("ip", "link", "add", self.localname, "type", "gretap", args = ("ip", "link", "add", self.localname, "type", "gretap",
"remote", str(remoteip)) "remote", str(remoteip))
if localip: if localip:
args += ("local", str(localip)) args += ("local", str(localip))
if ttl: if ttl:

View file

@ -313,15 +313,21 @@ class LxBrNet(PyCoreNet):
if not self.up: if not self.up:
return return
ebq.stopupdateloop(self) ebq.stopupdateloop(self)
utils.mutecall([constants.IP_BIN, "link", "set", self.brname, "down"])
utils.mutecall([constants.BRCTL_BIN, "delbr", self.brname]) try:
ebtablescmds(utils.mutecall, [ utils.check_cmd([constants.IP_BIN, "link", "set", self.brname, "down"])
[constants.EBTABLES_BIN, "-D", "FORWARD", utils.check_cmd([constants.BRCTL_BIN, "delbr", self.brname])
"--logical-in", self.brname, "-j", self.brname], ebtablescmds(utils.check_cmd, [
[constants.EBTABLES_BIN, "-X", self.brname]]) [constants.EBTABLES_BIN, "-D", "FORWARD", "--logical-in", self.brname, "-j", self.brname],
[constants.EBTABLES_BIN, "-X", self.brname]
])
except subprocess.CalledProcessError as e:
logger.exception("error during shutdown: %s", e.output)
# removes veth pairs used for bridge-to-bridge connections
for netif in self.netifs(): for netif in self.netifs():
# removes veth pairs used for bridge-to-bridge connections
netif.shutdown() netif.shutdown()
self._netif.clear() self._netif.clear()
self._linked.clear() self._linked.clear()
del self.session del self.session

View file

@ -461,7 +461,7 @@ class SimpleLxcNode(PyCoreNode):
self.attachnet(ifindex, net) self.attachnet(ifindex, net)
netif = self.netif(ifindex) netif = self.netif(ifindex)
netif.sethwaddr(hwaddr) netif.sethwaddr(hwaddr)
for address in utils.maketuple(addrlist): for address in utils.make_tuple(addrlist):
netif.addaddr(address) netif.addaddr(address)
return ifindex return ifindex
else: else:
@ -473,7 +473,7 @@ class SimpleLxcNode(PyCoreNode):
if hwaddr: if hwaddr:
self.sethwaddr(ifindex, hwaddr) self.sethwaddr(ifindex, hwaddr)
for address in utils.maketuple(addrlist): for address in utils.make_tuple(addrlist):
self.addaddr(ifindex, address) self.addaddr(ifindex, address)
self.ifup(ifindex) self.ifup(ifindex)

View file

@ -158,7 +158,7 @@ class PhysicalNode(PyCoreNode):
netif.localname = netif.name netif.localname = netif.name
if hwaddr: if hwaddr:
self.sethwaddr(ifindex, hwaddr) self.sethwaddr(ifindex, hwaddr)
for addr in utils.maketuple(addrlist): for addr in utils.make_tuple(addrlist):
self.addaddr(ifindex, addr) self.addaddr(ifindex, addr)
if self.up: if self.up:
self.cmd([constants.IP_BIN, "link", "set", "dev", netif.localname, "up"]) self.cmd([constants.IP_BIN, "link", "set", "dev", netif.localname, "up"])

View file

@ -372,7 +372,7 @@ class CoreServices(ConfigurableManager):
if cfg[:7] == 'file://': if cfg[:7] == 'file://':
src = cfg[7:] src = cfg[7:]
src = src.split('\n')[0] src = src.split('\n')[0]
src = utils.expandcorepath(src, node.session, node) src = utils.expand_corepath(src, node.session, node)
# TODO: glob here # TODO: glob here
node.nodefilecopy(filename, src, mode=0644) node.nodefilecopy(filename, src, mode=0644)
return True return True
@ -968,7 +968,7 @@ class CoreService(object):
elif key == "meta": elif key == "meta":
value = str(value) value = str(value)
else: else:
value = utils.maketuplefromstr(value, str) value = utils.make_tuple_fromstr(value, str)
if key == "dirs": if key == "dirs":
self._dirs = value self._dirs = value

View file

@ -57,7 +57,7 @@ class MgenSinkService(NrlService):
def generateconfig(cls, node, filename, services): def generateconfig(cls, node, filename, services):
cfg = "0.0 LISTEN UDP 5000\n" cfg = "0.0 LISTEN UDP 5000\n"
for ifc in node.netifs(): for ifc in node.netifs():
name = utils.sysctldevname(ifc.name) name = utils.sysctl_devname(ifc.name)
cfg += "0.0 Join 224.225.1.2 INTERFACE %s\n" % name cfg += "0.0 Join 224.225.1.2 INTERFACE %s\n" % name
return cfg return cfg

View file

@ -60,7 +60,7 @@ class IPForwardService(UtilService):
%(sysctl)s -w net.ipv4.conf.default.rp_filter=0 %(sysctl)s -w net.ipv4.conf.default.rp_filter=0
""" % {'sysctl': constants.SYSCTL_BIN} """ % {'sysctl': constants.SYSCTL_BIN}
for ifc in node.netifs(): for ifc in node.netifs():
name = utils.sysctldevname(ifc.name) name = utils.sysctl_devname(ifc.name)
cfg += "%s -w net.ipv4.conf.%s.forwarding=1\n" % (constants.SYSCTL_BIN, name) cfg += "%s -w net.ipv4.conf.%s.forwarding=1\n" % (constants.SYSCTL_BIN, name)
cfg += "%s -w net.ipv4.conf.%s.send_redirects=0\n" % \ cfg += "%s -w net.ipv4.conf.%s.send_redirects=0\n" % \
(constants.SYSCTL_BIN, name) (constants.SYSCTL_BIN, name)

View file

@ -7,7 +7,6 @@ import atexit
import os import os
import pprint import pprint
import random import random
import shlex
import shutil import shutil
import subprocess import subprocess
import tempfile import tempfile
@ -520,7 +519,7 @@ class Session(object):
environment_config_file = os.path.join(constants.CORE_CONF_DIR, "environment") environment_config_file = os.path.join(constants.CORE_CONF_DIR, "environment")
try: try:
if os.path.isfile(environment_config_file): if os.path.isfile(environment_config_file):
utils.readfileintodict(environment_config_file, env) utils.load_config(environment_config_file, env)
except IOError: except IOError:
logger.warn("environment configuration file does not exist: %s", environment_config_file) logger.warn("environment configuration file does not exist: %s", environment_config_file)
@ -528,7 +527,7 @@ class Session(object):
if self.user: if self.user:
environment_user_file = os.path.join("/home", self.user, ".core", "environment") environment_user_file = os.path.join("/home", self.user, ".core", "environment")
try: try:
utils.readfileintodict(environment_user_file, env) utils.load_config(environment_user_file, env)
except IOError: except IOError:
logger.warn("error reading user core environment settings file: %s", environment_user_file) logger.warn("error reading user core environment settings file: %s", environment_user_file)
@ -1189,7 +1188,7 @@ class Session(object):
header = "CORE session %s host entries" % self.session_id header = "CORE session %s host entries" % self.session_id
if remove: if remove:
logger.info("Removing /etc/hosts file entries.") logger.info("Removing /etc/hosts file entries.")
utils.filedemunge("/etc/hosts", header) utils.file_demunge("/etc/hosts", header)
return return
entries = [] entries = []
@ -1200,7 +1199,7 @@ class Session(object):
logger.info("Adding %d /etc/hosts file entries." % len(entries)) logger.info("Adding %d /etc/hosts file entries." % len(entries))
utils.filemunge("/etc/hosts", header, "\n".join(entries) + "\n") utils.file_munge("/etc/hosts", header, "\n".join(entries) + "\n")
def runtime(self): def runtime(self):
""" """
@ -1254,9 +1253,7 @@ class Session(object):
logger.info("running event %s at time %s cmd=%s" % (name, now, data)) logger.info("running event %s at time %s cmd=%s" % (name, now, data))
if not node_id: if not node_id:
# TODO: look to consolidate shlex to utils utils.mute_detach(data)
commands = shlex.split(data)
utils.mutedetach(commands)
else: else:
node = self.get_object(node_id) node = self.get_object(node_id)
node.cmd(data, wait=False) node.cmd(data, wait=False)

View file

@ -237,7 +237,7 @@ class XenNode(PyCoreNode):
# unpause VM # unpause VM
logger.warn("XEN PVM boot() unpause domU %s", self.vmname) logger.warn("XEN PVM boot() unpause domU %s", self.vmname)
utils.mutecheck_call([XM_PATH, "unpause", self.vmname]) utils.check_cmd([XM_PATH, "unpause", self.vmname])
self.booted = True self.booted = True
finally: finally:
@ -261,7 +261,7 @@ class XenNode(PyCoreNode):
try: try:
# RJE XXX what to do here # RJE XXX what to do here
if self.booted: if self.booted:
utils.mutecheck_call([XM_PATH, "destroy", self.vmname]) utils.check_cmd([XM_PATH, "destroy", self.vmname])
self.booted = False self.booted = False
except (OSError, subprocess.CalledProcessError): except (OSError, subprocess.CalledProcessError):
# ignore this error too, the VM may have exited already # ignore this error too, the VM may have exited already
@ -272,9 +272,9 @@ class XenNode(PyCoreNode):
while os.path.exists(self.lvpath): while os.path.exists(self.lvpath):
try: try:
subprocess.check_call([UDEVADM_PATH, "settle"]) subprocess.check_call([UDEVADM_PATH, "settle"])
utils.mutecall([LVCHANGE_PATH, "-an", self.lvpath]) utils.check_cmd([LVCHANGE_PATH, "-an", self.lvpath])
lvm_remove_count += 1 lvm_remove_count += 1
utils.mutecall([LVREMOVE_PATH, "-f", self.lvpath]) utils.check_cmd([LVREMOVE_PATH, "-f", self.lvpath])
except OSError: except OSError:
logger.exception("error during shutdown") logger.exception("error during shutdown")
@ -296,8 +296,8 @@ class XenNode(PyCoreNode):
""" """
if os.path.exists(self.lvpath): if os.path.exists(self.lvpath):
raise Exception, "LVM volume already exists" raise Exception, "LVM volume already exists"
utils.mutecheck_call([LVCREATE_PATH, "--size", self.disksize, utils.check_cmd([LVCREATE_PATH, "--size", self.disksize,
"--name", self.lvname, self.vgname]) "--name", self.lvname, self.vgname])
def createpartitions(self): def createpartitions(self):
""" """
@ -337,8 +337,8 @@ class XenNode(PyCoreNode):
persistdev = "/dev/mapper/" + lines[0].strip().split(" ")[0].strip() persistdev = "/dev/mapper/" + lines[0].strip().split(" ")[0].strip()
swapdev = "/dev/mapper/" + lines[1].strip().split(" ")[0].strip() swapdev = "/dev/mapper/" + lines[1].strip().split(" ")[0].strip()
subprocess.check_call([KPARTX_PATH, "-a", self.lvpath]) subprocess.check_call([KPARTX_PATH, "-a", self.lvpath])
utils.mutecheck_call([MKFSEXT4_PATH, "-L", "persist", persistdev]) utils.check_cmd([MKFSEXT4_PATH, "-L", "persist", persistdev])
utils.mutecheck_call([MKSWAP_PATH, "-f", "-L", "swap", swapdev]) utils.check_cmd([MKSWAP_PATH, "-f", "-L", "swap", swapdev])
return persistdev return persistdev
def untarpersistent(self, tarname, iso): def untarpersistent(self, tarname, iso):
@ -431,7 +431,7 @@ class XenNode(PyCoreNode):
for action in ("poweroff", "reboot", "suspend", "crash", "halt"): for action in ("poweroff", "reboot", "suspend", "crash", "halt"):
args.append("on_%s=destroy" % action) args.append("on_%s=destroy" % action)
args.append("extra=" + self.getconfigitem("xm_create_extra")) args.append("extra=" + self.getconfigitem("xm_create_extra"))
utils.mutecheck_call(args) utils.check_cmd(args)
# from class LxcNode # from class LxcNode
def privatedir(self, path): def privatedir(self, path):
@ -710,7 +710,7 @@ class XenNode(PyCoreNode):
if hwaddr: if hwaddr:
self.sethwaddr(ifindex, hwaddr) self.sethwaddr(ifindex, hwaddr)
for addr in utils.maketuple(addrlist): for addr in utils.make_tuple(addrlist):
self.addaddr(ifindex, addr) self.addaddr(ifindex, addr)
# self.ifup(ifindex) # self.ifup(ifindex)
return ifindex return ifindex

View file

@ -18,7 +18,7 @@ from string import Template
from core.constants import QUAGGA_STATE_DIR from core.constants import QUAGGA_STATE_DIR
from core.misc import ipaddress from core.misc import ipaddress
from core.misc.utils import mutecall from core.misc.utils import check_cmd
from core.netns import nodes from core.netns import nodes
# this is the /etc/core/core.conf default # this is the /etc/core/core.conf default
@ -33,8 +33,7 @@ try:
if os.path.exists(os.path.join(p, "zebra")): if os.path.exists(os.path.join(p, "zebra")):
quagga_path = p quagga_path = p
break break
mutecall([os.path.join(quagga_path, "zebra"), check_cmd([os.path.join(quagga_path, "zebra"), "-u", "root", "-g", "root", "-v"])
"-u", "root", "-g", "root", "-v"])
except OSError: except OSError:
sys.stderr.write("ERROR: running zebra failed\n") sys.stderr.write("ERROR: running zebra failed\n")
sys.exit(1) sys.exit(1)

View file

@ -13,12 +13,12 @@ import optparse
import sys import sys
from core.misc import ipaddress from core.misc import ipaddress
from core.misc.utils import mutecall from core.misc.utils import check_cmd
from core.netns import nodes from core.netns import nodes
from core.session import Session from core.session import Session
try: try:
mutecall(["iperf", "-v"]) check_cmd(["iperf", "-v"])
except OSError: except OSError:
sys.stderr.write("ERROR: running iperf failed\n") sys.stderr.write("ERROR: running iperf failed\n")
sys.exit(1) sys.exit(1)

View file

@ -13,12 +13,12 @@ import optparse
import sys import sys
from core.misc import ipaddress from core.misc import ipaddress
from core.misc.utils import mutecall from core.misc.utils import check_cmd
from core.netns import nodes from core.netns import nodes
from core.session import Session from core.session import Session
try: try:
mutecall(["iperf", "-v"]) check_cmd(["iperf", "-v"])
except OSError: except OSError:
sys.stderr.write("ERROR: running iperf failed\n") sys.stderr.write("ERROR: running iperf failed\n")
sys.exit(1) sys.exit(1)

View file

@ -35,7 +35,7 @@ from core.corehandlers import CoreDatagramRequestHandler
from core.enumerations import MessageFlags from core.enumerations import MessageFlags
from core.enumerations import RegisterTlvs from core.enumerations import RegisterTlvs
from core.misc import nodeutils from core.misc import nodeutils
from core.misc.utils import closeonexec from core.misc.utils import close_onexec
from core.misc.utils import daemonize from core.misc.utils import daemonize
from core.service import ServiceManager from core.service import ServiceManager
@ -84,11 +84,11 @@ def cored(cfg=None):
logger.exception("error starting main server on: %s:%s", host, port) logger.exception("error starting main server on: %s:%s", host, port)
sys.exit(1) sys.exit(1)
closeonexec(server.fileno()) close_onexec(server.fileno())
logger.info("main server started, listening on: %s:%s\n" % (host, port)) logger.info("main server started, listening on: %s:%s\n" % (host, port))
udpserver = startudp(server, (host, port)) udpserver = startudp(server, (host, port))
closeonexec(udpserver.fileno()) close_onexec(udpserver.fileno())
server.serve_forever() server.serve_forever()

View file

@ -21,7 +21,7 @@ from core.coreobj import PyCoreNet
from core.enumerations import EventTypes from core.enumerations import EventTypes
from core.enumerations import LinkTypes from core.enumerations import LinkTypes
from core.enumerations import NodeTypes from core.enumerations import NodeTypes
from core.misc.utils import maketuple from core.misc.utils import make_tuple
from core.mobility import WayPointMobility from core.mobility import WayPointMobility
from core.netns.nodes import CoreNode from core.netns.nodes import CoreNode
from core.session import Session from core.session import Session
@ -65,7 +65,7 @@ class CoreNs3Node(CoreNode, ns.network.Node):
self.attachnet(ifindex, net) self.attachnet(ifindex, net)
netif = self.netif(ifindex) netif = self.netif(ifindex)
netif.sethwaddr(hwaddr) netif.sethwaddr(hwaddr)
for addr in maketuple(addrlist): for addr in make_tuple(addrlist):
netif.addaddr(addr) netif.addaddr(addr)
addrstr = netif.addrlist[0] addrstr = netif.addrlist[0]