UI: Display error logs on VM crash. Fixed inspect_vm problem.

This commit is contained in:
Qubasa
2024-01-29 15:11:57 +07:00
parent 6da5ec6f1c
commit b10650b77b
4 changed files with 22 additions and 28 deletions

View File

@@ -35,6 +35,7 @@ class Machine:
self.deployment_info = json.loads( self.deployment_info = json.loads(
self.build_nix("config.system.clan.deployment.file").read_text() self.build_nix("config.system.clan.deployment.file").read_text()
) )
print(f"self_deployment_info: {self.deployment_info}")
@property @property
def deployment_address(self) -> str: def deployment_address(self) -> str:
@@ -46,6 +47,7 @@ class Machine:
def secrets_module(self) -> str: def secrets_module(self) -> str:
if not hasattr(self, "deployment_info"): if not hasattr(self, "deployment_info"):
self.get_deployment_info() self.get_deployment_info()
print(f"self_deployment_info2: {self.deployment_info}")
return self.deployment_info["secretsModule"] return self.deployment_info["secretsModule"]
@property @property
@@ -77,7 +79,8 @@ class Machine:
return Path(self.flake_path) return Path(self.flake_path)
print(nix_eval([f"{self.flake}"])) print(nix_eval([f"{self.flake}"]))
self.flake_path = run(nix_eval([f"{self.flake}"])).stdout.strip() print(f"self.flake:{self.flake}. Type: {type(self.flake)}")
self.flake_path = run(nix_eval([f"{self.flake} "])).stdout.strip()
return Path(self.flake_path) return Path(self.flake_path)
@property @property
@@ -95,6 +98,7 @@ class Machine:
system = config["system"] system = config["system"]
attr = f'clanInternals.machines."{system}".{self.name}.{attr}' attr = f'clanInternals.machines."{system}".{self.name}.{attr}'
print(f"attr: {attr}")
if attr in self.eval_cache and not refresh: if attr in self.eval_cache and not refresh:
return self.eval_cache[attr] return self.eval_cache[attr]
@@ -107,8 +111,10 @@ class Machine:
else: else:
flake = self.flake flake = self.flake
log.info(f"evaluating {flake}#{attr}") print(f"evaluating {flake}#{attr}")
output = run(nix_eval([f"{flake}#{attr}"])).stdout.strip() cmd = nix_eval([f"{flake}#{attr}"])
print(f"cmd: {cmd}")
output = run(cmd).stdout.strip()
self.eval_cache[attr] = output self.eval_cache[attr] = output
return output return output

View File

@@ -18,7 +18,7 @@ from collections.abc import Callable
# Kill the new process and all its children by sending a SIGTERM signal to the process group # Kill the new process and all its children by sending a SIGTERM signal to the process group
def _kill_group(proc: mp.Process) -> None: def _kill_group(proc: mp.Process) -> None:
pid = proc.pid pid = proc.pid
if proc.is_alive(): if proc.is_alive() and pid:
os.killpg(pid, signal.SIGTERM) os.killpg(pid, signal.SIGTERM)
else: else:
print(f"Process {proc.name} with pid {pid} is already dead", file=sys.stderr) print(f"Process {proc.name} with pid {pid} is already dead", file=sys.stderr)

View File

@@ -1,7 +1,5 @@
import sys
import tempfile import tempfile
import weakref import weakref
from collections.abc import Callable
from pathlib import Path from pathlib import Path
from typing import Any, ClassVar from typing import Any, ClassVar
@@ -18,12 +16,12 @@ from clan_vm_manager.models.interfaces import VMStatus
from .executor import MPProcess, spawn from .executor import MPProcess, spawn
gi.require_version("Gtk", "4.0") gi.require_version("Gtk", "4.0")
import threading
from gi.repository import Gio, GLib, GObject
import logging import logging
import multiprocessing as mp import multiprocessing as mp
import threading
from clan_cli.machines.machines import Machine from clan_cli.machines.machines import Machine
from gi.repository import Gio, GLib, GObject
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -57,11 +55,9 @@ class VM(GObject.Object):
return return
machine = Machine( machine = Machine(
name=self.data.flake.flake_attr, name=self.data.flake.flake_attr,
flake=self.data.flake.flake_url, flake=Path(self.data.flake.flake_url),
)
vm = vms.run.inspect_vm(
machine
) )
vm = vms.run.inspect_vm(machine)
self.process = spawn( self.process = spawn(
on_except=None, on_except=None,
log_dir=Path(str(self.log_dir.name)), log_dir=Path(str(self.log_dir.name)),
@@ -91,7 +87,7 @@ class VM(GObject.Object):
self._last_liveness = self.is_running() self._last_liveness = self.is_running()
# If the VM was running and now it is not, remove the watcher # If the VM was running and now it is not, remove the watcher
if prev_liveness == True and not self.is_running(): if prev_liveness and not self.is_running():
return GLib.SOURCE_REMOVE return GLib.SOURCE_REMOVE
return GLib.SOURCE_CONTINUE return GLib.SOURCE_CONTINUE

View File

@@ -1,5 +1,6 @@
from collections.abc import Callable from collections.abc import Callable
from functools import partial from functools import partial
import gi import gi
from clan_cli.history.add import HistoryEntry from clan_cli.history.add import HistoryEntry
@@ -7,7 +8,7 @@ from clan_vm_manager.models.use_join import Join, JoinValue
from clan_vm_manager.models.use_views import Views from clan_vm_manager.models.use_views import Views
gi.require_version("Adw", "1") gi.require_version("Adw", "1")
from gi.repository import Adw, Gdk, Gio, GObject, Gtk from gi.repository import Adw, Gio, GObject, Gtk
from clan_vm_manager.models.use_vms import VM, VMS from clan_vm_manager.models.use_vms import VM, VMS
@@ -144,22 +145,18 @@ class ClanList(Gtk.Box):
return row return row
def show_error_dialog(self, error: str) -> None: def show_error_dialog(self, error: str) -> None:
p = Views.use().main_window p = Views.use().main_window
# app = Gio.Application.get_default() # app = Gio.Application.get_default()
# p = Gtk.Application.get_active_window(app) # p = Gtk.Application.get_active_window(app)
dialog = Adw.MessageDialog( dialog = Adw.MessageDialog(heading="Error")
heading="Error"
)
dialog.add_response("ok", "ok") dialog.add_response("ok", "ok")
dialog.set_body(error) dialog.set_body(error)
dialog.set_transient_for(p) # set the parent window of the dialog dialog.set_transient_for(p) # set the parent window of the dialog
dialog.choose() dialog.choose()
def on_trust_clicked(self, item: JoinValue, widget: Gtk.Widget) -> None: def on_trust_clicked(self, item: JoinValue, widget: Gtk.Widget) -> None:
def on_join(_history: list[HistoryEntry]) -> None: def on_join(_history: list[HistoryEntry]) -> None:
VMS.use().refresh() VMS.use().refresh()
@@ -189,14 +186,9 @@ class ClanList(Gtk.Box):
row.set_state(True) row.set_state(True)
vm.stop() vm.stop()
def vm_status_changed(self, switch: Gtk.Switch, vm: VM, _vm: VM) -> None: def vm_status_changed(self, switch: Gtk.Switch, vm: VM, _vm: VM) -> None:
switch.set_active(vm.is_running()) switch.set_active(vm.is_running())
switch.set_state(vm.is_running()) switch.set_state(vm.is_running())
if vm.process and vm.process.proc.exitcode != 0: if not vm.is_running() and vm.process.proc.exitcode != 0:
print(f"====== {vm.is_running()}")
self.show_error_dialog(vm.read_log()) self.show_error_dialog(vm.read_log())