pub(crate) mod any;
pub(crate) mod compose;
#[cfg(test)]
pub(crate) mod contract;
pub(crate) mod ledger;
pub(crate) mod terraform;
mod vagrant;
use std::collections::BTreeMap;
use std::path::Path;
use std::time::Duration;
use crate::Result;
use crate::cli::{Call, CliError};
use crate::console::ShellCommand;
use crate::error::{Error, TimedOut};
use crate::events::Stream;
use crate::events::WarningCode;
use crate::graphical::{Console, ConsoleOffer, NativeApp, NativeConsole};
use crate::name::RunName;
use crate::process::ProcessRunner;
use crate::project::Project;
use crate::project::platform::Provider;
use crate::state::{Park, RunState};
use crate::status::MachineState;
use crate::store::{Lifetime, ResourceKind};
pub(crate) const CHECK_LIMIT: Duration = Duration::from_secs(300);
pub(crate) trait PickPorts: Send + Sync {
fn pick(&self, protocol: crate::project::platform::Protocol) -> std::io::Result<Option<crate::net::HeldPort>>;
fn claim(&self, ports: &[(crate::port::HostPort, crate::project::platform::Protocol)]) -> Result<()>;
}
pub(crate) struct NoPorts;
impl PickPorts for NoPorts {
fn pick(&self, _protocol: crate::project::platform::Protocol) -> std::io::Result<Option<crate::net::HeldPort>> {
Ok(None)
}
fn claim(&self, _ports: &[(crate::port::HostPort, crate::project::platform::Protocol)]) -> Result<()> {
Ok(())
}
}
pub(crate) struct ToolCx<'a> {
pub(crate) project: &'a Project,
pub(crate) run: &'a RunName,
pub(crate) key: &'a str,
pub(crate) state_dir: &'a Path,
pub(crate) provider: Option<Provider>,
pub(crate) env: Vec<(String, String)>,
pub(crate) host_env: &'a crate::host_env::HostEnv,
pub(crate) processes: &'a dyn ProcessRunner,
pub(crate) ssh_key: &'a Path,
pub(crate) ports: &'a dyn PickPorts,
pub(crate) sink: &'a (dyn Fn(Stream, &str) + Send + Sync),
pub(crate) steps: crate::steps::Steps<'a>,
pub(crate) warn: &'a (dyn Fn(WarningCode, &str) + Send + Sync),
pub(crate) made: &'a (dyn Fn() -> Result<Vec<String>> + Send + Sync),
pub(crate) fetched: Option<&'a (dyn Fn(crate::DownloadItem) + Send + Sync)>,
}
impl ToolCx<'_> {
pub(crate) fn call(&self, limit: Duration) -> Call<'_> {
Call {
runner: self.processes,
cwd: Some(self.project.dir.clone()),
env: self.env.clone(),
limit,
sink: self.sink,
host_env: self.host_env,
}
}
pub(crate) fn read(&self, limit: Duration) -> Call<'_> {
Call {
sink: &discard,
..self.call(limit)
}
}
pub(crate) fn var(&self, name: &str) -> Option<String> {
self.env
.iter()
.rev()
.find(|(k, _)| k == name)
.map(|(_, v)| v.clone())
.or_else(|| self.host_env.text(name).map(str::to_owned))
.filter(|v| !v.is_empty())
}
}
pub(crate) static NOTHING_MADE: &(dyn Fn() -> Result<Vec<String>> + Send + Sync) = &|| Ok(Vec::new());
pub(crate) fn unknown_version(cx: &ToolCx<'_>, found: Option<&crate::project::platform::Version>, tool: &str, min: &str) {
if found.is_none() {
let said = format!("`{tool}` printed no version the runner reads: it is used, the {min} it needs at least unchecked");
(cx.warn)(WarningCode::VersionUnknown, &said);
}
}
pub(crate) fn discard(_: Stream, _: &str) {}
pub(crate) fn tool_error<F: std::fmt::Display>(step: &str, e: CliError<F>) -> Error {
let step = step.to_owned();
match e {
CliError::Missing { tool } => Error::ToolMissing { step, tool: tool.to_owned() },
CliError::TooOld { tool, found, min } => Error::ToolTooOld {
tool: tool.to_owned(),
found,
required: min,
},
CliError::TimedOut { limit_s, last_line, .. } => Error::Timeout {
step,
timed_out: TimedOut::Ceiling { after_s: limit_s, last_line },
},
CliError::Stalled { after_s, last_line, .. } => Error::DownloadStalled {
what: crate::error::DownloadKind::Images,
step,
after_s,
last_line,
},
unread @ (CliError::Unexpected { .. } | CliError::Spawn { .. }) => Error::ToolFailed {
step,
reason: unread.to_string(),
},
CliError::Failed { tool, failure } => Error::ToolFailed {
step,
reason: format!("`{tool}` failed: {failure}"),
},
}
}
#[derive(Debug, Clone, Default)]
pub(crate) struct Seen {
pub(crate) machines: Vec<SeenMachine>,
pub(crate) outputs: BTreeMap<String, Output>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SeenMachine {
pub(crate) name: String,
pub(crate) state: MachineState,
pub(crate) published: Vec<SeenPort>,
pub(crate) kind: Option<String>,
pub(crate) address: Option<String>,
pub(crate) windows: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct SeenPort {
pub(crate) guest: crate::port::GuestPort,
pub(crate) host: crate::port::HostPort,
pub(crate) protocol: Option<crate::project::platform::Protocol>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Change {
pub(crate) added: Vec<String>,
pub(crate) removed: Vec<String>,
pub(crate) changed: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Output {
pub(crate) value: String,
pub(crate) sensitive: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Found {
pub(crate) key: String,
pub(crate) kind: ResourceKind,
pub(crate) native_id: String,
pub(crate) lifetime: Lifetime,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Ran {
pub(crate) exit: i32,
pub(crate) stdout: String,
pub(crate) stderr: String,
}
pub(crate) trait FormatDriver {
async fn probe(&self, cx: &ToolCx<'_>) -> Result<()>;
fn elevates(&self, _provider: Option<Provider>) -> bool {
false
}
fn ready_by_observation(&self, _failed_start: bool) -> bool {
true
}
async fn up(&self, cx: &ToolCx<'_>) -> Result<()>;
async fn down(&self, cx: &ToolCx<'_>) -> Result<()>;
async fn park(&self, cx: &ToolCx<'_>, mode: Park) -> Result<()>;
async fn resume(&self, cx: &ToolCx<'_>) -> Result<()>;
async fn update(&self, cx: &ToolCx<'_>) -> Result<()>;
async fn machines(&self, cx: &ToolCx<'_>) -> Result<Vec<String>>;
async fn reapply(&self, cx: &ToolCx<'_>, change: &Change) -> Result<()>;
async fn provision(&self, cx: &ToolCx<'_>, machine: Option<&str>, with: Option<&str>) -> Result<()>;
async fn attach(&self, cx: &ToolCx<'_>, machine: &str) -> Result<()>;
async fn retire(&self, cx: &ToolCx<'_>, machine: &str) -> Result<()>;
async fn package(&self, _cx: &ToolCx<'_>, machine: &str, _output: &std::path::Path) -> Result<()> {
Err(crate::Error::Unsupported {
what: format!("packaging `{machine}`: its format makes no box"),
})
}
async fn exec(&self, cx: &ToolCx<'_>, machine: &str, argv: &[String]) -> Result<Ran>;
async fn console(&self, cx: &ToolCx<'_>, machine: &str, user: Option<&str>) -> Result<ShellCommand>;
async fn observe(&self, cx: &ToolCx<'_>) -> Result<(Seen, RunState)>;
async fn found(&self, cx: &ToolCx<'_>) -> Result<Vec<Found>>;
async fn destroy_by_id(&self, cx: &ToolCx<'_>, key: &str, kind: ResourceKind, native_id: &str) -> Result<()>;
async fn graphical(&self, _cx: &ToolCx<'_>, _seen: &Seen, _machine: &SeenMachine) -> Result<Option<ConsoleOffer>> {
Ok(None)
}
async fn reach_graphical(&self, _cx: &ToolCx<'_>, machine: &str) -> Result<Console> {
Err(crate::graphical::no_console(machine, "its format has no graphical console"))
}
fn native_app(&self, _cx: &ToolCx<'_>, _seen: &Seen) -> Option<NativeApp> {
None
}
async fn native_console(&self, _cx: &ToolCx<'_>, machine: &str) -> Result<NativeConsole> {
Err(crate::graphical::native::no_app(machine))
}
async fn published(&self, _cx: &ToolCx<'_>) -> Result<Vec<Published>> {
Ok(Vec::new())
}
async fn usage(&self, _cx: &ToolCx<'_>) -> Result<Vec<crate::MachineUsage>> {
Ok(Vec::new())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Published {
pub(crate) name: String,
pub(crate) host: String,
pub(crate) port: u16,
}
#[cfg(test)]
pub(crate) async fn steps_said(base: ToolCx<'_>, run: impl AsyncFnOnce(&ToolCx<'_>)) -> Vec<String> {
let said = crate::steps::said::Said::default();
let say = |kind: crate::events::EventKind| said.keep(kind);
let cx = ToolCx {
steps: crate::steps::Steps::new(&say),
..base
};
run(&cx).await;
said.lines()
}