use super::*;
use serde::{Deserialize, Serialize};
pub type SandboxId = String;
#[derive(Clone)]
pub struct NetworkAttachment {
pub lease: NetworkLease,
pub nic: NicSpec,
pub identity: NetworkIdentity,
pub invariant_identity: bool,
}
impl NetworkAttachment {
pub(crate) fn journaled(&self) -> super::reconcile::JournaledLease<'_> {
super::reconcile::JournaledLease::cold_boot(&self.lease, self.invariant_identity)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SandboxState {
Starting,
Ready,
Running,
Stopping,
Stopped,
Failed,
Pausing,
Paused,
}
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"),
Self::Pausing => write!(f, "pausing"),
Self::Paused => write!(f, "paused"),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IdleAction {
#[default]
Kill,
Pause,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct LifecycleUpdate {
pub ttl_seconds: Option<u32>,
pub idle_timeout_seconds: Option<u32>,
pub on_idle: Option<IdleAction>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SandboxNetworkSpec {
pub mode: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SandboxMountSpec {
pub source: String,
pub target: String,
pub readonly: bool,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(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 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>,
pub idle_timeout_seconds: u32,
pub on_idle: IdleAction,
pub template_warm: Option<TemplateWarmRef>,
pub ready_probe: Option<crate::template_catalog::ReadyProbeSpec>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TemplateWarmRef {
pub snapshot_id: String,
pub vcpus: u32,
pub memory_mib: u64,
}
#[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 SandboxSummary {
pub id: SandboxId,
pub state: SandboxState,
pub labels: HashMap<String, String>,
pub ip_address: String,
pub created_at: DateTime<Utc>,
pub paused_at: Option<DateTime<Utc>>,
pub storage_bytes: u64,
}
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_status: Option<ExitStatus>,
pub error: Option<String>,
pub paused_at: Option<DateTime<Utc>>,
pub storage_bytes: u64,
pub ttl_deadline: Option<DateTime<Utc>>,
pub idle_timeout_seconds: u32,
pub on_idle: IdleAction,
}
pub struct SandboxNetworkInfo {
pub ip_address: String,
pub gateway: String,
}
pub mod action {
pub const CREATED: &str = "created";
pub const READY: &str = "ready";
pub const RUNNING: &str = "running";
pub const IDLE: &str = "idle";
pub const STOPPING: &str = "stopping";
pub const STOPPED: &str = "stopped";
pub const FAILED: &str = "failed";
pub const REMOVED: &str = "removed";
pub const PAUSING: &str = "pausing";
pub const PAUSED: &str = "paused";
pub const RESUMED: &str = "resumed";
}
#[derive(Debug, Clone)]
pub struct SandboxEvent {
pub sandbox_id: SandboxId,
pub action: String,
pub timestamp_ns: i64,
pub attributes: HashMap<String, String>,
pub sequence: u64,
}
impl SandboxEvent {
pub 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(),
sequence: 0,
}
}
pub fn with_attr(mut self, key: &str, value: &str) -> Self {
self.attributes.insert(key.to_owned(), value.to_owned());
self
}
#[must_use]
pub fn is_terminal(&self) -> bool {
matches!(
self.action.as_str(),
action::STOPPED | action::FAILED | action::REMOVED
)
}
}
#[derive(Debug, Clone)]
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,
}