forked from clan/clan-core
Merge pull request 'machines/machines: drop unused qmp wrapper' (#1683) from Mic92-main into main
This commit is contained in:
commit
1448e593e9
@ -1,10 +1,8 @@
|
|||||||
# Import the urllib.parse, enum and dataclasses modules
|
# Import the urllib.parse, enum and dataclasses modules
|
||||||
import dataclasses
|
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import urllib.request
|
import urllib.request
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from .errors import ClanError
|
from .errors import ClanError
|
||||||
|
|
||||||
@ -36,18 +34,10 @@ class FlakeId:
|
|||||||
return isinstance(self._value, str)
|
return isinstance(self._value, str)
|
||||||
|
|
||||||
|
|
||||||
# Parameters defined here will be DELETED from the nested uri
|
|
||||||
# so make sure there are no conflicts with other webservices
|
|
||||||
@dataclass
|
|
||||||
class MachineParams:
|
|
||||||
dummy_opt: str = "dummy"
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class MachineData:
|
class MachineData:
|
||||||
flake_id: FlakeId
|
flake_id: FlakeId
|
||||||
name: str = "defaultVM"
|
name: str = "defaultVM"
|
||||||
params: MachineParams = dataclasses.field(default_factory=MachineParams)
|
|
||||||
|
|
||||||
def get_id(self) -> str:
|
def get_id(self) -> str:
|
||||||
return f"{self.flake_id}#{self.name}"
|
return f"{self.flake_id}#{self.name}"
|
||||||
@ -120,23 +110,9 @@ class ClanURI:
|
|||||||
|
|
||||||
def _parse_machine_query(self, machine_frag: str) -> MachineData:
|
def _parse_machine_query(self, machine_frag: str) -> MachineData:
|
||||||
comp = urllib.parse.urlparse(machine_frag)
|
comp = urllib.parse.urlparse(machine_frag)
|
||||||
query = urllib.parse.parse_qs(comp.query)
|
|
||||||
machine_name = comp.path
|
machine_name = comp.path
|
||||||
|
|
||||||
machine_params: dict[str, Any] = {}
|
machine = MachineData(flake_id=self.flake_id, name=machine_name)
|
||||||
for dfield in dataclasses.fields(MachineParams):
|
|
||||||
if dfield.name in query:
|
|
||||||
values = query[dfield.name]
|
|
||||||
if len(values) > 1:
|
|
||||||
raise ClanError(f"Multiple values for parameter: {dfield.name}")
|
|
||||||
machine_params[dfield.name] = values[0]
|
|
||||||
|
|
||||||
# Remove the field from the query dictionary
|
|
||||||
# clan uri and nested uri share one namespace for query parameters
|
|
||||||
# we need to make sure there are no conflicts
|
|
||||||
del query[dfield.name]
|
|
||||||
params = MachineParams(**machine_params)
|
|
||||||
machine = MachineData(flake_id=self.flake_id, name=machine_name, params=params)
|
|
||||||
return machine
|
return machine
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1,14 +1,10 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from collections.abc import Generator
|
|
||||||
from contextlib import contextmanager
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from clan_cli.clan_uri import ClanURI, MachineData
|
from clan_cli.clan_uri import ClanURI, MachineData
|
||||||
from clan_cli.dirs import vm_state_dir
|
|
||||||
from clan_cli.qemu.qmp import QEMUMonitorProtocol
|
|
||||||
|
|
||||||
from ..cmd import run_no_stdout
|
from ..cmd import run_no_stdout
|
||||||
from ..errors import ClanError
|
from ..errors import ClanError
|
||||||
@ -18,28 +14,6 @@ from ..ssh import Host, parse_deployment_address
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class QMPWrapper:
|
|
||||||
def __init__(self, state_dir: Path) -> None:
|
|
||||||
# These sockets here are just symlinks to the real sockets which
|
|
||||||
# are created by the run.py file. The reason being that we run into
|
|
||||||
# file path length issues on Linux. If no qemu process is running
|
|
||||||
# the symlink will be dangling.
|
|
||||||
self._qmp_socket: Path = state_dir / "qmp.sock"
|
|
||||||
self._qga_socket: Path = state_dir / "qga.sock"
|
|
||||||
|
|
||||||
@contextmanager
|
|
||||||
def qmp_ctx(self) -> Generator[QEMUMonitorProtocol, None, None]:
|
|
||||||
rpath = self._qmp_socket.resolve()
|
|
||||||
if not rpath.exists():
|
|
||||||
raise ClanError(f"qmp socket {rpath} does not exist. Is the VM running?")
|
|
||||||
qmp = QEMUMonitorProtocol(str(rpath))
|
|
||||||
qmp.connect()
|
|
||||||
try:
|
|
||||||
yield qmp
|
|
||||||
finally:
|
|
||||||
qmp.close()
|
|
||||||
|
|
||||||
|
|
||||||
class Machine:
|
class Machine:
|
||||||
name: str
|
name: str
|
||||||
flake: str | Path
|
flake: str | Path
|
||||||
@ -49,7 +23,6 @@ class Machine:
|
|||||||
build_cache: dict[str, Path]
|
build_cache: dict[str, Path]
|
||||||
_flake_path: Path | None
|
_flake_path: Path | None
|
||||||
_deployment_info: None | dict
|
_deployment_info: None | dict
|
||||||
vm: QMPWrapper
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -80,10 +53,6 @@ class Machine:
|
|||||||
self._deployment_info: None | dict = deployment_info
|
self._deployment_info: None | dict = deployment_info
|
||||||
self.nix_options = nix_options
|
self.nix_options = nix_options
|
||||||
|
|
||||||
state_dir = vm_state_dir(flake_url=str(self.flake), vm_name=self.data.name)
|
|
||||||
|
|
||||||
self.vm: QMPWrapper = QMPWrapper(state_dir)
|
|
||||||
|
|
||||||
def flush_caches(self) -> None:
|
def flush_caches(self) -> None:
|
||||||
self._deployment_info = None
|
self._deployment_info = None
|
||||||
self._flake_path = None
|
self._flake_path = None
|
||||||
|
@ -51,10 +51,9 @@ def test_remote_with_clanparams() -> None:
|
|||||||
|
|
||||||
|
|
||||||
def test_remote_with_all_params() -> None:
|
def test_remote_with_all_params() -> None:
|
||||||
uri = ClanURI("clan://https://example.com?password=12345#myVM#secondVM?dummy_opt=1")
|
uri = ClanURI("clan://https://example.com?password=12345#myVM#secondVM")
|
||||||
assert uri.machine.name == "myVM"
|
assert uri.machine.name == "myVM"
|
||||||
assert uri._machines[1].name == "secondVM"
|
assert uri._machines[1].name == "secondVM"
|
||||||
assert uri._machines[1].params.dummy_opt == "1"
|
|
||||||
assert uri.flake_id.url == "https://example.com?password=12345"
|
assert uri.flake_id.url == "https://example.com?password=12345"
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user