2024-03-30 21:03:37 +01:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
2024-01-28 20:44:12 +01:00
|
|
|
use async_trait::async_trait;
|
|
|
|
use eyre::Context;
|
|
|
|
|
2024-02-03 18:54:17 +01:00
|
|
|
use crate::{cli, cuddle_file::CuddleFile, MainAction};
|
2024-01-28 20:44:12 +01:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2024-03-02 15:23:54 +01:00
|
|
|
pub enum CuddleEnv {
|
|
|
|
Prod,
|
|
|
|
Dev,
|
|
|
|
}
|
|
|
|
|
2024-03-30 21:03:37 +01:00
|
|
|
impl Display for CuddleEnv {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-03-02 15:23:54 +01:00
|
|
|
match self {
|
2024-03-30 21:03:37 +01:00
|
|
|
CuddleEnv::Prod => f.write_str("prod"),
|
|
|
|
CuddleEnv::Dev => f.write_str("dev"),
|
2024-03-02 15:23:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryInto<CuddleEnv> for String {
|
|
|
|
type Error = eyre::Error;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<CuddleEnv, Self::Error> {
|
|
|
|
let env = match self.as_str() {
|
|
|
|
"prod" => CuddleEnv::Prod,
|
|
|
|
"dev" => CuddleEnv::Dev,
|
|
|
|
_ => eyre::bail!("was not a valid env: {}", self),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 20:44:12 +01:00
|
|
|
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,
|
2024-01-28 22:59:32 +01:00
|
|
|
folder: ".cuddle/tmp".into(),
|
2024-01-28 20:44:12 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-02 15:23:54 +01:00
|
|
|
pub async fn releaser(&self, env: CuddleEnv) -> eyre::Result<()> {
|
2024-01-28 20:44:12 +01:00
|
|
|
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-03-02 15:23:54 +01:00
|
|
|
.get(&self.env.as_ref().unwrap_or(&env.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(()),
|
|
|
|
};
|
|
|
|
|
2024-03-30 21:03:37 +01:00
|
|
|
if let Some(clusters) = &self.cuddle_file.vars.clusters {
|
|
|
|
let cluster = match 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(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let cuddle_releaser_image = "docker.io/kasperhermansen/cuddle-releaser:main-1706726858";
|
|
|
|
|
|
|
|
let folder = client.host().directory(&self.folder);
|
|
|
|
|
|
|
|
let ssh_sock = std::env::var("SSH_AUTH_SOCK").context("SSH_AUTH_SOCK not set")?;
|
|
|
|
|
|
|
|
let cuddle_releaser = client
|
|
|
|
.container()
|
|
|
|
.from(cuddle_releaser_image)
|
|
|
|
.with_env_variable("RUST_LOG", "trace")
|
|
|
|
.with_directory("/mnt/templates", folder)
|
|
|
|
.with_unix_socket(
|
|
|
|
ssh_sock.clone(),
|
|
|
|
client.host().unix_socket(ssh_sock.clone()),
|
|
|
|
);
|
|
|
|
|
|
|
|
let time = chrono::Local::now();
|
|
|
|
|
|
|
|
cuddle_releaser
|
|
|
|
.with_exec(vec!["echo", &time.to_rfc3339()])
|
|
|
|
.with_exec(vec![
|
|
|
|
"cuddle-releaser",
|
|
|
|
"release",
|
|
|
|
&format!("--upstream={}", options.upstream),
|
|
|
|
&format!("--folder={}", "/mnt/templates/k8s"),
|
|
|
|
&format!("--cluster={}", options.cluster),
|
|
|
|
&format!("--namespace={}", options.namespace),
|
|
|
|
&format!("--app={}", options.app),
|
|
|
|
])
|
|
|
|
.sync()
|
|
|
|
.await?;
|
|
|
|
}
|
2024-01-28 20:44:12 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2024-03-02 15:23:54 +01:00
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl MainAction for CuddleReleaser {
|
|
|
|
async fn execute_main(&self, _ctx: &mut cli::Context) -> eyre::Result<()> {
|
2024-03-02 16:54:44 +01:00
|
|
|
self.releaser(CuddleEnv::Prod).await?;
|
|
|
|
|
2024-03-02 15:23:54 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|