From 5c1a7424c61f642e859d596d421f44f20c14fb85 Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Thu, 21 Mar 2024 16:35:19 -0400 Subject: [PATCH] feat(runner): support debian 11 and 12 --- src/runner/check_system.rs | 27 ++++++++++--- src/runner/setup.rs | 82 +++++++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 6 deletions(-) diff --git a/src/runner/check_system.rs b/src/runner/check_system.rs index 71519303..5f5d0d74 100644 --- a/src/runner/check_system.rs +++ b/src/runner/check_system.rs @@ -2,6 +2,15 @@ use std::process::Command; use crate::prelude::*; +/// Returns the OS and version of the system +/// +/// ## Example output +/// ``` +/// ("Ubuntu", "20.04") +/// ("Ubuntu", "22.04") +/// ("Debian", "11") +/// ("Debian", "12") +/// ``` fn get_os_details() -> Result<(String, String)> { let lsb_output = Command::new("lsb_release") .args(["-i", "-r", "-s"]) @@ -36,20 +45,28 @@ fn get_arch() -> Result { Ok(output_str.trim().to_string()) } +#[derive(Eq, PartialEq, Hash)] pub struct SystemInfo { pub os: String, pub os_version: String, pub arch: String, } +/// Checks if the system is supported +/// +/// Supported systems: +/// - Ubuntu 20.04 on amd64 +/// - Ubuntu 22.04 on amd64 +/// - Debian 11 on amd64 +/// - Debian 12 on amd64 pub fn check_system() -> Result { let (os, os_version) = get_os_details()?; debug!("OS: {}, Version: {}", os, os_version); - if os != "Ubuntu" { - bail!("Only Ubuntu is supported at the moment"); - } - if !["20.04", "22.04"].contains(&os_version.as_str()) { - bail!("Only Ubuntu 20.04 and 22.04 are supported at the moment"); + match (os.as_str(), os_version.as_str()) { + ("Ubuntu", "20.04") | ("Ubuntu", "22.04") | ("Debian", "11") | ("Debian", "12") => (), + ("Ubuntu", _) => bail!("Only Ubuntu 20.04 and 22.04 are supported at the moment"), + ("Debian", _) => bail!("Only Debian 11 and 12 are supported at the moment"), + _ => bail!("Only Ubuntu and Debian are supported at the moment"), } let arch = get_arch()?; debug!("Arch: {}", arch); diff --git a/src/runner/setup.rs b/src/runner/setup.rs index 968dc16a..e3205ce4 100644 --- a/src/runner/setup.rs +++ b/src/runner/setup.rs @@ -1,8 +1,10 @@ use std::{ + collections::HashMap, env, process::{Command, Stdio}, }; +use lazy_static::lazy_static; use url::Url; use super::{check_system::SystemInfo, helpers::download_file::download_file}; @@ -37,9 +39,66 @@ fn run_with_sudo(command_args: &[&str]) -> Result<()> { Ok(()) } +lazy_static! { + static ref SYSTEM_INFO_TO_CODSPEED_VALGRIND_FILENAME: HashMap = { + let mut m = HashMap::new(); + m.insert( + SystemInfo { + os: "Ubuntu".to_string(), + os_version: "20.04".to_string(), + arch: "amd64".to_string(), + }, + format!( + "valgrind_{}_ubuntu-{}_amd64.deb", + VALGRIND_CODSPEED_VERSION, "20.04" + ), + ); + m.insert( + SystemInfo { + os: "Ubuntu".to_string(), + os_version: "22.04".to_string(), + arch: "amd64".to_string(), + }, + format!( + "valgrind_{}_ubuntu-{}_amd64.deb", + VALGRIND_CODSPEED_VERSION, "22.04" + ), + ); + m.insert( + SystemInfo { + os: "Debian".to_string(), + os_version: "11".to_string(), + arch: "amd64".to_string(), + }, + format!( + "valgrind_{}_ubuntu-{}_amd64.deb", + VALGRIND_CODSPEED_VERSION, "20.04" + ), + ); + m.insert( + SystemInfo { + os: "Debian".to_string(), + os_version: "12".to_string(), + arch: "amd64".to_string(), + }, + format!( + "valgrind_{}_ubuntu-{}_amd64.deb", + VALGRIND_CODSPEED_VERSION, "20.04" + ), + ); + m + }; +} + async fn install_valgrind(system_info: &SystemInfo) -> Result<()> { debug!("Installing valgrind"); - let valgrind_deb_url = format!("https://github.com/CodSpeedHQ/valgrind-codspeed/releases/download/{}/valgrind_{}_ubuntu-{}_amd64.deb", VALGRIND_CODSPEED_VERSION, VALGRIND_CODSPEED_VERSION, system_info.os_version); + let valgrind_deb_url = format!( + "https://github.com/CodSpeedHQ/valgrind-codspeed/releases/download/{}/{}", + VALGRIND_CODSPEED_VERSION, + SYSTEM_INFO_TO_CODSPEED_VALGRIND_FILENAME + .get(system_info) + .context("Unsupported system")? + ); let deb_path = env::temp_dir().join("valgrind-codspeed.deb"); download_file(&Url::parse(valgrind_deb_url.as_str()).unwrap(), &deb_path).await?; @@ -85,3 +144,24 @@ pub async fn setup(system_info: &SystemInfo, config: &Config) -> Result<()> { info!("Environment ready"); Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_system_info_to_codspeed_valgrind_version() { + let system_info = SystemInfo { + os: "Debian".to_string(), + os_version: "11".to_string(), + arch: "amd64".to_string(), + }; + assert_eq!( + SYSTEM_INFO_TO_CODSPEED_VALGRIND_FILENAME[&system_info], + format!( + "valgrind_{}_ubuntu-{}_amd64.deb", + VALGRIND_CODSPEED_VERSION, "20.04" + ) + ); + } +}