Skip to content

Commit

Permalink
Include commit date in NetworkNode metadata
Browse files Browse the repository at this point in the history
not provided by built, so it needs to be retrieved manually
  • Loading branch information
internet-catte authored and spb committed Jul 2, 2024
1 parent a4432bc commit 9ce3408
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 37 deletions.
121 changes: 84 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions sable_network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ debug = []

[build-dependencies]
built = { version = "0.5", features = [ "git2" ] }
git2 = { version = "0.15", default-features = false }
chrono = "0.4"

[dev-dependencies]
tracing-subscriber = "0.3"
Expand Down
42 changes: 42 additions & 0 deletions sable_network/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
use std::{env, fs, io::Write, path};

use chrono::DateTime;

fn main() {
built::write_built_file().expect("Failed to acquire build-time information");

let root = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR");
let out = path::Path::new(&env::var("OUT_DIR").expect("OUT_DIR not set")).join("built.rs");
let _ = write_head_date(root, out);
}

fn write_head_date(root: String, dest: path::PathBuf) -> Result<(), git2::Error> {
let mut f = fs::OpenOptions::new()
.append(true)
.open(dest)
.expect("could not append to built.rs");
match git2::Repository::discover(root) {
Ok(repo) => {
let head = repo.head()?.peel_to_commit()?.time();
let head_secs = head.seconds() + (head.offset_minutes() as i64 * 60);
let head_time = DateTime::from_timestamp(head_secs, 0).map(|dt| dt.to_rfc2822());
f.write_all(
format!(
"\npub const GIT_COMMIT_TIME_UTC: Option<&str> = {:?};\n",
head_time
)
.as_bytes(),
)
.expect("could not write to built.rs");
Ok(())
}
Err(ref e)
if e.class() == git2::ErrorClass::Repository
&& e.code() == git2::ErrorCode::NotFound =>
{
f.write_all(
format!("\npub const GIT_COMMIT_TIME_UTC: Option<&str> = None;\n").as_bytes(),
)
.expect("could not write to built.rs");
Ok(())
}
Err(e) => Err(e),
}
}
13 changes: 13 additions & 0 deletions sable_network/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ where
my_id: ServerId,
name: ServerName,
version: String,
commit_date: String,
net: RwLock<Arc<Network>>,
event_log: Arc<ReplicatedEventLog>,
epoch: EpochId,
Expand Down Expand Up @@ -89,6 +90,7 @@ impl<Policy: crate::policy::PolicyService> NetworkNode<Policy> {
my_id: id,
name,
version: Self::build_version(),
commit_date: Self::get_commit_date(),
net: RwLock::new(Arc::new(net)),
epoch,
event_log,
Expand Down Expand Up @@ -193,6 +195,17 @@ impl<Policy: crate::policy::PolicyService> NetworkNode<Policy> {
)
}

/// The server's HEAD commit date
pub fn commit_date(&self) -> &str {
&self.commit_date
}

fn get_commit_date() -> String {
crate::build_data::GIT_COMMIT_TIME_UTC
.unwrap_or("unknown")
.to_string()
}

/// The server's build flags
pub fn server_flags(&self) -> state::ServerFlags {
let mut ret = state::ServerFlags::empty();
Expand Down
1 change: 1 addition & 0 deletions sable_network/src/node/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl<Policy: PolicyService + Saveable> NetworkNode<Policy> {
my_id: state.id,
name: state.name,
version: Self::build_version(),
commit_date: Self::get_commit_date(),
net: RwLock::new(Arc::new(state.net)),
epoch: state.epoch,
id_generator: state.id_generator,
Expand Down

0 comments on commit 9ce3408

Please sign in to comment.