defined custom core command error that defaults to printing command output as well

This commit is contained in:
Blake J. Harnden 2018-03-02 16:22:20 -08:00
parent 29a3496eda
commit 974559843a
15 changed files with 90 additions and 81 deletions

View file

@ -4,11 +4,11 @@ implementing specific node types.
"""
import socket
import subprocess
import threading
from socket import AF_INET
from socket import AF_INET6
from core import CoreCommandError
from core import constants
from core import logger
from core.coreobj import PyCoreNetIf
@ -68,7 +68,7 @@ class CtrlNet(LxBrNet):
Startup functionality for the control network.
:return: nothing
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
if self.detectoldbridge():
return
@ -134,16 +134,15 @@ class CtrlNet(LxBrNet):
if self.serverintf is not None:
try:
utils.check_cmd([constants.BRCTL_BIN, "delif", self.brname, self.serverintf])
except subprocess.CalledProcessError as e:
logger.exception("error deleting server interface %s from bridge %s: %s",
self.serverintf, self.brname, e.output)
except CoreCommandError:
logger.exception("error deleting server interface %s from bridge %s", self.serverintf, self.brname)
if self.updown_script is not None:
try:
logger.info("interface %s updown script (%s shutdown) called", self.brname, self.updown_script)
utils.check_cmd([self.updown_script, self.brname, "shutdown"])
except subprocess.CalledProcessError as e:
logger.exception("error issuing shutdown script shutdown: %s", e.output)
except CoreCommandError:
logger.exception("error issuing shutdown script shutdown")
LxBrNet.shutdown(self)
@ -328,7 +327,7 @@ class HubNode(LxBrNet):
:param int objid: node id
:param str name: node namee
:param bool start: start flag
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
LxBrNet.__init__(self, session, objid, name, start)
@ -479,7 +478,7 @@ class RJ45Node(PyCoreNode, PyCoreNetIf):
Set the interface in the up state.
:return: nothing
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
# interface will also be marked up during net.attach()
self.savestate()
@ -500,8 +499,8 @@ class RJ45Node(PyCoreNode, PyCoreNetIf):
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.TC_BIN, "qdisc", "del", "dev", self.localname, "root"])
except subprocess.CalledProcessError as e:
logger.exception("error shutting down: %s", e.output)
except CoreCommandError:
logger.exception("error shutting down")
self.up = False
self.restorestate()
@ -619,7 +618,7 @@ class RJ45Node(PyCoreNode, PyCoreNetIf):
:param str addr: address to add
:return: nothing
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
if self.up:
utils.check_cmd([constants.IP_BIN, "addr", "add", str(addr), "dev", self.name])
@ -632,7 +631,7 @@ class RJ45Node(PyCoreNode, PyCoreNetIf):
:param str addr: address to delete
:return: nothing
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
if self.up:
utils.check_cmd([constants.IP_BIN, "addr", "del", str(addr), "dev", self.name])
@ -645,7 +644,7 @@ class RJ45Node(PyCoreNode, PyCoreNetIf):
interface for emulation purposes. TODO: save/restore the PROMISC flag
:return: nothing
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
self.old_up = False
self.old_addrs = []
@ -672,7 +671,7 @@ class RJ45Node(PyCoreNode, PyCoreNetIf):
Restore the addresses and other interface state after using it.
:return: nothing
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
for addr in self.old_addrs:
if addr[1] is None:
@ -704,7 +703,7 @@ class RJ45Node(PyCoreNode, PyCoreNetIf):
:param list[str]|str args: command to run
:return: exist status and combined stdout and stderr
:rtype: tuple[int, str]
:raises subprocess.CalledProcessError: when a non-zero exit status occurs
:raises CoreCommandError: when a non-zero exit status occurs
"""
raise NotImplementedError

View file

@ -3,11 +3,11 @@ TODO: probably goes away, or implement the usage of "unshare", or docker formal.
"""
import socket
import subprocess
import threading
from socket import AF_INET
from socket import AF_INET6
from core import CoreCommandError
from core import constants
from core import logger
from core.coreobj import PyCoreNet
@ -81,7 +81,7 @@ class OvsNet(PyCoreNet):
"""
:return:
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
utils.check_cmd([constants.OVS_BIN, "add-br", self.bridge_name])
@ -112,8 +112,8 @@ class OvsNet(PyCoreNet):
[constants.EBTABLES_BIN, "-D", "FORWARD", "--logical-in", self.bridge_name, "-j", 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)
except CoreCommandError:
logger.exception("error bringing bridge down and removing it")
# removes veth pairs used for bridge-to-bridge connections
for interface in self.netifs():
@ -404,16 +404,16 @@ class OvsCtrlNet(OvsNet):
if self.serverintf:
try:
utils.check_cmd([constants.OVS_BIN, "del-port", self.bridge_name, self.serverintf])
except subprocess.CalledProcessError as e:
logger.exception("error deleting server interface %s to controlnet bridge %s: %s",
self.serverintf, self.bridge_name, e.output)
except CoreCommandError:
logger.exception("error deleting server interface %s to controlnet bridge %s",
self.serverintf, self.bridge_name)
if self.updown_script:
try:
logger.info("interface %s updown script (%s shutdown) called", self.bridge_name, self.updown_script)
utils.check_cmd([self.updown_script, self.bridge_name, "shutdown"])
except subprocess.CalledProcessError as e:
logger.exception("error during updown script shutdown: %s", e.output)
except CoreCommandError:
logger.exception("error during updown script shutdown")
OvsNet.shutdown(self)

View file

@ -2,9 +2,9 @@
virtual ethernet classes that implement the interfaces available under Linux.
"""
import subprocess
import time
from core import CoreCommandError
from core import constants
from core import logger
from core.coreobj import PyCoreNetIf
@ -31,7 +31,7 @@ class VEth(PyCoreNetIf):
:param mtu: interface mtu
:param net: network
:param bool start: start flag
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
# note that net arg is ignored
PyCoreNetIf.__init__(self, node=node, name=name, mtu=mtu)
@ -45,7 +45,7 @@ class VEth(PyCoreNetIf):
Interface startup logic.
:return: nothing
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
utils.check_cmd([constants.IP_BIN, "link", "add", "name", self.localname,
"type", "veth", "peer", "name", self.name])
@ -64,14 +64,14 @@ class VEth(PyCoreNetIf):
if self.node:
try:
self.node.check_cmd([constants.IP_BIN, "-6", "addr", "flush", "dev", self.name])
except subprocess.CalledProcessError as e:
logger.exception("error shutting down interface: %s", e.output)
except CoreCommandError:
logger.exception("error shutting down interface")
if self.localname:
try:
utils.check_cmd([constants.IP_BIN, "link", "delete", self.localname])
except subprocess.CalledProcessError as e:
logger.exception("error deleting link: %s", e.output)
except CoreCommandError:
logger.exception("error deleting link")
self.up = False
@ -125,8 +125,8 @@ class TunTap(PyCoreNetIf):
try:
self.node.check_cmd([constants.IP_BIN, "-6", "addr", "flush", "dev", self.name])
except subprocess.CalledProcessError as e:
logger.exception("error shutting down tunnel tap: %s", e.output)
except CoreCommandError:
logger.exception("error shutting down tunnel tap")
self.up = False
@ -212,7 +212,7 @@ class TunTap(PyCoreNetIf):
end of the TAP.
:return: nothing
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
self.waitfordevicelocal()
netns = str(self.node.pid)
@ -254,7 +254,7 @@ class GreTap(PyCoreNetIf):
:param ttl: ttl value
:param key: gre tap key
:param bool start: start flag
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
PyCoreNetIf.__init__(self, node=node, name=name, mtu=mtu)
self.session = session
@ -297,8 +297,8 @@ class GreTap(PyCoreNetIf):
utils.check_cmd(args)
args = ["ip", "link", "del", self.localname]
utils.check_cmd(args)
except subprocess.CalledProcessError as e:
logger.exception("error during shutdown: %s", e.output)
except CoreCommandError:
logger.exception("error during shutdown")
self.localname = None

View file

@ -4,10 +4,10 @@ Linux Ethernet bridging and ebtables rules.
"""
import os
import subprocess
import threading
import time
from core import CoreCommandError
from core import constants
from core import logger
from core.coreobj import PyCoreNet
@ -272,7 +272,7 @@ class LxBrNet(PyCoreNet):
Linux bridge starup logic.
:return: nothing
:raises subprocess.CalledProcessError: when there is a command exception
:raises CoreCommandError: when there is a command exception
"""
utils.check_cmd([constants.BRCTL_BIN, "addbr", self.brname])
@ -311,8 +311,8 @@ class LxBrNet(PyCoreNet):
[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)
except CoreCommandError:
logger.exception("error during shutdown")
# removes veth pairs used for bridge-to-bridge connections
for netif in self.netifs():

View file

@ -7,9 +7,9 @@ import random
import shutil
import signal
import string
import subprocess
import threading
from core import CoreCommandError
from core import constants
from core import logger
from core.coreobj import PyCoreNetIf
@ -189,7 +189,7 @@ class SimpleLxcNode(PyCoreNode):
:param list[str]|str args: command to run
:return: combined stdout and stderr
:rtype: str
:raises subprocess.CalledProcessError: when a non-zero exit status occurs
:raises CoreCommandError: when a non-zero exit status occurs
"""
return self.client.check_cmd(args)
@ -209,14 +209,14 @@ class SimpleLxcNode(PyCoreNode):
:param str source: source directory to mount
:param str target: target directory to create
:return: nothing
:raises subprocess.CalledProcessError: when a non-zero exit status occurs
:raises CoreCommandError: when a non-zero exit status occurs
"""
source = os.path.abspath(source)
logger.info("mounting %s at %s" % (source, target))
cmd = 'mkdir -p "%s" && %s -n --bind "%s" "%s"' % (target, constants.MOUNT_BIN, source, target)
status, output = self.client.shcmd_result(cmd)
if status:
raise subprocess.CalledProcessError(status, cmd, output)
raise CoreCommandError(status, cmd, output)
self._mounts.append((source, target))
def umount(self, target):
@ -229,8 +229,8 @@ class SimpleLxcNode(PyCoreNode):
logger.info("unmounting: %s", target)
try:
self.check_cmd([constants.UMOUNT_BIN, "-n", "-l", target])
except subprocess.CalledProcessError as e:
logger.exception("error during unmount: %s", e.output)
except CoreCommandError:
logger.exception("error during unmount")
def newifindex(self):
"""
@ -339,7 +339,7 @@ class SimpleLxcNode(PyCoreNode):
:param int ifindex: index of interface to set hardware address for
:param core.misc.ipaddress.MacAddress addr: hardware address to set
:return: nothing
:raises subprocess.CalledProcessError: when a non-zero exit status occurs
:raises CoreCommandError: when a non-zero exit status occurs
"""
self._netif[ifindex].sethwaddr(addr)
if self.up:
@ -372,7 +372,7 @@ class SimpleLxcNode(PyCoreNode):
:param int ifindex: index of interface to delete address from
:param str addr: address to delete from interface
:return: nothing
:raises subprocess.CalledProcessError: when a non-zero exit status occurs
:raises CoreCommandError: when a non-zero exit status occurs
"""
try:
self._netif[ifindex].deladdr(addr)
@ -389,7 +389,7 @@ class SimpleLxcNode(PyCoreNode):
:param int ifindex: index of interface to delete address types from
:param tuple[str] address_types: address types to delete
:return: nothing
:raises subprocess.CalledProcessError: when a non-zero exit status occurs
:raises CoreCommandError: when a non-zero exit status occurs
"""
interface_name = self.ifname(ifindex)
addresses = self.client.getaddr(interface_name, rescan=True)
@ -488,7 +488,7 @@ class SimpleLxcNode(PyCoreNode):
:param str srcname: source file name
:param str filename: file name to add
:return: nothing
:raises subprocess.CalledProcessError: when a non-zero exit status occurs
:raises CoreCommandError: when a non-zero exit status occurs
"""
logger.info("adding file from %s to %s", srcname, filename)
directory = os.path.dirname(filename)
@ -496,7 +496,7 @@ class SimpleLxcNode(PyCoreNode):
cmd = 'mkdir -p "%s" && mv "%s" "%s" && sync' % (directory, srcname, filename)
status, output = self.client.shcmd_result(cmd)
if status:
raise subprocess.CalledProcessError(status, cmd, output)
raise CoreCommandError(status, cmd, output)
class LxcNode(SimpleLxcNode):

View file

@ -6,10 +6,10 @@ by invoking the vcmd shell command.
"""
import os
import subprocess
import vcmd
from core import CoreCommandError
from core import constants
from core import logger
from core.misc import utils
@ -106,11 +106,11 @@ class VnodeClient(object):
:param list[str]|str args: command to run
:return: combined stdout and stderr
:rtype: str
:raises subprocess.CalledProcessError: when there is a non-zero exit status
:raises core.CoreCommandError: when there is a non-zero exit status
"""
status, output = self.cmd_output(args)
if status != 0:
raise subprocess.CalledProcessError(status, args, output)
raise CoreCommandError(status, args, output)
return output.strip()
def popen(self, args):