use super::*;
pub type SandboxId = String;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SandboxState {
Starting,
Ready,
Running,
Stopping,
Stopped,
Failed,
}
impl std::fmt::Display for SandboxState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Starting => write!(f, "starting"),
Self::Ready => write!(f, "ready"),
Self::Running => write!(f, "running"),
Self::Stopping => write!(f, "stopping"),
Self::Stopped => write!(f, "stopped"),
Self::Failed => write!(f, "failed"),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct SandboxNetworkSpec {
pub mode: String,
}
#[derive(Debug, Clone)]
pub struct SandboxMountSpec {
pub source: String,
pub target: String,
pub readonly: bool,
}
#[derive(Debug, Clone, Default)]
pub struct SandboxSpec {
pub id: Option<String>,
pub labels: HashMap<String, String>,
pub kernel: String,
pub rootfs: String,
pub boot_args: String,
pub vcpus: u32,
pub memory_mib: u64,
pub image: String,
pub cmd: Vec<String>,
pub env: HashMap<String, String>,
pub working_dir: String,
pub user: String,
pub mounts: Vec<SandboxMountSpec>,
pub network: SandboxNetworkSpec,
pub ttl_seconds: u32,
pub ssh_public_key: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct RestoreSandboxSpec {
pub id: Option<String>,
pub snapshot_id: String,
pub labels: HashMap<String, String>,
pub network_override: bool,
pub ttl_seconds: u32,
}
pub struct SandboxInstance {
pub id: SandboxId,
pub labels: HashMap<String, String>,
pub spec: SandboxSpec,
pub state: SandboxState,
pub process: Option<fc_sdk::FirecrackerProcess>,
pub vm: Option<Arc<fc_sdk::Vm>>,
pub network: Option<NetworkAllocation>,
pub vm_dir: PathBuf,
pub vsock_uds_path: Option<PathBuf>,
pub created_at: DateTime<Utc>,
pub ready_at: Option<DateTime<Utc>>,
pub last_exited_at: Option<DateTime<Utc>>,
pub last_exit_code: Option<i32>,
pub error: Option<String>,
pub cow_handle: Option<CowHandle>,
pub restore_origin_dir: Option<PathBuf>,
}
impl SandboxInstance {
pub(super) fn new(
id: SandboxId,
spec: SandboxSpec,
network: Option<NetworkAllocation>,
vm_dir: PathBuf,
) -> Self {
Self {
id,
labels: spec.labels.clone(),
spec,
state: SandboxState::Starting,
process: None,
vm: None,
network,
vm_dir,
vsock_uds_path: None,
created_at: Utc::now(),
ready_at: None,
last_exited_at: None,
last_exit_code: None,
error: None,
cow_handle: None,
restore_origin_dir: None,
}
}
pub fn socket_path(&self) -> PathBuf {
self.vm_dir.join("firecracker.sock")
}
}
pub struct SandboxSummary {
pub id: SandboxId,
pub state: SandboxState,
pub labels: HashMap<String, String>,
pub ip_address: String,
pub created_at: DateTime<Utc>,
}
pub struct SandboxInfo {
pub id: SandboxId,
pub state: SandboxState,
pub labels: HashMap<String, String>,
pub vcpus: u32,
pub memory_mib: u64,
pub network: Option<SandboxNetworkInfo>,
pub created_at: DateTime<Utc>,
pub ready_at: Option<DateTime<Utc>>,
pub last_exited_at: Option<DateTime<Utc>>,
pub last_exit_code: Option<i32>,
pub error: Option<String>,
}
pub struct SandboxNetworkInfo {
pub ip_address: String,
pub gateway: String,
pub tap_name: String,
}
#[derive(Debug, Clone)]
pub struct SandboxEvent {
pub sandbox_id: SandboxId,
pub action: String,
pub timestamp_ns: i64,
pub attributes: HashMap<String, String>,
}
impl SandboxEvent {
pub(super) fn new(sandbox_id: &str, action: &str) -> Self {
Self {
sandbox_id: sandbox_id.to_owned(),
action: action.to_owned(),
timestamp_ns: Utc::now().timestamp_nanos_opt().unwrap_or(0),
attributes: HashMap::new(),
}
}
pub(super) fn with_attr(mut self, key: &str, value: &str) -> Self {
self.attributes.insert(key.to_owned(), value.to_owned());
self
}
}
pub struct CheckpointInfo {
pub snapshot_id: String,
pub snapshot_dir: String,
pub created_at: String,
}
pub struct CheckpointSummary {
pub id: String,
pub sandbox_id: String,
pub name: String,
pub labels: HashMap<String, String>,
pub snapshot_dir: String,
pub created_at: String,
}