forked from clan/clan-core
Merge pull request 'UI: Added tray icon' (#831) from Qubasa-main into main
This commit is contained in:
commit
33787a6aab
@ -177,7 +177,7 @@ class Machine:
|
||||
[
|
||||
"--impure",
|
||||
"--expr",
|
||||
f'(builtins.fetchTree {{ type = "file"; url = "{config_json.name}"; }}).narHash',
|
||||
f'(builtins.fetchTree {{ type = "file"; url = "file://{config_json.name}"; }}).narHash',
|
||||
]
|
||||
)
|
||||
).stdout.strip()
|
||||
|
@ -12,7 +12,6 @@ from collections.abc import Iterator
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from typing import IO
|
||||
|
||||
from ..cmd import Log, run
|
||||
from ..dirs import machine_gcroot, module_root, user_cache_dir, vm_state_dir
|
||||
@ -147,9 +146,7 @@ def qemu_command(
|
||||
|
||||
|
||||
# TODO move this to the Machines class
|
||||
def get_vm_create_info(
|
||||
machine: Machine, vm: VmConfig, nix_options: list[str]
|
||||
) -> dict[str, str]:
|
||||
def build_vm(machine: Machine, vm: VmConfig, nix_options: list[str]) -> dict[str, str]:
|
||||
config = nix_config()
|
||||
system = config["system"]
|
||||
|
||||
@ -272,19 +269,12 @@ def start_waypipe(cid: int | None, title_prefix: str) -> Iterator[None]:
|
||||
proc.kill()
|
||||
|
||||
|
||||
def run_vm(
|
||||
vm: VmConfig,
|
||||
nix_options: list[str] = [],
|
||||
log_fd: IO[str] | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
log_fd can be used to stream the output of all commands to a UI
|
||||
"""
|
||||
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}")
|
||||
|
||||
# TODO: We should get this from the vm argument
|
||||
nixos_config = get_vm_create_info(machine, vm, nix_options)
|
||||
nixos_config = build_vm(machine, vm, nix_options)
|
||||
|
||||
# store the temporary rootfs inside XDG_CACHE_HOME on the host
|
||||
# otherwise, when using /tmp, we risk running out of memory
|
||||
|
@ -26,7 +26,6 @@ def test_history_add(
|
||||
"add",
|
||||
str(uri),
|
||||
]
|
||||
|
||||
cli.run(cmd)
|
||||
|
||||
history_file = user_history_file()
|
||||
|
@ -6,6 +6,8 @@ from .app import MainApplication
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# TODO: Trayicon support
|
||||
# https://github.com/nicotine-plus/nicotine-plus/blob/b08552584eb6f35782ad77da93ae4aae3362bf64/pynicotine/gtkgui/widgets/trayicon.py#L982
|
||||
def main() -> None:
|
||||
app = MainApplication()
|
||||
return app.run(sys.argv)
|
||||
|
@ -16,6 +16,7 @@ from clan_vm_manager.models.interfaces import ClanConfig
|
||||
from clan_vm_manager.models.use_join import GLib, GObject
|
||||
from clan_vm_manager.models.use_vms import VMS
|
||||
|
||||
from .trayicon import TrayIcon
|
||||
from .windows.main_window import MainWindow
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@ -29,9 +30,11 @@ class MainApplication(Adw.Application):
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
super().__init__(
|
||||
*args,
|
||||
application_id="lol.clan.vm.manager",
|
||||
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
|
||||
**kwargs,
|
||||
)
|
||||
self.tray_icon: TrayIcon | None = None
|
||||
|
||||
self.add_main_option(
|
||||
"debug",
|
||||
@ -42,7 +45,7 @@ class MainApplication(Adw.Application):
|
||||
None,
|
||||
)
|
||||
|
||||
self.win: Adw.ApplicationWindow | None = None
|
||||
self.window: Adw.ApplicationWindow | None = None
|
||||
self.connect("shutdown", self.on_shutdown)
|
||||
self.connect("activate", self.show_window)
|
||||
|
||||
@ -70,19 +73,33 @@ class MainApplication(Adw.Application):
|
||||
|
||||
def on_shutdown(self, app: Gtk.Application) -> None:
|
||||
log.debug("Shutting down")
|
||||
|
||||
if self.tray_icon is not None:
|
||||
self.tray_icon.destroy()
|
||||
|
||||
VMS.use().kill_all()
|
||||
|
||||
def on_window_hide_unhide(self, *_args: Any) -> None:
|
||||
assert self.window is not None
|
||||
if self.window.is_visible():
|
||||
self.window.hide()
|
||||
return
|
||||
|
||||
self.window.present()
|
||||
|
||||
def dummy_menu_entry(self) -> None:
|
||||
log.info("Dummy menu entry called")
|
||||
|
||||
def do_activate(self) -> None:
|
||||
self.show_window()
|
||||
|
||||
def show_window(self, app: Any = None) -> None:
|
||||
if not self.win:
|
||||
if not self.window:
|
||||
self.init_style()
|
||||
self.win = MainWindow(config=ClanConfig(initial_view="list"))
|
||||
self.win.set_application(self)
|
||||
icon_path = assets.loc / "clan_black.png"
|
||||
self.win.set_default_icon_name(str(icon_path))
|
||||
self.win.present()
|
||||
self.window = MainWindow(config=ClanConfig(initial_view="list"))
|
||||
self.window.set_application(self)
|
||||
self.tray_icon = TrayIcon(self)
|
||||
self.window.present()
|
||||
|
||||
# TODO: For css styling
|
||||
def init_style(self) -> None:
|
||||
|
@ -1,5 +1,4 @@
|
||||
from dataclasses import dataclass
|
||||
from enum import StrEnum
|
||||
|
||||
import gi
|
||||
|
||||
@ -9,8 +8,3 @@ gi.require_version("Gtk", "4.0")
|
||||
@dataclass
|
||||
class ClanConfig:
|
||||
initial_view: str
|
||||
|
||||
|
||||
class VMStatus(StrEnum):
|
||||
RUNNING = "Running"
|
||||
STOPPED = "Stopped"
|
||||
|
@ -1,6 +1,7 @@
|
||||
import os
|
||||
import tempfile
|
||||
import weakref
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import IO, Any, ClassVar
|
||||
|
||||
@ -13,7 +14,6 @@ from clan_cli.history.list import list_history
|
||||
|
||||
from clan_vm_manager import assets
|
||||
from clan_vm_manager.errors.show_error import show_error_dialog
|
||||
from clan_vm_manager.models.interfaces import VMStatus
|
||||
|
||||
from .executor import MPProcess, spawn
|
||||
|
||||
@ -98,27 +98,26 @@ class VM(GObject.Object):
|
||||
# Define a custom signal with the name "vm_stopped" and a string argument for the message
|
||||
__gsignals__: ClassVar = {
|
||||
"vm_status_changed": (GObject.SignalFlags.RUN_FIRST, None, [GObject.Object]),
|
||||
"build_vm": (GObject.SignalFlags.RUN_FIRST, None, [GObject.Object, bool]),
|
||||
}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
icon: Path,
|
||||
status: VMStatus,
|
||||
data: HistoryEntry,
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.data = data
|
||||
self.process = MPProcess("dummy", mp.Process(), Path("./dummy"))
|
||||
self._watcher_id: int = 0
|
||||
self._stop_watcher_id: int = 0
|
||||
self._stop_timer_init: datetime | None = None
|
||||
self._logs_id: int = 0
|
||||
self._log_file: IO[str] | None = None
|
||||
self.status = status
|
||||
self._last_liveness: bool = False
|
||||
self.log_dir = tempfile.TemporaryDirectory(
|
||||
prefix="clan_vm-", suffix=f"-{self.data.flake.flake_attr}"
|
||||
)
|
||||
self._finalizer = weakref.finalize(self, self.stop)
|
||||
self.connect("vm_status_changed", self._start_logs_task)
|
||||
|
||||
uri = ClanURI.from_str(
|
||||
url=self.data.flake.flake_url, flake_attr=self.data.flake.flake_attr
|
||||
@ -136,49 +135,46 @@ class VM(GObject.Object):
|
||||
)
|
||||
|
||||
def __start(self) -> None:
|
||||
if self.is_running():
|
||||
log.warn("VM is already running")
|
||||
return
|
||||
log.info(f"Starting VM {self.get_id()}")
|
||||
vm = vms.run.inspect_vm(self.machine)
|
||||
|
||||
GLib.idle_add(self.emit, "build_vm", self, True)
|
||||
vms.run.build_vm(self.machine, vm, [])
|
||||
GLib.idle_add(self.emit, "build_vm", self, False)
|
||||
|
||||
self.process = spawn(
|
||||
on_except=None,
|
||||
log_dir=Path(str(self.log_dir.name)),
|
||||
func=vms.run.run_vm,
|
||||
vm=vm,
|
||||
)
|
||||
log.debug("Starting VM")
|
||||
log.debug(f"Started VM {self.get_id()}")
|
||||
GLib.idle_add(self.emit, "vm_status_changed", self)
|
||||
log.debug(f"Starting logs watcher on file: {self.process.out_file}")
|
||||
self._logs_id = GLib.timeout_add(50, self._get_logs_task)
|
||||
if self._logs_id == 0:
|
||||
raise ClanError("Failed to add logs watcher")
|
||||
|
||||
log.debug(f"Starting VM watcher for: {self.machine.name}")
|
||||
self._watcher_id = GLib.timeout_add(50, self._vm_watcher_task)
|
||||
if self._watcher_id == 0:
|
||||
raise ClanError("Failed to add watcher")
|
||||
|
||||
self.machine.qmp_connect()
|
||||
|
||||
def start(self) -> None:
|
||||
if self.is_running():
|
||||
log.warn("VM is already running")
|
||||
return
|
||||
|
||||
threading.Thread(target=self.__start).start()
|
||||
|
||||
# Every 50ms check if the VM is still running
|
||||
self._watcher_id = GLib.timeout_add(50, self._vm_watcher_task)
|
||||
if self._watcher_id == 0:
|
||||
raise ClanError("Failed to add watcher")
|
||||
|
||||
def _vm_watcher_task(self) -> bool:
|
||||
if self.is_running() != self._last_liveness:
|
||||
if not self.is_running():
|
||||
self.emit("vm_status_changed", self)
|
||||
prev_liveness = self._last_liveness
|
||||
self._last_liveness = self.is_running()
|
||||
|
||||
# If the VM was running and now it is not, remove the watcher
|
||||
if prev_liveness and not self.is_running():
|
||||
log.debug("Removing VM watcher")
|
||||
return GLib.SOURCE_REMOVE
|
||||
return GLib.SOURCE_CONTINUE
|
||||
|
||||
def _start_logs_task(self, obj: Any, vm: Any) -> None:
|
||||
if self.is_running():
|
||||
log.debug(f"Starting logs watcher on file: {self.process.out_file}")
|
||||
self._logs_id = GLib.timeout_add(50, self._get_logs_task)
|
||||
else:
|
||||
log.debug("Not starting logs watcher")
|
||||
return GLib.SOURCE_CONTINUE
|
||||
|
||||
def _get_logs_task(self) -> bool:
|
||||
if not self.process.out_file.exists():
|
||||
@ -192,15 +188,15 @@ class VM(GObject.Object):
|
||||
self._log_file = None
|
||||
return GLib.SOURCE_REMOVE
|
||||
|
||||
line = os.read(self._log_file.fileno(), 4096)
|
||||
if len(line) != 0:
|
||||
print(line.decode("utf-8"), end="", flush=True)
|
||||
|
||||
if not self.is_running():
|
||||
log.debug("Removing logs watcher")
|
||||
self._log_file = None
|
||||
return GLib.SOURCE_REMOVE
|
||||
|
||||
line = os.read(self._log_file.fileno(), 4096)
|
||||
if len(line) != 0:
|
||||
print(line.decode("utf-8"), end="", flush=True)
|
||||
|
||||
return GLib.SOURCE_CONTINUE
|
||||
|
||||
def is_running(self) -> bool:
|
||||
@ -209,12 +205,32 @@ class VM(GObject.Object):
|
||||
def get_id(self) -> str:
|
||||
return f"{self.data.flake.flake_url}#{self.data.flake.flake_attr}"
|
||||
|
||||
def __shutdown_watchdog(self) -> None:
|
||||
if self.is_running():
|
||||
assert self._stop_timer_init is not None
|
||||
diff = datetime.now() - self._stop_timer_init
|
||||
if diff.seconds > 10:
|
||||
log.error(f"VM {self.get_id()} has not stopped. Killing it")
|
||||
self.process.kill_group()
|
||||
return GLib.SOURCE_CONTINUE
|
||||
else:
|
||||
log.info(f"VM {self.get_id()} has stopped")
|
||||
return GLib.SOURCE_REMOVE
|
||||
|
||||
def __stop(self) -> None:
|
||||
log.info(f"Stopping VM {self.get_id()}")
|
||||
|
||||
self.machine.qmp_command("system_powerdown")
|
||||
self._stop_timer_init = datetime.now()
|
||||
self._stop_watcher_id = GLib.timeout_add(100, self.__shutdown_watchdog)
|
||||
if self._stop_watcher_id == 0:
|
||||
raise ClanError("Failed to add stop watcher")
|
||||
|
||||
def stop(self) -> None:
|
||||
if not self.is_running():
|
||||
return
|
||||
log.info(f"Stopping VM {self.get_id()}")
|
||||
# TODO: add fallback to kill the process if the QMP command fails
|
||||
self.machine.qmp_command("system_powerdown")
|
||||
threading.Thread(target=self.__stop).start()
|
||||
|
||||
def read_whole_log(self) -> str:
|
||||
if not self.process.out_file.exists():
|
||||
@ -296,7 +312,6 @@ def get_saved_vms() -> list[VM]:
|
||||
|
||||
base = VM(
|
||||
icon=Path(icon),
|
||||
status=VMStatus.STOPPED,
|
||||
data=entry,
|
||||
)
|
||||
vm_list.append(base)
|
||||
|
1218
pkgs/clan-vm-manager/clan_vm_manager/trayicon.py
Normal file
1218
pkgs/clan-vm-manager/clan_vm_manager/trayicon.py
Normal file
File diff suppressed because it is too large
Load Diff
@ -100,12 +100,10 @@ class ClanList(Gtk.Box):
|
||||
box.set_valign(Gtk.Align.CENTER)
|
||||
|
||||
add_button = Gtk.MenuButton()
|
||||
add_button.set_icon_name("list-add")
|
||||
add_button.set_has_frame(False)
|
||||
add_button.set_menu_model(menu_model)
|
||||
|
||||
add_button.set_label("Add machine")
|
||||
box.append(add_button)
|
||||
box.append(Gtk.Label.new("Add machine"))
|
||||
|
||||
grp.set_header_suffix(box)
|
||||
|
||||
@ -192,6 +190,7 @@ class ClanList(Gtk.Box):
|
||||
|
||||
switch.connect("notify::active", partial(self.on_row_toggle, vm))
|
||||
vm.connect("vm_status_changed", partial(self.vm_status_changed, switch))
|
||||
vm.connect("build_vm", self.build_vm)
|
||||
|
||||
# suffix.append(box)
|
||||
row.add_suffix(box)
|
||||
@ -295,6 +294,12 @@ class ClanList(Gtk.Box):
|
||||
row.set_state(True)
|
||||
vm.stop()
|
||||
|
||||
def build_vm(self, vm: VM, _vm: VM, building: bool) -> None:
|
||||
if building:
|
||||
log.info("Building VM")
|
||||
else:
|
||||
log.info("VM built")
|
||||
|
||||
def vm_status_changed(self, switch: Gtk.Switch, vm: VM, _vm: VM) -> None:
|
||||
switch.set_active(vm.is_running())
|
||||
switch.set_state(vm.is_running())
|
||||
|
@ -57,7 +57,7 @@ python3.pkgs.buildPythonApplication {
|
||||
'';
|
||||
desktopItems = [
|
||||
(makeDesktopItem {
|
||||
name = "clan-vm-manager";
|
||||
name = "lol.clan.vm.manager";
|
||||
exec = "clan-vm-manager %u";
|
||||
icon = ./clan_vm_manager/assets/clan_white.png;
|
||||
desktopName = "cLAN Manager";
|
||||
|
@ -20,18 +20,18 @@ mkShell {
|
||||
|
||||
ln -snf ${clan-vm-manager} result
|
||||
|
||||
|
||||
# install desktop file
|
||||
set -eou pipefail
|
||||
DESKTOP_DST=~/.local/share/applications/clan-vm-manager.desktop
|
||||
DESKTOP_SRC=${clan-vm-manager}/share/applications/clan-vm-manager.desktop
|
||||
DESKTOP_FILE_NAME=lol.clan.vm.manager.desktop
|
||||
DESKTOP_DST=~/.local/share/applications/$DESKTOP_FILE_NAME
|
||||
DESKTOP_SRC=${clan-vm-manager}/share/applications/$DESKTOP_FILE_NAME
|
||||
# UI_BIN="env GTK_DEBUG=interactive ${clan-vm-manager}/bin/clan-vm-manager"
|
||||
UI_BIN="${clan-vm-manager}/bin/clan-vm-manager"
|
||||
|
||||
cp -f $DESKTOP_SRC $DESKTOP_DST
|
||||
sleep 2
|
||||
sed -i "s|Exec=.*clan-vm-manager|Exec=$UI_BIN|" $DESKTOP_DST
|
||||
xdg-mime default clan-vm-manager.desktop x-scheme-handler/clan
|
||||
xdg-mime default $DESKTOP_FILE_NAME x-scheme-handler/clan
|
||||
echo "==== Validating desktop file installation ===="
|
||||
set -x
|
||||
desktop-file-validate $DESKTOP_DST
|
||||
|
Loading…
Reference in New Issue
Block a user