cloud_detect/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
//! # Cloud Detect
//!
//! A library to detect the cloud service provider of a host.
//!
//! ## Usage
//!
//! Add the following to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! # ...
//! cloud_detect = "2"
//! tokio = { version = "1", features = ["full"] }
//! tracing-subscriber = { version = "0.3", features = ["env-filter"] } # Optional; for logging
//! ```
//!
//! ## Examples
//!
//! Detect the cloud provider and print the result (with default timeout).
//!
//! ```rust
//! use cloud_detect::detect;
//!
//! #[tokio::main]
//! async fn main() {
//! tracing_subscriber::fmt::init(); // Optional; for logging
//!
//! let provider = detect(None).await;
//! println!("Detected provider: {}", provider);
//! }
//! ```
//!
//! Detect the cloud provider and print the result (with custom timeout).
//!
//! ```rust
//! use cloud_detect::detect;
//!
//! #[tokio::main]
//! async fn main() {
//! tracing_subscriber::fmt::init(); // Optional; for logging
//!
//! let provider = detect(Some(10)).await;
//! println!("Detected provider: {}", provider);
//! }
//! ```
use std::fmt::Debug;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, LazyLock};
use std::time::Duration;
use async_trait::async_trait;
use strum::Display;
use tokio::sync::mpsc::Sender;
use tokio::sync::{mpsc, Mutex, Notify};
use tracing::{debug, instrument};
use crate::providers::*;
#[cfg(feature = "blocking")]
pub mod blocking;
pub(crate) mod providers;
/// Maximum time allowed for detection.
pub const DEFAULT_DETECTION_TIMEOUT: u64 = 5; // seconds
/// Represents an identifier for a cloud service provider.
#[non_exhaustive]
#[derive(Debug, Default, Display, Eq, PartialEq)]
pub enum ProviderId {
/// Unknown cloud service provider.
#[default]
#[strum(serialize = "unknown")]
Unknown,
/// Alibaba Cloud.
#[strum(serialize = "alibaba")]
Alibaba,
/// Amazon Web Services (AWS).
#[strum(serialize = "aws")]
AWS,
/// Microsoft Azure.
#[strum(serialize = "azure")]
Azure,
/// DigitalOcean.
#[strum(serialize = "digitalocean")]
DigitalOcean,
/// Google Cloud Platform (GCP).
#[strum(serialize = "gcp")]
GCP,
/// Oracle Cloud Infrastructure (OCI).
#[strum(serialize = "oci")]
OCI,
/// OpenStack.
#[strum(serialize = "openstack")]
OpenStack,
/// Vultr.
#[strum(serialize = "vultr")]
Vultr,
}
/// Represents a cloud service provider.
#[async_trait]
pub(crate) trait Provider: Send + Sync {
fn identifier(&self) -> ProviderId;
async fn identify(&self, tx: Sender<ProviderId>, timeout: Duration);
}
type P = Arc<dyn Provider>;
static PROVIDERS: LazyLock<Mutex<Vec<P>>> = LazyLock::new(|| {
Mutex::new(vec![
Arc::new(alibaba::Alibaba) as P,
Arc::new(aws::Aws) as P,
Arc::new(azure::Azure) as P,
Arc::new(digitalocean::DigitalOcean) as P,
Arc::new(gcp::Gcp) as P,
Arc::new(oci::Oci) as P,
Arc::new(openstack::OpenStack) as P,
Arc::new(vultr::Vultr) as P,
])
});
/// Returns a list of currently supported providers.
///
/// # Examples
///
/// Print the list of supported providers.
///
/// ```
/// use cloud_detect::supported_providers;
///
/// #[tokio::main]
/// async fn main() {
/// let providers = supported_providers().await;
/// println!("Supported providers: {:?}", providers);
/// }
/// ```
pub async fn supported_providers() -> Vec<String> {
let guard = PROVIDERS.lock().await;
let providers: Vec<String> = guard.iter().map(|p| p.identifier().to_string()).collect();
drop(guard);
providers
}
/// Detects the host's cloud provider.
///
/// Returns [ProviderId::Unknown] if the detection failed or timed out. If the detection was successful, it returns
/// a value from [ProviderId](enum.ProviderId.html).
///
/// # Arguments
///
/// * `timeout` - Maximum time (seconds) allowed for detection. Defaults to [DEFAULT_DETECTION_TIMEOUT](constant.DEFAULT_DETECTION_TIMEOUT.html) if `None`.
///
/// # Examples
///
/// Detect the cloud provider and print the result (with default timeout).
///
/// ```
/// use cloud_detect::detect;
///
/// #[tokio::main]
/// async fn main() {
/// let provider = detect(None).await;
/// println!("Detected provider: {}", provider);
/// }
/// ```
///
/// Detect the cloud provider and print the result (with custom timeout).
///
/// ```
/// use cloud_detect::detect;
///
/// #[tokio::main]
/// async fn main() {
/// let provider = detect(Some(10)).await;
/// println!("Detected provider: {}", provider);
/// }
/// ```
#[instrument]
pub async fn detect(timeout: Option<u64>) -> ProviderId {
let timeout = Duration::from_secs(timeout.unwrap_or(DEFAULT_DETECTION_TIMEOUT));
let (tx, mut rx) = mpsc::channel::<ProviderId>(1);
let guard = PROVIDERS.lock().await;
let provider_entries: Vec<P> = guard.iter().cloned().collect();
let providers_count = provider_entries.len();
let mut handles = Vec::with_capacity(providers_count);
// Create a counter that will be decremented as tasks complete
let counter = Arc::new(AtomicUsize::new(providers_count));
let complete = Arc::new(Notify::new());
for provider in provider_entries {
let tx = tx.clone();
let counter = counter.clone();
let complete = complete.clone();
handles.push(tokio::spawn(async move {
debug!("Spawning task for provider: {}", provider.identifier());
provider.identify(tx, timeout).await;
// Decrement counter and notify if we're the last task
if counter.fetch_sub(1, Ordering::SeqCst) == 1 {
complete.notify_one();
}
}));
}
tokio::select! {
biased;
// Priority 1: If we receive an identifier, return it immediately
res = rx.recv() => {
debug!("Received result from channel: {:?}", res);
res.unwrap_or_default()
}
// Priority 2: If all tasks complete without finding an identifier
_ = complete.notified() => {
debug!("All providers have finished identifying");
Default::default()
}
// Priority 3: If we time out
_ = tokio::time::sleep(timeout) => {
debug!("Detection timed out");
Default::default()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_supported_providers() {
let providers = supported_providers().await;
assert_eq!(providers.len(), 8);
assert!(providers.contains(&alibaba::IDENTIFIER.to_string()));
assert!(providers.contains(&aws::IDENTIFIER.to_string()));
assert!(providers.contains(&azure::IDENTIFIER.to_string()));
assert!(providers.contains(&digitalocean::IDENTIFIER.to_string()));
assert!(providers.contains(&gcp::IDENTIFIER.to_string()));
assert!(providers.contains(&oci::IDENTIFIER.to_string()));
assert!(providers.contains(&openstack::IDENTIFIER.to_string()));
assert!(providers.contains(&vultr::IDENTIFIER.to_string()));
}
}