Include version info in builds

This commit is contained in:
Aode (lion) 2021-09-20 12:49:07 -05:00
parent d26ff4a7cb
commit a53c8a0a63
8 changed files with 110 additions and 21 deletions

1
Cargo.lock generated
View file

@ -1969,6 +1969,7 @@ dependencies = [
"sled",
"structopt",
"thiserror",
"toml",
"tracing",
"tracing-actix-web",
"tracing-error",

View file

@ -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

View file

@ -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")?;

View file

@ -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<String> {
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 {

View file

@ -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(),
)
}

View file

@ -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(())
}

View file

@ -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 {

View file

@ -17,7 +17,7 @@
<body>
<header>
<div class="header-text">
<h1>@config.software_name()<span class="smaller">@config.software_version()</span></h1>
<h1>@Config::software_name()<span class="smaller">@Config::software_version()</span></h1>
<p>on @config.hostname()</p>
</div>
</header>