Skip to content

Commit

Permalink
feat: add system info to upload metadata runner property
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Jun 6, 2024
1 parent a28d707 commit bf9ced0
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 79 deletions.
53 changes: 51 additions & 2 deletions src/run/runner/check_system.rs → src/run/check_system.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::process::Command;

use serde::{Deserialize, Serialize};

use crate::prelude::*;

/// Returns the OS and version of the system
Expand Down Expand Up @@ -45,14 +47,54 @@ fn get_arch() -> Result<String> {
Ok(output_str.trim().to_string())
}

#[derive(Eq, PartialEq, Hash)]
fn get_host() -> Result<String> {
let host_output = Command::new("hostname")
.output()
.map_err(|_| anyhow!("Failed to get host info"))?;
if !host_output.status.success() {
bail!("Failed to get host info");
}
let output_str =
String::from_utf8(host_output.stdout).map_err(|_| anyhow!("Failed to parse host info"))?;
Ok(output_str.trim().to_string())
}

fn get_user() -> Result<String> {
let user_output = Command::new("whoami")
.output()
.map_err(|_| anyhow!("Failed to get user info"))?;
if !user_output.status.success() {
bail!("Failed to get user info");
}
let output_str =
String::from_utf8(user_output.stdout).map_err(|_| anyhow!("Failed to parse user info"))?;
Ok(output_str.trim().to_string())
}

#[derive(Eq, PartialEq, Hash, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SystemInfo {
pub os: String,
pub os_version: String,
pub arch: String,
pub host: String,
pub user: String,
}

/// Checks if the system is supported
#[cfg(test)]
impl SystemInfo {
pub fn test() -> Self {
SystemInfo {
os: "Ubuntu".to_string(),
os_version: "20.04".to_string(),
arch: "amd64".to_string(),
host: "host".to_string(),
user: "user".to_string(),
}
}
}

/// Checks if the system is supported and returns the system info
///
/// Supported systems:
/// - Ubuntu 20.04 on amd64
Expand All @@ -73,9 +115,16 @@ pub fn check_system() -> Result<SystemInfo> {
if arch != "amd64" && arch != "arm64" {
bail!("Only amd64 and arm64 are supported at the moment");
}
let user = get_user()?;
debug!("User: {}", user);
let host = get_host()?;
debug!("Host: {}", host);

Ok(SystemInfo {
os,
os_version,
arch,
host,
user,
})
}
9 changes: 8 additions & 1 deletion src/run/ci_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use git2::Repository;
use simplelog::SharedLogger;

use crate::prelude::*;
use crate::run::check_system::SystemInfo;
use crate::run::config::Config;
use crate::run::uploader::{Runner, UploadMetadata};

Expand Down Expand Up @@ -71,7 +72,12 @@ pub trait CIProvider {
/// let instruments = Instruments::new();
/// let metadata = provider.get_upload_metadata(&config, "abc123").unwrap();
/// ```
fn get_upload_metadata(&self, config: &Config, archive_hash: &str) -> Result<UploadMetadata> {
fn get_upload_metadata(
&self,
config: &Config,
system_info: &SystemInfo,
archive_hash: &str,
) -> Result<UploadMetadata> {
let provider_metadata = self.get_provider_metadata()?;

let commit_hash = get_commit_hash(&provider_metadata.repository_root_path)?;
Expand All @@ -86,6 +92,7 @@ pub trait CIProvider {
name: "codspeed-runner".into(),
version: crate::VERSION.into(),
instruments: config.instruments.get_active_instrument_names(),
system_info: system_info.clone(),
},
platform: self.get_provider_slug().into(),
})
Expand Down
6 changes: 4 additions & 2 deletions src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::run::{config::Config, logger::Logger};
use crate::VERSION;
use clap::Args;

mod check_system;
pub mod ci_provider;
mod helpers;
mod instruments;
Expand Down Expand Up @@ -118,12 +119,13 @@ pub async fn run(args: RunArgs, api_client: &CodSpeedAPIClient) -> Result<()> {
config.set_token(codspeed_config.auth.token.clone());
}

let run_data = runner::run(&config).await?;
let system_info = check_system::check_system()?;
let run_data = runner::run(&config, &system_info).await?;

if !config.skip_upload {
start_group!("Upload the results");
logger.persist_log_to_profile_folder(&run_data)?;
let upload_result = uploader::upload(&config, &provider, &run_data).await?;
let upload_result = uploader::upload(&config, &system_info, &provider, &run_data).await?;
end_group!();

if provider.get_provider_slug() == "local" {
Expand Down
1 change: 0 additions & 1 deletion src/run/runner/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod check_system;
mod helpers;
mod run;
mod setup;
Expand Down
10 changes: 5 additions & 5 deletions src/run/runner/run.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::prelude::*;
use crate::run::{config::Config, instruments::mongo_tracer::MongoTracer};
use crate::run::{
check_system::SystemInfo, config::Config, instruments::mongo_tracer::MongoTracer,
};

use std::path::PathBuf;

use super::{
check_system::check_system,
helpers::{perf_maps::harvest_perf_maps, profile_folder::create_profile_folder},
setup::setup,
valgrind,
Expand All @@ -14,11 +15,10 @@ pub struct RunData {
pub profile_folder: PathBuf,
}

pub async fn run(config: &Config) -> Result<RunData> {
pub async fn run(config: &Config, system_info: &SystemInfo) -> Result<RunData> {
if !config.skip_setup {
start_group!("Prepare the environment");
let system_info = check_system()?;
setup(&system_info, config).await?;
setup(system_info, config).await?;
end_group!();
}
//TODO: add valgrind version check
Expand Down
100 changes: 39 additions & 61 deletions src/run/runner/setup.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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};
use crate::run::config::Config;
use super::helpers::download_file::download_file;
use crate::run::{check_system::SystemInfo, config::Config};
use crate::{prelude::*, MONGODB_TRACER_VERSION, VALGRIND_CODSPEED_VERSION};

/// Run a command with sudo if available
Expand Down Expand Up @@ -40,55 +38,23 @@ 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
fn get_codspeed_valgrind_filename(system_info: &SystemInfo) -> Result<String> {
let version = match (
system_info.os.as_str(),
system_info.os_version.as_str(),
system_info.arch.as_str(),
) {
("Ubuntu", "20.04", "amd64") => "20.04",
("Ubuntu", "22.04", "amd64") => "22.04",
("Debian", "11", "amd64") => "20.04",
("Debian", "12", "amd64") => "20.04",
_ => bail!("Unsupported system"),
};

Ok(format!(
"valgrind_{}_ubuntu_{}_amd64.deb",
VALGRIND_CODSPEED_VERSION, version
))
}

fn check_installed_valgrind() -> Result<bool> {
Expand Down Expand Up @@ -119,9 +85,7 @@ async fn install_valgrind(system_info: &SystemInfo) -> Result<()> {
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")?
get_codspeed_valgrind_filename(system_info)?
);
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 @@ -174,18 +138,32 @@ mod tests {
use super::*;

#[test]
fn test_system_info_to_codspeed_valgrind_version() {
fn test_system_info_to_codspeed_valgrind_version_ubuntu() {
let system_info = SystemInfo {
os: "Ubuntu".to_string(),
os_version: "22.04".to_string(),
arch: "amd64".to_string(),
host: "host".to_string(),
user: "user".to_string(),
};
assert_eq!(
get_codspeed_valgrind_filename(&system_info).unwrap(),
"valgrind_3.21.0-0codspeed1_ubuntu_22.04_amd64.deb"
);
}

#[test]
fn test_system_info_to_codspeed_valgrind_version_debian() {
let system_info = SystemInfo {
os: "Debian".to_string(),
os_version: "11".to_string(),
arch: "amd64".to_string(),
host: "host".to_string(),
user: "user".to_string(),
};
assert_eq!(
SYSTEM_INFO_TO_CODSPEED_VALGRIND_FILENAME[&system_info],
format!(
"valgrind_{}_ubuntu-{}_amd64.deb",
VALGRIND_CODSPEED_VERSION, "20.04"
)
get_codspeed_valgrind_filename(&system_info).unwrap(),
"valgrind_3.21.0-0codspeed1_ubuntu_20.04_amd64.deb"
);
}
}
7 changes: 6 additions & 1 deletion src/run/uploader/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use serde::{Deserialize, Serialize};

use crate::run::{ci_provider::interfaces::ProviderMetadata, instruments::InstrumentNames};
use crate::run::{
check_system::SystemInfo, ci_provider::interfaces::ProviderMetadata,
instruments::InstrumentNames,
};

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
Expand All @@ -21,6 +24,8 @@ pub struct Runner {
pub name: String,
pub version: String,
pub instruments: Vec<InstrumentNames>,
#[serde(flatten)]
pub system_info: SystemInfo,
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ expression: upload_metadata
"version": "2.1.0",
"instruments": [
"MongoDB"
]
],
"os": "Ubuntu",
"osVersion": "20.04",
"arch": "amd64",
"host": "host",
"user": "user"
},
"platform": "github-actions",
"commitHash": "5bd77cb0da72bef094893ed45fb793ff16ecfbe3",
Expand Down
13 changes: 9 additions & 4 deletions src/run/uploader/upload.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::run::{ci_provider::CIProvider, config::Config, runner::RunData};
use crate::run::{
check_system::SystemInfo, ci_provider::CIProvider, config::Config, runner::RunData,
};
use crate::{prelude::*, request_client::REQUEST_CLIENT};
use async_compression::tokio::write::GzipEncoder;
use base64::{engine::general_purpose, Engine as _};
Expand Down Expand Up @@ -76,14 +78,15 @@ pub struct UploadResult {
#[allow(clippy::borrowed_box)]
pub async fn upload(
config: &Config,
system_info: &SystemInfo,
provider: &Box<dyn CIProvider>,
run_data: &RunData,
) -> Result<UploadResult> {
let (archive_buffer, archive_hash) = get_profile_archive_buffer(run_data).await?;

debug!("CI provider detected: {:#?}", provider.get_provider_name());

let upload_metadata = provider.get_upload_metadata(config, &archive_hash)?;
let upload_metadata = provider.get_upload_metadata(config, system_info, &archive_hash)?;
debug!("Upload metadata: {:#?}", upload_metadata);
if upload_metadata.tokenless {
let hash = upload_metadata.get_hash();
Expand All @@ -110,7 +113,6 @@ mod tests {
use url::Url;

use super::*;
use crate::run::runner::RunData;
use std::path::PathBuf;

// TODO: remove the ignore when implementing network mocking
Expand All @@ -129,6 +131,7 @@ mod tests {
env!("CARGO_MANIFEST_DIR")
)),
};
let system_info = SystemInfo::test();
async_with_vars(
[
("GITHUB_ACTIONS", Some("true")),
Expand Down Expand Up @@ -159,7 +162,9 @@ mod tests {
],
async {
let provider = crate::run::ci_provider::get_provider(&config).unwrap();
upload(&config, &provider, &run_data).await.unwrap();
upload(&config, &system_info, &provider, &run_data)
.await
.unwrap();
},
)
.await;
Expand Down
Loading

0 comments on commit bf9ced0

Please sign in to comment.