diff --git a/Cargo.lock b/Cargo.lock index 9e86922..be63d90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -825,6 +825,7 @@ dependencies = [ name = "feos-utils" version = "0.5.0" dependencies = [ + "cc", "chrono", "dhcproto", "futures", diff --git a/Makefile b/Makefile index 8ecc74e..80a65b5 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ all: clippy cargo build --target=x86_64-unknown-linux-musl --all release: clippy - cargo build --release --target=x86_64-unknown-linux-musl --all + cargo build --release --features git-version --target=x86_64-unknown-linux-musl --all run: all sudo ./target/debug/feos --ipam $(IPAM) diff --git a/feos/Cargo.toml b/feos/Cargo.toml index ef88b04..1db77c0 100644 --- a/feos/Cargo.toml +++ b/feos/Cargo.toml @@ -56,4 +56,7 @@ prost = { workspace = true } hyper-util = { workspace = true } once_cell = { workspace = true } tower = { workspace = true } -tempfile = { workspace = true } \ No newline at end of file +tempfile = { workspace = true } + +[features] +git-version = ["feos-utils/git-version"] \ No newline at end of file diff --git a/feos/services/host-service/src/worker/info.rs b/feos/services/host-service/src/worker/info.rs index c4b5236..bf2d950 100644 --- a/feos/services/host-service/src/worker/info.rs +++ b/feos/services/host-service/src/worker/info.rs @@ -325,7 +325,7 @@ pub async fn handle_get_version_info( let result = fs::read_to_string(path) .await .map(|kernel_version| { - let feos_version = env!("CARGO_PKG_VERSION").to_string(); + let feos_version = feos_utils::version::full_version_string(); GetVersionInfoResponse { kernel_version: kernel_version.trim().to_string(), feos_version, diff --git a/feos/src/lib.rs b/feos/src/lib.rs index cb6f625..556ba37 100644 --- a/feos/src/lib.rs +++ b/feos/src/lib.rs @@ -9,7 +9,6 @@ use image_service::IMAGE_SERVICE_SOCKET; use log::{error, info, warn}; use nix::unistd::Uid; use setup::*; -use std::env; use tokio::{fs, net::UnixListener, sync::mpsc}; use tokio_stream::wrappers::UnixListenerStream; use tonic::transport::Server; @@ -25,7 +24,7 @@ pub async fn run_server(restarted_after_upgrade: bool) -> Result<()> { ╚═╝ ╚══════╝ ╚═════╝ ╚══════╝ v{} ", - env!("CARGO_PKG_VERSION") + feos_utils::version::full_version_string() ); let log_handle = feos_utils::feos_logger::Builder::new() diff --git a/feos/utils/Cargo.toml b/feos/utils/Cargo.toml index 89fa0ce..a186829 100644 --- a/feos/utils/Cargo.toml +++ b/feos/utils/Cargo.toml @@ -15,4 +15,11 @@ netlink-packet-route = { workspace = true } pnet = { workspace = true } rtnetlink = { workspace = true } socket2 = { workspace = true } -libc = { workspace = true } \ No newline at end of file +libc = { workspace = true } + +[build-dependencies] +cc = "1.0" + +[features] +default = [] +git-version = [] \ No newline at end of file diff --git a/feos/utils/build.rs b/feos/utils/build.rs new file mode 100644 index 0000000..fcb16a4 --- /dev/null +++ b/feos/utils/build.rs @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-License-Identifier: Apache-2.0 + +use std::process::Command; + +fn main() { + println!("cargo:rerun-if-changed=.git/HEAD"); + + let output = Command::new("git") + .args(["rev-parse", "--short", "HEAD"]) + .output(); + + if let Ok(output) = output { + if output.status.success() { + let commit_hash = String::from_utf8_lossy(&output.stdout).trim().to_string(); + println!("cargo:rustc-env=GIT_COMMIT_HASH={commit_hash}"); + } + } +} diff --git a/feos/utils/src/lib.rs b/feos/utils/src/lib.rs index dd82dcd..8842808 100644 --- a/feos/utils/src/lib.rs +++ b/feos/utils/src/lib.rs @@ -5,3 +5,4 @@ pub mod feos_logger; pub mod filesystem; pub mod host; pub mod network; +pub mod version; diff --git a/feos/utils/src/version.rs b/feos/utils/src/version.rs new file mode 100644 index 0000000..ec16fca --- /dev/null +++ b/feos/utils/src/version.rs @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-License-Identifier: Apache-2.0 + +#[cfg(not(feature = "git-version"))] +pub fn full_version_string() -> String { + env!("CARGO_PKG_VERSION").to_string() +} + +#[cfg(feature = "git-version")] +pub fn full_version_string() -> String { + let version = env!("CARGO_PKG_VERSION"); + let commit_hash = option_env!("GIT_COMMIT_HASH").unwrap_or("???????"); + format!("{} ({})", version, commit_hash) +}