Merge pull request 'integegrate remote-viewer instead of spicy' (#589) from Mic92-opengl-vm into main
All checks were successful
assets1 / test (push) Successful in 19s
checks / test (push) Successful in 41s
checks-impure / test (push) Successful in 1m5s

This commit is contained in:
clan-bot 2023-11-30 10:11:10 +00:00
commit 76b177778e
7 changed files with 34 additions and 36 deletions

View File

@ -23,7 +23,9 @@ class SopsKey:
def get_public_key(privkey: str) -> str:
cmd = nix_shell(["age"], ["age-keygen", "-y"])
try:
res = subprocess.run(cmd, input=privkey, stdout=subprocess.PIPE, text=True)
res = subprocess.run(
cmd, input=privkey, stdout=subprocess.PIPE, text=True, check=True
)
except subprocess.CalledProcessError as e:
raise ClanError(
"Failed to get public key for age private key. Is the key malformed?"

View File

@ -68,7 +68,7 @@ class Command:
while self.p.poll() is None:
# Check if stderr is ready to be read from
rlist, _, _ = select.select([self.p.stderr, self.p.stdout], [], [], 0)
rlist, _, _ = select.select([self.p.stderr, self.p.stdout], [], [], 1)
for fd in rlist:
try:
for line in fd:

View File

@ -0,0 +1,4 @@
[Default Applications]
x-scheme-handler/spice=remote-viewer.desktop
x-scheme-handler/spice+unix=remote-viewer.desktop
x-scheme-handler/spice+tls=remote-viewer.desktop

View File

@ -0,0 +1,4 @@
[Desktop Entry]
Name=Remote Viewer
Type=Application
Exec=remote-viewer %u

View File

@ -3,14 +3,13 @@ import asyncio
import json
import os
import shlex
import subprocess
import sys
import tempfile
from collections.abc import Iterator
from pathlib import Path
from threading import Condition, Thread
from uuid import UUID
from ..dirs import module_root
from ..nix import nix_build, nix_config, nix_eval, nix_shell
from ..task_manager import BaseTask, Command, create_task
from .inspect import VmConfig, inspect_vm
@ -64,10 +63,12 @@ def qemu_command(
"-audiodev", "spice,id=audio0",
"-device", "intel-hda",
"-device", "hda-duplex,audiodev=audio0",
"-vga", "none",
"-device", "virtio-gpu-gl",
"-display", "spice-app,gl=on",
"-device", "virtio-serial-pci",
"-chardev", "spicevmc,id=vdagent0,name=vdagent",
"-device", "virtserialport,chardev=vdagent0,name=com.redhat.spice.0",
"-spice", f"unix=on,addr={spice_socket},disable-ticketing=on",
"-device", "qemu-xhci,id=spicepass",
"-chardev", "spicevmc,id=usbredirchardev1,name=usbredir",
"-device", "usb-redir,chardev=usbredirchardev1,id=usbredirdev1",
@ -86,21 +87,6 @@ def qemu_command(
return command
def start_spicy(spice_socket: Path, stop_condition: Condition) -> None:
while not spice_socket.exists():
with stop_condition:
if stop_condition.wait(0.1):
return
spicy = nix_shell(["spice-gtk"], ["spicy", f"--uri=spice+unix://{spice_socket}"])
proc = subprocess.Popen(spicy)
while proc.poll() is None:
with stop_condition:
if stop_condition.wait(0.1):
proc.terminate()
proc.wait()
class BuildVmTask(BaseTask):
def __init__(self, uuid: UUID, vm: VmConfig, nix_options: list[str] = []) -> None:
super().__init__(uuid, num_cmds=7)
@ -229,22 +215,24 @@ class BuildVmTask(BaseTask):
disk_img=disk_img,
spice_socket=spice_socket,
)
stop_condition = Condition()
spice_thread = Thread(
target=start_spicy, args=(spice_socket, stop_condition), name="qemu"
)
spice_thread.start()
print("$ " + shlex.join(qemu_cmd))
try:
cmd.run(nix_shell(["qemu"], qemu_cmd), name="qemu")
finally:
with stop_condition:
stop_condition.notify()
spice_thread.join()
packages = ["qemu"]
if self.vm.graphics:
packages.append("virt-viewer")
env = os.environ.copy()
remote_viewer_mimetypes = module_root() / "vms" / "mimetypes"
env[
"XDG_DATA_DIRS"
] = f"{remote_viewer_mimetypes}:{env.get('XDG_DATA_DIRS', '')}"
print(env["XDG_DATA_DIRS"])
cmd.run(nix_shell(packages, qemu_cmd), name="qemu", env=env)
def run_vm(vm: VmConfig, nix_options: list[str] = []) -> BuildVmTask:
def run_vm(
vm: VmConfig, nix_options: list[str] = [], env: dict[str, str] = {}
) -> BuildVmTask:
return create_task(BuildVmTask, vm, nix_options)

View File

@ -7,7 +7,7 @@ from typing import IO, Any
import pytest
_FILE = None | int | IO[Any]
_FILE = None | int | IO[Any]
class Command:

View File

@ -1,7 +1,7 @@
import json
import tempfile
from pathlib import Path
from typing import Any
from typing import Any, Optional
import pytest
from cli import Cli
@ -213,11 +213,11 @@ def test_map_type() -> None:
def test_cast() -> None:
assert config.cast(value=["true"], type=bool, opt_description="foo-option") is True
assert (
config.cast(value=["null"], type=str | None, opt_description="foo-option")
config.cast(value=["null"], type=Optional[str], opt_description="foo-option") # noqa: UP007
is None
)
assert (
config.cast(value=["bar"], type=str | None, opt_description="foo-option")
config.cast(value=["bar"], type=Optional[str], opt_description="foo-option") # noqa: UP007
== "bar"
)