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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for CuddleEnv {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match self {
|
|
|
|
CuddleEnv::Prod => "prod".into(),
|
|
|
|
CuddleEnv::Dev => "dev".into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
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-31 19:50:16 +01:00
|
|
|
let cuddle_releaser_image = "docker.io/kasperhermansen/cuddle-releaser:main-1706726858";
|
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 22:58:23 +01:00
|
|
|
.with_directory("/mnt/templates", folder)
|
2024-01-28 21:19:02 +01:00
|
|
|
.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),
|
2024-01-28 22:59:32 +01:00
|
|
|
&format!("--folder={}", "/mnt/templates/k8s"),
|
2024-01-28 20:44:12 +01:00
|
|
|
&format!("--cluster={}", options.cluster),
|
|
|
|
&format!("--namespace={}", options.namespace),
|
|
|
|
&format!("--app={}", options.app),
|
|
|
|
])
|
|
|
|
.sync()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
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<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|