From a53c8a0a63d5cc93a13923fd204e038e00c928f7 Mon Sep 17 00:00:00 2001 From: "Aode (lion)" Date: Mon, 20 Sep 2021 12:49:07 -0500 Subject: [PATCH] Include version info in builds --- Cargo.lock | 1 + Cargo.toml | 1 + src/build.rs | 44 ++++++++++++++++++++++++++++++++++++++++ src/config.rs | 45 +++++++++++++++++++++++++++++++++++++---- src/data/state.rs | 7 +------ src/main.rs | 27 +++++++++++++++++-------- src/routes/nodeinfo.rs | 4 ++-- templates/index.rs.html | 2 +- 8 files changed, 110 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e13f51c..7c5fc83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1969,6 +1969,7 @@ dependencies = [ "sled", "structopt", "thiserror", + "toml", "tracing", "tracing-actix-web", "tracing-error", diff --git a/Cargo.toml b/Cargo.toml index 9d77525..631f1d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -73,6 +73,7 @@ branch = "asonix/tracing-error-work-around" anyhow = "1.0" dotenv = "0.15.0" ructe = { version = "0.13.0", features = ["sass", "mime03"] } +toml = "0.5.8" [profile.dev.package.rsa] opt-level = 3 diff --git a/src/build.rs b/src/build.rs index 4430193..8db87a2 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,8 +1,52 @@ use ructe::Ructe; +use std::{fs::File, io::Read, path::Path, process::Command}; + +fn git_info() { + if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output() { + if output.status.success() { + let git_hash = String::from_utf8_lossy(&output.stdout); + println!("cargo:rustc-env=GIT_HASH={}", git_hash); + } + } + + if let Ok(output) = Command::new("git") + .args(["rev-parse", "--abbrev-ref", "HEAD"]) + .output() + { + if output.status.success() { + let git_branch = String::from_utf8_lossy(&output.stdout); + println!("cargo:rustc-env=GIT_BRANCH={}", git_branch); + } + } +} + +fn version_info() -> Result<(), anyhow::Error> { + let cargo_toml = Path::new(&std::env::var("CARGO_MANIFEST_DIR")?).join("Cargo.toml"); + + let mut file = File::open(&cargo_toml)?; + + let mut cargo_data = String::new(); + file.read_to_string(&mut cargo_data)?; + + let data: toml::Value = toml::from_str(&cargo_data)?; + + if let Some(version) = data["package"]["version"].as_str() { + println!("cargo:rustc-env=PKG_VERSION={}", version); + } + + if let Some(name) = data["package"]["name"].as_str() { + println!("cargo:rustc-env=PKG_NAME={}", name); + } + + Ok(()) +} fn main() -> Result<(), anyhow::Error> { dotenv::dotenv().ok(); + git_info(); + version_info()?; + let mut ructe = Ructe::from_env()?; let mut statics = ructe.statics()?; statics.add_sass_file("scss/index.scss")?; diff --git a/src/config.rs b/src/config.rs index 5b617fd..cf512e8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -144,12 +144,49 @@ impl Config { format!("relay@{}", self.hostname) } - pub(crate) fn software_name(&self) -> String { - "AodeRelay".to_owned() + pub(crate) fn software_name() -> &'static str { + "AodeRelay" } - pub(crate) fn software_version(&self) -> String { - "v0.3.0-main".to_owned() + pub(crate) fn software_version() -> String { + if let Some(git) = Self::git_version() { + return format!("v{}-{}", Self::version(), git); + } + + format!("v{}", Self::version()) + } + + fn git_version() -> Option { + let branch = Self::git_branch()?; + let hash = Self::git_hash()?; + + Some(format!("{}-{}", branch, hash)) + } + + fn name() -> &'static str { + env!("PKG_NAME") + } + + fn version() -> &'static str { + env!("PKG_VERSION") + } + + fn git_branch() -> Option<&'static str> { + option_env!("GIT_BRANCH") + } + + fn git_hash() -> Option<&'static str> { + option_env!("GIT_HASH") + } + + pub(crate) fn user_agent(&self) -> String { + format!( + "{} ({}/{}; +{})", + Self::software_name(), + Self::name(), + Self::software_version(), + self.generate_url(UrlKind::Index), + ) } pub(crate) fn source_code(&self) -> &Url { diff --git a/src/data/state.rs b/src/data/state.rs index e5ed311..373afad 100644 --- a/src/data/state.rs +++ b/src/data/state.rs @@ -48,12 +48,7 @@ impl State { Requests::new( self.config.generate_url(UrlKind::MainKey).to_string(), self.private_key.clone(), - format!( - "Actix Web 4.0.0-beta.9 ({}/{}; +{})", - self.config.software_name(), - self.config.software_version(), - self.config.generate_url(UrlKind::Index), - ), + self.config.user_agent(), self.breakers.clone(), ) } diff --git a/src/main.rs b/src/main.rs index aaa45a5..c565ade 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use activitystreams::url::Url; use actix_web::{web, App, HttpServer}; use opentelemetry::{sdk::Resource, KeyValue}; use opentelemetry_otlp::WithExportConfig; @@ -27,12 +28,10 @@ use self::{ routes::{actor, inbox, index, nodeinfo, nodeinfo_meta, statics}, }; -#[actix_rt::main] -async fn main() -> Result<(), anyhow::Error> { - dotenv::dotenv().ok(); - - let config = Config::build()?; - +fn init_subscriber( + software_name: &'static str, + opentelemetry_url: Option<&Url>, +) -> Result<(), anyhow::Error> { LogTracer::init()?; let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")); @@ -46,12 +45,12 @@ async fn main() -> Result<(), anyhow::Error> { .with(ErrorLayer::default()) .with(format_layer); - if let Some(url) = config.opentelemetry_url() { + if let Some(url) = opentelemetry_url { let tracer = opentelemetry_otlp::new_pipeline() .tracing() .with_trace_config(opentelemetry::sdk::trace::config().with_resource( - Resource::new(vec![KeyValue::new("service.name", config.software_name())]), + Resource::new(vec![KeyValue::new("service.name", software_name)]), )) .with_exporter( opentelemetry_otlp::new_exporter() @@ -68,6 +67,17 @@ async fn main() -> Result<(), anyhow::Error> { tracing::subscriber::set_global_default(subscriber)?; } + Ok(()) +} + +#[actix_rt::main] +async fn main() -> Result<(), anyhow::Error> { + dotenv::dotenv().ok(); + + let config = Config::build()?; + + init_subscriber(Config::software_name(), config.opentelemetry_url())?; + let db = Db::build(&config)?; let args = Args::new(); @@ -133,6 +143,7 @@ async fn main() -> Result<(), anyhow::Error> { .bind(bind_address)? .run() .await?; + Ok(()) } diff --git a/src/routes/nodeinfo.rs b/src/routes/nodeinfo.rs index 89c4621..9a0bcd7 100644 --- a/src/routes/nodeinfo.rs +++ b/src/routes/nodeinfo.rs @@ -31,8 +31,8 @@ pub(crate) async fn route( web::Json(NodeInfo { version: NodeInfoVersion, software: Software { - name: config.software_name().to_lowercase(), - version: config.software_version(), + name: Config::software_name().to_lowercase(), + version: Config::software_version(), }, protocols: vec![Protocol::ActivityPub], services: Services { diff --git a/templates/index.rs.html b/templates/index.rs.html index dee297e..20aa855 100644 --- a/templates/index.rs.html +++ b/templates/index.rs.html @@ -17,7 +17,7 @@
-

@config.software_name()@config.software_version()

+

@Config::software_name()@Config::software_version()

on @config.hostname()