Skip to content

Commit

Permalink
feat(blocking): initial implementation of detection
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhil-prabhu committed Jan 2, 2025
1 parent 44035f2 commit cf6dea2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
27 changes: 23 additions & 4 deletions src/blocking/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pub(crate) mod providers;

use std::sync::mpsc::Sender;
use std::sync::{Arc, LazyLock, Mutex};
use std::sync::{mpsc, Arc, LazyLock, Mutex};
use std::time::Duration;

use anyhow::Result;

use crate::blocking::providers::*;
use crate::ProviderId;
use crate::{ProviderId, DEFAULT_DETECTION_TIMEOUT};

/// Represents a cloud service provider.
#[allow(dead_code)]
Expand Down Expand Up @@ -49,7 +49,26 @@ pub fn supported_providers() -> Result<Vec<String>> {
Ok(providers)
}

#[allow(unused_variables)]
pub fn detect(timeout: Option<u64>) -> Result<ProviderId> {
todo!()
let timeout = Duration::from_secs(timeout.unwrap_or(DEFAULT_DETECTION_TIMEOUT));
let (tx, rx) = mpsc::channel::<ProviderId>();
let guard = PROVIDERS
.lock()
.map_err(|_| anyhow::anyhow!("Error locking providers"))?;
let provider_entries: Vec<P> = guard.iter().cloned().collect();
let providers_count = provider_entries.len();
let mut handles = Vec::with_capacity(providers_count);

for provider in provider_entries {
let tx = tx.clone();
let handle = std::thread::spawn(move || provider.identify(tx, timeout));

handles.push(handle);
}

if let Ok(provider_id) = rx.recv_timeout(timeout) {
return Ok(provider_id);
}

Ok(ProviderId::Unknown)
}
4 changes: 0 additions & 4 deletions src/blocking/providers/alibaba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ use tracing::{debug, error, info, instrument};
use crate::blocking::Provider;
use crate::ProviderId;

#[allow(unused)]
const METADATA_URI: &str = "http://100.100.100.200";
#[allow(unused)]
const METADATA_PATH: &str = "/latest/meta-data/latest/meta-data/instance/virtualization-solution";
#[allow(unused)]
const VENDOR_FILE: &str = "/sys/class/dmi/id/product_name";
#[allow(unused)]
const IDENTIFIER: ProviderId = ProviderId::Alibaba;

pub(crate) struct Alibaba;
Expand Down
6 changes: 0 additions & 6 deletions src/blocking/providers/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ use tracing::{debug, error, info, instrument};
use crate::blocking::Provider;
use crate::ProviderId;

#[allow(unused)]
const METADATA_URI: &str = "http://169.254.169.254";
#[allow(unused)]
const METADATA_PATH: &str = "/latest/dynamic/instance-identity/document";
#[allow(unused)]
const METADATA_TOKEN_PATH: &str = "/latest/api/token";
#[allow(unused)]
const PRODUCT_VERSION_FILE: &str = "/sys/class/dmi/id/product_version";
#[allow(unused)]
const BIOS_VENDOR_FILE: &str = "/sys/class/dmi/id/bios_vendor";
#[allow(unused)]
const IDENTIFIER: ProviderId = ProviderId::AWS;

#[derive(Serialize, Deserialize)]
Expand Down

0 comments on commit cf6dea2

Please sign in to comment.