Refactored for better config & cleaner debug mode

This commit is contained in:
Cian Hughes
2023-10-27 14:11:00 +01:00
parent 258d31c170
commit 0c4135817d
9 changed files with 196 additions and 175 deletions

View File

@@ -1,18 +1,12 @@
from fnmatch import fnmatch
from pathlib import Path
from typing import Annotated
import typer
from config import (
CLEANUP_IMAGES,
CLIENT,
CWD_MOUNT,
CWD_MOUNTDIR,
DOCKERFILE_DIR,
)
import config
from create_img import create_img
from debug import debug_mode
from debug import debug_guard
from utils import ensure_build_dir
from docker.types import Mount
@@ -28,22 +22,24 @@ def filter_validation_response(response: str) -> str:
def validation_result() -> str:
dockerfile = DOCKERFILE_DIR / "validate.dockerfile"
image, _ = CLIENT.images.build(
dockerfile = config.DOCKERFILE_DIR / "validate.dockerfile"
image, _ = config.CLIENT.images.build(
path=".",
dockerfile=str(dockerfile),
tag="validate",
buildargs={"CWD_MOUNTDIR": str(CWD_MOUNTDIR)},
rm=CLEANUP_IMAGES,
buildargs={"CWD_MOUNTDIR": str(config.CWD_MOUNTDIR)},
rm=config.CLEANUP_IMAGES,
pull=True,
quiet=True,
)
response = CLIENT.containers.run(
response = config.CLIENT.containers.run(
image,
mounts=[CWD_MOUNT,],
mounts=[
config.CWD_MOUNT,
],
remove=True,
)
if CLEANUP_IMAGES:
if config.CLEANUP_IMAGES:
image.remove(force=True)
return response
@@ -55,34 +51,17 @@ def validate() -> (bool, str):
def write_disk(disk: str) -> None:
CLIENT.containers.run(
config.CLIENT.containers.run(
"alpine",
mounts=[CWD_MOUNT, Mount("/ignition_disk", disk, type="bind")],
mounts=[config.CWD_MOUNT, Mount("/ignition_disk", disk, type="bind")],
privileged=True,
command=f"dd if={CWD_MOUNTDIR}/build/ignition.img of=/ignition_disk"
command=f"dd if={config.CWD_MOUNTDIR}/build/ignition.img of=/ignition_disk",
)
@debug_guard
@ensure_build_dir
def create_ignition_disk(
disk: str,
hostname: str,
password: str,
switch_ip_address: str,
switch_port: int,
swarm_token: str,
debug: bool = False,
) -> None:
create_img(hostname, password, switch_ip_address, switch_port, swarm_token)
valid, response = validate()
if not valid:
print(response)
raise typer.Exit(1)
else:
print("Valid ignition image created!")
write_disk(disk)
def main(
disk: Annotated[str, typer.Option(help="Path to the disk to write to", prompt=True)],
hostname: Annotated[str, typer.Option(help="Hostname for the new node", prompt=True)],
password: Annotated[
@@ -101,15 +80,16 @@ def main(
swarm_token: Annotated[
str, typer.Option(help="Swarm token for connecting to the swarm", prompt=True)
],
debug: Annotated[bool, typer.Option(help="Enable debug mode")] = False,
) -> None:
debug_mode(debug)
Path("build").mkdir(exist_ok=True, parents=True)
f = create_ignition_disk
if debug:
f = ss(f) # noqa: F821, # type: ignore #? ss is installed in debug_mode
f(disk, hostname, password, switch_ip_address, switch_port, swarm_token)
create_img(hostname, password, switch_ip_address, switch_port, swarm_token)
valid, response = validate()
if not valid:
print(response)
raise typer.Exit(1)
else:
print("Valid ignition image created!")
write_disk(disk)
if __name__ == "__main__":
typer.run(main)
typer.run(create_ignition_disk)