2024-02-29 18:26:45 +00:00
|
|
|
import logging
|
2024-03-06 08:05:10 +00:00
|
|
|
from collections.abc import Callable
|
2023-12-01 14:18:05 +00:00
|
|
|
from pathlib import Path
|
2024-04-24 12:41:29 +00:00
|
|
|
from typing import Any, ClassVar
|
2023-12-17 21:18:52 +00:00
|
|
|
|
2024-01-19 17:52:51 +00:00
|
|
|
import gi
|
2024-03-05 16:23:21 +00:00
|
|
|
from clan_cli.clan_uri import ClanURI
|
2024-01-17 12:11:49 +00:00
|
|
|
from clan_cli.history.add import HistoryEntry
|
2023-12-02 15:16:48 +00:00
|
|
|
|
2024-06-05 09:23:12 +00:00
|
|
|
from clan_app import assets
|
|
|
|
from clan_app.components.gkvstore import GKVStore
|
|
|
|
from clan_app.components.vmobj import VMObject
|
|
|
|
from clan_app.singletons.use_views import ViewStack
|
|
|
|
from clan_app.views.logs import Logs
|
2024-01-17 12:11:49 +00:00
|
|
|
|
2024-02-29 18:26:45 +00:00
|
|
|
gi.require_version("GObject", "2.0")
|
2024-01-19 17:52:51 +00:00
|
|
|
gi.require_version("Gtk", "4.0")
|
2024-04-24 12:41:29 +00:00
|
|
|
from gi.repository import Gio, GLib, GObject
|
2024-01-29 06:40:30 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2024-01-04 15:49:21 +00:00
|
|
|
|
|
|
|
|
2024-02-29 18:26:45 +00:00
|
|
|
class VMStore(GKVStore):
|
|
|
|
def __init__(self) -> None:
|
2024-03-03 09:47:38 +00:00
|
|
|
super().__init__(VMObject, lambda vm: vm.data.flake.flake_attr)
|
2024-02-29 18:26:45 +00:00
|
|
|
|
|
|
|
|
2024-04-24 12:41:29 +00:00
|
|
|
class Emitter(GObject.GObject):
|
|
|
|
__gsignals__: ClassVar = {
|
|
|
|
"is_ready": (GObject.SignalFlags.RUN_FIRST, None, []),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-03 09:47:38 +00:00
|
|
|
class ClanStore:
|
|
|
|
_instance: "None | ClanStore" = None
|
2024-02-29 15:46:09 +00:00
|
|
|
_clan_store: GKVStore[str, VMStore]
|
2024-01-20 09:11:52 +00:00
|
|
|
|
2024-04-24 12:41:29 +00:00
|
|
|
_emitter: Emitter
|
|
|
|
|
2024-03-17 13:08:39 +00:00
|
|
|
# set the vm that is outputting logs
|
|
|
|
# build logs are automatically streamed to the logs-view
|
|
|
|
_logging_vm: VMObject | None = None
|
|
|
|
|
2024-01-20 09:11:52 +00:00
|
|
|
# Make sure the VMS class is used as a singleton
|
|
|
|
def __init__(self) -> None:
|
|
|
|
raise RuntimeError("Call use() instead")
|
|
|
|
|
|
|
|
@classmethod
|
2024-03-03 09:47:38 +00:00
|
|
|
def use(cls: Any) -> "ClanStore":
|
2024-01-20 09:11:52 +00:00
|
|
|
if cls._instance is None:
|
|
|
|
cls._instance = cls.__new__(cls)
|
2024-02-29 15:46:09 +00:00
|
|
|
cls._clan_store = GKVStore(
|
|
|
|
VMStore, lambda store: store.first().data.flake.flake_url
|
|
|
|
)
|
2024-04-24 12:41:29 +00:00
|
|
|
cls._emitter = Emitter()
|
2024-02-29 18:26:45 +00:00
|
|
|
|
2024-01-20 09:11:52 +00:00
|
|
|
return cls._instance
|
|
|
|
|
2024-04-24 12:41:29 +00:00
|
|
|
def emit(self, signal: str) -> None:
|
|
|
|
self._emitter.emit(signal)
|
|
|
|
|
|
|
|
def connect(self, signal: str, cb: Callable[(...), Any]) -> None:
|
|
|
|
self._emitter.connect(signal, cb)
|
|
|
|
|
2024-03-17 13:08:39 +00:00
|
|
|
def set_logging_vm(self, ident: str) -> VMObject | None:
|
|
|
|
vm = self.get_vm(ClanURI(f"clan://{ident}"))
|
|
|
|
if vm is not None:
|
|
|
|
self._logging_vm = vm
|
|
|
|
|
|
|
|
return self._logging_vm
|
|
|
|
|
2024-03-06 08:05:10 +00:00
|
|
|
def register_on_deep_change(
|
|
|
|
self, callback: Callable[[GKVStore, int, int, int], None]
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Register a callback that is called when a clan_store or one of the included VMStores changes
|
|
|
|
"""
|
|
|
|
|
|
|
|
def on_vmstore_change(
|
|
|
|
store: VMStore, position: int, removed: int, added: int
|
|
|
|
) -> None:
|
|
|
|
callback(store, position, removed, added)
|
|
|
|
|
|
|
|
def on_clanstore_change(
|
|
|
|
store: "GKVStore", position: int, removed: int, added: int
|
|
|
|
) -> None:
|
|
|
|
if added > 0:
|
2024-03-07 12:04:48 +00:00
|
|
|
store.values()[position].register_on_change(on_vmstore_change)
|
2024-03-06 08:05:10 +00:00
|
|
|
callback(store, position, removed, added)
|
|
|
|
|
|
|
|
self.clan_store.register_on_change(on_clanstore_change)
|
|
|
|
|
2024-02-29 15:46:09 +00:00
|
|
|
@property
|
|
|
|
def clan_store(self) -> GKVStore[str, VMStore]:
|
|
|
|
return self._clan_store
|
|
|
|
|
2024-03-03 06:50:49 +00:00
|
|
|
def create_vm_task(self, vm: HistoryEntry) -> bool:
|
|
|
|
self.push_history_entry(vm)
|
|
|
|
return GLib.SOURCE_REMOVE
|
|
|
|
|
|
|
|
def push_history_entry(self, entry: HistoryEntry) -> None:
|
2024-03-03 09:01:08 +00:00
|
|
|
# TODO: We shouldn't do this here but in the list view
|
2024-03-03 06:50:49 +00:00
|
|
|
if entry.flake.icon is None:
|
2024-03-12 16:19:20 +00:00
|
|
|
icon: Path = assets.loc / "placeholder.jpeg"
|
2024-03-03 06:50:49 +00:00
|
|
|
else:
|
2024-03-12 16:19:20 +00:00
|
|
|
icon = Path(entry.flake.icon)
|
2024-03-03 06:50:49 +00:00
|
|
|
|
2024-03-17 13:08:39 +00:00
|
|
|
def log_details(gfile: Gio.File) -> None:
|
|
|
|
self.log_details(vm, gfile)
|
|
|
|
|
|
|
|
vm = VMObject(icon=icon, data=entry, build_log_cb=log_details)
|
2024-03-03 06:50:49 +00:00
|
|
|
self.push(vm)
|
|
|
|
|
2024-03-17 13:08:39 +00:00
|
|
|
def log_details(self, vm: VMObject, gfile: Gio.File) -> None:
|
|
|
|
views = ViewStack.use().view
|
|
|
|
logs_view: Logs = views.get_child_by_name("logs") # type: ignore
|
|
|
|
|
|
|
|
def file_read_callback(
|
|
|
|
source_object: Gio.File, result: Gio.AsyncResult, _user_data: Any
|
|
|
|
) -> None:
|
|
|
|
try:
|
|
|
|
# Finish the asynchronous read operation
|
|
|
|
res = source_object.load_contents_finish(result)
|
|
|
|
_success, contents, _etag_out = res
|
|
|
|
|
|
|
|
# Convert the byte array to a string and print it
|
|
|
|
logs_view.set_message(contents.decode("utf-8"))
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error reading file: {e}")
|
|
|
|
|
|
|
|
# only one vm can output logs at a time
|
|
|
|
if vm == self._logging_vm:
|
|
|
|
gfile.load_contents_async(None, file_read_callback, None)
|
|
|
|
|
|
|
|
# we cannot check this type, python is not smart enough
|
|
|
|
|
2024-03-03 09:47:38 +00:00
|
|
|
def push(self, vm: VMObject) -> None:
|
2024-03-12 16:19:20 +00:00
|
|
|
url = str(vm.data.flake.flake_url)
|
2024-02-29 18:26:45 +00:00
|
|
|
|
2024-03-05 16:10:30 +00:00
|
|
|
# Only write to the store if the Clan is not already in it
|
2024-02-29 18:26:45 +00:00
|
|
|
# Every write to the KVStore rerenders bound widgets to the clan_store
|
2024-02-29 15:46:09 +00:00
|
|
|
if url not in self.clan_store:
|
2024-02-29 18:26:45 +00:00
|
|
|
log.debug(f"Creating new VMStore for {url}")
|
|
|
|
vm_store = VMStore()
|
|
|
|
vm_store.append(vm)
|
|
|
|
self.clan_store[url] = vm_store
|
|
|
|
else:
|
|
|
|
vm_store = self.clan_store[url]
|
2024-03-05 16:10:30 +00:00
|
|
|
machine = vm.data.flake.flake_attr
|
|
|
|
old_vm = vm_store.get(machine)
|
|
|
|
|
|
|
|
if old_vm:
|
|
|
|
log.info(
|
|
|
|
f"VM {vm.data.flake.flake_attr} already exists in store. Updating data field."
|
|
|
|
)
|
|
|
|
old_vm.update(vm.data)
|
|
|
|
else:
|
|
|
|
log.debug(f"Appending VM {vm.data.flake.flake_attr} to store")
|
|
|
|
vm_store.append(vm)
|
2024-01-21 11:12:08 +00:00
|
|
|
|
2024-03-03 09:47:38 +00:00
|
|
|
def remove(self, vm: VMObject) -> None:
|
2024-03-12 16:19:20 +00:00
|
|
|
del self.clan_store[str(vm.data.flake.flake_url)][vm.data.flake.flake_attr]
|
2024-02-29 15:46:09 +00:00
|
|
|
|
2024-03-05 16:23:21 +00:00
|
|
|
def get_vm(self, uri: ClanURI) -> None | VMObject:
|
2024-03-08 16:47:27 +00:00
|
|
|
vm_store = self.clan_store.get(str(uri.flake_id))
|
2024-03-07 12:04:48 +00:00
|
|
|
if vm_store is None:
|
2024-02-29 18:26:45 +00:00
|
|
|
return None
|
2024-03-07 12:04:48 +00:00
|
|
|
machine = vm_store.get(uri.machine.name, None)
|
|
|
|
return machine
|
2024-02-07 08:41:04 +00:00
|
|
|
|
2024-03-03 09:47:38 +00:00
|
|
|
def get_running_vms(self) -> list[VMObject]:
|
2024-02-29 15:46:09 +00:00
|
|
|
return [
|
|
|
|
vm
|
|
|
|
for clan in self.clan_store.values()
|
|
|
|
for vm in clan.values()
|
|
|
|
if vm.is_running()
|
|
|
|
]
|
2024-01-20 09:11:52 +00:00
|
|
|
|
|
|
|
def kill_all(self) -> None:
|
|
|
|
for vm in self.get_running_vms():
|
2024-02-16 09:10:49 +00:00
|
|
|
vm.kill()
|