Skip to content

Commit abe4904

Browse files
committed
[NFC] monitor: extract libvirt screenshot code
1 parent 01823a5 commit abe4904

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

monitor/src/libvirt.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
use core::str;
2-
use std::process::{Command, Stdio};
2+
use std::{
3+
fs::rename,
4+
path::Path,
5+
process::{Command, Stdio},
6+
};
37

4-
use jane_eyre::eyre::{self, Context};
8+
use jane_eyre::eyre::{self, eyre, Context};
59

6-
use crate::DOTENV;
10+
use crate::{shell::SHELL, DOTENV};
711

812
pub fn list_runner_guests() -> eyre::Result<Vec<String>> {
913
let output = Command::new("../list-libvirt-guests.sh")
@@ -30,3 +34,23 @@ pub fn list_runner_guests() -> eyre::Result<Vec<String>> {
3034
pub fn libvirt_prefix() -> String {
3135
format!("{}-", DOTENV.libvirt_prefix)
3236
}
37+
38+
pub fn update_screenshot(guest_name: &str, output_dir: &Path) -> Result<(), eyre::Error> {
39+
let new_path = output_dir.join("screenshot.png.new");
40+
let exit_status = SHELL
41+
.lock()
42+
.map_err(|e| eyre!("Mutex poisoned: {e:?}"))?
43+
.run(
44+
include_str!("screenshot-guest.sh"),
45+
[Path::new(guest_name), &new_path],
46+
)?
47+
.spawn()?
48+
.wait()?;
49+
if !exit_status.success() {
50+
eyre::bail!("Command exited with status {}", exit_status);
51+
}
52+
let path = output_dir.join("screenshot.png");
53+
rename(new_path, path)?;
54+
55+
Ok(())
56+
}

monitor/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ fn monitor_thread() -> eyre::Result<()> {
467467
// Update dashboard data, for the API.
468468
if let Ok(mut dashboard) = DASHBOARD.write() {
469469
*dashboard = Some(Dashboard::render(&profile_runner_counts, &runners)?);
470-
runners.update_runner_screenshots();
470+
runners.update_screenshots();
471471
}
472472

473473
// Handle one request from the API.

monitor/src/runner.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::{
22
collections::{BTreeMap, BTreeSet},
33
fmt::Debug,
4-
fs::{self, rename},
5-
path::{Path, PathBuf},
4+
fs,
5+
path::PathBuf,
66
process::Command,
77
time::{Duration, SystemTime, UNIX_EPOCH},
88
};
@@ -13,7 +13,12 @@ use mktemp::Temp;
1313
use serde::Serialize;
1414
use tracing::{error, info, trace, warn};
1515

16-
use crate::{data::get_runner_data_path, github::ApiRunner, libvirt::libvirt_prefix, shell::SHELL};
16+
use crate::{
17+
data::get_runner_data_path,
18+
github::ApiRunner,
19+
libvirt::{libvirt_prefix, update_screenshot},
20+
shell::SHELL,
21+
};
1722

1823
#[derive(Debug, Serialize)]
1924
pub struct Runners {
@@ -194,38 +199,23 @@ impl Runners {
194199
}
195200
}
196201

197-
pub fn update_runner_screenshots(&self) {
202+
pub fn update_screenshots(&self) {
198203
for &id in self.runners.keys() {
199-
if let Err(error) = self.update_runner_screenshot(id) {
204+
if let Err(error) = self.update_screenshot(id) {
200205
error!(id, ?error, "Failed to update screenshot for runner");
201206
}
202207
}
203208
}
204209

205-
fn update_runner_screenshot(&self, id: usize) -> eyre::Result<()> {
210+
fn update_screenshot(&self, id: usize) -> eyre::Result<()> {
206211
let Some(runner) = self.runners.get(&id) else {
207212
bail!("No runner with id exists: {id}");
208213
};
209214
let Some(guest_name) = runner.guest_name.as_deref() else {
210215
bail!("Tried to screenshot a runner with no libvirt guest: {id}");
211216
};
212-
let new_path = get_runner_data_path(id, "screenshot.png.new")?;
213-
let exit_status = SHELL
214-
.lock()
215-
.map_err(|e| eyre!("Mutex poisoned: {e:?}"))?
216-
.run(
217-
include_str!("screenshot-guest.sh"),
218-
[Path::new(guest_name), &new_path],
219-
)?
220-
.spawn()?
221-
.wait()?;
222-
if !exit_status.success() {
223-
eyre::bail!("Command exited with status {}", exit_status);
224-
}
225-
226-
// Update the runner’s screenshot.png atomically
227-
let path = get_runner_data_path(id, "screenshot.png")?;
228-
rename(new_path, path)?;
217+
let output_dir = get_runner_data_path(id, ".")?;
218+
update_screenshot(guest_name, &output_dir)?;
229219

230220
Ok(())
231221
}

0 commit comments

Comments
 (0)