From a4840c07d6696860c4914d371ba56e9cee754af0 Mon Sep 17 00:00:00 2001 From: Delan Azabani Date: Thu, 2 Jan 2025 10:43:48 +0800 Subject: [PATCH] monitor: fix create_dir_all failure when updating screenshots (#6) --- monitor/src/data.rs | 37 ++++++++++++++++++++++++++----------- monitor/src/id.rs | 7 ++++--- monitor/src/main.rs | 3 ++- monitor/src/profile.rs | 2 +- monitor/src/runner.rs | 6 +++--- 5 files changed, 36 insertions(+), 19 deletions(-) diff --git a/monitor/src/data.rs b/monitor/src/data.rs index cd81465..4ad6721 100644 --- a/monitor/src/data.rs +++ b/monitor/src/data.rs @@ -8,7 +8,7 @@ use tracing::info; use crate::DOTENV; -pub fn get_data_path(path: impl AsRef) -> eyre::Result { +pub fn get_data_path<'p>(path: impl Into>) -> eyre::Result { let data = if let Some(path) = &DOTENV.monitor_data_path { path.into() } else { @@ -17,24 +17,39 @@ pub fn get_data_path(path: impl AsRef) -> eyre::Result { 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) -> eyre::Result { - let runner_data = get_data_path("runners")?.join(id.to_string()); +pub fn get_runner_data_path<'p>( + id: usize, + path: impl Into>, +) -> eyre::Result { + 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) -> eyre::Result { - let profile_data = get_data_path("profiles")?.join(key); +pub fn get_profile_data_path<'p>( + key: &str, + path: impl Into>, +) -> eyre::Result { + 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.. { @@ -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 diff --git a/monitor/src/id.rs b/monitor/src/id.rs index 8408c22..0f0d26e 100644 --- a/monitor/src/id.rs +++ b/monitor/src/id.rs @@ -1,6 +1,7 @@ use std::{ fs::File, io::{Read, Write}, + path::Path, }; use jane_eyre::eyre::{self, Context}; @@ -15,7 +16,7 @@ pub struct IdGen { impl IdGen { pub fn new_load() -> eyre::Result { - 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")?; @@ -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)?; diff --git a/monitor/src/main.rs b/monitor/src/main.rs index 417f382..4ada623 100644 --- a/monitor/src/main.rs +++ b/monitor/src/main.rs @@ -14,6 +14,7 @@ use std::{ fs::File, io::Read, net::IpAddr, + path::Path, process::exit, sync::{LazyLock, RwLock}, thread, @@ -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| 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) diff --git a/monitor/src/profile.rs b/monitor/src/profile.rs index 1320ac1..8ff2686 100644 --- a/monitor/src/profile.rs +++ b/monitor/src/profile.rs @@ -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(()) diff --git a/monitor/src/runner.rs b/monitor/src/runner.rs index ec1b72e..423b742 100644 --- a/monitor/src/runner.rs +++ b/monitor/src/runner.rs @@ -2,7 +2,7 @@ use std::{ collections::{BTreeMap, BTreeSet}, fmt::Debug, fs, - path::PathBuf, + path::{Path, PathBuf}, process::Command, time::{Duration, SystemTime, UNIX_EPOCH}, }; @@ -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(()) @@ -226,7 +226,7 @@ impl Runner { /// /// For use by [`Runners::new`] only. Does not create a runner. fn new(id: usize) -> eyre::Result { - 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}]");