mirror of
https://github.com/moghtech/komodo.git
synced 2026-08-01 18:59:59 -05:00
build_build
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use async_timing_util::unix_timestamp_ms;
|
||||
use diff::Diff;
|
||||
use mungos::{doc, to_bson};
|
||||
use types::{
|
||||
traits::Permissioned, Build, Log, Operation, PermissionLevel, Update, UpdateStatus,
|
||||
UpdateTarget,
|
||||
@@ -147,6 +148,62 @@ impl State {
|
||||
Ok(new_build)
|
||||
}
|
||||
|
||||
pub async fn build_build(&self, build_id: &str, user: &RequestUser) -> anyhow::Result<Update> {
|
||||
let mut build = self
|
||||
.get_build_check_permissions(build_id, user, PermissionLevel::Write)
|
||||
.await?;
|
||||
build.version.increment();
|
||||
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Build(build_id.to_string()),
|
||||
operation: Operation::BuildBuild,
|
||||
start_ts: unix_timestamp_ms() as i64,
|
||||
status: UpdateStatus::InProgress,
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
version: build.version.clone().into(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let server = self.db.get_server(&build.server_id).await?;
|
||||
|
||||
let update_logs = self
|
||||
.periphery
|
||||
.build(&server, &build)
|
||||
.await
|
||||
.context("failed at call to periphery to build")?;
|
||||
|
||||
match update_logs {
|
||||
Some(logs) => {
|
||||
update.logs.extend(logs);
|
||||
update.success = all_logs_success(&update.logs);
|
||||
if update.success {
|
||||
self.db
|
||||
.builds
|
||||
.update_one::<Build>(
|
||||
build_id,
|
||||
mungos::Update::Set(doc! {
|
||||
"version": to_bson(&build.version)
|
||||
.context("failed at converting version to bson")?
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
update
|
||||
.logs
|
||||
.push(Log::error("build", "builder busy".to_string()));
|
||||
}
|
||||
}
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(unix_timestamp_ms() as i64);
|
||||
self.update_update(update.clone()).await?;
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
pub async fn reclone_build(
|
||||
&self,
|
||||
build_id: &str,
|
||||
|
||||
@@ -81,6 +81,20 @@ pub fn router() -> Router {
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/build/:id",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(build_id): Path<BuildId>| async move {
|
||||
let update = state
|
||||
.build_build(&build_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(update))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/reclone/:id",
|
||||
post(
|
||||
|
||||
@@ -25,6 +25,11 @@ impl MonitorClient {
|
||||
self.patch("/api/build/update", build).await
|
||||
}
|
||||
|
||||
pub async fn build_build(&self, id: &str) -> anyhow::Result<Update> {
|
||||
self.post::<(), _>(&format!("/api/build/build/{id}"), None)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn reclone_build(&self, id: &str) -> anyhow::Result<Update> {
|
||||
self.post::<(), _>(&format!("/api/build/reclone/{id}"), None)
|
||||
.await
|
||||
|
||||
+10
-11
@@ -256,16 +256,6 @@ pub struct Build {
|
||||
pub docker_account: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
|
||||
pub struct BuildRecord {
|
||||
pub start_ts: i64,
|
||||
pub end_ts: i64,
|
||||
pub success: bool,
|
||||
pub logs: Vec<Log>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub version: Option<Version>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
|
||||
pub struct Update {
|
||||
#[serde(
|
||||
@@ -283,6 +273,8 @@ pub struct Update {
|
||||
pub status: UpdateStatus,
|
||||
pub success: bool,
|
||||
pub operator: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub version: Option<Version>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Diff)]
|
||||
@@ -412,11 +404,18 @@ pub struct Command {
|
||||
pub struct Version {
|
||||
pub major: u64,
|
||||
pub minor: u64,
|
||||
pub patch: u64,
|
||||
}
|
||||
|
||||
impl ToString for Version {
|
||||
fn to_string(&self) -> String {
|
||||
format!("{}.{}", self.major, self.minor)
|
||||
format!("{}.{}.{}", self.major, self.minor, self.patch)
|
||||
}
|
||||
}
|
||||
|
||||
impl Version {
|
||||
pub fn increment(&mut self) {
|
||||
self.patch += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user