Skip to content

Commit

Permalink
feat(runner): support debian 11 and 12
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Mar 21, 2024
1 parent 7b403fe commit 5c1a742
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
27 changes: 22 additions & 5 deletions src/runner/check_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -36,20 +45,28 @@ fn get_arch() -> Result<String> {
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<SystemInfo> {
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);
Expand Down
82 changes: 81 additions & 1 deletion src/runner/setup.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -37,9 +39,66 @@ fn run_with_sudo(command_args: &[&str]) -> Result<()> {
Ok(())
}

lazy_static! {
static ref SYSTEM_INFO_TO_CODSPEED_VALGRIND_FILENAME: HashMap<SystemInfo, String> = {
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?;

Expand Down Expand Up @@ -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"
)
);
}
}

0 comments on commit 5c1a742

Please sign in to comment.