isothrum-runner 2.1.1

The Isothrum runner: executes the run manifest Isoloom writes next to a target's files (lifecycle, state and locks, drivers, checks, events).
Documentation
//! The engine: one per embedder. It holds the operations in flight, the per-run locks and the
//! ports being pinned, never in process-wide statics, so two runners in one process never share
//! them.

use std::collections::BTreeMap;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, PoisonError};

use isoloom_manifest::{Arch, HostOs, Protocol};

use crate::Result;
use crate::clock::{Clock, SystemClock};
use crate::encoding::wire_name;
use crate::error::Error;
use crate::events::EventLog;
use crate::host_env::HostEnv;
use crate::manifest::load_manifest;
use crate::name::RunName;
use crate::net::{HostPorts, LoopbackPorts};
use crate::process::{ProcessRunner, SystemProcesses};
use crate::run::Running;
use crate::state::Op;
use crate::store::{RunLocks, RunRecord, Store};

/// How a runner is built: its home, and the ports it reaches the world through. Start from
/// [`RunnerConfig::new`] and set a field to change it (`config.processes = Arc::new(fake)`).
#[derive(Clone)]
#[non_exhaustive]
pub struct RunnerConfig {
    /// The runner's home (`runs/` under it). An app passes its data folder; the
    /// `isothrum` command line uses `~/.isothrum`.
    pub home: PathBuf,
    /// Starts the tools.
    pub processes: Arc<dyn ProcessRunner>,
    /// The host's ports.
    pub ports: Arc<dyn HostPorts>,
    /// The time.
    pub(crate) clock: Arc<dyn Clock>,
    /// The host's operating system (it picks per-host argv and filters steps).
    pub host: HostOs,
    /// The `PATH` the runner hands on where it isn't inherited: to an elevated Hyper-V session,
    /// and to Vagrant on an ESXi host from macOS, whose relays look for the real `ovftool` in it
    /// and put their wrapper first (default: the runner's own). The tools the runner starts as
    /// its children inherit its environment as it is.
    pub path: Option<OsString>,
    /// The user's own Terraform plugin cache (`TF_PLUGIN_CACHE_DIR`), which the runs use as it
    /// is; without one, they share one under the home.
    pub(crate) tf_plugin_cache: Option<PathBuf>,
    /// The variables of the runner's environment it hands on where they aren't inherited (the
    /// proxies, the CA bundles: [`HostEnv`]).
    pub(crate) host_env: HostEnv,
    /// The host's CPU architecture: which build of a Vagrant box counts as there already.
    pub(crate) arch: Arch,
}

impl RunnerConfig {
    /// A runner at `home` on this host: real processes (told the Docker CLI's config, see
    /// [`SystemProcesses`]), the loopback, the system's clock, and what it needs of this process's
    /// environment, read now (`PATH`, `TF_PLUGIN_CACHE_DIR`, the proxies, the CA bundles,
    /// Terraform's CLI configuration, Vagrant's variables, `USERPROFILE`).
    pub fn new(home: impl Into<PathBuf>) -> RunnerConfig {
        RunnerConfig {
            home: home.into(),
            processes: Arc::new(SystemProcesses::default()),
            ports: Arc::new(LoopbackPorts),
            clock: Arc::new(SystemClock),
            host: this_host(),
            path: std::env::var_os("PATH"),
            tf_plugin_cache: std::env::var_os("TF_PLUGIN_CACHE_DIR").filter(|d| !d.is_empty()).map(PathBuf::from),
            host_env: HostEnv::capture(),
            arch: this_arch(),
        }
    }
}

impl std::fmt::Debug for RunnerConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RunnerConfig")
            .field("home", &self.home)
            .field("host", &self.host)
            .finish_non_exhaustive()
    }
}

/// This build's host operating system.
#[must_use]
pub(crate) fn this_host() -> HostOs {
    if cfg!(windows) {
        HostOs::Windows
    } else if cfg!(target_os = "macos") {
        HostOs::Macos
    } else {
        HostOs::Linux
    }
}

/// This build's CPU architecture, as Vagrant names a box's build (an x86-64 build reads as
/// `amd64`, an ARM one as `arm64`).
#[must_use]
pub(crate) fn this_arch() -> Arch {
    if cfg!(target_arch = "aarch64") { Arch::Arm64 } else { Arch::Amd64 }
}

/// The runner.
pub struct Runner {
    pub(crate) config: RunnerConfig,
    pub(crate) store: Store,
    pub(crate) locks: RunLocks,
    flight: Mutex<BTreeMap<RunName, Op>>,
    reserved: Mutex<BTreeMap<(u16, Protocol), RunName>>,
}

impl Runner {
    /// A runner with `config`; its home (with its missing parents) and the home's `runs/` folder
    /// are created, owner-only (on Windows, a home outside the user's profile gets an owner-only
    /// DACL).
    ///
    /// # Errors
    ///
    /// Fails with `io` when the home can't be created or made owner-only (on Windows, its owner
    /// can't be read or its DACL set), or its `runs/` folder can't be created.
    pub fn new(config: RunnerConfig) -> Result<Runner> {
        let store = Store::open(&config.home, config.host_env.get("USERPROFILE").map(Path::new))?;
        Ok(Runner {
            config,
            store,
            locks: RunLocks::default(),
            flight: Mutex::default(),
            reserved: Mutex::default(),
        })
    }

    /// Reads and checks the manifest at `manifest`, as run `name` (default: the manifest's
    /// `environment.name`).
    ///
    /// # Errors
    ///
    /// Fails as [`load_manifest`](crate::load_manifest) does; with `run_name` when the run's name
    /// can't name a run; with `unsupported` when this runner can't run the manifest here (its target
    /// doesn't run on this host, or it has a delivery its driver doesn't take).
    pub fn open(&self, manifest: &Path, name: Option<RunName>) -> Result<Running<'_>> {
        let loaded = load_manifest(manifest)?;
        let name = match name {
            Some(name) => name,
            None => RunName::new(loaded.manifest.environment.name.as_str())?,
        };
        Running::new(self, loaded, name)
    }

    /// Every recorded run, with the state its last operation left it in. Reads files only.
    ///
    /// # Errors
    ///
    /// Fails with `io` when a record can't be read.
    pub fn runs(&self) -> Result<Vec<RunRecord>> {
        self.store.records()
    }

    /// Run `name`'s logged events, oldest first, with how many of its lines it couldn't read as
    /// one (written by another version of the runner, or cut short).
    ///
    /// # Errors
    ///
    /// Fails with `io` when the run's event log can't be read.
    pub fn events(&self, name: &RunName) -> Result<EventLog> {
        self.store.events(name)
    }

    /// The public half of the runner's SSH key, one line: what an embedder authorizes where a
    /// run's SSH goes (a Proxmox node it jumps through). The key is made when the runner has none.
    ///
    /// # Errors
    ///
    /// Fails with `tool_missing` when `ssh-keygen` isn't installed, `tool_failed` or `timeout`
    /// when it can't make the key, and `io` when the runner's `ssh/` folder can't be created or
    /// its public key can't be read.
    pub async fn ssh_public_key(&self) -> Result<String> {
        crate::ssh::ensure_key(self.config.processes.as_ref(), &self.config.home).await
    }

    /// The operations in flight in this runner.
    pub(crate) fn in_flight(&self) -> Vec<(RunName, Op)> {
        self.flight
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .iter()
            .map(|(n, o)| (n.clone(), *o))
            .collect()
    }

    /// Marks `op` on `name` in flight until the guard drops.
    pub(crate) fn enter(&self, name: &RunName, op: Op) -> Flight<'_> {
        self.flight.lock().unwrap_or_else(PoisonError::into_inner).insert(name.clone(), op);
        Flight {
            runner: self,
            name: name.clone(),
        }
    }

    /// The operation in flight on `name`, if any.
    pub(crate) fn op_of(&self, name: &RunName) -> Option<Op> {
        self.flight.lock().unwrap_or_else(PoisonError::into_inner).get(name).copied()
    }

    /// Reserves `port` for `name` while it starts; false when another run holds it.
    pub(crate) fn reserve(&self, name: &RunName, port: u16, protocol: Protocol) -> bool {
        let mut reserved = self.reserved.lock().unwrap_or_else(PoisonError::into_inner);
        let holder = reserved.entry((port, protocol)).or_insert_with(|| name.clone());
        holder == name
    }

    /// A free loopback port over `protocol` that no run of this runner holds, reserved for `name`
    /// until this operation ends. The returned socket keeps it bound until dropped, so a port is
    /// never handed out twice; a port another run holds is refused and held meanwhile so it isn't
    /// offered again. None when none is left.
    ///
    /// # Errors
    ///
    /// The loopback couldn't be bound.
    pub(crate) fn pick_reserved(&self, name: &RunName, protocol: Protocol) -> std::io::Result<Option<crate::net::HeldPort>> {
        self.pick_where(protocol, |port| self.reserve(name, port, protocol))
    }

    /// A free loopback port over `protocol` that no run of this runner but `name` holds, not
    /// reserved: for a query, which holds no reservation past its end (a console's tunnel, whose
    /// `ssh` binds it at once). None when none is left.
    ///
    /// # Errors
    ///
    /// The loopback couldn't be bound.
    pub(crate) fn pick_unreserved(&self, name: &RunName, protocol: Protocol) -> std::io::Result<Option<crate::net::HeldPort>> {
        self.pick_where(protocol, |port| !self.reserved_elsewhere(name, port, protocol))
    }

    /// A free loopback port over `protocol` that `take` accepts; one it refuses is held meanwhile
    /// so it isn't offered again. None when none is left.
    fn pick_where(&self, protocol: Protocol, take: impl Fn(u16) -> bool) -> std::io::Result<Option<crate::net::HeldPort>> {
        let mut refused = Vec::new();
        for _ in 0..64 {
            let Some(port) = self.config.ports.pick_free(protocol)? else {
                return Ok(None);
            };
            if take(port.port) {
                return Ok(Some(port));
            }
            refused.push(port);
        }
        Ok(None)
    }

    /// Whether another run than `name` holds `port`.
    pub(crate) fn reserved_elsewhere(&self, name: &RunName, port: u16, protocol: Protocol) -> bool {
        self.reserved
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .get(&(port, protocol))
            .is_some_and(|h| h != name)
    }

    /// Each `ports` entry (a host port and its protocol) must be free before run `name` starts:
    /// not reserved by another run of this runner starting meanwhile, and not already bound on
    /// this computer.
    ///
    /// # Errors
    ///
    /// Fails with `port_in_use` at the first port that is reserved elsewhere or already bound.
    pub(crate) fn ensure_ports_free(&self, name: &RunName, ports: impl IntoIterator<Item = (u16, Protocol)>) -> Result<()> {
        for (port, protocol) in ports {
            if self.reserved_elsewhere(name, port, protocol) || self.config.ports.in_use(port, protocol) {
                return Err(Error::PortInUse {
                    port,
                    protocol: wire_name(&protocol),
                });
            }
        }
        Ok(())
    }

    /// Frees every port `name` reserved.
    pub(crate) fn release(&self, name: &RunName) {
        self.reserved.lock().unwrap_or_else(PoisonError::into_inner).retain(|_, holder| holder != name);
    }
}

impl std::fmt::Debug for Runner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Runner")
            .field("config", &self.config)
            .field("in_flight", &self.in_flight())
            .finish_non_exhaustive()
    }
}

/// An operation in flight; cleared when dropped, whatever the outcome (an error, a cancellation).
pub(crate) struct Flight<'r> {
    runner: &'r Runner,
    name: RunName,
}

impl Drop for Flight<'_> {
    fn drop(&mut self) {
        self.runner.flight.lock().unwrap_or_else(PoisonError::into_inner).remove(&self.name);
        self.runner.release(&self.name);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn runner(home: &Path) -> Runner {
        Runner::new(RunnerConfig::new(home)).unwrap()
    }

    #[test]
    fn two_runners_share_nothing() {
        // Regression: the in-flight map and the port reservations kept in statics let two
        // embedders (or two tests) in one process see each other's operations and ports.
        let (a, b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
        let (a, b) = (runner(a.path()), runner(b.path()));
        let name = RunName::new("demo").unwrap();
        let flight = a.enter(&name, Op::Up);
        assert!(a.reserve(&name, 8080, Protocol::Tcp));
        assert_eq!(a.in_flight(), [(name.clone(), Op::Up)]);
        assert_eq!(b.in_flight(), []);
        assert!(b.reserve(&RunName::new("other").unwrap(), 8080, Protocol::Tcp), "b's ports aren't a's");
        drop(flight);
        assert_eq!(a.in_flight(), []);
    }

    #[test]
    fn a_port_reserved_by_one_run_is_refused_to_another_until_released() {
        let home = tempfile::tempdir().unwrap();
        let r = runner(home.path());
        let (one, two) = (RunName::new("one").unwrap(), RunName::new("two").unwrap());
        assert!(r.reserve(&one, 41000, Protocol::Tcp));
        assert!(r.reserve(&one, 41000, Protocol::Tcp), "its own reservation again");
        assert!(!r.reserve(&two, 41000, Protocol::Tcp));
        assert!(r.reserved_elsewhere(&two, 41000, Protocol::Tcp) && !r.reserved_elsewhere(&one, 41000, Protocol::Tcp));
        assert!(r.reserve(&two, 41000, Protocol::Udp), "another protocol");
        r.release(&one);
        assert!(r.reserve(&two, 41000, Protocol::Tcp));
    }
}