Skip to content

Commit

Permalink
monitor: fix create_dir_all failure when updating screenshots (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
delan committed Jan 2, 2025
1 parent 6ad78c6 commit a4840c0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
37 changes: 26 additions & 11 deletions monitor/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tracing::info;

use crate::DOTENV;

pub fn get_data_path(path: impl AsRef<Path>) -> eyre::Result<PathBuf> {
pub fn get_data_path<'p>(path: impl Into<Option<&'p Path>>) -> eyre::Result<PathBuf> {
let data = if let Some(path) = &DOTENV.monitor_data_path {
path.into()
} else {
Expand All @@ -17,24 +17,39 @@ pub fn get_data_path(path: impl AsRef<Path>) -> eyre::Result<PathBuf> {

fs::create_dir_all(&data)?;

Ok(data.join(path))
Ok(match path.into() {
Some(path) => data.join(path),
None => data,
})
}

pub fn get_runner_data_path(id: usize, path: impl AsRef<Path>) -> eyre::Result<PathBuf> {
let runner_data = get_data_path("runners")?.join(id.to_string());
pub fn get_runner_data_path<'p>(
id: usize,
path: impl Into<Option<&'p Path>>,
) -> eyre::Result<PathBuf> {
let runner_data = get_data_path(Path::new("runners"))?.join(id.to_string());

Ok(runner_data.join(path))
Ok(match path.into() {
Some(path) => runner_data.join(path),
None => runner_data,
})
}

pub fn get_profile_data_path(key: &str, path: impl AsRef<Path>) -> eyre::Result<PathBuf> {
let profile_data = get_data_path("profiles")?.join(key);
pub fn get_profile_data_path<'p>(
key: &str,
path: impl Into<Option<&'p Path>>,
) -> eyre::Result<PathBuf> {
let profile_data = get_data_path(Path::new("profiles"))?.join(key);

Ok(profile_data.join(path))
Ok(match path.into() {
Some(path) => profile_data.join(path),
None => profile_data,
})
}

#[tracing::instrument]
pub fn run_migrations() -> eyre::Result<()> {
let migrations_dir = get_data_path("migrations")?;
let migrations_dir = get_data_path(Path::new("migrations"))?;
create_dir_all(&migrations_dir)?;

for version in 1.. {
Expand All @@ -45,9 +60,9 @@ pub fn run_migrations() -> eyre::Result<()> {
match version {
1 => {
info!("Moving per-runner data to runners subdirectory");
let runners_dir = get_data_path("runners")?;
let runners_dir = get_data_path(Path::new("runners"))?;
create_dir_all(&runners_dir)?;
for entry in read_dir(get_data_path(".")?)? {
for entry in read_dir(get_data_path(None)?)? {
let entry = entry?;
// Move entries that parse as a runner id (usize)
if entry
Expand Down
7 changes: 4 additions & 3 deletions monitor/src/id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fs::File,
io::{Read, Write},
path::Path,
};

use jane_eyre::eyre::{self, Context};
Expand All @@ -15,7 +16,7 @@ pub struct IdGen {

impl IdGen {
pub fn new_load() -> eyre::Result<Self> {
if let Ok(mut file) = File::open(get_data_path("last-runner-id")?) {
if let Ok(mut file) = File::open(get_data_path(Path::new("last-runner-id"))?) {
let mut last = String::default();
file.read_to_string(&mut last)
.wrap_err("Failed to read last runner id")?;
Expand Down Expand Up @@ -45,8 +46,8 @@ impl IdGen {
}

fn write_last(&self, last: usize) -> eyre::Result<()> {
let path = get_data_path("last-runner-id")?;
let new_path = get_data_path("last-runner-id.new")?;
let path = get_data_path(Path::new("last-runner-id"))?;
let new_path = get_data_path(Path::new("last-runner-id.new"))?;
let mut file = File::create(&new_path)?;
file.write_all(last.to_string().as_bytes())?;
std::fs::rename(&new_path, &path)?;
Expand Down
3 changes: 2 additions & 1 deletion monitor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::{
fs::File,
io::Read,
net::IpAddr,
path::Path,
process::exit,
sync::{LazyLock, RwLock},
thread,
Expand Down Expand Up @@ -232,7 +233,7 @@ async fn main() -> eyre::Result<()> {
.and(warp::filters::method::get())
.and(warp::filters::header::optional("If-None-Match"))
.and_then(|runner_id, if_none_match: Option<String>| async move {
let path = get_runner_data_path(runner_id, "screenshot.png")
let path = get_runner_data_path(runner_id, Path::new("screenshot.png"))
.wrap_err("Failed to compute path")
.map_err(InternalError)?;
let mut file = File::open(path)
Expand Down
2 changes: 1 addition & 1 deletion monitor/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl Profile {
}

fn try_update_screenshot(&self) -> eyre::Result<()> {
let output_dir = get_profile_data_path(&self.base_vm_name, ".")?;
let output_dir = get_profile_data_path(&self.base_vm_name, None)?;
update_screenshot(&self.base_vm_name, &output_dir)?;

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions monitor/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
collections::{BTreeMap, BTreeSet},
fmt::Debug,
fs,
path::PathBuf,
path::{Path, PathBuf},
process::Command,
time::{Duration, SystemTime, UNIX_EPOCH},
};
Expand Down Expand Up @@ -214,7 +214,7 @@ impl Runners {
let Some(guest_name) = runner.guest_name.as_deref() else {
bail!("Tried to screenshot a runner with no libvirt guest: {id}");
};
let output_dir = get_runner_data_path(id, ".")?;
let output_dir = get_runner_data_path(id, None)?;
update_screenshot(guest_name, &output_dir)?;

Ok(())
Expand All @@ -226,7 +226,7 @@ impl Runner {
///
/// For use by [`Runners::new`] only. Does not create a runner.
fn new(id: usize) -> eyre::Result<Self> {
let created_time = get_runner_data_path(id, "created-time")?;
let created_time = get_runner_data_path(id, Path::new("created-time"))?;
let created_time = fs::metadata(created_time)?.modified()?;
trace!(?created_time, "[{id}]");

Expand Down

0 comments on commit a4840c0

Please sign in to comment.