2020-07-10 07:01:28 +01:00
|
|
|
import os
|
2020-07-10 16:32:47 +01:00
|
|
|
import sys
|
|
|
|
from enum import Enum
|
2020-07-10 07:01:28 +01:00
|
|
|
|
2020-07-10 16:32:47 +01:00
|
|
|
from invoke import task, Context
|
2020-06-02 22:48:57 +01:00
|
|
|
|
2020-07-10 16:32:47 +01:00
|
|
|
DAEMON_DIR: str = "daemon"
|
|
|
|
VCMD_DIR: str = "netns"
|
|
|
|
GUI_DIR: str = "gui"
|
2020-07-10 07:01:28 +01:00
|
|
|
|
|
|
|
|
2020-07-10 16:32:47 +01:00
|
|
|
class OsName(Enum):
|
|
|
|
UBUNTU = "ubuntu"
|
|
|
|
CENTOS = "centos"
|
|
|
|
|
|
|
|
|
|
|
|
class OsLike(Enum):
|
|
|
|
DEBIAN = "debian"
|
|
|
|
|
|
|
|
|
|
|
|
class OsInfo:
|
|
|
|
def __init__(self, name: OsName, like: OsLike, version: str) -> None:
|
|
|
|
self.name: OsName = name
|
|
|
|
self.like: OsLike = like
|
|
|
|
self.version: str = 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")
|
|
|
|
|
|
|
|
|
2020-07-10 16:32:47 +01:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
2020-07-10 16:32:47 +01:00
|
|
|
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()
|
|
|
|
key, value = line.split("=")
|
2020-07-10 16:32:47 +01:00
|
|
|
d[key] = value.strip('"')
|
|
|
|
name_value = d["ID"]
|
|
|
|
like_value = d["ID_LIKE"]
|
|
|
|
try:
|
|
|
|
name = OsName(name_value)
|
|
|
|
like = OsLike(like_value)
|
|
|
|
except ValueError:
|
|
|
|
print(f"unsupported os({name_value}) like({like_value})")
|
|
|
|
sys.exit(1)
|
|
|
|
version = d["VERSION_ID"]
|
|
|
|
return OsInfo(name, like, version)
|
|
|
|
|
|
|
|
|
2020-07-10 16:51:40 +01:00
|
|
|
def install_system(c: Context, os_info: OsInfo, hide: bool) -> None:
|
2020-07-10 07:01:28 +01:00
|
|
|
print("installing system dependencies...")
|
2020-07-10 16:32:47 +01:00
|
|
|
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 "
|
2020-07-10 16:51:40 +01:00
|
|
|
"ethtool tk python3-tk", hide=hide
|
2020-07-10 07:01:28 +01:00
|
|
|
)
|
2020-07-10 16:32:47 +01:00
|
|
|
|
|
|
|
|
2020-07-10 16:51:40 +01:00
|
|
|
def install_grpcio(c: Context, hide: bool) -> None:
|
2020-07-10 07:01:28 +01:00
|
|
|
print("installing grpcio-tools...")
|
2020-07-10 16:51:40 +01:00
|
|
|
c.run("python3 -m pip install --user grpcio-tools", hide=hide)
|
2020-07-10 16:32:47 +01:00
|
|
|
|
|
|
|
|
2020-07-10 16:51:40 +01:00
|
|
|
def build(c: Context, hide: bool) -> None:
|
2020-07-10 07:01:28 +01:00
|
|
|
print("building core...")
|
2020-07-10 16:51:40 +01:00
|
|
|
c.run("./bootstrap.sh", hide=hide)
|
|
|
|
c.run("./configure", hide=hide)
|
2020-07-10 17:55:30 +01:00
|
|
|
c.run("make", hide=hide)
|
2020-07-10 16:32:47 +01:00
|
|
|
|
|
|
|
|
2020-07-10 16:51:40 +01:00
|
|
|
def install_core(c: Context, hide: bool) -> None:
|
2020-07-10 17:55:30 +01:00
|
|
|
print("installing core vcmd...")
|
2020-07-10 07:01:28 +01:00
|
|
|
with c.cd(VCMD_DIR):
|
2020-07-10 16:51:40 +01:00
|
|
|
c.run("sudo make install", hide=hide)
|
2020-07-10 17:55:30 +01:00
|
|
|
print("installing core gui...")
|
2020-07-10 07:01:28 +01:00
|
|
|
with c.cd(GUI_DIR):
|
2020-07-10 16:51:40 +01:00
|
|
|
c.run("sudo make install", hide=hide)
|
2020-07-10 16:32:47 +01:00
|
|
|
|
|
|
|
|
2020-07-10 16:51:40 +01:00
|
|
|
def install_poetry(c: Context, dev: bool, hide: bool) -> None:
|
2020-07-10 07:01:28 +01:00
|
|
|
print("installing poetry...")
|
2020-07-10 16:51:40 +01:00
|
|
|
c.run("pipx install poetry", hide=hide)
|
2020-07-10 16:32:47 +01:00
|
|
|
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...")
|
2020-07-10 16:51:40 +01:00
|
|
|
c.run(f"poetry install {args}", hide=hide)
|
2020-07-10 16:32:47 +01:00
|
|
|
if dev:
|
|
|
|
c.run("poetry run pre-commit install")
|
|
|
|
|
|
|
|
|
2020-07-10 16:51:40 +01:00
|
|
|
def install_ospf_mdr(c: Context, os_info: OsInfo, hide: bool) -> None:
|
|
|
|
if c.run("which zebra", warn=True, hide=hide):
|
2020-07-10 16:32:47 +01:00
|
|
|
print("quagga already installed, skipping ospf mdr")
|
|
|
|
return
|
2020-07-10 17:20:13 +01:00
|
|
|
print("installing ospf mdr dependencies...")
|
2020-07-10 16:32:47 +01:00
|
|
|
if os_info.like == OsLike.DEBIAN:
|
2020-07-10 16:51:40 +01:00
|
|
|
c.run("sudo apt install -y libtool gawk libreadline-dev", hide=hide)
|
2020-07-10 17:20:13 +01:00
|
|
|
print("cloning ospf mdr...")
|
2020-07-10 16:32:47 +01:00
|
|
|
clone_dir = "/tmp/ospf-mdr"
|
|
|
|
c.run(
|
2020-07-10 16:51:40 +01:00
|
|
|
f"git clone https://github.com/USNavalResearchLaboratory/ospf-mdr {clone_dir}",
|
|
|
|
hide=hide
|
2020-07-10 16:32:47 +01:00
|
|
|
)
|
|
|
|
with c.cd(clone_dir):
|
2020-07-10 17:20:13 +01:00
|
|
|
print("building ospf mdr...")
|
2020-07-10 16:51:40 +01:00
|
|
|
c.run("./bootstrap.sh", hide=hide)
|
2020-07-10 16:32:47 +01:00
|
|
|
c.run(
|
|
|
|
"./configure --disable-doc --enable-user=root --enable-group=root "
|
|
|
|
"--with-cflags=-ggdb --sysconfdir=/usr/local/etc/quagga --enable-vtysh "
|
2020-07-10 16:51:40 +01:00
|
|
|
"--localstatedir=/var/run/quagga",
|
|
|
|
hide=hide
|
2020-07-10 16:32:47 +01:00
|
|
|
)
|
2020-07-10 17:55:30 +01:00
|
|
|
c.run("make", hide=hide)
|
2020-07-10 17:20:13 +01:00
|
|
|
print("installing ospf mdr...")
|
2020-07-10 16:51:40 +01:00
|
|
|
c.run("sudo make install", hide=hide)
|
2020-07-10 16:32:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2020-07-10 16:51:40 +01:00
|
|
|
def install(c, dev=False, verbose=False):
|
2020-07-10 16:32:47 +01:00
|
|
|
"""
|
|
|
|
install core
|
|
|
|
"""
|
2020-07-10 16:51:40 +01:00
|
|
|
hide = not verbose
|
2020-07-10 16:32:47 +01:00
|
|
|
os_info = get_os()
|
2020-07-10 16:51:40 +01:00
|
|
|
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)
|
2020-07-10 18:27:17 +01:00
|
|
|
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
|
|
|
|
2020-06-02 22:48:57 +01:00
|
|
|
|
|
|
|
@task
|
2020-07-08 07:38:12 +01:00
|
|
|
def daemon(c):
|
|
|
|
"""
|
2020-07-10 07:01:28 +01:00
|
|
|
start core-daemon
|
2020-07-08 07:38:12 +01:00
|
|
|
"""
|
2020-07-10 07:01:28 +01:00
|
|
|
python = get_python(c)
|
|
|
|
with c.cd(DAEMON_DIR):
|
2020-07-08 07:38:12 +01:00
|
|
|
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
|
2020-07-08 07:38:12 +01:00
|
|
|
)
|
2020-06-02 22:48:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2020-07-08 07:38:12 +01:00
|
|
|
def gui(c):
|
|
|
|
"""
|
2020-07-10 07:01:28 +01:00
|
|
|
start core-pygui
|
2020-07-08 07:38:12 +01:00
|
|
|
"""
|
2020-07-10 07:01:28 +01:00
|
|
|
with c.cd(DAEMON_DIR):
|
|
|
|
c.run("poetry run scripts/core-pygui", pty=True)
|
2020-07-08 07:38:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def test(c):
|
|
|
|
"""
|
2020-07-10 07:01:28 +01:00
|
|
|
run core tests
|
2020-07-08 07:38:12 +01:00
|
|
|
"""
|
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)
|
2020-07-08 07:38:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
@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-08 07:38:12 +01:00
|
|
|
"""
|
2020-07-10 07:01:28 +01:00
|
|
|
with c.cd(DAEMON_DIR):
|
2020-07-08 07:38:12 +01:00
|
|
|
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-08 07:38:12 +01:00
|
|
|
"""
|
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)
|