use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use arcbox_vm_driver::net::{NetworkIdentity, NetworkLease};
use arcbox_vm_driver::{PreparedVm, VmHandle};
use chrono::{DateTime, Utc};
use uuid::Uuid;
use crate::agent::ExitStatus;
use crate::sandbox::{ComputerId, ComputerSpec, ComputerState};
use crate::snapshot_cow::CowHandle;
pub type Runtime = Arc<Mutex<ComputerRuntime>>;
pub struct ComputerRuntime {
pub id: ComputerId,
pub(crate) record_generation: Option<Uuid>,
pub labels: HashMap<String, String>,
pub spec: ComputerSpec,
pub state: ComputerState,
pub prepared: Option<Arc<dyn PreparedVm>>,
pub handle: Option<Arc<dyn VmHandle>>,
pub network: Option<NetworkLease>,
pub(crate) net_identity: Option<NetworkIdentity>,
pub vm_dir: PathBuf,
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 cow_handle: Option<CowHandle>,
pub(crate) pool_slot_id: Option<String>,
pub(crate) net_invariant: bool,
pub paused_at: Option<DateTime<Utc>>,
pub pause_snapshot_id: Option<String>,
pub ttl_deadline: Option<DateTime<Utc>>,
}
impl ComputerRuntime {
pub(crate) fn new(
id: ComputerId,
spec: ComputerSpec,
network: Option<NetworkLease>,
vm_dir: PathBuf,
) -> Self {
Self::new_inner(id, spec, network, vm_dir, None)
}
pub(crate) fn new_with_generation(
id: ComputerId,
spec: ComputerSpec,
network: Option<NetworkLease>,
vm_dir: PathBuf,
generation: Uuid,
) -> Self {
Self::new_inner(id, spec, network, vm_dir, Some(generation))
}
fn new_inner(
id: ComputerId,
spec: ComputerSpec,
network: Option<NetworkLease>,
vm_dir: PathBuf,
record_generation: Option<Uuid>,
) -> Self {
Self {
id,
record_generation,
labels: spec.labels.clone(),
spec,
state: ComputerState::Starting,
prepared: None,
handle: None,
net_identity: None,
network,
vm_dir,
created_at: Utc::now(),
ready_at: None,
last_exited_at: None,
last_exit_status: None,
error: None,
cow_handle: None,
pool_slot_id: None,
net_invariant: false,
paused_at: None,
pause_snapshot_id: None,
ttl_deadline: None,
}
}
}