phoebus_os/installer/live/desktop/common/Bridge.qml

235 lines
7.1 KiB
Bash

pragma Singleton
import QtQuick
import Quickshell
import Quickshell.Io
// Typed JSON bridge to the privileged Rust installer. The frontend never
// scrapes terminal output: every call is a typed request and every reply is a
// typed JSON object. Secrets are written over the process stdin only.
Singleton {
id: root
// True once the bridge process is running and accepting requests.
property bool ready: false
property bool crashed: false
property string crashMessage: ""
property string lastError: ""
property var handshake: null
property var config: null
property bool restarting: false
property var pending: ({})
property int nextId: 1
signal event(string name, var data)
signal availabilityChanged
signal handshakeReady
// Emitted when the backend exits while the frontend is still using it. The
// frontend must reset any state that depended on the backend (notably AI
// validation) and offer a recovery.
signal backendLost(string message)
readonly property var bridgeCommand: {
const override = Quickshell.env("PHOEBUS_BRIDGE_CMD");
if (override && override.length > 0)
return override.split(" ").filter(part => part.length > 0);
// Absolute paths: Quickshell's Process execs the command directly and
// does not perform PATH lookup.
return ["/usr/bin/sudo", "-n", "/usr/local/bin/phoebus-installer", "--gui-bridge"];
}
// Retried until the first successful handshake, so a process-start race can
// never leave the frontend permanently stuck.
Timer {
interval: 400
repeat: true
running: proc.running && root.handshake === null
onTriggered: root.refreshHandshake(null)
}
Timer {
id: restartTimer
interval: 500
repeat: false
onTriggered: {
root.start();
root.restarting = false;
}
}
function start() {
if (!proc.running)
proc.running = true;
}
function stop() {
if (proc.running)
proc.running = false;
}
// Restart the backend after a crash. The bridge keeps no privileged state,
// so this is safe; the frontend re-pushes its configuration on the next
// action and must revalidate the AI connection.
function restart() {
if (root.restarting)
return;
root.restarting = true;
root.handshake = null;
root.stop();
restartTimer.start();
}
// Complete every outstanding request when the process dies, so no caller is
// left waiting and no busy indicator can stay stuck forever.
function failPending(message) {
const ids = Object.keys(root.pending);
for (let i = 0; i < ids.length; i++) {
const cb = root.pending[ids[i]];
delete root.pending[ids[i]];
try {
cb({ ok: false, error: message, error_kind: "backend_gone" });
} catch (e) {
console.log("bridge: pending callback failed: " + e);
}
}
}
function call(op, payload, onDone) {
const id = root.nextId++;
if (onDone)
root.pending[id] = onDone;
const request = Object.assign({ id: id, op: op }, payload || {});
const line = JSON.stringify(request) + "\n";
if (!proc.running) {
// Quickshell's Process.write() returns void, so it cannot be used
// to detect a failed write. Check the process state instead.
delete root.pending[id];
root.lastError = "backend not running";
if (onDone)
onDone({ ok: false, error: "the installer backend is not running", error_kind: "backend_gone" });
return;
}
proc.write(line);
}
function handleLine(line) {
const text = ("" + line).trim();
if (text.length === 0)
return;
let msg;
try {
msg = JSON.parse(text);
} catch (e) {
console.log("bridge: unparsable line: " + text);
return;
}
if (msg.event === "progress") {
root.event("progress", msg);
return;
}
if (msg.event === "log") {
root.event("log", msg);
return;
}
if (msg.id !== undefined && root.pending[msg.id]) {
const cb = root.pending[msg.id];
delete root.pending[msg.id];
cb(msg);
}
}
// Convenience wrappers.
function refreshHandshake(cb) {
call("handshake", {}, reply => {
if (reply.ok) {
root.handshake = reply.data;
root.handshakeReady();
}
if (cb)
cb(reply);
});
}
function listDisks(cb) {
call("disks", {}, cb);
}
function readiness(cb) {
call("readiness", {}, cb);
}
function options(cb) {
call("options", {}, cb);
}
function aiCatalog(cb) {
call("ai_catalog", {}, cb);
}
function setConfig(cfg, cb) {
call("set_config", { config: cfg }, reply => {
if (reply.ok)
root.config = cfg;
if (cb)
cb(reply);
});
}
function setSecrets(secrets, cb) {
call("set_secrets", { secrets: secrets }, cb);
}
function validate(cb) {
call("validate", {}, cb);
}
function plan(cb) {
call("plan", {}, cb);
}
function destructivePreview(cb) {
call("destructive_preview", {}, cb);
}
function aiCheck(cb) {
call("ai_check", {}, cb);
}
function aiTest(cb) {
call("ai_test", {}, cb);
}
function approve(eraseText, finalConfirmed, cb) {
call("approve", { erase: eraseText, final_confirm: finalConfirmed }, cb);
}
function startInstall(planDigest, eraseText, cb) {
call("start_install", {
erase: eraseText,
final_confirm: true,
plan_digest: planDigest
}, cb);
}
function logTail(lines, cb) {
call("log_tail", { lines: lines }, cb);
}
Process {
id: proc
command: root.bridgeCommand
stdinEnabled: true
running: false
onStarted: {
root.ready = true;
root.crashed = false;
root.crashMessage = "";
root.availabilityChanged();
}
onExited: exitCode => {
const wasRunning = root.ready;
root.ready = false;
root.crashed = true;
root.crashMessage = "The installer backend stopped (exit " + exitCode + ").";
root.failPending("the installer backend stopped before answering");
root.availabilityChanged();
if (wasRunning && !root.restarting)
root.backendLost(root.crashMessage);
}
stdout: SplitParser {
onRead: line => root.handleLine(line)
}
stderr: SplitParser {
onRead: line => {
root.lastError = line;
console.log("phoebus-bridge: " + line);
}
}
}
}