refactor: drop use of RunInfo

The run log replaces all uses of RunInfo now that the CI broker builds
report pages.

Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius
2024-03-31 15:08:03 +03:00
parent 2fa62ab846
commit 8bd8b9b21b
5 changed files with 12 additions and 160 deletions
+11 -29
View File
@@ -13,7 +13,6 @@ use crate::{
NativeMessageError, NativeMessageError,
}, },
run::{Run, RunError}, run::{Run, RunError},
runinfo::{RunInfo, RunInfoBuilder, RunInfoError},
runlog::RunLogError, runlog::RunLogError,
runspec::RunSpecError, runspec::RunSpecError,
}; };
@@ -23,7 +22,7 @@ use crate::{
pub struct Engine { pub struct Engine {
config: Config, config: Config,
adminlog: AdminLog, adminlog: AdminLog,
run_info_builder: RunInfoBuilder, result: Option<RunResult>,
} }
impl Engine { impl Engine {
@@ -45,7 +44,7 @@ impl Engine {
Ok(Self { Ok(Self {
config, config,
adminlog, adminlog,
run_info_builder: RunInfo::builder(), result: None,
}) })
} }
@@ -70,8 +69,6 @@ impl Engine {
let repo = req.repo(); let repo = req.repo();
let commit = req.commit(); let commit = req.commit();
self.run_info_builder.repo(repo);
self.run_info_builder.commit(commit);
match self.run_helper(repo, commit) { match self.run_helper(repo, commit) {
Ok(true) => success = true, Ok(true) => success = true,
Ok(false) => (), Ok(false) => (),
@@ -121,18 +118,11 @@ impl Engine {
#[allow(clippy::result_large_err)] #[allow(clippy::result_large_err)]
fn finish(&mut self) -> Result<(), EngineError> { fn finish(&mut self) -> Result<(), EngineError> {
// Write response message indicating the run has finished. // Write response message indicating the run has finished.
let ri = self.run_info_builder.build()?; match &self.result {
match &ri.result { Some(RunResult::Success) => write_succeeded()?,
RunResult::Success => write_succeeded()?, Some(RunResult::Failure) => write_failed()?,
RunResult::Failure => write_failed()?, Some(RunResult::Error(s)) => write_errored(s)?,
RunResult::Error(s) => write_errored(s)?, _ => write_errored(&format!("unknown result {:?}", self.result))?,
_ => write_errored(&format!("unknown result {}", ri.result))?,
}
// Persist the run info. If this fails, it's a node admin problem.
if let Err(e) = ri.write() {
self.adminlog
.writeln(&format!("failed to write run info: {}", e))?;
} }
// Log that we've reached the end successfully. // Log that we've reached the end successfully.
@@ -153,10 +143,6 @@ impl Engine {
// Set the file where the run info should be written, now that // Set the file where the run info should be written, now that
// we have the run directory. // we have the run directory.
let run_log_filename = run_dir.join("log.html"); let run_log_filename = run_dir.join("log.html");
self.run_info_builder
.log(&self.config.state, run_log_filename.clone());
self.run_info_builder.run_info(run_dir.join("run.yaml"));
self.run_info_builder.run_id(run_id.clone());
// Get node git storage. We do this before responding to the // Get node git storage. We do this before responding to the
// trigger, so that if this fails, we haven't said that a run // trigger, so that if this fails, we haven't said that a run
@@ -178,16 +164,15 @@ impl Engine {
// succeeded or failed. // succeeded or failed.
let result = run.run(); let result = run.run();
if let Ok(run_log) = result { if let Ok(run_log) = result {
let result = if run_log.all_commands_succeeded() { self.result = if run_log.all_commands_succeeded() {
RunResult::Success Some(RunResult::Success)
} else { } else {
RunResult::Failure Some(RunResult::Failure)
}; };
self.run_info_builder.result(result);
let all = run_log.all_commands_succeeded(); let all = run_log.all_commands_succeeded();
Ok(all) Ok(all)
} else { } else {
self.run_info_builder.result(RunResult::Failure); self.result = Some(RunResult::Failure);
Ok(false) Ok(false)
} }
} }
@@ -238,9 +223,6 @@ pub enum EngineError {
#[error(transparent)] #[error(transparent)]
Message(#[from] NativeMessageError), Message(#[from] NativeMessageError),
#[error(transparent)]
RunInfo(#[from] RunInfoError),
#[error(transparent)] #[error(transparent)]
RunLog(#[from] RunLogError), RunLog(#[from] RunLogError),
-1
View File
@@ -3,6 +3,5 @@ pub mod engine;
pub mod logfile; pub mod logfile;
pub mod msg; pub mod msg;
pub mod run; pub mod run;
pub mod runinfo;
pub mod runlog; pub mod runlog;
pub mod runspec; pub mod runspec;
+1 -4
View File
@@ -2,7 +2,7 @@ use std::path::PathBuf;
use radicle_ci_broker::msg::{MessageError, Request, Response, RunId, RunResult}; use radicle_ci_broker::msg::{MessageError, Request, Response, RunId, RunResult};
use crate::{config::ConfigError, logfile::LogError, runinfo::RunInfoError, runspec::RunSpecError}; use crate::{config::ConfigError, logfile::LogError, runspec::RunSpecError};
/// Read a request from stdin. /// Read a request from stdin.
pub fn read_request() -> Result<Request, NativeMessageError> { pub fn read_request() -> Result<Request, NativeMessageError> {
@@ -72,9 +72,6 @@ pub enum NativeMessageError {
#[error(transparent)] #[error(transparent)]
Log(#[from] LogError), Log(#[from] LogError),
#[error(transparent)]
RunInfo(#[from] RunInfoError),
#[error(transparent)] #[error(transparent)]
RunSpec(#[from] RunSpecError), RunSpec(#[from] RunSpecError),
} }
-4
View File
@@ -7,7 +7,6 @@ use radicle_ci_broker::msg::{Oid, RepoId};
use crate::{ use crate::{
msg::NativeMessageError, msg::NativeMessageError,
runinfo::RunInfoError,
runlog::{RunLog, RunLogError}, runlog::{RunLog, RunLogError},
runspec::{RunSpec, RunSpecError}, runspec::{RunSpec, RunSpecError},
}; };
@@ -220,9 +219,6 @@ pub enum RunError {
#[error(transparent)] #[error(transparent)]
Message(#[from] NativeMessageError), Message(#[from] NativeMessageError),
#[error(transparent)]
RunInfo(#[from] RunInfoError),
#[error(transparent)] #[error(transparent)]
RunLog(#[from] RunLogError), RunLog(#[from] RunLogError),
-122
View File
@@ -1,122 +0,0 @@
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use time::{macros::format_description, OffsetDateTime};
use radicle_ci_broker::msg::{Oid, RepoId, RunId, RunResult};
/// Metadata about a run.
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[allow(dead_code)]
pub struct RunInfo {
/// Repository ID.
pub repo: String,
/// Commit ID.
pub commit: String,
/// Identifier for the CI run.
pub id: String,
/// Result of the CI run.
pub result: RunResult,
/// Name of log file.
pub log: PathBuf,
/// Name of run info file.
#[serde(skip)]
pub run_info: Option<PathBuf>,
/// Timestamp of when the run ended (the value was created).
/// ISO8601 format.
pub timestamp: String,
}
impl RunInfo {
pub fn builder() -> RunInfoBuilder {
RunInfoBuilder::default()
}
pub fn write(&self) -> Result<(), RunInfoError> {
if let Some(filename) = &self.run_info {
let yaml = serde_yaml::to_string(&self).map_err(RunInfoError::SerializeRunInfo)?;
std::fs::write(filename, yaml.as_bytes())
.map_err(|e| RunInfoError::WriteRunInfo(filename.into(), e))?;
}
Ok(())
}
}
#[derive(Debug, Default)]
#[allow(dead_code)]
pub struct RunInfoBuilder {
repo: Option<RepoId>,
commit: Option<Oid>,
run_id: Option<String>,
result: Option<RunResult>,
log: Option<PathBuf>,
run_info: Option<PathBuf>,
}
impl RunInfoBuilder {
pub fn repo(&mut self, repo: RepoId) {
self.repo = Some(repo);
}
pub fn commit(&mut self, commit: Oid) {
self.commit = Some(commit);
}
pub fn run_id(&mut self, id: RunId) {
self.run_id = Some(format!("{}", id));
}
pub fn result(&mut self, result: RunResult) {
self.result = Some(result);
}
pub fn log(&mut self, state: &Path, log: PathBuf) {
self.log = Some(log.strip_prefix(state).unwrap().into());
}
pub fn run_info(&mut self, filename: PathBuf) {
self.run_info = Some(filename);
}
pub fn build(&self) -> Result<RunInfo, RunInfoError> {
let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
let now = OffsetDateTime::now_utc()
.format(fmt)
.map_err(RunInfoError::TimeFormat)?;
Ok(RunInfo {
repo: self.repo.map(|x| x.to_string()).unwrap_or("".into()),
commit: self.commit.map(|x| x.to_string()).unwrap_or("".into()),
id: self.run_id.as_deref().unwrap_or("<unknown>").into(),
result: self
.result
.clone()
.ok_or(RunInfoError::MissingInfo("result".into()))?,
log: self.log.as_deref().unwrap_or(Path::new("<unknown>")).into(),
run_info: self.run_info.clone(),
timestamp: now,
})
}
}
#[derive(Debug, thiserror::Error)]
pub enum RunInfoError {
#[error("programming error: field {0} is not set")]
MissingInfo(String),
#[error("failed to write file for run metadata: {0}")]
WriteRunInfo(PathBuf, #[source] std::io::Error),
#[error("failed to serialize run metadata: programming error")]
SerializeRunInfo(#[source] serde_yaml::Error),
#[error("failed to format current time as a string")]
TimeFormat(#[source] time::error::Format),
}