core-extra/tasks.py

214 lines
5.6 KiB
Python
Raw Normal View History

2020-07-10 07:01:28 +01:00
import os
import sys
from enum import Enum
2020-07-10 07:01:28 +01:00
from invoke import task, Context
DAEMON_DIR: str = "daemon"
VCMD_DIR: str = "netns"
GUI_DIR: str = "gui"
2020-07-10 07:01:28 +01:00
class OsName(Enum):
UBUNTU = "ubuntu"
CENTOS = "centos"
class OsLike(Enum):
DEBIAN = "debian"
2020-07-10 18:45:03 +01:00
REDHAT = "rhel fedora"
class OsInfo:
2020-07-10 18:45:03 +01:00
def __init__(self, name: OsName, like: OsLike, version: float) -> None:
self.name: OsName = name
self.like: OsLike = like
2020-07-10 18:45:03 +01:00
self.version: float = version
def get_python(c: Context) -> str:
2020-07-10 07:01:28 +01:00
with c.cd(DAEMON_DIR):
venv = c.run("poetry env info -p", hide=True).stdout.strip()
return os.path.join(venv, "bin", "python")
def get_pytest(c: Context) -> str:
2020-07-10 07:01:28 +01:00
with c.cd(DAEMON_DIR):
venv = c.run("poetry env info -p", hide=True).stdout.strip()
return os.path.join(venv, "bin", "pytest")
def get_os() -> OsInfo:
2020-07-10 07:01:28 +01:00
d = {}
with open("/etc/os-release", "r") as f:
for line in f.readlines():
line = line.strip()
if not line:
continue
2020-07-10 07:01:28 +01:00
key, value = line.split("=")
d[key] = value.strip('"')
name_value = d["ID"]
like_value = d["ID_LIKE"]
2020-07-10 18:45:03 +01:00
version_value = d["VERSION_ID"]
try:
name = OsName(name_value)
like = OsLike(like_value)
2020-07-10 18:45:03 +01:00
version = float(version_value)
except ValueError:
2020-07-10 18:45:03 +01:00
print(
f"unsupported os({name_value}) like({like_value}) version({version_value}"
)
sys.exit(1)
return OsInfo(name, like, version)
def install_system(c: Context, os_info: OsInfo, hide: bool) -> None:
2020-07-10 07:01:28 +01:00
print("installing system dependencies...")
if os_info.like == OsLike.DEBIAN:
2020-07-10 07:01:28 +01:00
c.run(
"sudo apt install -y automake pkg-config gcc libev-dev ebtables iproute2 "
"ethtool tk python3-tk",
hide=hide
)
elif os_info.like == OsLike.REDHAT:
c.run(
"sudo yum install -y automake pkgconf-pkg-config gcc gcc-c++ libev-devel "
"iptables-ebtables iproute python3-devel python3-tkinter tk ethtool",
hide=hide
2020-07-10 07:01:28 +01:00
)
def install_grpcio(c: Context, hide: bool) -> None:
2020-07-10 07:01:28 +01:00
print("installing grpcio-tools...")
c.run(
"python3 -m pip install --only-binary \":all:\" --user grpcio-tools", hide=hide
)
def build(c: Context, hide: bool) -> None:
2020-07-10 07:01:28 +01:00
print("building core...")
c.run("./bootstrap.sh", hide=hide)
c.run("./configure", hide=hide)
c.run("make", hide=hide)
def install_core(c: Context, hide: bool) -> None:
print("installing core vcmd...")
2020-07-10 07:01:28 +01:00
with c.cd(VCMD_DIR):
c.run("sudo make install", hide=hide)
print("installing core gui...")
2020-07-10 07:01:28 +01:00
with c.cd(GUI_DIR):
c.run("sudo make install", hide=hide)
def install_poetry(c: Context, dev: bool, hide: bool) -> None:
2020-07-10 07:01:28 +01:00
print("installing poetry...")
c.run("pipx install poetry", hide=hide)
args = "" if dev else "--no-dev"
2020-07-10 07:01:28 +01:00
with c.cd(DAEMON_DIR):
print("installing core environment using poetry...")
c.run(f"poetry install {args}", hide=hide)
if dev:
c.run("poetry run pre-commit install")
def install_ospf_mdr(c: Context, os_info: OsInfo, hide: bool) -> None:
if c.run("which zebra", warn=True, hide=hide):
print("quagga already installed, skipping ospf mdr")
return
print("installing ospf mdr dependencies...")
if os_info.like == OsLike.DEBIAN:
c.run("sudo apt install -y libtool gawk libreadline-dev", hide=hide)
elif os_info.like == OsLike.REDHAT:
c.run("sudo yum install -y libtool gawk readline-devel", hide=hide)
print("cloning ospf mdr...")
clone_dir = "/tmp/ospf-mdr"
c.run(
f"git clone https://github.com/USNavalResearchLaboratory/ospf-mdr {clone_dir}",
hide=hide
)
with c.cd(clone_dir):
print("building ospf mdr...")
c.run("./bootstrap.sh", hide=hide)
c.run(
"./configure --disable-doc --enable-user=root --enable-group=root "
"--with-cflags=-ggdb --sysconfdir=/usr/local/etc/quagga --enable-vtysh "
"--localstatedir=/var/run/quagga",
hide=hide
)
c.run("make", hide=hide)
print("installing ospf mdr...")
c.run("sudo make install", hide=hide)
@task
def install(c, dev=False, verbose=False):
"""
install core
"""
hide = not verbose
os_info = get_os()
install_system(c, os_info, hide)
install_grpcio(c, hide)
build(c, hide)
install_core(c, hide)
install_poetry(c, dev, hide)
install_ospf_mdr(c, os_info, hide)
print("please open a new terminal or re-login to leverage invoke for running core")
print("# run daemon")
print("inv daemon")
print("# run gui")
print("inv gui")
2020-07-10 07:01:28 +01:00
@task
def daemon(c):
"""
2020-07-10 07:01:28 +01:00
start core-daemon
"""
2020-07-10 07:01:28 +01:00
python = get_python(c)
with c.cd(DAEMON_DIR):
c.run(
2020-07-10 07:01:28 +01:00
f"sudo {python} scripts/core-daemon "
"-f data/core.conf -l data/logging.conf",
pty=True
)
@task
def gui(c):
"""
2020-07-10 07:01:28 +01:00
start core-pygui
"""
2020-07-10 07:01:28 +01:00
with c.cd(DAEMON_DIR):
c.run("poetry run scripts/core-pygui", pty=True)
@task
def test(c):
"""
2020-07-10 07:01:28 +01:00
run core tests
"""
2020-07-10 07:01:28 +01:00
pytest = get_pytest(c)
with c.cd(DAEMON_DIR):
c.run(f"sudo {pytest} -v --lf -x tests", pty=True)
@task
def test_mock(c):
"""
2020-07-10 07:01:28 +01:00
run core tests using mock to avoid running as sudo
"""
2020-07-10 07:01:28 +01:00
with c.cd(DAEMON_DIR):
c.run("poetry run pytest -v --mock --lf -x tests", pty=True)
@task
def test_emane(c):
"""
2020-07-10 07:01:28 +01:00
run core emane tests
"""
2020-07-10 07:01:28 +01:00
pytest = get_pytest(c)
with c.cd(DAEMON_DIR):
c.run(f"{pytest} -v --lf -x tests/emane", pty=True)