use std::collections::HashMap;
use std::future::Future;
use std::path::PathBuf;
use std::pin::Pin;
use std::process::Stdio;
use std::sync::{Arc, OnceLock};
use arc_swap::ArcSwap;
use async_lock::OnceCell;
use futures_lite::io::AsyncWriteExt;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::RemoteLaunch;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct SshServer {
pub name: String,
pub target: String,
}
impl SshServer {
#[must_use]
pub fn id(&self) -> &str {
&self.name
}
}
#[derive(Debug, thiserror::Error)]
pub enum SshServerError {
#[error("an SSH server needs a non-empty name and target")]
Blank,
#[error("an SSH server named `{0}` is already configured")]
Duplicate(String),
}
#[derive(Debug, thiserror::Error)]
pub enum SshUploadError {
#[error("could not start ssh: {0}")]
Spawn(std::io::Error),
#[error("could not send the script to ssh: {0}")]
Write(std::io::Error),
#[error("writing the script to `{target}` failed ({status}): {stderr}")]
Failed {
target: String,
status: std::process::ExitStatus,
stderr: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct SshPlatform {
pub os: String,
pub arch: String,
}
#[derive(Debug, thiserror::Error)]
pub enum SshProbeError {
#[error("could not start ssh: {0}")]
Spawn(#[from] std::io::Error),
#[error(transparent)]
Upload(#[from] SshUploadError),
#[error("the probe of `{target}` exited with {status}: {stderr}")]
Failed {
target: String,
status: std::process::ExitStatus,
stderr: String,
},
#[error("the probe answered `{0}`, which is not `uname -s -m` output")]
Unreadable(String),
}
pub async fn probe_ssh_os(server: &SshServer) -> Result<SshPlatform, SshProbeError> {
SshClient::default().probe_os(server).await
}
#[derive(Debug, Default)]
struct Masters {
dir: OnceLock<tempfile::TempDir>,
opened: std::sync::Mutex<HashMap<String, Arc<OnceCell<()>>>>,
}
impl Masters {
fn socket(&self, target: &str) -> PathBuf {
let dir = self.dir.get_or_init(|| {
tempfile::Builder::new()
.prefix("ssh-")
.tempdir()
.expect("the private directory of the SSH control sockets can be created")
});
let digest = Sha256::digest(target.as_bytes());
dir.path().join(hex::encode(&digest[..6]))
}
}
impl Drop for Masters {
fn drop(&mut self) {
let Some(dir) = self.dir.get() else { return };
let Ok(sockets) = std::fs::read_dir(dir.path()) else {
return;
};
for socket in sockets.flatten() {
let _ = std::process::Command::new("ssh")
.args(["-O", "exit", "-o"])
.arg(format!("ControlPath={}", socket.path().display()))
.args(["--", "master"])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
}
}
}
#[derive(Debug, Clone, Default)]
pub struct SshClient {
config: Option<PathBuf>,
masters: Arc<Masters>,
}
impl SshClient {
#[cfg(test)]
pub(crate) fn with_config(config: PathBuf) -> Self {
Self {
config: Some(config),
masters: Arc::default(),
}
}
pub(crate) fn ssh(&self, server: &SshServer, line: &str) -> std::process::Command {
let mut command = self.options(server);
command.arg("--").arg(&server.target).arg(line);
command
}
fn options(&self, server: &SshServer) -> std::process::Command {
let mut command = std::process::Command::new("ssh");
command.args(["-o", "BatchMode=yes", "-o", "ConnectTimeout=10"]);
command
.args(["-o", "ControlMaster=auto", "-o", "ControlPersist=10m"])
.arg("-o")
.arg(format!(
"ControlPath={}",
self.masters.socket(&server.target).display()
));
if let Some(config) = &self.config {
command.arg("-F").arg(config);
}
command
}
async fn ensure_master(&self, server: &SshServer) -> Result<(), SshUploadError> {
let opened = Arc::clone(
self.masters
.opened
.lock()
.expect("the map of opened masters is never held across a panic")
.entry(server.target.clone())
.or_default(),
);
opened
.get_or_try_init(|| async {
let output = async_process::Command::from(self.ssh(server, "true"))
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.output()
.await
.map_err(SshUploadError::Spawn)?;
if output.status.success() {
return Ok(());
}
Err(SshUploadError::Failed {
target: server.target.clone(),
status: output.status,
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
})
})
.await?;
Ok(())
}
pub(crate) async fn command(
&self,
server: &SshServer,
script: &str,
) -> Result<std::process::Command, SshUploadError> {
self.ensure_master(server).await?;
let path = format!("/tmp/aither-{}.sh", uuid::Uuid::new_v4().simple());
let upload = |error: std::io::Error| SshUploadError::Spawn(error);
let mut child =
async_process::Command::from(self.ssh(server, &format!("umask 077; cat > {path}")))
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()
.map_err(upload)?;
let mut stdin = child.stdin.take().expect("the upload's stdin is piped");
let body = format!("rm -f \"$0\"\n{script}");
let written = stdin.write_all(body.as_bytes()).await;
drop(stdin);
let output = child.output().await.map_err(upload)?;
if !output.status.success() {
return Err(SshUploadError::Failed {
target: server.target.clone(),
status: output.status,
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
});
}
written.map_err(SshUploadError::Write)?;
Ok(self.ssh(server, &format!("sh {path}")))
}
pub(crate) async fn probe_os(&self, server: &SshServer) -> Result<SshPlatform, SshProbeError> {
let output = async_process::Command::from(self.command(server, "uname -s -m").await?)
.stdin(Stdio::null())
.output()
.await?;
if !output.status.success() {
return Err(SshProbeError::Failed {
target: server.target.clone(),
status: output.status,
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
});
}
let answer = String::from_utf8_lossy(&output.stdout).trim().to_string();
let mut words = answer.split_whitespace();
match (words.next(), words.next(), words.next()) {
(Some(os), Some(arch), None) => Ok(SshPlatform {
os: os.to_string(),
arch: arch.to_string(),
}),
_ => Err(SshProbeError::Unreadable(answer)),
}
}
}
#[derive(Debug, Clone)]
pub struct PreparedSsh {
pub(crate) heel: Option<String>,
pub(crate) launch: RemoteLaunch,
}
impl PreparedSsh {
#[must_use]
pub const fn new(heel: Option<String>, launch: RemoteLaunch) -> Self {
Self { heel, launch }
}
}
#[derive(Debug, thiserror::Error)]
#[error("preparing the SSH server `{server}` failed: {source}")]
pub struct SshPrepareError {
pub server: String,
#[source]
pub source: Box<dyn std::error::Error + Send + Sync>,
}
pub trait PrepareSsh: Send + Sync + 'static {
type Error: std::error::Error + Send + Sync + 'static;
fn prepare(
&self,
server: &SshServer,
) -> impl Future<Output = Result<PreparedSsh, Self::Error>> + Send;
}
impl PrepareSsh for PreparedSsh {
type Error = std::convert::Infallible;
fn prepare(
&self,
_server: &SshServer,
) -> impl Future<Output = Result<Self, Self::Error>> + Send {
std::future::ready(Ok(self.clone()))
}
}
trait PrepareSshImpl: Send + Sync {
fn prepare<'a>(
&'a self,
server: &'a SshServer,
) -> Pin<Box<dyn Future<Output = Result<PreparedSsh, SshPrepareError>> + Send + 'a>>;
}
impl<T: PrepareSsh> PrepareSshImpl for T {
fn prepare<'a>(
&'a self,
server: &'a SshServer,
) -> Pin<Box<dyn Future<Output = Result<PreparedSsh, SshPrepareError>> + Send + 'a>> {
Box::pin(async move {
PrepareSsh::prepare(self, server)
.await
.map_err(|error| SshPrepareError {
server: server.name.clone(),
source: Box::new(error),
})
})
}
}
#[derive(Clone)]
pub struct AnyPrepareSsh(Arc<dyn PrepareSshImpl>);
impl AnyPrepareSsh {
#[must_use]
pub fn new(prepare: impl PrepareSsh) -> Self {
Self(Arc::new(prepare))
}
}
impl std::fmt::Debug for AnyPrepareSsh {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("AnyPrepareSsh")
}
}
#[derive(Debug)]
pub struct SshEnvironment {
server: SshServer,
prepare: AnyPrepareSsh,
prepared: OnceCell<PreparedSsh>,
}
impl SshEnvironment {
pub(crate) const fn server(&self) -> &SshServer {
&self.server
}
pub(crate) async fn prepared(&self) -> Result<&PreparedSsh, SshPrepareError> {
self.prepared
.get_or_try_init(|| self.prepare.0.prepare(&self.server))
.await
}
}
#[derive(Debug, Clone, Default)]
pub struct SshEnvironments {
client: SshClient,
servers: Arc<ArcSwap<Vec<Arc<SshEnvironment>>>>,
}
impl SshEnvironments {
#[cfg(test)]
pub(crate) fn with_client(mut self, client: SshClient) -> Self {
self.client = client;
self
}
pub(crate) const fn client(&self) -> &SshClient {
&self.client
}
pub(crate) fn add(
&self,
server: SshServer,
prepare: impl PrepareSsh,
) -> Result<(), SshServerError> {
let SshServer { name, target } = server;
let server = SshServer {
name: name.trim().to_string(),
target: target.trim().to_string(),
};
if server.name.is_empty() || server.target.is_empty() {
return Err(SshServerError::Blank);
}
let environment = Arc::new(SshEnvironment {
server,
prepare: AnyPrepareSsh::new(prepare),
prepared: OnceCell::new(),
});
let mut duplicate = false;
self.servers.rcu(|servers| {
duplicate = servers
.iter()
.any(|existing| existing.server.name == environment.server.name);
let mut next = Vec::clone(servers);
if !duplicate {
next.push(Arc::clone(&environment));
}
next
});
if duplicate {
return Err(SshServerError::Duplicate(environment.server.name.clone()));
}
Ok(())
}
pub(crate) fn remove(&self, name: &str) -> Option<SshServer> {
let mut removed = None;
self.servers.rcu(|servers| {
let (gone, kept): (Vec<_>, Vec<_>) = servers
.iter()
.cloned()
.partition(|environment| environment.server.name == name);
removed = gone.into_iter().next();
kept
});
removed.map(|environment| environment.server.clone())
}
pub(crate) fn list(&self) -> Vec<SshServer> {
self.servers
.load()
.iter()
.map(|environment| environment.server.clone())
.collect()
}
pub(crate) fn get(&self, name: &str) -> Option<Arc<SshEnvironment>> {
self.servers
.load()
.iter()
.find(|environment| environment.server.name == name)
.cloned()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn server(name: &str, target: &str) -> SshServer {
SshServer {
name: name.to_string(),
target: target.to_string(),
}
}
fn prepared() -> PreparedSsh {
PreparedSsh::new(
None,
RemoteLaunch::new("/opt/tools/bin", "/opt/tools/bin/bash"),
)
}
#[test]
fn servers_join_and_leave_a_live_set_every_clone_sees() {
let set = SshEnvironments::default();
let clone = set.clone();
set.add(server(" build ", " builder@build.internal "), prepared())
.unwrap();
assert_eq!(
clone.list(),
[server("build", "builder@build.internal")],
"names and targets are trimmed and the clone sees the addition"
);
assert!(matches!(
clone.add(server("build", "other@build.internal"), prepared()),
Err(SshServerError::Duplicate(name)) if name == "build"
));
assert!(matches!(
set.add(server("staging", " "), prepared()),
Err(SshServerError::Blank)
));
assert_eq!(
clone.remove("build"),
Some(server("build", "builder@build.internal"))
);
assert_eq!(set.list(), []);
assert_eq!(set.remove("build"), None);
}
struct Counting {
runs: Arc<std::sync::atomic::AtomicUsize>,
failures: usize,
}
#[derive(Debug, thiserror::Error)]
#[error("the server is unreachable")]
struct Unreachable;
impl PrepareSsh for Counting {
type Error = Unreachable;
async fn prepare(&self, _server: &SshServer) -> Result<PreparedSsh, Unreachable> {
let run = self.runs.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
for _ in 0..10 {
tokio::task::yield_now().await;
}
if run < self.failures {
Err(Unreachable)
} else {
Ok(prepared())
}
}
}
fn environment(failures: usize) -> (Arc<SshEnvironment>, Arc<std::sync::atomic::AtomicUsize>) {
let runs = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let set = SshEnvironments::default();
set.add(
server("build", "builder@build.internal"),
Counting {
runs: Arc::clone(&runs),
failures,
},
)
.unwrap();
(set.get("build").unwrap(), runs)
}
#[tokio::test]
async fn a_server_is_prepared_on_its_first_use_and_only_once() {
let (environment, runs) = environment(0);
assert_eq!(
runs.load(std::sync::atomic::Ordering::SeqCst),
0,
"adding the server prepares nothing"
);
let (first, second) = tokio::join!(environment.prepared(), environment.prepared());
first.unwrap();
second.unwrap();
environment.prepared().await.unwrap();
assert_eq!(
runs.load(std::sync::atomic::Ordering::SeqCst),
1,
"two concurrent first commands and a later one share one preparation"
);
}
#[tokio::test]
async fn a_failed_preparation_is_reported_and_retried_by_the_next_command() {
let (environment, runs) = environment(1);
let error = environment.prepared().await.unwrap_err();
assert_eq!(error.server, "build");
assert!(error.source.to_string().contains("unreachable"), "{error}");
environment
.prepared()
.await
.expect("the next command prepares again");
environment.prepared().await.unwrap();
assert_eq!(runs.load(std::sync::atomic::Ordering::SeqCst), 2);
}
}