use tokio::sync::mpsc;
use crate::error::RuntimeError;
use crate::instance::Instance;
#[derive(Debug, Clone)]
pub enum RuntimeEvent {
Started(Instance),
Stopped {
id: String,
name: String,
},
Updated(Instance),
BackendDisconnected { backend: String, reason: String },
BackendReconnected { backend: String },
}
#[async_trait::async_trait]
pub trait RuntimeBackend: Send + Sync {
fn name(&self) -> &'static str;
async fn connect(&mut self) -> Result<(), RuntimeError>;
async fn list_instances(&self) -> Result<Vec<Instance>, RuntimeError>;
async fn watch(
&self,
tx: mpsc::Sender<RuntimeEvent>,
cancel: tokio_util::sync::CancellationToken,
) -> Result<(), RuntimeError>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeBackendKind {
Auto,
Docker,
Podman,
Systemd,
Incus,
Kubernetes,
}
impl RuntimeBackendKind {
pub fn from_str_loose(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"auto" => Some(Self::Auto),
"docker" => Some(Self::Docker),
"podman" => Some(Self::Podman),
"systemd" => Some(Self::Systemd),
"incus" | "lxc" | "lxd" => Some(Self::Incus),
"kubernetes" | "k8s" => Some(Self::Kubernetes),
_ => None,
}
}
}
impl std::fmt::Display for RuntimeBackendKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Auto => write!(f, "auto"),
Self::Docker => write!(f, "docker"),
Self::Podman => write!(f, "podman"),
Self::Systemd => write!(f, "systemd"),
Self::Incus => write!(f, "incus"),
Self::Kubernetes => write!(f, "kubernetes"),
}
}
}