2023-12-30 21:55:29 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
2023-12-30 22:40:08 +01:00
|
|
|
use dagger_sdk::{Container, ContainerWithNewFileOptsBuilder, Socket};
|
2023-12-30 22:07:44 +01:00
|
|
|
use eyre::Context;
|
2023-12-30 21:55:29 +01:00
|
|
|
|
|
|
|
use crate::{dagger_middleware::DaggerMiddleware, leptos_service::LeptosService};
|
|
|
|
|
|
|
|
use super::RustService;
|
|
|
|
|
|
|
|
pub struct SshAgent {
|
2023-12-30 22:07:44 +01:00
|
|
|
client: dagger_sdk::Query,
|
2023-12-30 21:55:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SshAgent {
|
2023-12-30 22:07:44 +01:00
|
|
|
pub fn new(client: dagger_sdk::Query) -> Self {
|
|
|
|
Self { client }
|
2023-12-30 21:55:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl DaggerMiddleware for SshAgent {
|
|
|
|
async fn handle(&self, container: Container) -> eyre::Result<Container> {
|
2023-12-30 22:07:44 +01:00
|
|
|
let sock_var =
|
|
|
|
std::env::var("SSH_AUTH_SOCK").context("failed to find variable SSH_AUTH_SOCK")?;
|
|
|
|
|
|
|
|
let socket = self.client.host().unix_socket(&sock_var);
|
|
|
|
|
2023-12-30 22:41:56 +01:00
|
|
|
let home = container.env_variable("HOME").await.unwrap_or("".into());
|
2023-12-30 22:40:08 +01:00
|
|
|
|
2023-12-30 22:00:00 +01:00
|
|
|
let c = container
|
2023-12-30 22:40:08 +01:00
|
|
|
.with_new_file_opts(
|
|
|
|
format!("{home}/.ssh/config"),
|
2023-12-30 22:40:49 +01:00
|
|
|
ContainerWithNewFileOptsBuilder::default()
|
|
|
|
.contents(
|
|
|
|
r#"
|
2023-12-30 22:40:08 +01:00
|
|
|
Host *
|
|
|
|
StrictHostKeyChecking no
|
|
|
|
UserKnownHostsFile=/dev/null
|
|
|
|
"#,
|
2023-12-30 22:40:49 +01:00
|
|
|
)
|
|
|
|
.build()?,
|
2023-12-30 22:40:08 +01:00
|
|
|
)
|
2023-12-30 22:07:44 +01:00
|
|
|
.with_unix_socket(&sock_var, socket)
|
|
|
|
.with_env_variable("SSH_AUTH_SOCK", &sock_var);
|
2023-12-30 21:55:29 +01:00
|
|
|
|
|
|
|
Ok(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SshAgentExt {
|
2023-12-30 22:07:44 +01:00
|
|
|
fn with_ssh_agent(&mut self) -> &mut Self {
|
2023-12-30 21:55:29 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SshAgentExt for RustService {
|
2023-12-30 22:07:44 +01:00
|
|
|
fn with_ssh_agent(&mut self) -> &mut Self {
|
|
|
|
let client = self.client.clone();
|
|
|
|
|
2023-12-30 21:55:29 +01:00
|
|
|
self.with_stage(super::RustServiceStage::BeforeDeps(Arc::new(
|
2023-12-30 22:07:44 +01:00
|
|
|
SshAgent::new(client.clone()),
|
2023-12-30 21:55:29 +01:00
|
|
|
)))
|
2023-12-30 21:58:17 +01:00
|
|
|
.with_stage(super::RustServiceStage::AfterBase(Arc::new(SshAgent::new(
|
2023-12-30 22:07:44 +01:00
|
|
|
client,
|
2023-12-30 21:58:17 +01:00
|
|
|
))))
|
2023-12-30 21:55:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SshAgentExt for LeptosService {
|
2023-12-30 22:07:44 +01:00
|
|
|
fn with_ssh_agent(&mut self) -> &mut Self {
|
|
|
|
let client = self.client.clone();
|
|
|
|
|
2023-12-30 21:55:29 +01:00
|
|
|
self.with_stage(super::RustServiceStage::BeforeDeps(Arc::new(
|
2023-12-30 22:07:44 +01:00
|
|
|
SshAgent::new(client.clone()),
|
2023-12-30 21:55:29 +01:00
|
|
|
)))
|
2023-12-30 21:58:17 +01:00
|
|
|
.with_stage(super::RustServiceStage::AfterBase(Arc::new(SshAgent::new(
|
2023-12-30 22:07:44 +01:00
|
|
|
client,
|
2023-12-30 21:58:17 +01:00
|
|
|
))))
|
2023-12-30 21:55:29 +01:00
|
|
|
}
|
|
|
|
}
|