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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//! What a client asks of a home besides its operations: a run's record, forgetting it, the runs
//! past their lifetime, the way into a machine, its screen and its hypervisor's app, where its
//! services are reached, the runner's key, and what the runs' downloads fetched. The client never
//! opens the home's files.
use isothrum_runner::{Download, DownloadRemoval, DownloadToAdopt, DownloadsToRemove, MachineUsage, NativeConsole, RunName, ServiceEndpoint, ShellCommand};
use super::console::{self, HeldConsole};
use super::{Client, Reply, Request, Result, unexpected};
use crate::proto::{Command, OpOptions, Target};
use crate::v1::RunRow;
use crate::wire::write_line;
impl Client {
/// `command`'s reply, as `pick` takes it; a reply of another kind is a protocol error.
async fn answer<T>(&self, command: Command, pick: impl FnOnce(Reply) -> std::result::Result<T, Box<Reply>>) -> Result<T> {
pick(self.request(command).await?).map_err(|other| unexpected(&other))
}
/// Run `run`'s record; none for a run never recorded.
///
/// # Errors
///
/// The store couldn't be read, or the connection ended.
pub async fn record(&self, run: &RunName) -> Result<Option<RunRow>> {
self.answer(Command::Record { run: run.clone() }, |reply| match reply {
Reply::Run { run: row, .. } => Ok(row.map(|r| *r)),
other => Err(Box::new(other)),
})
.await
}
/// Forgets run `run`'s record (what it created is left as it is).
///
/// # Errors
///
/// `locked` while an operation of the run is queued or running; the store couldn't be
/// written, or the connection ended.
pub async fn forget(&self, run: &RunName) -> Result<()> {
self.request(Command::Forget { run: run.clone() }).await.map(drop)
}
/// The recorded runs whose lifetime passed and that may still hold something: a reaper's
/// `down` each.
///
/// # Errors
///
/// The store couldn't be read, or the connection ended.
pub async fn expired(&self) -> Result<Vec<RunRow>> {
self.answer(Command::Expired, |reply| match reply {
Reply::Runs { runs, .. } => Ok(runs),
other => Err(Box::new(other)),
})
.await
}
/// The command that opens an interactive console into `machine` of run `run` (`target`:
/// where to open it, else as recorded), as `user` when given, for this client to run in its
/// own terminal.
///
/// # Errors
///
/// Where the run's format has no way in; the connection ended.
pub async fn console_command(&self, run: &RunName, target: Option<Target>, machine: &str, user: Option<&str>) -> Result<ShellCommand> {
let command = Command::ConsoleCommand {
run: run.clone(),
target,
machine: machine.to_owned(),
user: user.map(str::to_owned),
};
self.answer(command, |reply| match reply {
Reply::Shell { command: shell, .. } => Ok(shell),
other => Err(Box::new(other)),
})
.await
}
/// What opens running `machine` of run `run` in its hypervisor's own app, its hypervisor
/// asked with `options`' values and secrets.
///
/// # Errors
///
/// `no_console` when the hypervisor has no app or the machine isn't running; the
/// connection ended.
pub async fn native_console(&self, run: &RunName, target: Option<Target>, options: OpOptions, machine: &str) -> Result<NativeConsole> {
let command = Command::NativeConsole {
run: run.clone(),
target,
options,
machine: machine.to_owned(),
};
self.answer(command, |reply| match reply {
Reply::Native { console, .. } => Ok(console),
other => Err(Box::new(other)),
})
.await
}
/// Running `machine`'s graphical console, its hypervisor asked with `options`' values and
/// secrets, on a connection of its own that keeps its tunnel (when one carries it) open until
/// the [`HeldConsole`] is dropped.
///
/// # Errors
///
/// `no_console` when the machine has none, or it doesn't answer; `server_auth_failed` when
/// a server refuses the credentials; the service doesn't answer.
pub async fn graphical_console(&self, run: &RunName, target: Option<Target>, options: OpOptions, machine: &str) -> Result<HeldConsole> {
let (mut reader, mut writer) = self.dedicated().await?;
let command = Command::GraphicalConsole {
run: run.clone(),
target,
options,
machine: machine.to_owned(),
};
write_line(&mut writer, &Request { id: 1, command }).await?;
let console = console::of_reply(reader.next::<Reply>().await?)?;
Ok(HeldConsole {
console,
reader,
_writer: writer,
})
}
/// The service endpoints run `run` publishes, each where this computer reaches it: as the run
/// gives it, or at a local forward through its jump host, which the service keeps open. One
/// whose forward didn't open says why.
///
/// # Errors
///
/// The run's outputs can't be read; the connection ended.
pub async fn service_endpoints(&self, run: &RunName, target: Option<Target>, options: OpOptions) -> Result<Vec<ServiceEndpoint>> {
let command = Command::ServiceEndpoints {
run: run.clone(),
target,
options,
};
self.answer(command, |reply| match reply {
Reply::Endpoints { endpoints, .. } => Ok(endpoints),
other => Err(Box::new(other)),
})
.await
}
/// What each running machine of run `run` uses now (CPU and memory), by name, as its engine
/// samples it once; none for a format whose tool reports no per-machine figure.
///
/// # Errors
///
/// The run's engine doesn't answer, or its answer can't be read; the connection ended.
pub async fn usage(&self, run: &RunName, target: Option<Target>, options: OpOptions) -> Result<Vec<MachineUsage>> {
let command = Command::Usage {
run: run.clone(),
target,
options,
};
self.answer(command, |reply| match reply {
Reply::Usage { machines, .. } => Ok(machines),
other => Err(Box::new(other)),
})
.await
}
/// What the runs' own downloads fetched and the home records (images a pull pulled, boxes an
/// `up` added), oldest first, each with its size when it can be read.
///
/// # Errors
///
/// The store couldn't be read, or the connection ended.
pub async fn downloads(&self) -> Result<Vec<Download>> {
self.answer(Command::Downloads, |reply| match reply {
Reply::Downloads { downloads, .. } => Ok(downloads),
other => Err(Box::new(other)),
})
.await
}
/// Removes the recorded downloads `which` names, each on its own (never forced): how each
/// went, one its tool refused kept and said, the others removed.
///
/// # Errors
///
/// The store couldn't be read or written, or the connection ended.
pub async fn remove_downloads(&self, which: DownloadsToRemove) -> Result<Vec<DownloadRemoval>> {
self.answer(Command::RemoveDownloads { which }, |reply| match reply {
Reply::DownloadsRemoved { removed, .. } => Ok(removed),
other => Err(Box::new(other)),
})
.await
}
/// Adopts `adopted`, downloads fetched outside the runs (by an earlier program's own record),
/// into the home's record, each one on this computer now as it was fetched: they are listed
/// and removed like the runs' own. The downloads recorded for them, new or not.
///
/// # Errors
///
/// The engine or Vagrant couldn't say what they hold (nothing was recorded), the store
/// couldn't be written, or the connection ended.
pub async fn adopt_downloads(&self, adopted: Vec<DownloadToAdopt>) -> Result<Vec<Download>> {
self.answer(Command::AdoptDownloads { adopted }, |reply| match reply {
Reply::DownloadsAdopted { adopted: recorded, .. } => Ok(recorded),
other => Err(Box::new(other)),
})
.await
}
/// The IPv4 address blocks (CIDR) the networks of the engine Docker context `docker_context`
/// reaches hold (the current one when none is named), but those of run `except`'s own
/// networks: what a deployment's networks must stay clear of.
///
/// # Errors
///
/// Docker is missing or the engine can't say, or the connection ended.
pub async fn address_blocks_in_use(&self, except: Option<RunName>, docker_context: Option<String>) -> Result<Vec<String>> {
self.answer(Command::AddressBlocksInUse { except, docker_context }, |reply| match reply {
Reply::AddressBlocks { blocks, .. } => Ok(blocks),
other => Err(Box::new(other)),
})
.await
}
/// The public half of the runner's SSH key, one line (made when it has none).
///
/// # Errors
///
/// The key couldn't be made (`tool_missing` without `ssh-keygen`) or read; the connection
/// ended.
pub async fn public_key(&self) -> Result<String> {
self.answer(Command::PublicKey, |reply| match reply {
Reply::PublicKey { key, .. } => Ok(key),
other => Err(Box::new(other)),
})
.await
}
}