2024-01-28 20:44:12 +01:00
|
|
|
use async_trait::async_trait;
|
|
|
|
use eyre::Context;
|
|
|
|
|
|
|
|
use crate::{cuddle_file::CuddleFile, MainAction};
|
|
|
|
|
|
|
|
pub struct CuddleReleaser {
|
|
|
|
client: dagger_sdk::Query,
|
2024-01-28 20:45:25 +01:00
|
|
|
env: Option<String>,
|
2024-01-28 20:44:12 +01:00
|
|
|
cuddle_file: CuddleFile,
|
|
|
|
|
|
|
|
folder: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CuddleReleaserOptions {
|
|
|
|
upstream: String,
|
|
|
|
cluster: String,
|
|
|
|
namespace: String,
|
|
|
|
app: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CuddleReleaser {
|
|
|
|
pub async fn new(client: dagger_sdk::Query) -> eyre::Result<Self> {
|
|
|
|
let cuddle_file = CuddleFile::from_cuddle_file().await?;
|
|
|
|
|
2024-01-28 20:45:25 +01:00
|
|
|
let env = std::env::var("CUDDLE_ENV").ok();
|
2024-01-28 20:44:12 +01:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
client,
|
|
|
|
cuddle_file,
|
|
|
|
env,
|
|
|
|
folder: ".cuddle/tmp/k8s".into(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl MainAction for CuddleReleaser {
|
|
|
|
async fn execute_main(&self) -> eyre::Result<()> {
|
|
|
|
let client = self.client.clone();
|
|
|
|
|
|
|
|
if self.cuddle_file.deployment.is_none() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let chosen_cluster = match self
|
|
|
|
.cuddle_file
|
|
|
|
.deployment
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.env
|
|
|
|
.0
|
2024-01-28 20:45:25 +01:00
|
|
|
.get(&self.env.as_ref().unwrap_or(&"prod".to_string()).to_string())
|
2024-01-28 20:44:12 +01:00
|
|
|
{
|
|
|
|
Some(c) => match c.clusters.first().take() {
|
|
|
|
Some(c) => c,
|
|
|
|
None => return Ok(()),
|
|
|
|
},
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let cluster = match self.cuddle_file.vars.clusters.0.get(chosen_cluster) {
|
|
|
|
Some(c) => c,
|
|
|
|
None => eyre::bail!("no cluster found for: {}", chosen_cluster),
|
|
|
|
};
|
|
|
|
|
|
|
|
let options = CuddleReleaserOptions {
|
|
|
|
cluster: chosen_cluster.clone(),
|
|
|
|
namespace: cluster.namespace.clone(),
|
|
|
|
app: self.cuddle_file.vars.service.clone(),
|
|
|
|
upstream: self
|
|
|
|
.cuddle_file
|
|
|
|
.deployment
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.registry
|
|
|
|
.clone(),
|
|
|
|
};
|
|
|
|
|
2024-01-28 22:57:37 +01:00
|
|
|
let cuddle_releaser_image = "docker.io/kasperhermansen/cuddle-releaser:main-1706479027";
|
2024-01-28 20:44:12 +01:00
|
|
|
|
|
|
|
let folder = client.host().directory(&self.folder);
|
|
|
|
|
2024-01-28 21:19:02 +01:00
|
|
|
let ssh_sock = std::env::var("SSH_AUTH_SOCK").context("SSH_AUTH_SOCK not set")?;
|
|
|
|
|
2024-01-28 20:44:12 +01:00
|
|
|
let cuddle_releaser = client
|
|
|
|
.container()
|
|
|
|
.from(cuddle_releaser_image)
|
2024-01-28 22:45:25 +01:00
|
|
|
.with_env_variable("RUST_LOG", "trace")
|
2024-01-28 21:19:02 +01:00
|
|
|
.with_mounted_directory("/mnt/templates", folder)
|
|
|
|
.with_unix_socket(
|
|
|
|
ssh_sock.clone(),
|
|
|
|
client.host().unix_socket(ssh_sock.clone()),
|
|
|
|
);
|
2024-01-28 20:44:12 +01:00
|
|
|
|
2024-01-28 22:24:28 +01:00
|
|
|
let time = chrono::Local::now();
|
|
|
|
|
2024-01-28 20:44:12 +01:00
|
|
|
cuddle_releaser
|
2024-01-28 22:24:28 +01:00
|
|
|
.with_exec(vec!["echo", &time.to_rfc3339()])
|
2024-01-28 20:44:12 +01:00
|
|
|
.with_exec(vec![
|
|
|
|
"cuddle-releaser",
|
|
|
|
"release",
|
|
|
|
&format!("--upstream={}", options.upstream),
|
|
|
|
&format!("--folder={}", "/mnt/templates"),
|
|
|
|
&format!("--cluster={}", options.cluster),
|
|
|
|
&format!("--namespace={}", options.namespace),
|
|
|
|
&format!("--app={}", options.app),
|
|
|
|
])
|
|
|
|
.sync()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|