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 Jul 5, 2024
1 parent 12a810c commit 462f97e
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 163 deletions.
92 changes: 91 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ git2 = "0.18.3"
nestify = "0.3.3"
gql_client = { git = "https://github.com/adriencaccia/gql-client-rs" }
serde_yaml = "0.9.34"
sysinfo = { version = "0.30.12", features = ["serde"] }

[dev-dependencies]
temp-env = { version = "0.3.6", features = ["async_closure"] }
Expand Down
77 changes: 77 additions & 0 deletions src/run/check_system.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::process::Command;

use serde::{Deserialize, Serialize};
use sysinfo::System;

use crate::prelude::*;

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,
}

#[cfg(test)]
impl SystemInfo {
pub fn test() -> Self {
SystemInfo {
os: "Ubuntu".to_string(),
os_version: "20.04".to_string(),
arch: "x86_64".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 x86_64
/// - Ubuntu 22.04 on x86_64
/// - Debian 11 on x86_64
/// - Debian 12 on x86_64
pub fn check_system() -> Result<SystemInfo> {
let os = System::name().ok_or(anyhow!("Failed to get OS name"))?;
let os_version = System::os_version().ok_or(anyhow!("Failed to get OS version"))?;
debug!("OS: {}, Version: {}", os, os_version);
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 = System::cpu_arch().ok_or(anyhow!("Failed to get CPU architecture"))?;
debug!("Arch: {}", arch);
if arch != "x86_64" && arch != "arm64" {
bail!("Only x86_64 and arm64 are supported at the moment");
}
let user = get_user()?;
debug!("User: {}", user);
let host = System::host_name().ok_or(anyhow!("Failed to get host name"))?;
debug!("Host: {}", host);

Ok(SystemInfo {
os,
os_version,
arch,
host,
user,
})
}
11 changes: 9 additions & 2 deletions 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,13 +72,18 @@ 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)?;

Ok(UploadMetadata {
version: Some(2),
version: Some(3),
tokenless: config.token.is_none(),
provider_metadata,
profile_md5: archive_hash.into(),
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
81 changes: 0 additions & 81 deletions src/run/runner/check_system.rs

This file was deleted.

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
Loading

0 comments on commit 462f97e

Please sign in to comment.