use std::path::PathBuf;
use std::sync::Arc;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::config::VmSpec;
use crate::network::NetworkAllocation;
pub type VmId = String;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum VmState {
Created,
Running,
Paused,
Stopped,
Failed,
}
impl std::fmt::Display for VmState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Created => write!(f, "created"),
Self::Running => write!(f, "running"),
Self::Paused => write!(f, "paused"),
Self::Stopped => write!(f, "stopped"),
Self::Failed => write!(f, "failed"),
}
}
}
pub struct VmInstance {
pub id: VmId,
pub name: String,
pub spec: VmSpec,
pub state: VmState,
pub process: Option<fc_sdk::FirecrackerProcess>,
pub vm: Option<Arc<fc_sdk::Vm>>,
pub network: Option<NetworkAllocation>,
pub socket_path: PathBuf,
pub created_at: DateTime<Utc>,
pub started_at: Option<DateTime<Utc>>,
}
impl VmInstance {
pub fn new(id: VmId, name: String, spec: VmSpec, socket_path: PathBuf) -> Self {
Self {
id,
name,
spec,
state: VmState::Created,
process: None,
vm: None,
network: None,
socket_path,
created_at: Utc::now(),
started_at: None,
}
}
}
#[derive(Debug, Clone)]
pub struct VmSummary {
pub id: VmId,
pub name: String,
pub state: VmState,
pub vcpus: u64,
pub memory_mib: u64,
pub ip_address: Option<String>,
pub created_at: DateTime<Utc>,
}
#[derive(Debug, Clone)]
pub struct VmInfo {
pub id: VmId,
pub name: String,
pub state: VmState,
pub spec: VmSpec,
pub network: Option<NetworkAllocation>,
pub socket_path: PathBuf,
pub created_at: DateTime<Utc>,
pub started_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone)]
pub struct VmMetrics {
pub vm_id: VmId,
pub balloon_target_mib: Option<i64>,
pub balloon_actual_mib: Option<i64>,
}