Added ref to Qubasa-main in template/new-clan/flake.nix
This commit is contained in:
@@ -12,7 +12,7 @@ from ..errors import ClanError
|
|||||||
from ..nix import nix_command, nix_shell
|
from ..nix import nix_command, nix_shell
|
||||||
|
|
||||||
DEFAULT_URL: AnyUrl = parse_obj_as(
|
DEFAULT_URL: AnyUrl = parse_obj_as(
|
||||||
AnyUrl, "git+https://git.clan.lol/clan/clan-core#new-clan"
|
AnyUrl, "git+https://git.clan.lol/clan/clan-core?ref=Qubasa-main#new-clan" # TODO: Change me back to main branch
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -115,6 +115,10 @@ class BaseTask:
|
|||||||
self.status = TaskStatus.RUNNING
|
self.status = TaskStatus.RUNNING
|
||||||
try:
|
try:
|
||||||
self.run()
|
self.run()
|
||||||
|
# TODO: We need to check, if too many commands have been initialized,
|
||||||
|
# but not run. This would deadlock the log_lines() function.
|
||||||
|
# Idea: Run next(cmds) and check if it raises StopIteration if not,
|
||||||
|
# we have too many commands
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# FIXME: fix exception handling here
|
# FIXME: fix exception handling here
|
||||||
traceback.print_exception(*sys.exc_info())
|
traceback.print_exception(*sys.exc_info())
|
||||||
|
|||||||
@@ -1,3 +1,23 @@
|
|||||||
from typing import NewType
|
from typing import NewType
|
||||||
|
from pathlib import Path
|
||||||
|
import logging
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
FlakeName = NewType("FlakeName", str)
|
FlakeName = NewType("FlakeName", str)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_path(base_dir: Path, value: Path) -> Path:
|
||||||
|
user_path = (base_dir / value).resolve()
|
||||||
|
|
||||||
|
# Check if the path is within the data directory
|
||||||
|
if not str(user_path).startswith(str(base_dir)):
|
||||||
|
if not str(user_path).startswith("/tmp/pytest"):
|
||||||
|
raise ValueError(
|
||||||
|
f"Destination out of bounds. Expected {user_path} to start with {base_dir}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
log.warning(
|
||||||
|
f"Detected pytest tmpdir. Skipping path validation for {user_path}"
|
||||||
|
)
|
||||||
|
return user_path
|
||||||
@@ -9,15 +9,16 @@ from pathlib import Path
|
|||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from ..dirs import specific_flake_dir
|
from ..dirs import specific_flake_dir, clan_flakes_dir
|
||||||
from ..nix import nix_build, nix_config, nix_shell
|
from ..nix import nix_build, nix_config, nix_shell, nix_eval
|
||||||
from ..task_manager import BaseTask, Command, create_task
|
from ..task_manager import BaseTask, Command, create_task
|
||||||
from .inspect import VmConfig, inspect_vm
|
from .inspect import VmConfig, inspect_vm
|
||||||
|
from ..flakes.create import create_flake
|
||||||
|
from ..types import validate_path
|
||||||
|
|
||||||
class BuildVmTask(BaseTask):
|
class BuildVmTask(BaseTask):
|
||||||
def __init__(self, uuid: UUID, vm: VmConfig) -> None:
|
def __init__(self, uuid: UUID, vm: VmConfig) -> None:
|
||||||
super().__init__(uuid, num_cmds=6)
|
super().__init__(uuid, num_cmds=7)
|
||||||
self.vm = vm
|
self.vm = vm
|
||||||
|
|
||||||
def get_vm_create_info(self, cmds: Iterator[Command]) -> dict:
|
def get_vm_create_info(self, cmds: Iterator[Command]) -> dict:
|
||||||
@@ -39,6 +40,19 @@ class BuildVmTask(BaseTask):
|
|||||||
with open(vm_json) as f:
|
with open(vm_json) as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
|
|
||||||
|
def get_clan_name(self, cmds: Iterator[Command]) -> str:
|
||||||
|
clan_dir = self.vm.flake_url
|
||||||
|
cmd = next(cmds)
|
||||||
|
cmd.run(
|
||||||
|
nix_eval(
|
||||||
|
[
|
||||||
|
f'{clan_dir}#clanInternals.clanName'
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
clan_name = "".join(cmd.stdout).strip()
|
||||||
|
return clan_name
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
cmds = self.commands()
|
cmds = self.commands()
|
||||||
|
|
||||||
@@ -47,15 +61,17 @@ class BuildVmTask(BaseTask):
|
|||||||
|
|
||||||
# TODO: We should get this from the vm argument
|
# TODO: We should get this from the vm argument
|
||||||
vm_config = self.get_vm_create_info(cmds)
|
vm_config = self.get_vm_create_info(cmds)
|
||||||
|
clan_name = self.get_clan_name(cmds)
|
||||||
|
|
||||||
# TODO: Don't use a temporary directory, instead create a new flake directory
|
|
||||||
with tempfile.TemporaryDirectory() as tmpdir_:
|
flake_dir = clan_flakes_dir() / clan_name
|
||||||
tmpdir = Path(tmpdir_)
|
validate_path(clan_flakes_dir(), flake_dir)
|
||||||
xchg_dir = tmpdir / "xchg"
|
|
||||||
|
xchg_dir = flake_dir / "xchg"
|
||||||
xchg_dir.mkdir()
|
xchg_dir.mkdir()
|
||||||
secrets_dir = tmpdir / "secrets"
|
secrets_dir = flake_dir / "secrets"
|
||||||
secrets_dir.mkdir()
|
secrets_dir.mkdir()
|
||||||
disk_img = f"{tmpdir_}/disk.img"
|
disk_img = f"{flake_dir}/disk.img"
|
||||||
|
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
env["CLAN_DIR"] = str(self.vm.flake_url)
|
env["CLAN_DIR"] = str(self.vm.flake_url)
|
||||||
|
|||||||
@@ -6,26 +6,11 @@ from pydantic import AnyUrl, BaseModel, validator
|
|||||||
|
|
||||||
from ..dirs import clan_data_dir, clan_flakes_dir
|
from ..dirs import clan_data_dir, clan_flakes_dir
|
||||||
from ..flakes.create import DEFAULT_URL
|
from ..flakes.create import DEFAULT_URL
|
||||||
|
from ..types import validate_path
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def validate_path(base_dir: Path, value: Path) -> Path:
|
|
||||||
user_path = (base_dir / value).resolve()
|
|
||||||
|
|
||||||
# Check if the path is within the data directory
|
|
||||||
if not str(user_path).startswith(str(base_dir)):
|
|
||||||
if not str(user_path).startswith("/tmp/pytest"):
|
|
||||||
raise ValueError(
|
|
||||||
f"Destination out of bounds. Expected {user_path} to start with {base_dir}"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
log.warning(
|
|
||||||
f"Detected pytest tmpdir. Skipping path validation for {user_path}"
|
|
||||||
)
|
|
||||||
return user_path
|
|
||||||
|
|
||||||
|
|
||||||
class ClanDataPath(BaseModel):
|
class ClanDataPath(BaseModel):
|
||||||
dest: Path
|
dest: Path
|
||||||
|
|
||||||
|
|||||||
@@ -10,13 +10,16 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def temporary_dir() -> Iterator[Path]:
|
def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
|
||||||
if os.getenv("TEST_KEEP_TEMPORARY_DIR") is not None:
|
if os.getenv("TEST_KEEP_TEMPORARY_DIR") is not None:
|
||||||
temp_dir = tempfile.mkdtemp(prefix="pytest-")
|
temp_dir = tempfile.mkdtemp(prefix="pytest-")
|
||||||
path = Path(temp_dir)
|
path = Path(temp_dir)
|
||||||
log.info("Keeping temporary test directory: ", path)
|
log.debug("Temp HOME directory: %s", str(path))
|
||||||
|
monkeypatch.setenv("HOME", str(temp_dir))
|
||||||
yield path
|
yield path
|
||||||
else:
|
else:
|
||||||
log.debug("TEST_KEEP_TEMPORARY_DIR not set, using TemporaryDirectory")
|
log.debug("TEST_KEEP_TEMPORARY_DIR not set, using TemporaryDirectory")
|
||||||
with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath:
|
with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath:
|
||||||
|
monkeypatch.setenv("HOME", str(dirpath))
|
||||||
|
log.debug("Temp HOME directory: %s", str(dirpath))
|
||||||
yield Path(dirpath)
|
yield Path(dirpath)
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ def test_configure_machine(
|
|||||||
capsys: pytest.CaptureFixture,
|
capsys: pytest.CaptureFixture,
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
) -> None:
|
) -> None:
|
||||||
monkeypatch.setenv("HOME", str(temporary_dir))
|
|
||||||
cli = Cli()
|
cli = Cli()
|
||||||
cli.run(["config", "-m", "machine1", "clan.jitsi.enable", "true"])
|
cli.run(["config", "-m", "machine1", "clan.jitsi.enable", "true"])
|
||||||
# clear the output buffer
|
# clear the output buffer
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ from pathlib import Path
|
|||||||
import pytest
|
import pytest
|
||||||
from api import TestClient
|
from api import TestClient
|
||||||
from cli import Cli
|
from cli import Cli
|
||||||
|
from clan_cli.flakes.create import DEFAULT_URL
|
||||||
|
from clan_cli.dirs import clan_flakes_dir, clan_data_dir
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def cli() -> Cli:
|
def cli() -> Cli:
|
||||||
@@ -14,15 +15,16 @@ def cli() -> Cli:
|
|||||||
|
|
||||||
@pytest.mark.impure
|
@pytest.mark.impure
|
||||||
def test_create_flake_api(
|
def test_create_flake_api(
|
||||||
monkeypatch: pytest.MonkeyPatch, api: TestClient, temporary_dir: Path
|
monkeypatch: pytest.MonkeyPatch, api: TestClient, temporary_home: Path
|
||||||
) -> None:
|
) -> None:
|
||||||
flake_dir = temporary_dir / "flake_dir"
|
monkeypatch.chdir(clan_flakes_dir())
|
||||||
flake_dir_str = str(flake_dir.resolve())
|
flake_name = "flake_dir"
|
||||||
|
flake_dir = clan_flakes_dir() / flake_name
|
||||||
response = api.post(
|
response = api.post(
|
||||||
"/api/flake/create",
|
"/api/flake/create",
|
||||||
json=dict(
|
json=dict(
|
||||||
dest=flake_dir_str,
|
dest=str(flake_dir),
|
||||||
url="git+https://git.clan.lol/clan/clan-core#new-clan",
|
url=str(DEFAULT_URL),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,19 +36,21 @@ def test_create_flake_api(
|
|||||||
@pytest.mark.impure
|
@pytest.mark.impure
|
||||||
def test_create_flake(
|
def test_create_flake(
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
temporary_dir: Path,
|
|
||||||
capsys: pytest.CaptureFixture,
|
capsys: pytest.CaptureFixture,
|
||||||
|
temporary_home: Path,
|
||||||
cli: Cli,
|
cli: Cli,
|
||||||
) -> None:
|
) -> None:
|
||||||
monkeypatch.chdir(temporary_dir)
|
monkeypatch.chdir(clan_flakes_dir())
|
||||||
flake_dir = temporary_dir / "flake_dir"
|
flake_name = "flake_dir"
|
||||||
flake_dir_str = str(flake_dir.resolve())
|
flake_dir = clan_flakes_dir() / flake_name
|
||||||
cli.run(["flake", "create", flake_dir_str])
|
|
||||||
|
cli.run(["flakes", "create", flake_name])
|
||||||
assert (flake_dir / ".clan-flake").exists()
|
assert (flake_dir / ".clan-flake").exists()
|
||||||
monkeypatch.chdir(flake_dir)
|
monkeypatch.chdir(flake_dir)
|
||||||
cli.run(["machines", "create", "machine1"])
|
cli.run(["machines", "create", "machine1", flake_name])
|
||||||
capsys.readouterr() # flush cache
|
capsys.readouterr() # flush cache
|
||||||
cli.run(["machines", "list"])
|
|
||||||
|
cli.run(["machines", "list", flake_name])
|
||||||
assert "machine1" in capsys.readouterr().out
|
assert "machine1" in capsys.readouterr().out
|
||||||
flake_show = subprocess.run(
|
flake_show = subprocess.run(
|
||||||
["nix", "flake", "show", "--json"],
|
["nix", "flake", "show", "--json"],
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
description = "<Put your description here>";
|
description = "<Put your description here>";
|
||||||
|
|
||||||
inputs.clan-core.url = "git+https://git.clan.lol/clan/clan-core";
|
inputs.clan-core.url = "git+https://git.clan.lol/clan/clan-core?ref=Qubasa-main";
|
||||||
|
|
||||||
outputs = { self, clan-core, ... }:
|
outputs = { self, clan-core, ... }:
|
||||||
let
|
let
|
||||||
|
|||||||
Reference in New Issue
Block a user