All tests implemented. Formatt, lint & typecheck

This commit is contained in:
Cian Hughes
2023-11-03 17:42:25 +00:00
parent 2f452892b0
commit 1f312c89c7
12 changed files with 180 additions and 73 deletions

View File

@@ -10,4 +10,4 @@ __all__ = [
"autoignition",
"create_img",
"create_disk",
]
]

View File

@@ -1,20 +1,23 @@
#!/usr/bin/env python
def main() -> None:
"""Entry point for the CLI
"""
"""Entry point for the CLI"""
from .config import config
config.update_config("cli")
from .node_deployer import app
app()
def debug() -> None:
"""Entry point for the debug CLI
"""
"""Entry point for the debug CLI"""
from .config import config
config.update_config("debug")
from .node_deployer import app
# Below, we set the default value of the debug flag
# for the base function of each command to True
def unwrap(f): # Not a closure, just here to avoid polluting the namespace
@@ -28,8 +31,9 @@ def debug() -> None:
defaults = list(f.__defaults__)
defaults[-1] = True
f.__defaults__ = tuple(defaults)
app()
if __name__ == "__main__":
main()
main()

View File

@@ -39,7 +39,7 @@ def create_driver(port: int) -> webdriver.Remote:
def convert_json_via_fuelignition(
container: docker.models.containers.Container, # type: ignore
driver: webdriver.Remote,
fuelignition_json: Path,
fuelignition_json: Path,
img_path: Path,
) -> None:
"""Converts a fuel-ignition json file to an ignition disk image file
@@ -91,7 +91,7 @@ def convert_json_via_fuelignition(
f.write(container_image.read())
def build_fuelignition() -> docker.models.images.Image: # type: ignore
def build_fuelignition() -> docker.models.images.Image: # type: ignore
"""Builds the fuel-ignition docker image
Returns:
@@ -148,7 +148,8 @@ def json_to_img(
json_path: Annotated[
Path,
typer.Option(
"--json-path", "-i",
"--json-path",
"-i",
help="The fuel-ignition json for configuring the disk image",
prompt=True,
exists=True,
@@ -158,7 +159,8 @@ def json_to_img(
img_path: Annotated[
Path,
typer.Option(
"--img-path", "-o",
"--img-path",
"-o",
help="The file to output the disk image to",
prompt=True,
dir_okay=False,
@@ -176,7 +178,7 @@ def json_to_img(
flag_value=True,
expose_value=config.DEBUG,
hidden=not config.DEBUG,
)
),
] = False,
) -> None:
"""Converts a fuel-ignition json file to an ignition disk image file

View File

@@ -9,15 +9,17 @@ import tomllib
CLIENT = docker.from_env(version="auto")
MAX_PORT: int = 65535
def __get_project_root():
r = Path(__file__)
while r.name != "src":
r = r.parent
return r.parent
PROJECT_ROOT: Path = __get_project_root()
ConfigLabel = Union[str, list[str]] # After PEP695 support: type ConfigLabel = str | list[str]
ConfigLabel = Union[str, list[str]] # After PEP695 support: type ConfigLabel = str | list[str]
class Config(SimpleNamespace):
@@ -82,7 +84,8 @@ class Config(SimpleNamespace):
"FUELIGNITION_BUILD_DIR", self.FUELIGNITION_BUILD_DIR
)
config["DOCKERFILE_DIR"] = src_dir / config.get("DOCKERFILE_DIR", self.DOCKERFILE_DIR)
config["CWD_MOUNT"] = docker.types.Mount( # type: ignore <- I really wish docker-py had typeshed stubs
# I really wish docker-py had typeshed stubs
config["CWD_MOUNT"] = docker.types.Mount( # type: ignore
target=str(cwd_mountdir),
source=str(PROJECT_ROOT),
type="bind",

View File

@@ -168,7 +168,7 @@ def create_ignition_disk(
is_flag=True,
flag_value=True,
hidden=not config.DEBUG,
)
),
] = False,
) -> None:
"""Creates an ignition image and writes it to the specified disk
@@ -202,15 +202,15 @@ def create_ignition_disk(
# Guard against the user specifying no disk
if disk is None:
raise typer.BadParameter("No disk specified")
create_img(
hostname = hostname,
password = password,
switch_ip = switch_ip,
switch_port = switch_port,
swarm_token = swarm_token,
img_path = config.BUILD_DIR / "ignition.img",
debug = debug,
hostname=hostname,
password=password,
switch_ip=switch_ip,
switch_port=switch_port,
swarm_token=swarm_token,
img_path=config.BUILD_DIR / "ignition.img",
debug=debug,
)
valid, response = validate()
if not valid:

View File

@@ -163,7 +163,7 @@ def create_img(
flag_value=True,
hidden=not config.DEBUG,
),
] = False,
] = False,
) -> None:
"""Creates an ignition image for a node that will automatically join a swarm
@@ -195,7 +195,7 @@ def create_img(
raise typer.BadParameter("Password must be specified")
elif password is None:
password = ""
# get swarm configuration as JSON
swarm_config = {
"SWITCH_IP_ADDRESS": str(switch_ip),

View File

@@ -8,7 +8,8 @@ from .config import config
def get_debug_f(f: Callable) -> Callable:
import snoop # type: ignore
import snoop # type: ignore
return wraps(f)(snoop.snoop(**config.snoop["snoop"])(f))
@@ -43,7 +44,7 @@ def debug_guard(f: Callable) -> Callable:
debug_f = get_debug_f(f)
if kwargs.get("debug", False):
# Snoop depth is set to compensate for wrapper stack frames
return debug_f(*args, **kwargs) # noqa: F821 #* ss is installed in debug_mode
return debug_f(*args, **kwargs) # noqa: F821 #* ss is installed in debug_mode
else:
return f(*args, **kwargs)

View File

@@ -20,19 +20,12 @@ app = typer.Typer(
)
# Register commands
app.command(
help = str(create_ignition_disk.__doc__).split("Args:")[0].strip(),
**cmd_params
)(create_ignition_disk)
app.command(
help = str(create_img.__doc__).split("Args:")[0].strip(),
**cmd_params
)(create_img)
app.command(
help = str(json_to_img.__doc__).split("Args:")[0].strip(),
**cmd_params
)(json_to_img)
app.command(help=str(create_ignition_disk.__doc__).split("Args:")[0].strip(), **cmd_params)(
create_ignition_disk
)
app.command(help=str(create_img.__doc__).split("Args:")[0].strip(), **cmd_params)(create_img)
app.command(help=str(json_to_img.__doc__).split("Args:")[0].strip(), **cmd_params)(json_to_img)
if __name__ == "__main__":
config.update_config("cli")
app()
app()

View File

@@ -1,6 +1,6 @@
from functools import wraps
from pathlib import Path
from typing import Callable
from typing import Callable, List
import docker
from .config import config
@@ -45,14 +45,14 @@ def next_free_tcp_port(port: int) -> int:
Args:
port (int): The port to start searching from
Raises:
ValueError: If no free ports are found
Returns:
int: The next free port
"""
ports = []
ports: List[int] = []
try:
containers = config.CLIENT.containers.list(all=True)
ports = []
@@ -63,15 +63,14 @@ def next_free_tcp_port(port: int) -> int:
for x in list(container.ports.values())[0]:
ports.append(int(x["HostPort"]))
except docker.errors.NotFound: # type: ignore
#* This error is raised if container list changes between getting the list and
#* getting the ports. If this happens, just try again
# * This error is raised if container list changes between getting the list and
# * getting the ports. If this happens, just try again
return next_free_tcp_port(port)
if not ports:
return port
ports = set(ports)
while port in ports:
unique_ports = set(ports)
while port in unique_ports:
port += 1
if port > 65535:
raise ValueError("No free ports")
return port