Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version information #177

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
14 changes: 13 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ genrule(
""",
)

genrule(
name = "version_information",
outs = ["include/generated/version_information.hpp"],
cmd = """
echo "#pragma once" > $@
echo "#define PROJECT_NAME \\"everest-framework\\"" >> $@
echo "#define PROJECT_DESCRIPTION \\"\\"" >> $@
echo "#define PROJECT_VERSION \\"\\"" >> $@
echo "#define GIT_VERSION \\"\\"" >> $@
""",
)

cc_library(
name = "framework",
srcs = glob(["lib/**/*.cpp"]),
hdrs = glob(["include/**/*.hpp"]) + [":compile_time_settings"],
hdrs = glob(["include/**/*.hpp"]) + [":compile_time_settings", ":version_information"],
copts = ["-std=c++17"],
strip_include_prefix = "include",
visibility = ["//visibility:public"],
Expand Down
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
cmake_minimum_required(VERSION 3.14)

project(everest-framework
VERSION 0.12.0
VERSION 0.13.0
DESCRIPTION "The open operating system for e-mobility charging stations"
LANGUAGES CXX C
)

find_package(everest-cmake 0.1 REQUIRED
find_package(everest-cmake 0.3 REQUIRED
PATHS ../everest-cmake
)

Expand Down Expand Up @@ -92,6 +92,8 @@ else()
include(find-mqttc)
endif()

evc_generate_version_information()

set(EVEREST_FRAMEWORK_GENERATED_INC_DIR ${PROJECT_BINARY_DIR}/generated)
configure_file(
include/compile_time_settings.hpp.in
Expand Down
15 changes: 14 additions & 1 deletion include/framework/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
const auto TERMINAL_STYLE_ERROR = fmt::emphasis::bold | fg(fmt::terminal_color::red);
const auto TERMINAL_STYLE_OK = fmt::emphasis::bold | fg(fmt::terminal_color::green);
const auto TERMINAL_STYLE_BLUE = fmt::emphasis::bold | fg(fmt::terminal_color::blue);
const auto TERMINAL_STYLE_YELLOW = fmt::emphasis::bold | fg(fmt::terminal_color::yellow);

struct BootException : public std::runtime_error {
using std::runtime_error::runtime_error;
Expand Down Expand Up @@ -120,6 +121,8 @@

std::string run_as_user;

std::string version_information;

Check notice on line 124 in include/framework/runtime.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/framework/runtime.hpp#L124

struct member 'RuntimeSettings::version_information' is never used.

nlohmann::json config;

bool validate_schema;
Expand All @@ -144,17 +147,27 @@
const std::function<void()>& ready);
};

struct VersionInformation {
std::string project_name;

Check notice on line 151 in include/framework/runtime.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/framework/runtime.hpp#L151

struct member 'VersionInformation::project_name' is never used.
std::string project_version;

Check notice on line 152 in include/framework/runtime.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/framework/runtime.hpp#L152

struct member 'VersionInformation::project_version' is never used.
std::string git_version;

Check notice on line 153 in include/framework/runtime.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/framework/runtime.hpp#L153

struct member 'VersionInformation::git_version' is never used.
};

class ModuleLoader {
private:
std::shared_ptr<RuntimeSettings> runtime_settings;
std::string module_id;
std::string original_process_name;
ModuleCallbacks callbacks;
VersionInformation version_information;

Check notice on line 162 in include/framework/runtime.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/framework/runtime.hpp#L162

class member 'ModuleLoader::version_information' is never used.

bool parse_command_line(int argc, char* argv[]);

public:
explicit ModuleLoader(int argc, char* argv[], ModuleCallbacks callbacks);
explicit ModuleLoader(int argc, char* argv[], ModuleCallbacks callbacks) :
ModuleLoader(argc, argv, callbacks, {"undefined project", "undefined version", "undefined git version"}){};
explicit ModuleLoader(int argc, char* argv[], ModuleCallbacks callbacks,
const VersionInformation version_information);

Check warning on line 170 in include/framework/runtime.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/framework/runtime.hpp#L170

Function parameter 'version_information' should be passed by const reference.

int initialize();
};
Expand Down
1 change: 1 addition & 0 deletions include/utils/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
};

std::string name;
std::string version;

Check notice on line 75 in include/utils/types.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/utils/types.hpp#L75

struct member 'ModuleInfo::version' is never used.
std::vector<std::string> authors;
std::string license;
std::string id;
Expand Down
1 change: 1 addition & 0 deletions lib/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ ModuleInfo Config::get_module_info(const std::string& module_id) {
module_info.name = this->main[module_id]["module"].get<std::string>();
module_info.global_errors_enabled = this->manifests.at(module_info.name).at("enable_global_errors");
auto& module_metadata = this->manifests[module_info.name]["metadata"];
module_info.version = module_metadata["version"].get<std::string>();
for (auto& author : module_metadata["authors"]) {
module_info.authors.emplace_back(author.get<std::string>());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/everest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ void Everest::publish_metadata() {

json metadata = json({});
metadata["module"] = module_info.name;
metadata["version"] = module_info.version;
if (manifest.contains("provides")) {
metadata["provides"] = json({});

Expand Down Expand Up @@ -745,7 +746,6 @@ void Everest::telemetry_publish(const std::string& category, const std::string&
void Everest::signal_ready() {
BOOST_LOG_FUNCTION();

// EVLOG_info << "Module " << this->module_id << " initialized.";
const auto ready_topic = fmt::format("{}/ready", this->config.mqtt_module_prefix(this->module_id));

this->mqtt_abstraction.publish(ready_topic, json(true));
Expand Down
30 changes: 27 additions & 3 deletions lib/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <algorithm>
#include <cstdlib>
#include <fstream>

#include <boost/program_options.hpp>

Expand Down Expand Up @@ -365,6 +366,13 @@ RuntimeSettings::RuntimeSettings(const std::string& prefix_, const std::string&
validate_schema = defaults::VALIDATE_SCHEMA;
}
run_as_user = settings.value("run_as_user", "");
auto version_information_path = data_dir / "version_information.txt";
if (fs::exists(version_information_path)) {
std::ifstream ifs(version_information_path.string());
version_information = std::string(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
} else {
version_information = "unknown";
}
}

ModuleCallbacks::ModuleCallbacks(const std::function<void(ModuleAdapter module_adapter)>& register_module_adapter,
Expand All @@ -374,8 +382,9 @@ ModuleCallbacks::ModuleCallbacks(const std::function<void(ModuleAdapter module_a
register_module_adapter(register_module_adapter), everest_register(everest_register), init(init), ready(ready) {
}

ModuleLoader::ModuleLoader(int argc, char* argv[], ModuleCallbacks callbacks) :
runtime_settings(nullptr), callbacks(callbacks) {
ModuleLoader::ModuleLoader(int argc, char* argv[], ModuleCallbacks callbacks,
const VersionInformation version_information) :
runtime_settings(nullptr), callbacks(callbacks), version_information(version_information) {
if (!this->parse_command_line(argc, argv)) {
return;
}
Expand Down Expand Up @@ -523,8 +532,15 @@ int ModuleLoader::initialize() {
everest.signal_ready();

auto end_time = std::chrono::system_clock::now();
std::string version;
if (module_info.version.empty()) {
version = fmt::format(TERMINAL_STYLE_YELLOW, "missing in manifest");
} else {
version = fmt::format(TERMINAL_STYLE_OK, module_info.version);
}
EVLOG_info << "Module " << fmt::format(TERMINAL_STYLE_BLUE, "{}", module_id) << " initialized ["
<< std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() << "ms]";
<< std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() << "ms]"
<< fmt::format(" (version: {})", version);

everest.wait_for_main_loop_end();

Expand All @@ -540,6 +556,7 @@ int ModuleLoader::initialize() {

bool ModuleLoader::parse_command_line(int argc, char* argv[]) {
po::options_description desc("EVerest");
desc.add_options()("version", "Print version and exit");
desc.add_options()("help,h", "produce help message");
desc.add_options()("prefix", po::value<std::string>(), "Set main EVerest directory");
desc.add_options()("module,m", po::value<std::string>(),
Expand All @@ -556,6 +573,13 @@ bool ModuleLoader::parse_command_line(int argc, char* argv[]) {
return false;
}

if (vm.count("version") != 0) {
std::cout << argv[0] << " (" << this->version_information.project_name << " "
<< this->version_information.project_version << " " << this->version_information.git_version << ")"
<< std::endl;
return false;
}

const auto prefix_opt = parse_string_option(vm, "prefix");
const auto config_opt = parse_string_option(vm, "config");
this->runtime_settings = std::make_unique<RuntimeSettings>(prefix_opt, config_opt);
Expand Down
4 changes: 4 additions & 0 deletions schemas/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ properties:
- license
- authors
properties:
version:
description: Version of this module
type: string
default: ""
base_license:
description: URI pointing to the base license of this module (e.g. https://opensource.org/licenses/Apache-2.0)
type: string
Expand Down
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ target_sources(manager
system_unix.cpp
manager.cpp
)
# generate version information header
evc_generate_version_information()
target_include_directories(manager
PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/generated/include
)

target_link_libraries(manager
PRIVATE
Expand Down
11 changes: 11 additions & 0 deletions src/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "controller/ipc.hpp"
#include "system_unix.hpp"
#include <generated/version_information.hpp>

namespace po = boost::program_options;
namespace fs = std::filesystem;
Expand Down Expand Up @@ -491,6 +492,9 @@ int boot(const po::variables_map& vm) {
EVLOG_info << fmt::format(TERMINAL_STYLE_BLUE, " | |____ \\ / __/ | | __/\\__ \\ |_ ");
EVLOG_info << fmt::format(TERMINAL_STYLE_BLUE, " |______| \\/ \\___|_| \\___||___/\\__|");
EVLOG_info << "";
EVLOG_info << PROJECT_NAME << " " << PROJECT_VERSION << " " << GIT_VERSION;
EVLOG_info << rs->version_information;
EVLOG_info << "";

if (rs->mqtt_broker_socket_path.empty()) {
EVLOG_info << "Using MQTT broker " << rs->mqtt_broker_host << ":" << rs->mqtt_broker_port;
Expand Down Expand Up @@ -757,6 +761,7 @@ int boot(const po::variables_map& vm) {

int main(int argc, char* argv[]) {
po::options_description desc("EVerest manager");
desc.add_options()("version", "Print version and exit");
desc.add_options()("help,h", "produce help message");
desc.add_options()("check", "Check and validate all config files and exit (0=success)");
desc.add_options()("dump", po::value<std::string>(),
Expand Down Expand Up @@ -788,6 +793,12 @@ int main(int argc, char* argv[]) {
return EXIT_SUCCESS;
}

if (vm.count("version") != 0) {
std::cout << argv[0] << " (" << PROJECT_NAME << " " << PROJECT_VERSION << " " << GIT_VERSION << ") "
<< std::endl;
return EXIT_SUCCESS;
}

return boot(vm);

} catch (const BootException& e) {
Expand Down