1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::sync::mpsc::{Receiver, Sender, TryRecvError, channel};
use std::thread::JoinHandle;
use crate::remote::AdoptedMaster;
#[derive(Debug)]
pub enum InstallEvent {
Log(String),
/// A croft is already installed on the remote, so the user can be
/// dropped into it now while the (re)install continues in the
/// background. Fired at most once, before `Done`.
CanLaunch,
/// The fast cross-build path is unavailable and the installer wants to
/// compile on the remote box instead (slow). The UI must answer via
/// `respond_fallback`: `true` runs the remote compile, `false` aborts
/// the install so the user can run `croft setup-cross` and reconnect
/// for the fast path. Fired only while the install still blocks the
/// connect (never after `CanLaunch`).
FallbackPrompt {
reason: String,
},
Done(Result<(), String>),
}
pub struct InstallSession {
pub host: String,
pub path: Option<String>,
pub adopted: Option<AdoptedMaster>,
events_rx: Receiver<InstallEvent>,
fallback_answer_tx: Sender<bool>,
_handle: Option<JoinHandle<()>>,
}
impl InstallSession {
pub fn start(adopted: AdoptedMaster, host: String, path: Option<String>) -> Self {
let (tx, rx): (Sender<InstallEvent>, Receiver<InstallEvent>) = channel();
let adopted_for_thread = adopted.clone();
let log_tx_for_install: Sender<String> = {
let tx = tx.clone();
let (s, r) = channel::<String>();
std::thread::spawn(move || {
while let Ok(line) = r.recv() {
if tx.send(InstallEvent::Log(line)).is_err() {
break;
}
}
});
s
};
let can_launch_tx: Sender<()> = {
let tx = tx.clone();
let (s, r) = channel::<()>();
std::thread::spawn(move || {
if r.recv().is_ok() {
let _ = tx.send(InstallEvent::CanLaunch);
}
});
s
};
let (fallback_answer_tx, fallback_answer_rx) = channel::<bool>();
let prompt_tx = tx.clone();
let done_tx = tx.clone();
let handle = std::thread::spawn(move || {
let mut confirm_fallback = move |reason: &str| {
if prompt_tx
.send(InstallEvent::FallbackPrompt {
reason: reason.to_string(),
})
.is_err()
{
return false;
}
// Blocks until the dialog answers. A torn-down dialog drops
// the answer sender, which reads as a decline.
fallback_answer_rx.recv().unwrap_or(false)
};
let result = crate::remote::install_only_streaming(
adopted_for_thread,
log_tx_for_install,
can_launch_tx,
&mut confirm_fallback,
);
let payload = match result {
Ok(_) => Ok(()),
Err(e) => Err(format!("{e:#}")),
};
let _ = done_tx.send(InstallEvent::Done(payload));
});
Self {
host,
path,
adopted: Some(adopted),
events_rx: rx,
fallback_answer_tx,
_handle: Some(handle),
}
}
pub fn drain_events(&mut self) -> Vec<InstallEvent> {
let mut out = Vec::new();
loop {
match self.events_rx.try_recv() {
Ok(ev) => out.push(ev),
Err(TryRecvError::Empty) => break,
Err(TryRecvError::Disconnected) => break,
}
}
out
}
/// Answer a pending `FallbackPrompt`: `true` proceeds with the slow
/// remote compile, `false` aborts the install.
pub fn respond_fallback(&self, proceed: bool) {
let _ = self.fallback_answer_tx.send(proceed);
}
/// Hand the adopted control master to the caller, leaving `None`
/// behind. Taken once the TUI has surrendered the alt-screen and is
/// ready to launch through the existing connection.
pub fn take_adopted(&mut self) -> Option<AdoptedMaster> {
self.adopted.take()
}
}