From 0ab2b853f2f96026a7e1dcdfb8aedfa493f5181e Mon Sep 17 00:00:00 2001 From: Qubasa Date: Wed, 18 Oct 2023 12:17:46 +0200 Subject: [PATCH] Improved logging messages. Added ClanError if flake create or machine create already exist --- pkgs/clan-cli/clan_cli/custom_logger.py | 2 +- pkgs/clan-cli/clan_cli/flakes/create.py | 17 ++++++++++------- pkgs/clan-cli/clan_cli/machines/create.py | 2 ++ pkgs/clan-cli/clan_cli/task_manager.py | 2 +- pkgs/clan-cli/clan_cli/vms/create.py | 4 ++-- pkgs/clan-cli/default.nix | 2 -- pkgs/clan-cli/pyproject.toml | 2 +- pkgs/clan-cli/tests/test_vms_api_create.py | 11 ++++++----- 8 files changed, 23 insertions(+), 19 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/custom_logger.py b/pkgs/clan-cli/clan_cli/custom_logger.py index 1be10daed..09b63068b 100644 --- a/pkgs/clan-cli/clan_cli/custom_logger.py +++ b/pkgs/clan-cli/clan_cli/custom_logger.py @@ -15,7 +15,7 @@ def get_formatter(color: str) -> Callable[[logging.LogRecord], logging.Formatter reset = "\x1b[0m" filepath = Path(record.pathname).resolve() return logging.Formatter( - f"{filepath}:%(lineno)d::%(funcName)s\n{color}%(levelname)s{reset}: %(message)s" + f"{color}%(levelname)s{reset}: %(message)s\n {filepath}:%(lineno)d::%(funcName)s\n" ) return myformatter diff --git a/pkgs/clan-cli/clan_cli/flakes/create.py b/pkgs/clan-cli/clan_cli/flakes/create.py index c9d1d5391..c3bfb9558 100644 --- a/pkgs/clan-cli/clan_cli/flakes/create.py +++ b/pkgs/clan-cli/clan_cli/flakes/create.py @@ -6,6 +6,7 @@ from typing import Dict from pydantic import AnyUrl from pydantic.tools import parse_obj_as +from ..errors import ClanError from ..async_cmd import CmdOut, run, runforcli from ..dirs import clan_flakes_dir from ..nix import nix_command, nix_shell @@ -18,6 +19,8 @@ DEFAULT_URL: AnyUrl = parse_obj_as( async def create_flake(directory: Path, url: AnyUrl) -> Dict[str, CmdOut]: if not directory.exists(): directory.mkdir() + else: + raise ClanError(f"Flake at '{directory}' already exists") response = {} command = nix_command( [ @@ -27,27 +30,27 @@ async def create_flake(directory: Path, url: AnyUrl) -> Dict[str, CmdOut]: url, ] ) - out = await run(command, directory) + out = await run(command, cwd=directory) response["flake init"] = out command = nix_shell(["git"], ["git", "init"]) - out = await run(command, directory) + out = await run(command, cwd=directory) response["git init"] = out command = nix_shell(["git"], ["git", "add", "."]) - out = await run(command, directory) + out = await run(command, cwd=directory) response["git add"] = out command = nix_shell(["git"], ["git", "config", "user.name", "clan-tool"]) - out = await run(command, directory) + out = await run(command, cwd=directory) response["git config"] = out command = nix_shell(["git"], ["git", "config", "user.email", "clan@example.com"]) - out = await run(command, directory) + out = await run(command, cwd=directory) response["git config"] = out command = nix_shell(["git"], ["git", "commit", "-a", "-m", "Initial commit"]) - out = await run(command, directory) + out = await run(command, cwd=directory) response["git commit"] = out return response @@ -67,7 +70,7 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None: ) parser.add_argument( "--url", - type=AnyUrl, + type=str, help="url for the flake", default=DEFAULT_URL, ) diff --git a/pkgs/clan-cli/clan_cli/machines/create.py b/pkgs/clan-cli/clan_cli/machines/create.py index 153f28503..d4eedb483 100644 --- a/pkgs/clan-cli/clan_cli/machines/create.py +++ b/pkgs/clan-cli/clan_cli/machines/create.py @@ -13,6 +13,8 @@ log = logging.getLogger(__name__) async def create_machine(flake_name: FlakeName, machine_name: str) -> Dict[str, CmdOut]: folder = specific_machine_dir(flake_name, machine_name) + if folder.exists(): + raise ClanError(f"Machine '{machine_name}' already exists") folder.mkdir(parents=True, exist_ok=True) # create empty settings.json file inside the folder diff --git a/pkgs/clan-cli/clan_cli/task_manager.py b/pkgs/clan-cli/clan_cli/task_manager.py index 73e3bbec1..fa68ac9d6 100644 --- a/pkgs/clan-cli/clan_cli/task_manager.py +++ b/pkgs/clan-cli/clan_cli/task_manager.py @@ -80,7 +80,7 @@ class Command: if self.p.returncode != 0: raise ClanError(f"Failed to run command: {shlex.join(cmd)}") - self.log.debug("Successfully ran command") + class TaskStatus(str, Enum): diff --git a/pkgs/clan-cli/clan_cli/vms/create.py b/pkgs/clan-cli/clan_cli/vms/create.py index 2df7b9ac7..09d371555 100644 --- a/pkgs/clan-cli/clan_cli/vms/create.py +++ b/pkgs/clan-cli/clan_cli/vms/create.py @@ -34,9 +34,9 @@ class BuildVmTask(BaseTask): ] ) ) - vm_json = "".join(cmd.stdout) + vm_json = "".join(cmd.stdout).strip() self.log.debug(f"VM JSON path: {vm_json}") - with open(vm_json.strip()) as f: + with open(vm_json) as f: return json.load(f) def run(self) -> None: diff --git a/pkgs/clan-cli/default.nix b/pkgs/clan-cli/default.nix index c9fccd388..03c1017fa 100644 --- a/pkgs/clan-cli/default.nix +++ b/pkgs/clan-cli/default.nix @@ -10,7 +10,6 @@ , pytest-cov , pytest-xdist , pytest-subprocess -, pytest-parallel , pytest-timeout , python3 , runCommand @@ -46,7 +45,6 @@ let pytest pytest-cov pytest-subprocess - # pytest-parallel pytest-xdist pytest-timeout openssh diff --git a/pkgs/clan-cli/pyproject.toml b/pkgs/clan-cli/pyproject.toml index 432bc7390..88c0c945f 100644 --- a/pkgs/clan-cli/pyproject.toml +++ b/pkgs/clan-cli/pyproject.toml @@ -20,7 +20,7 @@ clan_cli = [ "config/jsonschema/*", "webui/assets/**/*"] testpaths = "tests" faulthandler_timeout = 60 log_level = "DEBUG" -log_format = "%(pathname)s:%(lineno)d::%(funcName)s\n %(levelname)s: %(message)s\n" +log_format = "%(levelname)s: %(message)s\n %(pathname)s:%(lineno)d::%(funcName)s" addopts = "--cov . --cov-report term --cov-report html:.reports/html --no-cov-on-fail --durations 5 --color=yes --maxfail=1 --new-first -nauto" # Add --pdb for debugging norecursedirs = "tests/helpers" markers = [ "impure" ] diff --git a/pkgs/clan-cli/tests/test_vms_api_create.py b/pkgs/clan-cli/tests/test_vms_api_create.py index f65254a46..fc2c7b3da 100644 --- a/pkgs/clan-cli/tests/test_vms_api_create.py +++ b/pkgs/clan-cli/tests/test_vms_api_create.py @@ -45,11 +45,12 @@ def remote_flake_with_vm_without_secrets( @pytest.fixture def create_user_with_age_key( monkeypatch: pytest.MonkeyPatch, + test_flake: FlakeForTest, age_keys: list["KeyPair"], ) -> None: monkeypatch.setenv("SOPS_AGE_KEY", age_keys[0].privkey) cli = Cli() - cli.run(["secrets", "users", "add", "user1", age_keys[0].pubkey]) + cli.run(["secrets", "users", "add", "user1", age_keys[0].pubkey, test_flake.name]) def generic_create_vm_test(api: TestClient, flake: Path, vm: str) -> None: @@ -95,10 +96,10 @@ def generic_create_vm_test(api: TestClient, flake: Path, vm: str) -> None: def test_create_local( api: TestClient, monkeypatch: pytest.MonkeyPatch, - flake_with_vm_with_secrets: Path, + flake_with_vm_with_secrets: FlakeForTest, create_user_with_age_key: None, ) -> None: - generic_create_vm_test(api, flake_with_vm_with_secrets, "vm_with_secrets") + generic_create_vm_test(api, flake_with_vm_with_secrets.path, "vm_with_secrets") @pytest.mark.skipif(not os.path.exists("/dev/kvm"), reason="Requires KVM") @@ -106,8 +107,8 @@ def test_create_local( def test_create_remote( api: TestClient, monkeypatch: pytest.MonkeyPatch, - remote_flake_with_vm_without_secrets: Path, + remote_flake_with_vm_without_secrets: FlakeForTest, ) -> None: generic_create_vm_test( - api, remote_flake_with_vm_without_secrets, "vm_without_secrets" + api, remote_flake_with_vm_without_secrets.path, "vm_without_secrets" )