updates to python based logging to use module named loggers, updated logging config file to align with these changes

This commit is contained in:
Blake Harnden 2021-04-21 21:09:35 -07:00
parent 55d5bb3859
commit 69652ac577
63 changed files with 717 additions and 606 deletions

View file

@ -21,6 +21,8 @@ from core.nodes.client import VnodeClient
from core.nodes.interface import CoreInterface, TunTap, Veth
from core.nodes.netclient import LinuxNetClient, get_net_client
logger = logging.getLogger(__name__)
if TYPE_CHECKING:
from core.emulator.distributed import DistributedServer
from core.emulator.session import Session
@ -404,7 +406,7 @@ class CoreNodeBase(NodeBase):
if iface_id not in self.ifaces:
raise CoreError(f"node({self.name}) interface({iface_id}) does not exist")
iface = self.ifaces.pop(iface_id)
logging.info("node(%s) removing interface(%s)", self.name, iface.name)
logger.info("node(%s) removing interface(%s)", self.name, iface.name)
iface.detachnet()
iface.shutdown()
@ -547,17 +549,17 @@ class CoreNode(CoreNodeBase):
output = self.host_cmd(vnoded, env=env)
self.pid = int(output)
logging.debug("node(%s) pid: %s", self.name, self.pid)
logger.debug("node(%s) pid: %s", self.name, self.pid)
# create vnode client
self.client = VnodeClient(self.name, self.ctrlchnlname)
# bring up the loopback interface
logging.debug("bringing up loopback interface")
logger.debug("bringing up loopback interface")
self.node_net_client.device_up("lo")
# set hostname for node
logging.debug("setting hostname: %s", self.name)
logger.debug("setting hostname: %s", self.name)
self.node_net_client.set_hostname(self.name)
# mark node as up
@ -588,18 +590,18 @@ class CoreNode(CoreNodeBase):
try:
self.host_cmd(f"kill -9 {self.pid}")
except CoreCommandError:
logging.exception("error killing process")
logger.exception("error killing process")
# remove node directory if present
try:
self.host_cmd(f"rm -rf {self.ctrlchnlname}")
except CoreCommandError:
logging.exception("error removing node directory")
logger.exception("error removing node directory")
# clear interface data, close client, and mark self and not up
self.ifaces.clear()
self.client.close()
self.up = False
except OSError:
logging.exception("error during shutdown")
logger.exception("error during shutdown")
finally:
self.rmnodedir()
@ -668,7 +670,7 @@ class CoreNode(CoreNodeBase):
:return: nothing
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.debug("node(%s) mounting: %s at %s", self.name, src_path, target_path)
logger.debug("node(%s) mounting: %s at %s", self.name, src_path, target_path)
self.cmd(f"mkdir -p {target_path}")
self.cmd(f"{MOUNT} -n --bind {src_path} {target_path}")
self._mounts.append((src_path, target_path))
@ -726,9 +728,9 @@ class CoreNode(CoreNodeBase):
if self.up:
flow_id = self.node_net_client.get_ifindex(veth.name)
veth.flow_id = int(flow_id)
logging.debug("interface flow index: %s - %s", veth.name, veth.flow_id)
logger.debug("interface flow index: %s - %s", veth.name, veth.flow_id)
mac = self.node_net_client.get_mac(veth.name)
logging.debug("interface mac: %s - %s", veth.name, mac)
logger.debug("interface mac: %s - %s", veth.name, mac)
veth.set_mac(mac)
try:
@ -867,7 +869,7 @@ class CoreNode(CoreNodeBase):
:return: nothing
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.info("adding file from %s to %s", src_path, file_path)
logger.info("adding file from %s to %s", src_path, file_path)
directory = file_path.parent
if self.server is None:
self.client.check_cmd(f"mkdir -p {directory}")
@ -898,7 +900,7 @@ class CoreNode(CoreNodeBase):
self.host_cmd(f"mkdir -m {0o755:o} -p {directory}")
self.server.remote_put_temp(host_path, contents)
self.host_cmd(f"chmod {mode:o} {host_path}")
logging.debug("node(%s) added file: %s; mode: 0%o", self.name, host_path, mode)
logger.debug("node(%s) added file: %s; mode: 0%o", self.name, host_path, mode)
def nodefilecopy(self, file_path: Path, src_path: Path, mode: int = None) -> None:
"""
@ -917,7 +919,7 @@ class CoreNode(CoreNodeBase):
self.server.remote_put(src_path, host_path)
if mode is not None:
self.host_cmd(f"chmod {mode:o} {host_path}")
logging.info("node(%s) copied file: %s; mode: %s", self.name, host_path, mode)
logger.info("node(%s) copied file: %s; mode: %s", self.name, host_path, mode)
class CoreNetworkBase(NodeBase):

View file

@ -11,6 +11,8 @@ from core.errors import CoreCommandError
from core.nodes.base import CoreNode
from core.nodes.netclient import LinuxNetClient, get_net_client
logger = logging.getLogger(__name__)
if TYPE_CHECKING:
from core.emulator.session import Session
@ -50,7 +52,7 @@ class DockerClient:
self.run(f"docker rm -f {self.name}")
def check_cmd(self, cmd: str, wait: bool = True, shell: bool = False) -> str:
logging.info("docker cmd output: %s", cmd)
logger.info("docker cmd output: %s", cmd)
return utils.cmd(f"docker exec {self.name} {cmd}", wait=wait, shell=shell)
def create_ns_cmd(self, cmd: str) -> str:
@ -60,7 +62,7 @@ class DockerClient:
args = f"docker inspect -f '{{{{.State.Pid}}}}' {self.name}"
output = self.run(args)
self.pid = output
logging.debug("node(%s) pid: %s", self.name, self.pid)
logger.debug("node(%s) pid: %s", self.name, self.pid)
return output
def copy_file(self, src_path: Path, dst_path: Path) -> str:
@ -169,7 +171,7 @@ class DockerNode(CoreNode):
:param dir_path: path to create
:return: nothing
"""
logging.debug("creating node dir: %s", dir_path)
logger.debug("creating node dir: %s", dir_path)
args = f"mkdir -p {dir_path}"
self.cmd(args)
@ -182,7 +184,7 @@ class DockerNode(CoreNode):
:return: nothing
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.debug("mounting source(%s) target(%s)", src_path, target_path)
logger.debug("mounting source(%s) target(%s)", src_path, target_path)
raise Exception("not supported")
def nodefile(self, file_path: Path, contents: str, mode: int = 0o644) -> None:
@ -194,7 +196,7 @@ class DockerNode(CoreNode):
:param mode: mode for file
:return: nothing
"""
logging.debug("nodefile filename(%s) mode(%s)", file_path, mode)
logger.debug("nodefile filename(%s) mode(%s)", file_path, mode)
temp = NamedTemporaryFile(delete=False)
temp.write(contents.encode("utf-8"))
temp.close()
@ -209,7 +211,7 @@ class DockerNode(CoreNode):
if self.server is not None:
self.host_cmd(f"rm -f {temp_path}")
temp_path.unlink()
logging.debug("node(%s) added file: %s; mode: 0%o", self.name, file_path, mode)
logger.debug("node(%s) added file: %s; mode: 0%o", self.name, file_path, mode)
def nodefilecopy(self, file_path: Path, src_path: Path, mode: int = None) -> None:
"""
@ -221,7 +223,7 @@ class DockerNode(CoreNode):
:param mode: mode to copy to
:return: nothing
"""
logging.info(
logger.info(
"node file copy file(%s) source(%s) mode(%s)", file_path, src_path, mode
)
self.cmd(f"mkdir -p {file_path.parent}")

View file

@ -15,6 +15,8 @@ from core.emulator.enumerations import TransportType
from core.errors import CoreCommandError, CoreError
from core.nodes.netclient import LinuxNetClient, get_net_client
logger = logging.getLogger(__name__)
if TYPE_CHECKING:
from core.emulator.distributed import DistributedServer
from core.emulator.session import Session
@ -274,7 +276,7 @@ class CoreInterface:
:return: True if parameter changed, False otherwise
"""
# treat None and 0 as unchanged values
logging.debug("setting param: %s - %s", key, value)
logger.debug("setting param: %s - %s", key, value)
if value is None or value < 0:
return False
@ -457,7 +459,7 @@ class TunTap(CoreInterface):
try:
self.node.node_net_client.device_flush(self.name)
except CoreCommandError:
logging.exception("error shutting down tunnel tap")
logger.exception("error shutting down tunnel tap")
self.up = False
@ -482,14 +484,14 @@ class TunTap(CoreInterface):
msg = f"attempt {i} failed with nonzero exit status {r}"
if i < attempts + 1:
msg += ", retrying..."
logging.info(msg)
logger.info(msg)
time.sleep(delay)
delay += delay
if delay > maxretrydelay:
delay = maxretrydelay
else:
msg += ", giving up"
logging.info(msg)
logger.info(msg)
return result
@ -500,7 +502,7 @@ class TunTap(CoreInterface):
:return: wait for device local response
"""
logging.debug("waiting for device local: %s", self.localname)
logger.debug("waiting for device local: %s", self.localname)
def localdevexists():
try:
@ -517,7 +519,7 @@ class TunTap(CoreInterface):
:return: nothing
"""
logging.debug("waiting for device node: %s", self.name)
logger.debug("waiting for device node: %s", self.name)
def nodedevexists():
try:
@ -634,5 +636,5 @@ class GreTap(CoreInterface):
self.net_client.device_down(self.localname)
self.net_client.delete_device(self.localname)
except CoreCommandError:
logging.exception("error during shutdown")
logger.exception("error during shutdown")
self.localname = None

View file

@ -12,6 +12,8 @@ from core.errors import CoreCommandError
from core.nodes.base import CoreNode
from core.nodes.interface import CoreInterface
logger = logging.getLogger(__name__)
if TYPE_CHECKING:
from core.emulator.session import Session
@ -145,7 +147,7 @@ class LxcNode(CoreNode):
:param dir_path: path to create
:return: nothing
"""
logging.info("creating node dir: %s", dir_path)
logger.info("creating node dir: %s", dir_path)
args = f"mkdir -p {dir_path}"
self.cmd(args)
@ -158,7 +160,7 @@ class LxcNode(CoreNode):
:return: nothing
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.debug("mounting source(%s) target(%s)", src_path, target_path)
logger.debug("mounting source(%s) target(%s)", src_path, target_path)
raise Exception("not supported")
def nodefile(self, file_path: Path, contents: str, mode: int = 0o644) -> None:
@ -170,7 +172,7 @@ class LxcNode(CoreNode):
:param mode: mode for file
:return: nothing
"""
logging.debug("nodefile filename(%s) mode(%s)", file_path, mode)
logger.debug("nodefile filename(%s) mode(%s)", file_path, mode)
temp = NamedTemporaryFile(delete=False)
temp.write(contents.encode("utf-8"))
temp.close()
@ -185,7 +187,7 @@ class LxcNode(CoreNode):
if self.server is not None:
self.host_cmd(f"rm -f {temp_path}")
temp_path.unlink()
logging.debug("node(%s) added file: %s; mode: 0%o", self.name, file_path, mode)
logger.debug("node(%s) added file: %s; mode: 0%o", self.name, file_path, mode)
def nodefilecopy(self, file_path: Path, src_path: Path, mode: int = None) -> None:
"""
@ -197,7 +199,7 @@ class LxcNode(CoreNode):
:param mode: mode to copy to
:return: nothing
"""
logging.info(
logger.info(
"node file copy file(%s) source(%s) mode(%s)", file_path, src_path, mode
)
self.cmd(f"mkdir -p {file_path.parent}")

View file

@ -26,6 +26,8 @@ from core.nodes.base import CoreNetworkBase
from core.nodes.interface import CoreInterface, GreTap, Veth
from core.nodes.netclient import get_net_client
logger = logging.getLogger(__name__)
if TYPE_CHECKING:
from core.emulator.distributed import DistributedServer
from core.emulator.session import Session
@ -90,7 +92,7 @@ class EbtablesQueue:
try:
del self.last_update_time[wlan]
except KeyError:
logging.exception(
logger.exception(
"error deleting last update time for wlan, ignored before: %s", wlan
)
if len(self.last_update_time) > 0:
@ -186,7 +188,7 @@ class EbtablesQueue:
try:
wlan.host_cmd(f"rm -f {self.atomic_file}")
except CoreCommandError:
logging.exception("error removing atomic file: %s", self.atomic_file)
logger.exception("error removing atomic file: %s", self.atomic_file)
def ebchange(self, wlan: "CoreNetwork") -> None:
"""
@ -309,7 +311,7 @@ class CoreNetwork(CoreNetworkBase):
:return: combined stdout and stderr
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.debug("network node(%s) cmd", self.name)
logger.debug("network node(%s) cmd", self.name)
output = utils.cmd(args, env, cwd, wait, shell)
self.session.distributed.execute(lambda x: x.remote_cmd(args, env, cwd, wait))
return output
@ -344,7 +346,7 @@ class CoreNetwork(CoreNetworkBase):
]
ebtablescmds(self.host_cmd, cmds)
except CoreCommandError:
logging.exception("error during shutdown")
logger.exception("error during shutdown")
# removes veth pairs used for bridge-to-bridge connections
for iface in self.get_ifaces():
iface.shutdown()
@ -763,7 +765,7 @@ class CtrlNet(CoreNetwork):
raise CoreError(f"old bridges exist for node: {self.id}")
super().startup()
logging.info("added control network bridge: %s %s", self.brname, self.prefix)
logger.info("added control network bridge: %s %s", self.brname, self.prefix)
if self.hostid and self.assign_address:
self.add_addresses(self.hostid)
@ -771,7 +773,7 @@ class CtrlNet(CoreNetwork):
self.add_addresses(-2)
if self.updown_script:
logging.info(
logger.info(
"interface %s updown script (%s startup) called",
self.brname,
self.updown_script,
@ -791,7 +793,7 @@ class CtrlNet(CoreNetwork):
try:
self.net_client.delete_iface(self.brname, self.serverintf)
except CoreCommandError:
logging.exception(
logger.exception(
"error deleting server interface %s from bridge %s",
self.serverintf,
self.brname,
@ -799,14 +801,14 @@ class CtrlNet(CoreNetwork):
if self.updown_script is not None:
try:
logging.info(
logger.info(
"interface %s updown script (%s shutdown) called",
self.brname,
self.updown_script,
)
self.host_cmd(f"{self.updown_script} {self.brname} shutdown")
except CoreCommandError:
logging.exception("error issuing shutdown script shutdown")
logger.exception("error issuing shutdown script shutdown")
super().shutdown()
@ -1006,7 +1008,7 @@ class WlanNode(CoreNetwork):
:param config: configuration for model being set
:return: nothing
"""
logging.debug("node(%s) setting model: %s", self.name, model.name)
logger.debug("node(%s) setting model: %s", self.name, model.name)
if model.config_type == RegisterTlvs.WIRELESS:
self.model = model(session=self.session, _id=self.id)
for iface in self.get_ifaces():
@ -1025,7 +1027,7 @@ class WlanNode(CoreNetwork):
def updatemodel(self, config: Dict[str, str]) -> None:
if not self.model:
raise CoreError(f"no model set to update for node({self.name})")
logging.debug(
logger.debug(
"node(%s) updating model(%s): %s", self.id, self.model.name, config
)
self.model.update_config(config)

View file

@ -16,6 +16,8 @@ from core.nodes.base import CoreNetworkBase, CoreNodeBase
from core.nodes.interface import DEFAULT_MTU, CoreInterface
from core.nodes.network import CoreNetwork, GreTap
logger = logging.getLogger(__name__)
if TYPE_CHECKING:
from core.emulator.session import Session
@ -163,7 +165,7 @@ class PhysicalNode(CoreNodeBase):
def new_iface(
self, net: CoreNetworkBase, iface_data: InterfaceData
) -> CoreInterface:
logging.info("creating interface")
logger.info("creating interface")
ips = iface_data.get_ips()
iface_id = iface_data.id
if iface_id is None:
@ -191,17 +193,17 @@ class PhysicalNode(CoreNodeBase):
self.mount(host_path, dir_path)
def mount(self, src_path: Path, target_path: Path) -> None:
logging.debug("node(%s) mounting: %s at %s", self.name, src_path, target_path)
logger.debug("node(%s) mounting: %s at %s", self.name, src_path, target_path)
self.cmd(f"mkdir -p {target_path}")
self.host_cmd(f"{MOUNT} --bind {src_path} {target_path}", cwd=self.directory)
self._mounts.append((src_path, target_path))
def umount(self, target_path: Path) -> None:
logging.info("unmounting '%s'", target_path)
logger.info("unmounting '%s'", target_path)
try:
self.host_cmd(f"{UMOUNT} -l {target_path}", cwd=self.directory)
except CoreCommandError:
logging.exception("unmounting failed for %s", target_path)
logger.exception("unmounting failed for %s", target_path)
def nodefile(self, file_path: Path, contents: str, mode: int = 0o644) -> None:
host_path = self.host_path(file_path)
@ -211,7 +213,7 @@ class PhysicalNode(CoreNodeBase):
with host_path.open("w") as f:
f.write(contents)
host_path.chmod(mode)
logging.info("created nodefile: '%s'; mode: 0%o", host_path, mode)
logger.info("created nodefile: '%s'; mode: 0%o", host_path, mode)
def cmd(self, args: str, wait: bool = True, shell: bool = False) -> str:
return self.host_cmd(args, wait=wait)
@ -415,7 +417,7 @@ class Rj45Node(CoreNodeBase):
if items[1][:4] == "fe80":
continue
self.old_addrs.append((items[1], None))
logging.info("saved rj45 state: addrs(%s) up(%s)", self.old_addrs, self.old_up)
logger.info("saved rj45 state: addrs(%s) up(%s)", self.old_addrs, self.old_up)
def restorestate(self) -> None:
"""
@ -425,7 +427,7 @@ class Rj45Node(CoreNodeBase):
:raises CoreCommandError: when there is a command exception
"""
localname = self.iface.localname
logging.info("restoring rj45 state: %s", localname)
logger.info("restoring rj45 state: %s", localname)
for addr in self.old_addrs:
self.net_client.create_address(localname, addr[0], addr[1])
if self.old_up: