From 258d31c170e6359f54c347b04b516e8a6c3b051c Mon Sep 17 00:00:00 2001 From: Cian Hughes Date: Fri, 27 Oct 2023 12:31:31 +0100 Subject: [PATCH] Added automatic install to disk --- autoignition.py | 12 ++------ config.py | 7 ++++- main.py => create_disk.py | 60 +++++++++++++++++++++++++-------------- 3 files changed, 48 insertions(+), 31 deletions(-) rename main.py => create_disk.py (70%) diff --git a/autoignition.py b/autoignition.py index 08f7230..a2d2b29 100644 --- a/autoignition.py +++ b/autoignition.py @@ -14,6 +14,7 @@ import typer from config import ( CLEANUP_IMAGES, CLIENT, + CWD_MOUNT, CWD_MOUNTDIR, DOCKERFILE_DIR, FUELIGNITION_BUILD_DIR, @@ -23,7 +24,6 @@ from config import ( SELENIUM_INIT_MESSAGE, ) from debug import debug_mode -import docker def create_driver(): @@ -104,7 +104,7 @@ def build_fuelignition(): dockerfile = "Dockerfile" if root_container: dockerfile = DOCKERFILE_DIR / "fuel-ignition.dockerfile" - image = CLIENT.images.build( + image, _ = CLIENT.images.build( path=str(FUELIGNITION_BUILD_DIR), dockerfile=str(dockerfile), tag="fuel-ignition", @@ -128,13 +128,7 @@ def json_to_img(fuelignition_json: str, img_path: str) -> None: detach=True, remove=True, ports={4444: 4444, 7900: 7900}, - mounts=[ - docker.types.Mount( - target=str(CWD_MOUNTDIR), - source=str(ROOT_DIR), - type="bind", - ) - ], + mounts=[CWD_MOUNT,], ) fuelignition_image = build_fuelignition() fuelignition_container = CLIENT.containers.run( diff --git a/config.py b/config.py index d470d4e..84afdac 100644 --- a/config.py +++ b/config.py @@ -18,8 +18,13 @@ def apply_config(config: dict) -> None: config["ROOT_DIR"] = Path(config["ROOT_DIR"]).absolute() config["BUILD_DIR"] = Path(config["BUILD_DIR"]).absolute() config["DOCKERFILE_DIR"] = Path(config["DOCKERFILE_DIR"]).absolute() - config["CWD_MOUNTDIR"] = Path(config["CWD_MOUNTDIR"]) config["FUELIGNITION_BUILD_DIR"] = config["BUILD_DIR"] / config["FUELIGNITION_BUILD_DIR"] + config["CWD_MOUNTDIR"] = Path(config["CWD_MOUNTDIR"]) + config["CWD_MOUNT"] = docker.types.Mount( + target=str(config["CWD_MOUNTDIR"]), + source=str(config["ROOT_DIR"]), + type="bind", + ) globals().update(config) diff --git a/main.py b/create_disk.py similarity index 70% rename from main.py rename to create_disk.py index 2a4964e..7d843cc 100644 --- a/main.py +++ b/create_disk.py @@ -1,4 +1,5 @@ from fnmatch import fnmatch +from pathlib import Path from typing import Annotated import typer @@ -6,13 +7,13 @@ import typer from config import ( CLEANUP_IMAGES, CLIENT, + CWD_MOUNT, CWD_MOUNTDIR, DOCKERFILE_DIR, - ROOT_DIR, ) from create_img import create_img from debug import debug_mode -import docker +from docker.types import Mount def filter_validation_response(response: str) -> str: @@ -28,7 +29,7 @@ def filter_validation_response(response: str) -> str: def validation_result() -> str: dockerfile = DOCKERFILE_DIR / "validate.dockerfile" - image = CLIENT.images.build( + image, _ = CLIENT.images.build( path=".", dockerfile=str(dockerfile), tag="validate", @@ -39,13 +40,7 @@ def validation_result() -> str: ) response = CLIENT.containers.run( image, - mounts=[ - docker.types.Mount( - target=str(CWD_MOUNTDIR), - source=str(ROOT_DIR), - type="bind", - ) - ], + mounts=[CWD_MOUNT,], remove=True, ) if CLEANUP_IMAGES: @@ -59,7 +54,36 @@ def validate() -> (bool, str): return (not bool(response), response) +def write_disk(disk: str) -> None: + CLIENT.containers.run( + "alpine", + mounts=[CWD_MOUNT, Mount("/ignition_disk", disk, type="bind")], + privileged=True, + command=f"dd if={CWD_MOUNTDIR}/build/ignition.img of=/ignition_disk" + ) + + +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[ str, @@ -80,17 +104,11 @@ def main( debug: Annotated[bool, typer.Option(help="Enable debug mode")] = False, ) -> None: debug_mode(debug) - # f = create_img - # if debug: - # f = ss(f) # noqa: F821, # type: ignore #? ss is installed in debug_mode - # f(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!") + 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) if __name__ == "__main__":