merge systemd + machinectl into one module/applet (step 1)
This commit is contained in:
parent
96bfc1fd5a
commit
6fc7e8bc8a
16 changed files with 261 additions and 1029 deletions
|
|
@ -1,152 +0,0 @@
|
|||
pragma Singleton
|
||||
|
||||
import QtQuick
|
||||
import Quickshell.Io
|
||||
import "." as S
|
||||
import NovaStats as NS
|
||||
|
||||
QtObject {
|
||||
id: root
|
||||
|
||||
property var machines: []
|
||||
|
||||
// cache: machineName -> {state, units, loading}
|
||||
property var _cache: ({})
|
||||
|
||||
readonly property bool anyUnhealthy: {
|
||||
for (const k of Object.keys(_cache)) {
|
||||
if ((_cache[k]?.units?.length ?? 0) > 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function machineState(name) {
|
||||
return _cache[name]?.state ?? "unknown";
|
||||
}
|
||||
function machineUnits(name) {
|
||||
return _cache[name]?.units ?? [];
|
||||
}
|
||||
function machineLoading(name) {
|
||||
return _cache[name]?.loading ?? false;
|
||||
}
|
||||
|
||||
signal machineReady(string machineName, string state, var units)
|
||||
signal machineJournalReady(string machineName, string unitName, string text)
|
||||
|
||||
// Per-call state: kept on root (not on the inline Process objects) so qmllint can see them.
|
||||
property string _machineName: ""
|
||||
property string _journalMachine: ""
|
||||
property string _journalUnit: ""
|
||||
property string _restartMachine: ""
|
||||
|
||||
function fetchMachine(name) {
|
||||
const c = Object.assign({}, _cache);
|
||||
c[name] = Object.assign({}, c[name] ?? {}, {
|
||||
loading: true
|
||||
});
|
||||
_cache = c;
|
||||
|
||||
root._machineName = name;
|
||||
_machineProc.command = ["sh", "-c", "busctl get-property --json=short --machine=" + name + " org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager SystemState 2>/dev/null || echo '{}'; " + "busctl call --json=short --machine=" + name + " org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager ListUnitsFiltered as 1 failed 2>/dev/null || echo '{}'"];
|
||||
if (_machineProc.running)
|
||||
_machineProc.running = false;
|
||||
_machineProc.running = true;
|
||||
}
|
||||
|
||||
function fetchMachineJournal(machineName, unitName) {
|
||||
root._journalMachine = machineName;
|
||||
root._journalUnit = unitName;
|
||||
_machineJournalProc.command = ["journalctl", "-M", machineName, "-u", unitName, "-n", "80", "--no-pager", "--output=short-precise"];
|
||||
if (_machineJournalProc.running)
|
||||
_machineJournalProc.running = false;
|
||||
_machineJournalProc.running = true;
|
||||
}
|
||||
|
||||
function restartMachineUnit(machineName, unitName) {
|
||||
root._restartMachine = machineName;
|
||||
_machineRestartProc.command = ["pkexec", "systemctl", "-M", machineName, "restart", unitName];
|
||||
if (_machineRestartProc.running)
|
||||
_machineRestartProc.running = false;
|
||||
_machineRestartProc.running = true;
|
||||
}
|
||||
|
||||
property Timer _poll: Timer {
|
||||
interval: NS.ModulesService.machinectlInterval ?? 15000
|
||||
running: NS.ModulesService.machinectlEnable
|
||||
repeat: true
|
||||
triggeredOnStart: true
|
||||
onTriggered: if (!root._listProc.running)
|
||||
root._listProc.running = true
|
||||
}
|
||||
|
||||
property Process _listProc: Process {
|
||||
command: ["busctl", "call", "--json=short", "org.freedesktop.machine1", "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", "ListMachines"]
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
try {
|
||||
const parsed = JSON.parse(text.trim());
|
||||
const newMachines = (parsed.data?.[0] || []).map(m => ({
|
||||
name: m[0],
|
||||
class: m[1],
|
||||
service: m[2]
|
||||
}));
|
||||
root.machines = newMachines;
|
||||
// drop stale cache entries
|
||||
const names = new Set(newMachines.map(m => m.name));
|
||||
const c = {};
|
||||
for (const k of Object.keys(root._cache)) {
|
||||
if (names.has(k))
|
||||
c[k] = root._cache[k];
|
||||
}
|
||||
root._cache = c;
|
||||
} catch (e) {
|
||||
root.machines = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2 lines of output: state, failed-units
|
||||
property Process _machineProc: Process {
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
const lines = text.trim().split("\n");
|
||||
let state = "unknown";
|
||||
let units = [];
|
||||
try {
|
||||
state = JSON.parse(lines[0] ?? "").data || "unknown";
|
||||
} catch (e) {}
|
||||
try {
|
||||
const parsed = JSON.parse(lines[1] ?? "");
|
||||
units = (parsed.data?.[0] || []).map(u => ({
|
||||
name: u[0],
|
||||
description: u[1],
|
||||
loadState: u[2],
|
||||
activeState: u[3],
|
||||
subState: u[4]
|
||||
}));
|
||||
} catch (e) {}
|
||||
const c = Object.assign({}, root._cache);
|
||||
c[root._machineName] = {
|
||||
state: state,
|
||||
units: units,
|
||||
loading: false
|
||||
};
|
||||
root._cache = c;
|
||||
root.machineReady(root._machineName, state, units);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
property Process _machineJournalProc: Process {
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: root.machineJournalReady(root._journalMachine, root._journalUnit, text)
|
||||
}
|
||||
}
|
||||
|
||||
property Process _machineRestartProc: Process {
|
||||
onRunningChanged: if (!running && root._restartMachine !== "")
|
||||
root.fetchMachine(root._restartMachine)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,100 +1,53 @@
|
|||
pragma Singleton
|
||||
|
||||
import QtQuick
|
||||
import Quickshell.Io
|
||||
import "." as S
|
||||
import NovaStats as NS
|
||||
|
||||
// Thin wrapper around NS.SystemdService: drives the poll Timer and parses the
|
||||
// JSON-encoded list properties into JS arrays for QML consumers. Restart helper
|
||||
// proxies through to the Rust singleton.
|
||||
QtObject {
|
||||
id: root
|
||||
|
||||
property string systemState: "unknown"
|
||||
property string userState: "unknown"
|
||||
property var systemUnits: []
|
||||
property var userUnits: []
|
||||
readonly property int totalFailedCount: systemUnits.length + userUnits.length
|
||||
readonly property string hostname: NS.SystemdService.hostname
|
||||
readonly property string systemState: NS.SystemdService.systemState
|
||||
readonly property string userState: NS.SystemdService.userState
|
||||
readonly property int failedCount: NS.SystemdService.failedCount
|
||||
|
||||
signal journalReady(string unitName, bool isUser, string text)
|
||||
|
||||
function refresh() {
|
||||
if (!_pollProc.running)
|
||||
_pollProc.running = true;
|
||||
}
|
||||
|
||||
// Per-fetch journal state: kept here (not on the Process) so qmllint can see them.
|
||||
property string _journalUnitName: ""
|
||||
property bool _journalIsUser: false
|
||||
|
||||
function fetchJournal(unitName, isUser) {
|
||||
_journalProc.command = isUser ? ["journalctl", "--user", "-u", unitName, "-n", "80", "--no-pager", "--output=short-precise"] : ["journalctl", "-u", unitName, "-n", "80", "--no-pager", "--output=short-precise"];
|
||||
root._journalUnitName = unitName;
|
||||
root._journalIsUser = isUser;
|
||||
if (_journalProc.running)
|
||||
_journalProc.running = false;
|
||||
_journalProc.running = true;
|
||||
}
|
||||
|
||||
function restartUnit(unitName, isUser) {
|
||||
_restartProc.command = isUser ? ["systemctl", "--user", "restart", unitName] : ["pkexec", "systemctl", "restart", unitName];
|
||||
if (_restartProc.running)
|
||||
_restartProc.running = false;
|
||||
_restartProc.running = true;
|
||||
}
|
||||
|
||||
function _parseState(json) {
|
||||
// Parsed [{ name, description, subState, scope, machine }, ...]
|
||||
readonly property var failedUnits: {
|
||||
try {
|
||||
return JSON.parse(json).data || "unknown";
|
||||
} catch (e) {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
function _parseUnits(json) {
|
||||
try {
|
||||
const parsed = JSON.parse(json);
|
||||
return (parsed.data?.[0] || []).map(u => ({
|
||||
name: u[0],
|
||||
description: u[1],
|
||||
loadState: u[2],
|
||||
activeState: u[3],
|
||||
subState: u[4]
|
||||
}));
|
||||
return JSON.parse(NS.SystemdService.failedUnitsJson);
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
property Timer _poll: Timer {
|
||||
interval: NS.ModulesService.systemdInterval ?? 15000
|
||||
// Parsed [{ name, class, service, systemState, failedUnits: [...] }, ...]
|
||||
readonly property var containers: {
|
||||
try {
|
||||
return JSON.parse(NS.SystemdService.containersJson);
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function restartUnit(name, scope, machine) {
|
||||
NS.SystemdService.restartUnit(name, scope ?? "system", machine ?? "");
|
||||
}
|
||||
|
||||
function refresh() {
|
||||
NS.SystemdService.poll();
|
||||
}
|
||||
|
||||
property Timer _pollTimer: Timer {
|
||||
interval: {
|
||||
const ms = NS.ModulesService.systemdInterval;
|
||||
return ms > 0 ? ms : 15000;
|
||||
}
|
||||
running: NS.ModulesService.systemdEnable
|
||||
repeat: true
|
||||
triggeredOnStart: true
|
||||
onTriggered: if (!root._pollProc.running)
|
||||
root._pollProc.running = true
|
||||
}
|
||||
|
||||
// 4 lines of output: systemState, systemUnits, userState, userUnits
|
||||
property Process _pollProc: Process {
|
||||
command: ["sh", "-c", "busctl get-property --json=short org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager SystemState 2>/dev/null || echo '{}'; " + "busctl call --json=short org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager ListUnitsFiltered as 1 failed 2>/dev/null || echo '{}'; " + "busctl --user get-property --json=short org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager SystemState 2>/dev/null || echo '{}'; " + "busctl --user call --json=short org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager ListUnitsFiltered as 1 failed 2>/dev/null || echo '{}'"]
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
const lines = text.trim().split("\n");
|
||||
root.systemState = root._parseState(lines[0] ?? "");
|
||||
root.systemUnits = root._parseUnits(lines[1] ?? "");
|
||||
root.userState = root._parseState(lines[2] ?? "");
|
||||
root.userUnits = root._parseUnits(lines[3] ?? "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
property Process _journalProc: Process {
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: root.journalReady(root._journalUnitName, root._journalIsUser, text)
|
||||
}
|
||||
}
|
||||
|
||||
property Process _restartProc: Process {
|
||||
onRunningChanged: if (!running)
|
||||
root.refresh()
|
||||
triggeredOnStart: false
|
||||
onTriggered: NS.SystemdService.poll()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ singleton CpuService 1.0 CpuService.qml
|
|||
singleton DockState 1.0 DockState.qml
|
||||
singleton IdleInhibitService 1.0 IdleInhibitService.qml
|
||||
singleton LockService 1.0 LockService.qml
|
||||
singleton MachinectlService 1.0 MachinectlService.qml
|
||||
singleton MprisService 1.0 MprisService.qml
|
||||
singleton NetworkService 1.0 NetworkService.qml
|
||||
singleton NiriIpc 1.0 NiriIpc.qml
|
||||
|
|
|
|||
Reference in a new issue