use std::path::PathBuf;
use state_machines::state_machine;
use crate::sickbay::HealthStatus;
use super::chart::{self, Health, Step};
state_machine! {
name: JailMachine,
dynamic: true, initial: Stopped,
states: [Stopped, Starting, Running, Draining, Stopping, Failed],
events {
start {
transition: { from: Stopped, to: Starting }
}
started {
transition: { from: Starting, to: Running }
}
drain {
transition: { from: Running, to: Draining }
}
stop {
transition: { from: [Running, Draining], to: Stopping }
}
stopped {
transition: { from: Stopping, to: Stopped }
}
fail {
transition: { from: [Starting, Running, Draining, Stopping], to: Failed }
}
recover {
transition: { from: Failed, to: Stopped }
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum State {
Stopped,
Starting,
Running,
Draining,
Stopping,
Failed,
}
#[derive(Debug, Clone)]
pub struct JailConfig {
pub name: String,
pub path: PathBuf,
pub hostname: Option<String>,
pub ips: Vec<std::net::IpAddr>,
}
impl JailConfig {
pub fn new(name: impl Into<String>, path: impl Into<PathBuf>) -> Self {
Self {
name: name.into(),
path: path.into(),
hostname: None,
ips: Vec::new(),
}
}
pub fn hostname(mut self, hostname: impl Into<String>) -> Self {
self.hostname = Some(hostname.into());
self
}
pub fn ip(mut self, addr: std::net::IpAddr) -> Self {
self.ips.push(addr);
self
}
}
#[derive(Debug)]
pub enum JailHandle {
Jid(i32),
Descriptor {
fd: super::ffi::JailDescriptor,
jid: i32,
},
}
impl JailHandle {
pub const fn jid(&self) -> i32 {
match self {
Self::Jid(jid) | Self::Descriptor { jid, .. } => *jid,
}
}
pub fn descriptor_fd(&self) -> Option<i32> {
match self {
Self::Jid(_) => None,
Self::Descriptor { fd, .. } => Some(fd.as_raw_fd()),
}
}
}
pub struct JailInstance {
pub machine: DynamicJailMachine<()>,
pub config: JailConfig,
pub jid: Option<i32>,
pub handle: Option<JailHandle>,
health: Health,
last_step: Option<Step>,
}
impl JailInstance {
pub fn new(config: JailConfig) -> Self {
let machine = JailMachine::new(()).into_dynamic();
Self {
machine,
config,
jid: None,
handle: None,
health: Health::default(),
last_step: None,
}
}
}
impl JailInstance {
pub const fn last_step(&self) -> Option<&Step> {
self.last_step.as_ref()
}
pub const fn health(&self) -> Health {
self.health
}
pub fn set_health(&mut self, status: HealthStatus) -> Result<(), chart::Error> {
let to = Health::from(status);
if !self.is_running() {
return Err(chart::Error::Illegal {
from: self.machine.current_state(),
to: JailMachineState::Running,
});
}
if to == self.health {
return Ok(());
}
self.last_step = Some(Step::health(self.health, to)?);
self.health = to;
Ok(())
}
pub fn state(&self) -> State {
match self.machine.current_state() {
JailMachineState::Stopped => State::Stopped,
JailMachineState::Starting => State::Starting,
JailMachineState::Running => State::Running,
JailMachineState::Draining => State::Draining,
JailMachineState::Stopping => State::Stopping,
JailMachineState::Failed => State::Failed,
}
}
pub fn is_running(&self) -> bool {
self.machine.current_state() == JailMachineState::Running
}
fn send(&mut self, event: JailMachineEvent) -> Result<(), state_machines::DynamicError> {
let from = self.machine.current_state();
self.machine.handle(event)?;
self.last_step = Step::phase(from, self.health, self.machine.current_state()).ok();
self.health = Health::default();
Ok(())
}
pub fn start(&mut self) -> Result<(), state_machines::DynamicError> {
self.send(JailMachineEvent::Start)
}
pub fn started(&mut self) -> Result<(), state_machines::DynamicError> {
self.send(JailMachineEvent::Started)
}
pub fn drain(&mut self) -> Result<(), state_machines::DynamicError> {
self.send(JailMachineEvent::Drain)
}
pub fn stop(&mut self) -> Result<(), state_machines::DynamicError> {
self.send(JailMachineEvent::Stop)
}
pub fn stopped(&mut self) -> Result<(), state_machines::DynamicError> {
self.send(JailMachineEvent::Stopped)
}
pub fn fail(&mut self) -> Result<(), state_machines::DynamicError> {
self.send(JailMachineEvent::Fail)
}
pub fn recover(&mut self) -> Result<(), state_machines::DynamicError> {
self.send(JailMachineEvent::Recover)
}
}
#[cfg(test)]
mod tests;