Improved logging messages. Added ClanError if flake create or machine create already exist
This commit is contained in:
@@ -15,7 +15,7 @@ def get_formatter(color: str) -> Callable[[logging.LogRecord], logging.Formatter
|
|||||||
reset = "\x1b[0m"
|
reset = "\x1b[0m"
|
||||||
filepath = Path(record.pathname).resolve()
|
filepath = Path(record.pathname).resolve()
|
||||||
return logging.Formatter(
|
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
|
return myformatter
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from typing import Dict
|
|||||||
from pydantic import AnyUrl
|
from pydantic import AnyUrl
|
||||||
from pydantic.tools import parse_obj_as
|
from pydantic.tools import parse_obj_as
|
||||||
|
|
||||||
|
from ..errors import ClanError
|
||||||
from ..async_cmd import CmdOut, run, runforcli
|
from ..async_cmd import CmdOut, run, runforcli
|
||||||
from ..dirs import clan_flakes_dir
|
from ..dirs import clan_flakes_dir
|
||||||
from ..nix import nix_command, nix_shell
|
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]:
|
async def create_flake(directory: Path, url: AnyUrl) -> Dict[str, CmdOut]:
|
||||||
if not directory.exists():
|
if not directory.exists():
|
||||||
directory.mkdir()
|
directory.mkdir()
|
||||||
|
else:
|
||||||
|
raise ClanError(f"Flake at '{directory}' already exists")
|
||||||
response = {}
|
response = {}
|
||||||
command = nix_command(
|
command = nix_command(
|
||||||
[
|
[
|
||||||
@@ -27,27 +30,27 @@ async def create_flake(directory: Path, url: AnyUrl) -> Dict[str, CmdOut]:
|
|||||||
url,
|
url,
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
out = await run(command, directory)
|
out = await run(command, cwd=directory)
|
||||||
response["flake init"] = out
|
response["flake init"] = out
|
||||||
|
|
||||||
command = nix_shell(["git"], ["git", "init"])
|
command = nix_shell(["git"], ["git", "init"])
|
||||||
out = await run(command, directory)
|
out = await run(command, cwd=directory)
|
||||||
response["git init"] = out
|
response["git init"] = out
|
||||||
|
|
||||||
command = nix_shell(["git"], ["git", "add", "."])
|
command = nix_shell(["git"], ["git", "add", "."])
|
||||||
out = await run(command, directory)
|
out = await run(command, cwd=directory)
|
||||||
response["git add"] = out
|
response["git add"] = out
|
||||||
|
|
||||||
command = nix_shell(["git"], ["git", "config", "user.name", "clan-tool"])
|
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
|
response["git config"] = out
|
||||||
|
|
||||||
command = nix_shell(["git"], ["git", "config", "user.email", "clan@example.com"])
|
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
|
response["git config"] = out
|
||||||
|
|
||||||
command = nix_shell(["git"], ["git", "commit", "-a", "-m", "Initial commit"])
|
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
|
response["git commit"] = out
|
||||||
|
|
||||||
return response
|
return response
|
||||||
@@ -67,7 +70,7 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None:
|
|||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--url",
|
"--url",
|
||||||
type=AnyUrl,
|
type=str,
|
||||||
help="url for the flake",
|
help="url for the flake",
|
||||||
default=DEFAULT_URL,
|
default=DEFAULT_URL,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
async def create_machine(flake_name: FlakeName, machine_name: str) -> Dict[str, CmdOut]:
|
async def create_machine(flake_name: FlakeName, machine_name: str) -> Dict[str, CmdOut]:
|
||||||
folder = specific_machine_dir(flake_name, machine_name)
|
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)
|
folder.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
# create empty settings.json file inside the folder
|
# create empty settings.json file inside the folder
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ class Command:
|
|||||||
if self.p.returncode != 0:
|
if self.p.returncode != 0:
|
||||||
raise ClanError(f"Failed to run command: {shlex.join(cmd)}")
|
raise ClanError(f"Failed to run command: {shlex.join(cmd)}")
|
||||||
|
|
||||||
self.log.debug("Successfully ran command")
|
|
||||||
|
|
||||||
|
|
||||||
class TaskStatus(str, Enum):
|
class TaskStatus(str, Enum):
|
||||||
|
|||||||
@@ -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}")
|
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)
|
return json.load(f)
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
, pytest-cov
|
, pytest-cov
|
||||||
, pytest-xdist
|
, pytest-xdist
|
||||||
, pytest-subprocess
|
, pytest-subprocess
|
||||||
, pytest-parallel
|
|
||||||
, pytest-timeout
|
, pytest-timeout
|
||||||
, python3
|
, python3
|
||||||
, runCommand
|
, runCommand
|
||||||
@@ -46,7 +45,6 @@ let
|
|||||||
pytest
|
pytest
|
||||||
pytest-cov
|
pytest-cov
|
||||||
pytest-subprocess
|
pytest-subprocess
|
||||||
# pytest-parallel
|
|
||||||
pytest-xdist
|
pytest-xdist
|
||||||
pytest-timeout
|
pytest-timeout
|
||||||
openssh
|
openssh
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ clan_cli = [ "config/jsonschema/*", "webui/assets/**/*"]
|
|||||||
testpaths = "tests"
|
testpaths = "tests"
|
||||||
faulthandler_timeout = 60
|
faulthandler_timeout = 60
|
||||||
log_level = "DEBUG"
|
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
|
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"
|
norecursedirs = "tests/helpers"
|
||||||
markers = [ "impure" ]
|
markers = [ "impure" ]
|
||||||
|
|||||||
@@ -45,11 +45,12 @@ def remote_flake_with_vm_without_secrets(
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def create_user_with_age_key(
|
def create_user_with_age_key(
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
|
test_flake: FlakeForTest,
|
||||||
age_keys: list["KeyPair"],
|
age_keys: list["KeyPair"],
|
||||||
) -> None:
|
) -> None:
|
||||||
monkeypatch.setenv("SOPS_AGE_KEY", age_keys[0].privkey)
|
monkeypatch.setenv("SOPS_AGE_KEY", age_keys[0].privkey)
|
||||||
cli = Cli()
|
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:
|
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(
|
def test_create_local(
|
||||||
api: TestClient,
|
api: TestClient,
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
flake_with_vm_with_secrets: Path,
|
flake_with_vm_with_secrets: FlakeForTest,
|
||||||
create_user_with_age_key: None,
|
create_user_with_age_key: None,
|
||||||
) -> 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")
|
@pytest.mark.skipif(not os.path.exists("/dev/kvm"), reason="Requires KVM")
|
||||||
@@ -106,8 +107,8 @@ def test_create_local(
|
|||||||
def test_create_remote(
|
def test_create_remote(
|
||||||
api: TestClient,
|
api: TestClient,
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
remote_flake_with_vm_without_secrets: Path,
|
remote_flake_with_vm_without_secrets: FlakeForTest,
|
||||||
) -> None:
|
) -> None:
|
||||||
generic_create_vm_test(
|
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"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user