isothrum-runner 2.1.3

The Isothrum runner: executes the run manifest Isoloom writes next to a target's files (lifecycle, state and locks, drivers, checks, events).
Documentation
//! Real processes, through `process-wrap` (no `unsafe` here): each tool in its own process group
//! (unix) or job object (Windows), so a timeout or a cancellation ends the whole tree it started
//! (`vagrant` is a wrapper around `ruby`, `cmd /C az` around `python`), headless on Windows. A
//! tool cut short is asked to stop first, on Unix (see [`super::stopping`]).

use std::path::Path;
use std::process::Stdio;
use std::sync::Arc;

use process_wrap::tokio::{ChildWrapper, CommandWrap};
use tokio::process::Command;

use super::stopping::{GRACE, Stopping};
use super::{Exit, OnLine, ProcessError, ProcessRunner, ProcessSpec, ledger, lines, shim};
use crate::BoxFuture;

/// Starts real processes. [`SystemProcesses::default`] reads the Docker CLI's config
/// (`$DOCKER_CONFIG/config.json`, else `~/.docker/config.json`) to learn whether the user picked
/// an engine context of their own.
///
/// A tool cut short (its operation's future dropped: a cancel, a step's timeout) is asked to stop
/// on Unix (SIGINT to its process group, so Terraform saves its state) and ended once it has
/// exited, or 30 seconds later; on Windows it is ended at once. Its clones share the tools
/// stopping: a process about to exit waits for them with [`SystemProcesses::stopped`].
#[derive(Debug, Clone)]
pub struct SystemProcesses {
    /// Drop `DOCKER_HOST` and `DOCKER_CONTEXT` for `docker` (the user picked a context).
    strip_engine_overrides: bool,
    /// The tools cut short that are still stopping.
    stopping: Arc<Stopping>,
}

impl Default for SystemProcesses {
    fn default() -> SystemProcesses {
        SystemProcesses::for_host(|v| std::env::var_os(v), std::env::home_dir().as_deref())
    }
}

/// The Docker CLI's config at `path`: none when there is none, and when it can't be read, which is
/// traced (whether the user picked a context is then unknown, so the inherited engine variables
/// stay).
fn read_cli_config(path: &Path) -> Option<String> {
    crate::store::if_there(std::fs::read_to_string(path)).unwrap_or_else(|e| {
        tracing::warn!(
            path = %path.display(),
            error = %e,
            "the Docker CLI's config couldn't be read: DOCKER_HOST and DOCKER_CONTEXT are kept for docker"
        );
        None
    })
}

impl SystemProcesses {
    /// Real processes, the Docker CLI's config read from `$DOCKER_CONFIG/config.json`, else
    /// `<home>/.docker/config.json` (`var` reads the environment, `home` is the user's): it says
    /// whether the user picked an engine context of their own. A `DOCKER_CONFIG` that isn't UTF-8
    /// is a path like any other.
    fn for_host(var: impl Fn(&str) -> Option<std::ffi::OsString>, home: Option<&Path>) -> SystemProcesses {
        let dir = var("DOCKER_CONFIG").map(std::path::PathBuf::from).or_else(|| home.map(|h| h.join(".docker")));
        let config = dir.and_then(|d| read_cli_config(&d.join("config.json")));
        SystemProcesses {
            strip_engine_overrides: shim::strips_engine_overrides(config.as_deref()),
            stopping: Arc::new(Stopping::new(GRACE)),
        }
    }

    /// Whether a tool cut short is still stopping.
    #[must_use]
    pub fn is_stopping(&self) -> bool {
        self.stopping.any()
    }

    /// Once every tool cut short has ended: at the latest 30 seconds after the last was cut short.
    /// At once when none is stopping.
    pub async fn stopped(&self) {
        self.stopping.stopped().await;
    }

    /// Ends every tool cut short that is still stopping, at once (a second Ctrl-C).
    pub fn end_stopping(&self) {
        self.stopping.end_now();
    }

    async fn run_to_end(&self, spec: &ProcessSpec, on_line: OnLine<'_>) -> Result<Exit, ProcessError> {
        if let Some(arg) = shim::refused_arg(&spec.program, &spec.args) {
            return Err(ProcessError::Refused {
                program: spec.program.clone(),
                why: format!("its argument `{}` holds a character cmd.exe would interpret", arg.escape_debug()),
            });
        }
        let io = |source| ProcessError::Io {
            program: spec.program.clone(),
            source,
        };
        // The program and its arguments only: values reach a tool through its environment,
        // which is never logged.
        tracing::debug!(program = %spec.program, args = ?spec.args, "starting a process");
        let mut cmd = self.command(spec).map_err(io)?;
        let child = cmd.spawn().map_err(|e| spawned(&spec.program, e))?;
        let recorded = spec.tools.as_deref().zip(child.id()).and_then(|(dir, pid)| ledger::Recorded::write(dir, pid));
        let mut tree = Tree(Some(child), Arc::clone(&self.stopping), recorded);
        let status = match tree.0.as_deref_mut() {
            Some(started) => lines::forward_lines(started, on_line).await.map_err(io)?,
            None => return Err(io(std::io::Error::other("the process went away"))),
        };
        // It exited on its own: what it left running is its business.
        tree.0 = None;
        tracing::debug!(program = %spec.program, code = ?status.code(), "process exited");
        if shim::missing_behind_cmd(&spec.program, status.code()) {
            return Err(ProcessError::Missing { program: spec.program.clone() });
        }
        Ok(Exit { code: status.code() })
    }

    /// The command for `spec`: its own group or job, its output piped, headless.
    fn command(&self, spec: &ProcessSpec) -> std::io::Result<CommandWrap> {
        let stdin = match &spec.stdin {
            Some(path) => Stdio::from(std::fs::File::open(path)?),
            None => Stdio::null(),
        };
        let strip = self.strip_engine_overrides && shim::uses_engine(&spec.program);
        let inherited = std::env::vars_os().filter_map(|(k, _)| k.into_string().ok());
        let withheld = shim::withheld(&spec.program, inherited);
        let (program, args) = wrapped(spec);
        let mut cmd = CommandWrap::with_new(program, |c: &mut Command| {
            c.args(args).stdin(stdin).stdout(Stdio::piped()).stderr(Stdio::piped());
            // Before the spec's own: what the run delivers is set again after.
            for var in &withheld {
                c.env_remove(var);
            }
            c.envs(spec.env.iter().map(|(k, v)| (k.as_str(), v.as_str())));
            if let Some(dir) = &spec.cwd {
                c.current_dir(dir);
            }
            if strip {
                for var in shim::ENGINE_OVERRIDES {
                    c.env_remove(var);
                }
            }
        });
        group(&mut cmd);
        Ok(cmd)
    }
}

impl ProcessRunner for SystemProcesses {
    fn run<'a>(&'a self, spec: &'a ProcessSpec, on_line: OnLine<'a>) -> BoxFuture<'a, Result<Exit, ProcessError>> {
        Box::pin(self.run_to_end(spec, on_line))
    }
}

/// A spawned tool's tree, stopped when dropped before the tool exited (a timeout, a
/// cancellation), with its entry in the run's tools when it was recorded: removed once the tool
/// ended.
struct Tree(Option<Box<dyn ChildWrapper>>, Arc<Stopping>, Option<ledger::Recorded>);

impl Drop for Tree {
    fn drop(&mut self) {
        if let Some(child) = self.0.take() {
            self.1.stop(child, self.2.take());
        }
    }
}

/// The program and arguments to start: a Windows `.cmd` tool through `cmd /C`.
fn wrapped(spec: &ProcessSpec) -> (&str, Vec<&str>) {
    let args = spec.args.iter().map(String::as_str);
    if cfg!(windows) && shim::SHIMMED.contains(&spec.program.as_str()) {
        return ("cmd", ["/C", spec.program.as_str()].into_iter().chain(args).collect());
    }
    (spec.program.as_str(), args.collect())
}

#[cfg(unix)]
fn group(cmd: &mut CommandWrap) {
    cmd.wrap(process_wrap::tokio::ProcessGroup::leader());
}

#[cfg(windows)]
fn group(cmd: &mut CommandWrap) {
    use windows::Win32::System::Threading::CREATE_NO_WINDOW;
    // Headless: a status poll would otherwise flash a console window every few seconds.
    cmd.wrap(process_wrap::tokio::CreationFlags(CREATE_NO_WINDOW));
    cmd.wrap(process_wrap::tokio::JobObject);
}

/// The error of a tool that couldn't start: missing, or the OS's reason.
fn spawned(program: &str, e: std::io::Error) -> ProcessError {
    match e.kind() {
        std::io::ErrorKind::NotFound => ProcessError::Missing { program: program.to_owned() },
        _ => ProcessError::Io {
            program: program.to_owned(),
            source: e,
        },
    }
}

#[cfg(test)]
#[path = "system_tests.rs"]
mod tests;