use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::ContainerPath;
use crate::shell_session::{ShellBackend, ShellSessionRegistry};
use crate::ssh::{PreparedSsh, SshEnvironment};
use crate::tool_distribution::ToolDistribution;
use crate::tool_runtime::{
HostProgram, PreparedEnvironment, PreparedTools, ToolCatalog, ToolRuntimeError, ToolTarget,
};
use crate::tool_transport::{
ContainerToolTransport, LocalToolTransport, SshToolTransport, ToolTransport,
ensure_command_line_tools, probe_tool_environment, successful_output,
};
#[derive(Debug, Clone)]
enum PreparedHome {
Local(PreparedTools<PathBuf>),
Container {
container_id: String,
tools: PreparedTools<ContainerPath>,
},
}
impl PreparedHome {
const fn backend(&self) -> ShellBackend {
match self {
Self::Local(_) => ShellBackend::Local,
Self::Container { .. } => ShellBackend::Container,
}
}
const fn catalog(&self) -> &ToolCatalog {
match self {
Self::Local(tools) => tools.catalog(),
Self::Container { tools, .. } => tools.catalog(),
}
}
}
#[derive(Debug)]
struct ToolSource {
distribution: ToolDistribution,
cache_root: PathBuf,
}
#[derive(Debug, Clone)]
pub struct PreparedToolEnvironments {
home: PreparedHome,
catalog: ToolCatalog,
source: Option<Arc<ToolSource>>,
}
impl ShellSessionRegistry {
pub async fn initialize_tools(
mut self,
distribution: &ToolDistribution,
cache_root: &Path,
) -> Result<Self, ToolRuntimeError> {
let availability = self.availability();
let home = if availability.container {
let runtime = self
.container_runtime()
.cloned()
.ok_or(ToolRuntimeError::MissingContainerRuntime)?;
let transport = ContainerToolTransport::new(runtime.clone());
let info = probe_tool_environment(&transport).await?;
let tools = distribution
.image_tools(&info.target)?
.verify(&transport)
.await?;
PreparedHome::Container {
container_id: runtime.container_id().into(),
tools,
}
} else if availability.local {
let target = ToolTarget::native()?;
ensure_command_line_tools(&LocalToolTransport, &target).await?;
let artifacts = cache_root.join("artifacts");
let (bundle, heel) = futures::try_join!(
distribution.load(&target, &artifacts),
distribution.prepare_host_program(HostProgram::Heel, &target, cache_root),
)?;
PreparedHome::Local(
bundle
.prepare_local(&cache_root.join("runtimes"))
.await?
.with_heel(heel),
)
} else {
return Err(ToolRuntimeError::NotInitialized(
"no configured home environment".into(),
));
};
let catalog = home.catalog().clone();
self.prepared_tools = Some(PreparedToolEnvironments {
home,
catalog,
source: Some(Arc::new(ToolSource {
distribution: distribution.clone(),
cache_root: cache_root.to_path_buf(),
})),
});
Ok(self)
}
fn prepared_environments(&self) -> Result<&PreparedToolEnvironments, ToolRuntimeError> {
let prepared = self
.prepared_tools
.as_ref()
.ok_or_else(|| ToolRuntimeError::NotInitialized("execution environments".into()))?;
if Some(prepared.home.backend()) != self.resolve_local_backend().ok() {
return Err(ToolRuntimeError::EnvironmentChanged(
PreparedEnvironment::HomeBackend,
));
}
if let PreparedHome::Container { container_id, .. } = &prepared.home
&& self
.container_runtime()
.map(crate::ContainerShellRuntime::container_id)
!= Some(container_id.as_str())
{
return Err(ToolRuntimeError::EnvironmentChanged(
PreparedEnvironment::Container,
));
}
Ok(prepared)
}
pub fn local_tools(&self) -> Result<&PreparedTools<PathBuf>, ToolRuntimeError> {
match &self.prepared_environments()?.home {
PreparedHome::Local(tools) => Ok(tools),
PreparedHome::Container { .. } => {
Err(ToolRuntimeError::NotInitialized("local environment".into()))
}
}
}
pub async fn host_tools(
&self,
distribution: &ToolDistribution,
cache_root: &Path,
) -> Result<PreparedTools<PathBuf>, ToolRuntimeError> {
if let PreparedHome::Local(tools) = &self.prepared_environments()?.home {
return Ok(tools.clone());
}
distribution
.load(&ToolTarget::native()?, &cache_root.join("artifacts"))
.await?
.prepare_local(&cache_root.join("runtimes"))
.await
}
pub(crate) fn container_tools(
&self,
) -> Result<&PreparedTools<ContainerPath>, ToolRuntimeError> {
match &self.prepared_environments()?.home {
PreparedHome::Container { tools, .. } => Ok(tools),
PreparedHome::Local(_) => Err(ToolRuntimeError::NotInitialized(
"container environment".into(),
)),
}
}
pub(crate) async fn ssh_tools<'e>(
&self,
environment: &'e SshEnvironment,
) -> Result<&'e PreparedSsh, ToolRuntimeError> {
let prepared = self.prepared_environments()?;
let catalog = prepared.catalog.clone();
let source = prepared.source.clone();
let client = self.ssh_environments().client().clone();
environment
.prepared(|| async move {
let source = source.ok_or_else(|| {
ToolRuntimeError::NotInitialized("the tool distribution".into())
})?;
let transport = SshToolTransport::over(client, environment.server().clone());
let info = probe_tool_environment(&transport).await?;
ensure_command_line_tools(&transport, &info.target).await?;
let bundle = source
.distribution
.load(&info.target, &source.cache_root.join("artifacts"))
.await?;
let (tools, heel) = futures::try_join!(
bundle.prepare_remote(&transport, &info.cache_root),
remote_heel(&transport, &info.target, &source),
)?;
if tools.catalog() != &catalog {
return Err(ToolRuntimeError::CatalogMismatch);
}
Ok(PreparedSsh { heel, tools })
})
.await
}
pub fn prepared_catalog(&self) -> Result<&ToolCatalog, ToolRuntimeError> {
self.prepared_environments()
.map(|prepared| &prepared.catalog)
}
#[cfg(test)]
pub(crate) fn with_test_tools(self, tools: PreparedTools<PathBuf>) -> Self {
self.with_test_home(PreparedHome::Local(tools))
}
#[cfg(test)]
pub(crate) fn with_test_container_tools(
self,
tools: PreparedTools<ContainerPath>,
container_id: String,
) -> Self {
self.with_test_home(PreparedHome::Container {
container_id,
tools,
})
}
#[cfg(test)]
fn with_test_home(mut self, home: PreparedHome) -> Self {
let catalog = home.catalog().clone();
self.prepared_tools = Some(PreparedToolEnvironments {
home,
catalog,
source: None,
});
self
}
#[cfg(test)]
pub(crate) fn with_test_ssh(
self,
server: crate::SshServer,
heel: Option<String>,
tools: PreparedTools<PathBuf>,
) -> Self {
let name = server.name.clone();
self.add_ssh_server(server).expect("a test server is valid");
self.ssh_environments()
.get(&name)
.expect("the server was just added")
.set_prepared(PreparedSsh { heel, tools });
self
}
}
async fn remote_heel(
transport: &SshToolTransport,
target: &ToolTarget,
source: &ToolSource,
) -> Result<Option<String>, ToolRuntimeError> {
let found = successful_output(
transport
.run(include_str!("../templates/ssh_heel_probe.sh"), None)
.await?,
)?;
if let Some(path) = found.lines().next() {
return Ok(Some(path.to_string()));
}
let local = source
.distribution
.prepare_host_program(HostProgram::Heel, target, &source.cache_root)
.await?;
let binary = async_fs::read(local.path()).await?;
let installed = successful_output(
transport
.run(
include_str!("../templates/ssh_heel_install.sh"),
Some(binary),
)
.await?,
)?;
let path = installed
.lines()
.next()
.ok_or(ToolRuntimeError::InvalidProbe)?;
Ok(Some(path.to_string()))
}