From a399cbb8d9e43c60a939271932bac130ac0ef7e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 15 Feb 2024 17:18:15 +0100 Subject: [PATCH] vms: move virtiofsd/waypipe into their own modules --- pkgs/clan-cli/clan_cli/vms/run.py | 83 +------------------------ pkgs/clan-cli/clan_cli/vms/virtiofsd.py | 41 ++++++++++++ pkgs/clan-cli/clan_cli/vms/waypipe.py | 50 +++++++++++++++ 3 files changed, 93 insertions(+), 81 deletions(-) create mode 100644 pkgs/clan-cli/clan_cli/vms/virtiofsd.py create mode 100644 pkgs/clan-cli/clan_cli/vms/waypipe.py diff --git a/pkgs/clan-cli/clan_cli/vms/run.py b/pkgs/clan-cli/clan_cli/vms/run.py index 1ea1f0f6..d3c201dc 100644 --- a/pkgs/clan-cli/clan_cli/vms/run.py +++ b/pkgs/clan-cli/clan_cli/vms/run.py @@ -1,15 +1,9 @@ import argparse -import contextlib import importlib import json import logging import os import random -import shutil -import socket -import subprocess -import time -from collections.abc import Iterator from dataclasses import dataclass, field from pathlib import Path from tempfile import TemporaryDirectory @@ -21,6 +15,8 @@ from ..machines.machines import Machine from ..nix import nix_shell from ..secrets.generate import generate_secrets from .inspect import VmConfig, inspect_vm +from .virtiofsd import start_virtiofsd +from .waypipe import start_waypipe log = logging.getLogger(__name__) @@ -233,81 +229,6 @@ def prepare_disk( return disk_img -VMADDR_CID_HYPERVISOR = 2 - - -def test_vsock_port(port: int) -> bool: - try: - s = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM) - s.connect((VMADDR_CID_HYPERVISOR, port)) - s.close() - return True - except OSError: - return False - - -@contextlib.contextmanager -def start_waypipe(cid: int | None, title_prefix: str) -> Iterator[None]: - if cid is None: - yield - return - waypipe = nix_shell( - ["git+https://git.clan.lol/clan/clan-core#waypipe"], - [ - "waypipe", - "--vsock", - "--socket", - f"s{cid}:3049", - "--title-prefix", - title_prefix, - "client", - ], - ) - with subprocess.Popen(waypipe) as proc: - try: - while not test_vsock_port(3049): - rc = proc.poll() - if rc is not None: - msg = f"waypipe exited unexpectedly with code {rc}" - raise ClanError(msg) - time.sleep(0.1) - yield - finally: - proc.kill() - - -@contextlib.contextmanager -def start_virtiofsd(socket_path: Path) -> Iterator[None]: - sandbox = "namespace" - if shutil.which("newuidmap") is None: - sandbox = "none" - virtiofsd = nix_shell( - ["nixpkgs#virtiofsd"], - [ - "virtiofsd", - "--socket-path", - str(socket_path), - "--cache", - "always", - "--sandbox", - sandbox, - "--shared-dir", - "/nix/store", - ], - ) - with subprocess.Popen(virtiofsd) as proc: - try: - while not socket_path.exists(): - rc = proc.poll() - if rc is not None: - msg = f"virtiofsd exited unexpectedly with code {rc}" - raise ClanError(msg) - time.sleep(0.1) - yield - finally: - proc.kill() - - def run_vm(vm: VmConfig, nix_options: list[str] = []) -> None: machine = Machine(vm.machine_name, vm.flake_url) log.debug(f"Creating VM for {machine}") diff --git a/pkgs/clan-cli/clan_cli/vms/virtiofsd.py b/pkgs/clan-cli/clan_cli/vms/virtiofsd.py new file mode 100644 index 00000000..8d859e27 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/vms/virtiofsd.py @@ -0,0 +1,41 @@ +import contextlib +import shutil +import subprocess +import time +from collections.abc import Iterator +from pathlib import Path + +from ..errors import ClanError +from ..nix import nix_shell + + +@contextlib.contextmanager +def start_virtiofsd(socket_path: Path) -> Iterator[None]: + sandbox = "namespace" + if shutil.which("newuidmap") is None: + sandbox = "none" + virtiofsd = nix_shell( + ["nixpkgs#virtiofsd"], + [ + "virtiofsd", + "--socket-path", + str(socket_path), + "--cache", + "always", + "--sandbox", + sandbox, + "--shared-dir", + "/nix/store", + ], + ) + with subprocess.Popen(virtiofsd) as proc: + try: + while not socket_path.exists(): + rc = proc.poll() + if rc is not None: + msg = f"virtiofsd exited unexpectedly with code {rc}" + raise ClanError(msg) + time.sleep(0.1) + yield + finally: + proc.kill() diff --git a/pkgs/clan-cli/clan_cli/vms/waypipe.py b/pkgs/clan-cli/clan_cli/vms/waypipe.py new file mode 100644 index 00000000..feb44343 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/vms/waypipe.py @@ -0,0 +1,50 @@ +import contextlib +import socket +import subprocess +import time +from collections.abc import Iterator + +from ..errors import ClanError +from ..nix import nix_shell + +VMADDR_CID_HYPERVISOR = 2 + + +def test_vsock_port(port: int) -> bool: + try: + s = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM) + s.connect((VMADDR_CID_HYPERVISOR, port)) + s.close() + return True + except OSError: + return False + + +@contextlib.contextmanager +def start_waypipe(cid: int | None, title_prefix: str) -> Iterator[None]: + if cid is None: + yield + return + waypipe = nix_shell( + ["git+https://git.clan.lol/clan/clan-core#waypipe"], + [ + "waypipe", + "--vsock", + "--socket", + f"s{cid}:3049", + "--title-prefix", + title_prefix, + "client", + ], + ) + with subprocess.Popen(waypipe) as proc: + try: + while not test_vsock_port(3049): + rc = proc.poll() + if rc is not None: + msg = f"waypipe exited unexpectedly with code {rc}" + raise ClanError(msg) + time.sleep(0.1) + yield + finally: + proc.kill()