Added automatic install to disk

This commit is contained in:
Cian Hughes
2023-10-27 12:31:31 +01:00
parent 202d3d355f
commit 258d31c170
3 changed files with 48 additions and 31 deletions

View File

@@ -14,6 +14,7 @@ import typer
from config import ( from config import (
CLEANUP_IMAGES, CLEANUP_IMAGES,
CLIENT, CLIENT,
CWD_MOUNT,
CWD_MOUNTDIR, CWD_MOUNTDIR,
DOCKERFILE_DIR, DOCKERFILE_DIR,
FUELIGNITION_BUILD_DIR, FUELIGNITION_BUILD_DIR,
@@ -23,7 +24,6 @@ from config import (
SELENIUM_INIT_MESSAGE, SELENIUM_INIT_MESSAGE,
) )
from debug import debug_mode from debug import debug_mode
import docker
def create_driver(): def create_driver():
@@ -104,7 +104,7 @@ def build_fuelignition():
dockerfile = "Dockerfile" dockerfile = "Dockerfile"
if root_container: if root_container:
dockerfile = DOCKERFILE_DIR / "fuel-ignition.dockerfile" dockerfile = DOCKERFILE_DIR / "fuel-ignition.dockerfile"
image = CLIENT.images.build( image, _ = CLIENT.images.build(
path=str(FUELIGNITION_BUILD_DIR), path=str(FUELIGNITION_BUILD_DIR),
dockerfile=str(dockerfile), dockerfile=str(dockerfile),
tag="fuel-ignition", tag="fuel-ignition",
@@ -128,13 +128,7 @@ def json_to_img(fuelignition_json: str, img_path: str) -> None:
detach=True, detach=True,
remove=True, remove=True,
ports={4444: 4444, 7900: 7900}, ports={4444: 4444, 7900: 7900},
mounts=[ mounts=[CWD_MOUNT,],
docker.types.Mount(
target=str(CWD_MOUNTDIR),
source=str(ROOT_DIR),
type="bind",
)
],
) )
fuelignition_image = build_fuelignition() fuelignition_image = build_fuelignition()
fuelignition_container = CLIENT.containers.run( fuelignition_container = CLIENT.containers.run(

View File

@@ -18,8 +18,13 @@ def apply_config(config: dict) -> None:
config["ROOT_DIR"] = Path(config["ROOT_DIR"]).absolute() config["ROOT_DIR"] = Path(config["ROOT_DIR"]).absolute()
config["BUILD_DIR"] = Path(config["BUILD_DIR"]).absolute() config["BUILD_DIR"] = Path(config["BUILD_DIR"]).absolute()
config["DOCKERFILE_DIR"] = Path(config["DOCKERFILE_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["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) globals().update(config)

View File

@@ -1,4 +1,5 @@
from fnmatch import fnmatch from fnmatch import fnmatch
from pathlib import Path
from typing import Annotated from typing import Annotated
import typer import typer
@@ -6,13 +7,13 @@ import typer
from config import ( from config import (
CLEANUP_IMAGES, CLEANUP_IMAGES,
CLIENT, CLIENT,
CWD_MOUNT,
CWD_MOUNTDIR, CWD_MOUNTDIR,
DOCKERFILE_DIR, DOCKERFILE_DIR,
ROOT_DIR,
) )
from create_img import create_img from create_img import create_img
from debug import debug_mode from debug import debug_mode
import docker from docker.types import Mount
def filter_validation_response(response: str) -> str: def filter_validation_response(response: str) -> str:
@@ -28,7 +29,7 @@ def filter_validation_response(response: str) -> str:
def validation_result() -> str: def validation_result() -> str:
dockerfile = DOCKERFILE_DIR / "validate.dockerfile" dockerfile = DOCKERFILE_DIR / "validate.dockerfile"
image = CLIENT.images.build( image, _ = CLIENT.images.build(
path=".", path=".",
dockerfile=str(dockerfile), dockerfile=str(dockerfile),
tag="validate", tag="validate",
@@ -39,13 +40,7 @@ def validation_result() -> str:
) )
response = CLIENT.containers.run( response = CLIENT.containers.run(
image, image,
mounts=[ mounts=[CWD_MOUNT,],
docker.types.Mount(
target=str(CWD_MOUNTDIR),
source=str(ROOT_DIR),
type="bind",
)
],
remove=True, remove=True,
) )
if CLEANUP_IMAGES: if CLEANUP_IMAGES:
@@ -59,7 +54,36 @@ def validate() -> (bool, str):
return (not bool(response), response) 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( 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)], hostname: Annotated[str, typer.Option(help="Hostname for the new node", prompt=True)],
password: Annotated[ password: Annotated[
str, str,
@@ -80,17 +104,11 @@ def main(
debug: Annotated[bool, typer.Option(help="Enable debug mode")] = False, debug: Annotated[bool, typer.Option(help="Enable debug mode")] = False,
) -> None: ) -> None:
debug_mode(debug) debug_mode(debug)
# f = create_img Path("build").mkdir(exist_ok=True, parents=True)
# if debug: f = create_ignition_disk
# f = ss(f) # noqa: F821, # type: ignore #? ss is installed in debug_mode if debug:
# f(hostname, password, switch_ip_address, switch_port, swarm_token) f = ss(f) # noqa: F821, # type: ignore #? ss is installed in debug_mode
create_img(hostname, password, switch_ip_address, switch_port, swarm_token) f(disk, 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!")
if __name__ == "__main__": if __name__ == "__main__":