From 1f1d4002b7455439cc37b7c4b0a05deee74f50e7 Mon Sep 17 00:00:00 2001 From: Delan Azabani Date: Wed, 1 Jan 2025 16:56:12 +0800 Subject: [PATCH] monitor: move per-runner data to data/runners --- create-runner.sh | 2 +- monitor/src/data.rs | 40 ++++++++++++++++++++++++++++++++++++++-- monitor/src/main.rs | 4 +++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/create-runner.sh b/create-runner.sh index 6020224..fd7b13f 100755 --- a/create-runner.sh +++ b/create-runner.sh @@ -13,7 +13,7 @@ register_runner=$script_dir/$configuration_name/register-runner.sh vm=$base_vm.$id >&2 printf '[*] Creating runner: %s\n' $vm -runner_data=$SERVO_CI_MONITOR_DATA_PATH/$id +runner_data=$SERVO_CI_MONITOR_DATA_PATH/runners/$id mkdir $runner_data touch $runner_data/created-time diff --git a/monitor/src/data.rs b/monitor/src/data.rs index 55be105..04765b3 100644 --- a/monitor/src/data.rs +++ b/monitor/src/data.rs @@ -1,9 +1,10 @@ use std::{ - fs, + fs::{self, create_dir_all, read_dir, rename, File}, path::{Path, PathBuf}, }; use jane_eyre::eyre; +use tracing::info; use crate::DOTENV; @@ -20,7 +21,42 @@ pub fn get_data_path(path: impl AsRef) -> eyre::Result { } pub fn get_runner_data_path(id: usize, path: impl AsRef) -> eyre::Result { - let runner_data = get_data_path(id.to_string())?; + let runner_data = get_data_path("runners")?.join(id.to_string()); Ok(runner_data.join(path)) } + +#[tracing::instrument] +pub fn run_migrations() -> eyre::Result<()> { + let migrations_dir = get_data_path("migrations")?; + create_dir_all(&migrations_dir)?; + + for version in 1.. { + let marker_path = migrations_dir.join(version.to_string()); + if marker_path.try_exists()? { + continue; + } + match version { + 1 => { + info!("Moving per-runner data to runners subdirectory"); + let runners_dir = get_data_path("runners")?; + create_dir_all(&runners_dir)?; + for entry in read_dir(get_data_path(".")?)? { + let entry = entry?; + // Move entries that parse as a runner id (usize) + if entry + .file_name() + .to_str() + .is_some_and(|n| n.parse::().is_ok()) + { + rename(entry.path(), runners_dir.join(entry.file_name()))?; + } + } + } + _ => break, + } + File::create(marker_path)?; + } + + Ok(()) +} diff --git a/monitor/src/main.rs b/monitor/src/main.rs index 3bb70f2..5b4ce7d 100644 --- a/monitor/src/main.rs +++ b/monitor/src/main.rs @@ -38,7 +38,7 @@ use warp::{ use crate::{ dashboard::Dashboard, - data::get_runner_data_path, + data::{get_runner_data_path, run_migrations}, github::{list_registered_runners_for_host, Cache}, id::IdGen, libvirt::list_runner_guests, @@ -126,6 +126,8 @@ async fn main() -> eyre::Result<()> { .with(EnvFilter::builder().from_env_lossy()) .init(); + run_migrations()?; + tokio::task::spawn(async move { let thread = thread::spawn(monitor_thread); loop {