added type hinting for core.nodes
This commit is contained in:
parent
4e71759ac9
commit
c0fcc91d10
10 changed files with 534 additions and 360 deletions
|
@ -3,27 +3,33 @@ import logging
|
|||
import os
|
||||
import time
|
||||
from tempfile import NamedTemporaryFile
|
||||
from typing import TYPE_CHECKING, Callable, Dict
|
||||
|
||||
from core import utils
|
||||
from core.emulator.distributed import DistributedServer
|
||||
from core.emulator.enumerations import NodeTypes
|
||||
from core.errors import CoreCommandError
|
||||
from core.nodes.base import CoreNode
|
||||
from core.nodes.interface import CoreInterface
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.emulator.session import Session
|
||||
|
||||
|
||||
class LxdClient:
|
||||
def __init__(self, name, image, run):
|
||||
def __init__(self, name: str, image: str, run: Callable) -> None:
|
||||
self.name = name
|
||||
self.image = image
|
||||
self.run = run
|
||||
self.pid = None
|
||||
|
||||
def create_container(self):
|
||||
def create_container(self) -> int:
|
||||
self.run(f"lxc launch {self.image} {self.name}")
|
||||
data = self.get_info()
|
||||
self.pid = data["state"]["pid"]
|
||||
return self.pid
|
||||
|
||||
def get_info(self):
|
||||
def get_info(self) -> Dict:
|
||||
args = f"lxc list {self.name} --format json"
|
||||
output = self.run(args)
|
||||
data = json.loads(output)
|
||||
|
@ -31,27 +37,27 @@ class LxdClient:
|
|||
raise CoreCommandError(-1, args, f"LXC({self.name}) not present")
|
||||
return data[0]
|
||||
|
||||
def is_alive(self):
|
||||
def is_alive(self) -> bool:
|
||||
try:
|
||||
data = self.get_info()
|
||||
return data["state"]["status"] == "Running"
|
||||
except CoreCommandError:
|
||||
return False
|
||||
|
||||
def stop_container(self):
|
||||
def stop_container(self) -> None:
|
||||
self.run(f"lxc delete --force {self.name}")
|
||||
|
||||
def create_cmd(self, cmd):
|
||||
def create_cmd(self, cmd: str) -> str:
|
||||
return f"lxc exec -nT {self.name} -- {cmd}"
|
||||
|
||||
def create_ns_cmd(self, cmd):
|
||||
def create_ns_cmd(self, cmd: str) -> str:
|
||||
return f"nsenter -t {self.pid} -m -u -i -p -n {cmd}"
|
||||
|
||||
def check_cmd(self, cmd, wait=True, shell=False):
|
||||
def check_cmd(self, cmd: str, wait: bool = True, shell: bool = False) -> str:
|
||||
args = self.create_cmd(cmd)
|
||||
return utils.cmd(args, wait=wait, shell=shell)
|
||||
|
||||
def copy_file(self, source, destination):
|
||||
def copy_file(self, source: str, destination: str) -> None:
|
||||
if destination[0] != "/":
|
||||
destination = os.path.join("/root/", destination)
|
||||
|
||||
|
@ -64,15 +70,15 @@ class LxcNode(CoreNode):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
session,
|
||||
_id=None,
|
||||
name=None,
|
||||
nodedir=None,
|
||||
bootsh="boot.sh",
|
||||
start=True,
|
||||
server=None,
|
||||
image=None,
|
||||
):
|
||||
session: "Session",
|
||||
_id: int = None,
|
||||
name: str = None,
|
||||
nodedir: str = None,
|
||||
bootsh: str = "boot.sh",
|
||||
start: bool = True,
|
||||
server: DistributedServer = None,
|
||||
image: str = None,
|
||||
) -> None:
|
||||
"""
|
||||
Create a LxcNode instance.
|
||||
|
||||
|
@ -91,7 +97,7 @@ class LxcNode(CoreNode):
|
|||
self.image = image
|
||||
super().__init__(session, _id, name, nodedir, bootsh, start, server)
|
||||
|
||||
def alive(self):
|
||||
def alive(self) -> bool:
|
||||
"""
|
||||
Check if the node is alive.
|
||||
|
||||
|
@ -100,7 +106,7 @@ class LxcNode(CoreNode):
|
|||
"""
|
||||
return self.client.is_alive()
|
||||
|
||||
def startup(self):
|
||||
def startup(self) -> None:
|
||||
"""
|
||||
Startup logic.
|
||||
|
||||
|
@ -114,7 +120,7 @@ class LxcNode(CoreNode):
|
|||
self.pid = self.client.create_container()
|
||||
self.up = True
|
||||
|
||||
def shutdown(self):
|
||||
def shutdown(self) -> None:
|
||||
"""
|
||||
Shutdown logic.
|
||||
|
||||
|
@ -129,7 +135,7 @@ class LxcNode(CoreNode):
|
|||
self.client.stop_container()
|
||||
self.up = False
|
||||
|
||||
def termcmdstring(self, sh="/bin/sh"):
|
||||
def termcmdstring(self, sh: str = "/bin/sh") -> str:
|
||||
"""
|
||||
Create a terminal command string.
|
||||
|
||||
|
@ -138,7 +144,7 @@ class LxcNode(CoreNode):
|
|||
"""
|
||||
return f"lxc exec {self.name} -- {sh}"
|
||||
|
||||
def privatedir(self, path):
|
||||
def privatedir(self, path: str) -> None:
|
||||
"""
|
||||
Create a private directory.
|
||||
|
||||
|
@ -147,9 +153,9 @@ class LxcNode(CoreNode):
|
|||
"""
|
||||
logging.info("creating node dir: %s", path)
|
||||
args = f"mkdir -p {path}"
|
||||
return self.cmd(args)
|
||||
self.cmd(args)
|
||||
|
||||
def mount(self, source, target):
|
||||
def mount(self, source: str, target: str) -> None:
|
||||
"""
|
||||
Create and mount a directory.
|
||||
|
||||
|
@ -161,7 +167,7 @@ class LxcNode(CoreNode):
|
|||
logging.debug("mounting source(%s) target(%s)", source, target)
|
||||
raise Exception("not supported")
|
||||
|
||||
def nodefile(self, filename, contents, mode=0o644):
|
||||
def nodefile(self, filename: str, contents: str, mode: int = 0o644) -> None:
|
||||
"""
|
||||
Create a node file with a given mode.
|
||||
|
||||
|
@ -188,7 +194,7 @@ class LxcNode(CoreNode):
|
|||
os.unlink(temp.name)
|
||||
logging.debug("node(%s) added file: %s; mode: 0%o", self.name, filename, mode)
|
||||
|
||||
def nodefilecopy(self, filename, srcfilename, mode=None):
|
||||
def nodefilecopy(self, filename: str, srcfilename: str, mode: int = None) -> None:
|
||||
"""
|
||||
Copy a file to a node, following symlinks and preserving metadata.
|
||||
Change file mode if specified.
|
||||
|
@ -214,7 +220,7 @@ class LxcNode(CoreNode):
|
|||
self.client.copy_file(source, filename)
|
||||
self.cmd(f"chmod {mode:o} {filename}")
|
||||
|
||||
def addnetif(self, netif, ifindex):
|
||||
def addnetif(self, netif: CoreInterface, ifindex: int) -> None:
|
||||
super().addnetif(netif, ifindex)
|
||||
# adding small delay to allow time for adding addresses to work correctly
|
||||
time.sleep(0.5)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue