Some checks reported errors
continuous-integration/drone/push Build encountered an error
Signed-off-by: kjuulh <contact@kjuulh.io>
109 lines
2.8 KiB
Rust
109 lines
2.8 KiB
Rust
use std::{
|
|
path::{Path, PathBuf},
|
|
process::Command,
|
|
};
|
|
|
|
use anyhow::Context;
|
|
use flux_releaser::{
|
|
app::{LocalApp, SharedLocalApp},
|
|
services::flux_local_cluster::extensions::FluxLocalClusterManagerExt,
|
|
};
|
|
use futures::{future::BoxFuture, FutureExt};
|
|
|
|
use crate::process::UploadStrategy;
|
|
|
|
#[derive(Default, Clone)]
|
|
pub struct Releaser {
|
|
registry: String,
|
|
service: String,
|
|
}
|
|
|
|
impl Releaser {
|
|
pub fn with_registry(&mut self, registry: impl Into<String>) -> &mut Self {
|
|
self.registry = registry.into();
|
|
|
|
self
|
|
}
|
|
|
|
pub fn with_service(&mut self, service: impl Into<String>) -> &mut Self {
|
|
self.service = service.into();
|
|
|
|
self
|
|
}
|
|
|
|
pub async fn release(&self, input_path: impl Into<PathBuf>) -> anyhow::Result<()> {
|
|
let input_path = input_path.into();
|
|
let branch = self.get_branch()?.ok_or(anyhow::anyhow!(
|
|
"failed to find branch, required for triggering release"
|
|
))?;
|
|
|
|
tracing::trace!("triggering release for: {}", input_path.display());
|
|
|
|
let local_app =
|
|
SharedLocalApp::new(LocalApp::new(&self.registry).await?).flux_local_cluster_manager();
|
|
|
|
let upload_id = local_app
|
|
.package_clusters(input_path)
|
|
.await
|
|
.context("failed to package clusters")?;
|
|
|
|
local_app
|
|
.commit_artifact(&self.service, &branch, upload_id)
|
|
.await
|
|
.context("failed to commit artifact")?;
|
|
|
|
local_app
|
|
.trigger_release(&self.service, &branch)
|
|
.await
|
|
.context("failed to trigger release")?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn get_branch(&self) -> anyhow::Result<Option<String>> {
|
|
let output = Command::new("git")
|
|
.args(["rev-parse", "--abbrev-ref", "HEAD"])
|
|
.output()?;
|
|
|
|
if output.status.success() {
|
|
let branch = std::str::from_utf8(&output.stdout)?.trim().to_string();
|
|
Ok(Some(branch))
|
|
} else {
|
|
let err = std::str::from_utf8(&output.stderr)?;
|
|
anyhow::bail!("Failed to get branch name: {}", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
impl UploadStrategy for Releaser {
|
|
fn upload(&self, input_path: &Path) -> BoxFuture<'_, anyhow::Result<()>> {
|
|
let input_path = input_path.to_path_buf();
|
|
|
|
async move {
|
|
self.release(input_path).await?;
|
|
|
|
Ok(())
|
|
}
|
|
.boxed()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use crate::releaser::Releaser;
|
|
|
|
#[tokio::test]
|
|
async fn can_upload_test() -> anyhow::Result<()> {
|
|
return Ok(());
|
|
let releaser = Releaser::default();
|
|
|
|
releaser
|
|
.release(
|
|
"/home/kjuulh/git/git.front.kjuulh.io/kjuulh/cuddle-rust-service-plan/.cuddle/tmp/cuddle-clusters",
|
|
)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|