Files
bootvine/src/console_session/watch_loop.rs
T
Willem Vanhulle d0063c2371 refactor: drive the watch loop with async streams in the Elm style
Every source is a stream of ScreenInput values: the systemd bus (resolved into
BootChange values), keys, frame ticks, full repaints, password requests and
SIGTERM. They are merged with stream::select and folded by one owned
WatchScreen, drawing once per ready batch. The live boot and the scripted demo
share the fold and differ only in their console desk. tokio current-thread
replaces poll(2) and signal-hook. Release binary: 1.74 MB -> 1.86 MB.
2026-10-21 12:49:33 +02:00

100 lines
3.1 KiB
Rust

use std::{path::Path, time::Duration};
use futures::{StreamExt, stream};
use tokio::{runtime::Builder, time::sleep};
use crate::{
boot_model::{BootState, Phase, boot_origin::BootOrigin},
console_session::{
ask_password::RequestWatch,
console_takeover::Console,
live_desk::{LiveDesk, full_repaint_inputs, password_request_inputs},
stream_sources::{frame_inputs, key_inputs, termination_inputs},
watch_screen::{WatchEnding, WatchScreen, watch_until_ending},
},
screen_settings::{color_scheme::Colors, config_file::Timing},
systemd_link::{
boot_feed::{RootRequest, bus_inputs},
link_error::Error,
switch_root::INITRD_RELEASE,
systemd_bus::{Scope, Session},
},
};
pub struct Settings {
pub scope: Scope,
pub shutdown: bool,
pub target: RootRequest,
pub timing: Timing,
pub colors: Colors,
}
fn boot_already_finished(settings: &Settings) -> bool {
settings.scope == Scope::System
&& !settings.shutdown
&& Session::open(Scope::System)
.and_then(|session| session.boot_in_progress())
.is_ok_and(|booting| !booting)
}
fn starting_phase(settings: &Settings) -> Phase {
match settings.scope {
_ if settings.shutdown => Phase::Shutdown,
Scope::User => Phase::User,
Scope::System if Path::new(INITRD_RELEASE).exists() => Phase::Initrd,
Scope::System => Phase::Host,
}
}
pub fn run(settings: &Settings) -> Result<(), Error> {
if boot_already_finished(settings) {
eprintln!("bootvine: boot already finished, leaving the console alone");
return Ok(());
}
Builder::new_current_thread()
.enable_io()
.enable_time()
.build()?
.block_on(watch_live_boot(settings, starting_phase(settings)))
}
async fn watch_live_boot(settings: &Settings, phase: Phase) -> Result<(), Error> {
let requests = RequestWatch::open();
let desk = LiveDesk::new(
Console::take(settings.scope == Scope::System),
requests.is_some(),
);
let password_inputs = requests.map(password_request_inputs).transpose()?;
let inputs = stream::select(
stream::select(
bus_inputs(phase, settings.scope, settings.target.clone()),
key_inputs()?,
),
stream::select(
stream::select(frame_inputs(), full_repaint_inputs()),
stream::select(
termination_inputs()?,
stream::iter(password_inputs).flatten(),
),
),
);
let mut screen = WatchScreen::new(
BootState::new(phase),
desk,
BootOrigin::read(),
settings.colors,
settings.timing,
Duration::ZERO,
);
let mut terminal = ratatui::try_init()?;
let ending = watch_until_ending(&mut screen, &mut terminal, inputs).await;
if matches!(ending, WatchEnding::BootCompleted) {
sleep(settings.timing.keep_final_frame_for).await;
}
ratatui::restore();
match ending {
WatchEnding::Failed(error) => Err(error),
WatchEnding::BootCompleted | WatchEnding::ViewerLeft => Ok(()),
}
}