mirror of
https://github.com/nix-community/disko.git
synced 2026-12-01 21:43:29 +01:00
disko2: Refactor dev mode, add disko dev eval
This commit is contained in:
1 parent
c5da562d0d
commit
fa44467f82
5 files changed
+92
-55
No files matched your search
+17
-28
@@ -2,17 +2,14 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from typing import Any, Literal
|
||||||
from typing import Any, Literal, assert_never
|
|
||||||
|
|
||||||
from disko_lib.ansi import disko_dev_ansi
|
from disko.mode_dev import run_dev
|
||||||
from disko_lib.eval_config import eval_disko_file, eval_flake
|
from disko_lib.eval_config import eval_config
|
||||||
from disko_lib.logging import LOGGER, debug, info
|
from disko_lib.logging import LOGGER, debug, info
|
||||||
from disko_lib.messages import err_missing_arguments
|
from disko_lib.messages.msgs import err_missing_mode
|
||||||
from disko_lib.messages.msgs import err_missing_mode, err_too_many_arguments
|
from disko_lib.result import DiskoError, DiskoResult, exit_on_error
|
||||||
from disko_lib.result import DiskoError, DiskoSuccess, DiskoResult, exit_on_error
|
|
||||||
from disko_lib.types.disk import generate_config
|
from disko_lib.types.disk import generate_config
|
||||||
from disko_lib.types.device import disko_dev_lsblk
|
|
||||||
|
|
||||||
Mode = Literal[
|
Mode = Literal[
|
||||||
"destroy",
|
"destroy",
|
||||||
@@ -49,31 +46,13 @@ MODE_DESCRIPTION: dict[Mode, str] = {
|
|||||||
def run_apply(
|
def run_apply(
|
||||||
*, mode: str, disko_file: str | None, flake: str | None, **_kwargs: dict[str, Any]
|
*, mode: str, disko_file: str | None, flake: str | None, **_kwargs: dict[str, Any]
|
||||||
) -> DiskoResult[dict[str, Any]]:
|
) -> DiskoResult[dict[str, Any]]:
|
||||||
# match would be nicer, but mypy doesn't understand type narrowing in tuples
|
return eval_config(disko_file=disko_file, flake=flake)
|
||||||
if not disko_file and not flake:
|
|
||||||
return DiskoError.single_message(err_missing_arguments, "validate args")
|
|
||||||
if not disko_file and flake:
|
|
||||||
return eval_flake(flake)
|
|
||||||
if disko_file and not flake:
|
|
||||||
return eval_disko_file(Path(disko_file))
|
|
||||||
|
|
||||||
return DiskoError.single_message(err_too_many_arguments, "validate args")
|
|
||||||
|
|
||||||
|
|
||||||
def run_generate() -> DiskoResult[dict[str, Any]]:
|
def run_generate() -> DiskoResult[dict[str, Any]]:
|
||||||
return generate_config()
|
return generate_config()
|
||||||
|
|
||||||
|
|
||||||
def run_dev(args: argparse.Namespace) -> DiskoResult[None]:
|
|
||||||
match args.dev_command:
|
|
||||||
case "lsblk":
|
|
||||||
return disko_dev_lsblk()
|
|
||||||
case "ansi":
|
|
||||||
return DiskoSuccess(None, disko_dev_ansi())
|
|
||||||
case _:
|
|
||||||
assert_never(args.dev_command)
|
|
||||||
|
|
||||||
|
|
||||||
def run(
|
def run(
|
||||||
args: argparse.Namespace,
|
args: argparse.Namespace,
|
||||||
) -> DiskoResult[None | dict[str, Any]]:
|
) -> DiskoResult[None | dict[str, Any]]:
|
||||||
@@ -115,6 +94,9 @@ def parse_args() -> argparse.Namespace:
|
|||||||
mode,
|
mode,
|
||||||
help=MODE_DESCRIPTION[mode],
|
help=MODE_DESCRIPTION[mode],
|
||||||
)
|
)
|
||||||
|
return parser
|
||||||
|
|
||||||
|
def add_common_apply_args(parser: argparse.ArgumentParser) -> None:
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"disko_file",
|
"disko_file",
|
||||||
nargs="?",
|
nargs="?",
|
||||||
@@ -126,10 +108,11 @@ def parse_args() -> argparse.Namespace:
|
|||||||
"-f",
|
"-f",
|
||||||
help="Flake to fetch the disko configuration from",
|
help="Flake to fetch the disko configuration from",
|
||||||
)
|
)
|
||||||
return parser
|
|
||||||
|
|
||||||
# Commands to apply an existing configuration
|
# Commands to apply an existing configuration
|
||||||
apply_parsers = [create_apply_parser(mode) for mode in APPLY_MODES]
|
apply_parsers = [create_apply_parser(mode) for mode in APPLY_MODES]
|
||||||
|
for parser in apply_parsers:
|
||||||
|
add_common_apply_args(parser)
|
||||||
|
|
||||||
# Other commands
|
# Other commands
|
||||||
generate_parser = mode_parsers.add_parser(
|
generate_parser = mode_parsers.add_parser(
|
||||||
@@ -144,6 +127,12 @@ def parse_args() -> argparse.Namespace:
|
|||||||
).add_subparsers(dest="dev_command")
|
).add_subparsers(dest="dev_command")
|
||||||
dev_parsers.add_parser("lsblk", help="List block devices the way disko sees them")
|
dev_parsers.add_parser("lsblk", help="List block devices the way disko sees them")
|
||||||
dev_parsers.add_parser("ansi", help="Print defined ansi color codes")
|
dev_parsers.add_parser("ansi", help="Print defined ansi color codes")
|
||||||
|
|
||||||
|
dev_eval_parser = dev_parsers.add_parser(
|
||||||
|
"eval", help="Evaluate a disko configuration and print the result as JSON"
|
||||||
|
)
|
||||||
|
add_common_apply_args(dev_eval_parser)
|
||||||
|
|
||||||
return root_parser.parse_args()
|
return root_parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
from logging import info
|
||||||
|
from typing import Any, assert_never
|
||||||
|
|
||||||
|
from disko_lib.ansi import Colors
|
||||||
|
from disko_lib.eval_config import eval_config
|
||||||
|
from disko_lib.result import DiskoError, DiskoSuccess, DiskoResult
|
||||||
|
from disko_lib.types.device import run_lsblk
|
||||||
|
|
||||||
|
|
||||||
|
def run_dev_lsblk() -> DiskoResult[None]:
|
||||||
|
output = run_lsblk()
|
||||||
|
if isinstance(output, DiskoError):
|
||||||
|
return output
|
||||||
|
|
||||||
|
print(output.value)
|
||||||
|
return DiskoSuccess(None, "run disko dev lsblk")
|
||||||
|
|
||||||
|
|
||||||
|
def run_dev_ansi() -> DiskoResult[None]:
|
||||||
|
import inspect
|
||||||
|
|
||||||
|
for name, value in inspect.getmembers(Colors):
|
||||||
|
if value != "_" and not name.startswith("_") and name != "RESET":
|
||||||
|
print("{:>30} {}".format(name, value + name + Colors.RESET))
|
||||||
|
|
||||||
|
return DiskoSuccess(None, "run disko dev ansi")
|
||||||
|
|
||||||
|
|
||||||
|
def run_dev_eval(
|
||||||
|
*, disko_file: str | None, flake: str | None, **_: Any
|
||||||
|
) -> DiskoResult[None]:
|
||||||
|
result = eval_config(disko_file=disko_file, flake=flake)
|
||||||
|
|
||||||
|
if isinstance(result, DiskoError):
|
||||||
|
return result
|
||||||
|
|
||||||
|
print(json.dumps(result.value, indent=2))
|
||||||
|
return DiskoSuccess(None, "run disko dev eval")
|
||||||
|
|
||||||
|
|
||||||
|
def run_dev(args: argparse.Namespace) -> DiskoResult[None]:
|
||||||
|
match args.dev_command:
|
||||||
|
case "lsblk":
|
||||||
|
return run_dev_lsblk()
|
||||||
|
case "ansi":
|
||||||
|
return run_dev_ansi()
|
||||||
|
case "eval":
|
||||||
|
return run_dev_eval(**vars(args))
|
||||||
|
case _:
|
||||||
|
assert_never(args.dev_command)
|
||||||
@@ -173,13 +173,3 @@ class Colors:
|
|||||||
kernel32 = __import__("ctypes").windll.kernel32
|
kernel32 = __import__("ctypes").windll.kernel32
|
||||||
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
|
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
|
||||||
del kernel32
|
del kernel32
|
||||||
|
|
||||||
|
|
||||||
def disko_dev_ansi() -> str:
|
|
||||||
import inspect
|
|
||||||
|
|
||||||
for name, value in inspect.getmembers(Colors):
|
|
||||||
if value != "_" and not name.startswith("_") and name != "RESET":
|
|
||||||
print("{:>30} {}".format(name, value + name + Colors.RESET))
|
|
||||||
|
|
||||||
return "run disko dev ansi"
|
|
||||||
@@ -7,6 +7,8 @@ from disko_lib.messages.msgs import (
|
|||||||
err_eval_config_failed,
|
err_eval_config_failed,
|
||||||
err_file_not_found,
|
err_file_not_found,
|
||||||
err_flake_uri_no_attr,
|
err_flake_uri_no_attr,
|
||||||
|
err_missing_arguments,
|
||||||
|
err_too_many_arguments,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .run_cmd import run
|
from .run_cmd import run
|
||||||
@@ -28,7 +30,7 @@ assert (
|
|||||||
), f"Can't find `eval-config.nix`, expected it next to {__file__}"
|
), f"Can't find `eval-config.nix`, expected it next to {__file__}"
|
||||||
|
|
||||||
|
|
||||||
def eval_config(args: dict[str, str]) -> DiskoResult[dict[str, Any]]:
|
def _eval_config(args: dict[str, str]) -> DiskoResult[dict[str, Any]]:
|
||||||
args_as_json = json.dumps(args)
|
args_as_json = json.dumps(args)
|
||||||
|
|
||||||
result = run(
|
result = run(
|
||||||
@@ -43,13 +45,12 @@ def eval_config(args: dict[str, str]) -> DiskoResult[dict[str, Any]]:
|
|||||||
args=args,
|
args=args,
|
||||||
stderr=result.messages[0].details["stderr"],
|
stderr=result.messages[0].details["stderr"],
|
||||||
)
|
)
|
||||||
return result
|
|
||||||
|
|
||||||
# We trust the output of `nix eval` to be valid JSON
|
# We trust the output of `nix eval` to be valid JSON
|
||||||
return DiskoSuccess(json.loads(result.value), "evaluate disko config")
|
return DiskoSuccess(json.loads(result.value), "evaluate disko config")
|
||||||
|
|
||||||
|
|
||||||
def eval_disko_file(config_file: Path) -> DiskoResult[dict[str, Any]]:
|
def _eval_disko_file(config_file: Path) -> DiskoResult[dict[str, Any]]:
|
||||||
abs_path = config_file.absolute()
|
abs_path = config_file.absolute()
|
||||||
|
|
||||||
if not abs_path.exists():
|
if not abs_path.exists():
|
||||||
@@ -59,10 +60,10 @@ def eval_disko_file(config_file: Path) -> DiskoResult[dict[str, Any]]:
|
|||||||
path=abs_path,
|
path=abs_path,
|
||||||
)
|
)
|
||||||
|
|
||||||
return eval_config({"diskoFile": str(abs_path)})
|
return _eval_config({"diskoFile": str(abs_path)})
|
||||||
|
|
||||||
|
|
||||||
def eval_flake(flake_uri: str) -> DiskoResult[dict[str, Any]]:
|
def _eval_flake(flake_uri: str) -> DiskoResult[dict[str, Any]]:
|
||||||
# arg parser should not allow empty strings
|
# arg parser should not allow empty strings
|
||||||
assert len(flake_uri) > 0
|
assert len(flake_uri) > 0
|
||||||
|
|
||||||
@@ -83,4 +84,18 @@ def eval_flake(flake_uri: str) -> DiskoResult[dict[str, Any]]:
|
|||||||
if flake_path.exists():
|
if flake_path.exists():
|
||||||
flake = str(flake_path.absolute())
|
flake = str(flake_path.absolute())
|
||||||
|
|
||||||
return eval_config({"flake": flake, "flakeAttr": flake_attr})
|
return _eval_config({"flake": flake, "flakeAttr": flake_attr})
|
||||||
|
|
||||||
|
|
||||||
|
def eval_config(
|
||||||
|
*, disko_file: str | None, flake: str | None
|
||||||
|
) -> DiskoResult[dict[str, Any]]:
|
||||||
|
# match would be nicer, but mypy doesn't understand type narrowing in tuples
|
||||||
|
if not disko_file and not flake:
|
||||||
|
return DiskoError.single_message(err_missing_arguments, "validate args")
|
||||||
|
if not disko_file and flake:
|
||||||
|
return _eval_flake(flake)
|
||||||
|
if disko_file and not flake:
|
||||||
|
return _eval_disko_file(Path(disko_file))
|
||||||
|
|
||||||
|
return DiskoError.single_message(err_too_many_arguments, "validate args")
|
||||||
@@ -110,13 +110,13 @@ class BlockDevice:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _run_lsblk() -> DiskoResult[str]:
|
def run_lsblk() -> DiskoResult[str]:
|
||||||
return run(["lsblk", "--json", "--tree", "--output", ",".join(LSBLK_OUTPUT_FIELDS)])
|
return run(["lsblk", "--json", "--tree", "--output", ",".join(LSBLK_OUTPUT_FIELDS)])
|
||||||
|
|
||||||
|
|
||||||
def list_block_devices(lsblk_output: str = "") -> DiskoResult[list[BlockDevice]]:
|
def list_block_devices(lsblk_output: str = "") -> DiskoResult[list[BlockDevice]]:
|
||||||
if not lsblk_output:
|
if not lsblk_output:
|
||||||
lsblk_result = _run_lsblk()
|
lsblk_result = run_lsblk()
|
||||||
|
|
||||||
if isinstance(lsblk_result, DiskoError):
|
if isinstance(lsblk_result, DiskoError):
|
||||||
return lsblk_result
|
return lsblk_result
|
||||||
@@ -129,12 +129,3 @@ def list_block_devices(lsblk_output: str = "") -> DiskoResult[list[BlockDevice]]
|
|||||||
blockdevices = [BlockDevice.from_json_dict(dev) for dev in lsblk_json]
|
blockdevices = [BlockDevice.from_json_dict(dev) for dev in lsblk_json]
|
||||||
|
|
||||||
return DiskoSuccess(blockdevices, "list block devices")
|
return DiskoSuccess(blockdevices, "list block devices")
|
||||||
|
|
||||||
|
|
||||||
def disko_dev_lsblk() -> DiskoResult[None]:
|
|
||||||
output = _run_lsblk()
|
|
||||||
if isinstance(output, DiskoError):
|
|
||||||
return output
|
|
||||||
|
|
||||||
print(output.value)
|
|
||||||
return DiskoSuccess(None, "run disko dev lsblk")
|
|
||||||
Reference in new issue
Block a user