2019-03-26 07:03:57 +01:00
|
|
|
|
|
|
|
/** Configuration file parsing.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2019-03-26 07:03:57 +01:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
2020-07-10 13:27:25 +02:00
|
|
|
#include <filesystem>
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2019-03-26 07:03:57 +01:00
|
|
|
#include <villas/log.hpp>
|
|
|
|
#include <villas/config.hpp>
|
2019-04-07 15:46:14 +02:00
|
|
|
#include <villas/boxes.hpp>
|
2019-03-26 07:03:57 +01:00
|
|
|
#include <villas/node/exceptions.hpp>
|
|
|
|
#include <villas/config_helper.hpp>
|
|
|
|
|
2020-07-10 13:27:25 +02:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
2019-03-26 07:03:57 +01:00
|
|
|
using namespace villas;
|
|
|
|
using namespace villas::node;
|
|
|
|
|
|
|
|
Config::Config() :
|
2020-09-11 14:57:05 +02:00
|
|
|
logger(logging.get("config")),
|
2019-03-26 07:03:57 +01:00
|
|
|
root(nullptr)
|
2020-09-11 14:57:05 +02:00
|
|
|
{ }
|
2019-03-26 07:03:57 +01:00
|
|
|
|
|
|
|
Config::Config(const std::string &u) :
|
|
|
|
Config()
|
|
|
|
{
|
2020-09-10 17:35:35 +02:00
|
|
|
root = load(u);
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Config::~Config()
|
|
|
|
{
|
|
|
|
if (root)
|
|
|
|
json_decref(root);
|
|
|
|
}
|
|
|
|
|
2020-07-06 13:24:12 +02:00
|
|
|
json_t * Config::load(std::FILE *f, bool resolveInc, bool resolveEnvVars)
|
|
|
|
{
|
2020-09-10 17:35:35 +02:00
|
|
|
json_t *root = decode(f);
|
2020-07-06 13:24:12 +02:00
|
|
|
|
|
|
|
if (resolveInc)
|
|
|
|
root = resolveIncludes(root);
|
|
|
|
|
|
|
|
if (resolveEnvVars)
|
|
|
|
root = expandEnvVars(root);
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_t * Config::load(const std::string &u, bool resolveInc, bool resolveEnvVars)
|
2019-03-26 07:03:57 +01:00
|
|
|
{
|
2020-06-15 22:22:20 +02:00
|
|
|
FILE *f;
|
|
|
|
AFILE *af = nullptr;
|
|
|
|
|
2019-03-26 07:03:57 +01:00
|
|
|
if (u == "-")
|
2020-06-15 22:22:20 +02:00
|
|
|
f = loadFromStdio();
|
2019-03-26 07:03:57 +01:00
|
|
|
else if (isLocalFile(u))
|
2020-06-15 22:22:20 +02:00
|
|
|
f = loadFromLocalFile(u);
|
|
|
|
else {
|
|
|
|
af = loadFromRemoteFile(u);
|
|
|
|
f = af->file;
|
|
|
|
}
|
|
|
|
|
2020-09-10 17:35:35 +02:00
|
|
|
json_t *root = load(f, resolveInc, resolveEnvVars);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
|
|
|
if (af)
|
|
|
|
afclose(af);
|
2019-03-26 07:03:57 +01:00
|
|
|
else
|
2020-06-15 22:22:20 +02:00
|
|
|
fclose(f);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
return root;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
FILE * Config::loadFromStdio()
|
2019-03-26 07:03:57 +01:00
|
|
|
{
|
|
|
|
logger->info("Reading configuration from standard input");
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
return stdin;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
FILE * Config::loadFromLocalFile(const std::string &u)
|
2019-03-26 07:03:57 +01:00
|
|
|
{
|
|
|
|
logger->info("Reading configuration from local file: {}", u);
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
FILE *f = fopen(u.c_str(), "r");
|
|
|
|
if (!f)
|
2019-03-26 07:03:57 +01:00
|
|
|
throw RuntimeError("Failed to open configuration from: {}", u);
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
return f;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
AFILE * Config::loadFromRemoteFile(const std::string &u)
|
2019-03-26 07:03:57 +01:00
|
|
|
{
|
|
|
|
logger->info("Reading configuration from remote URI: {}", u);
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
AFILE *f = afopen(u.c_str(), "r");
|
|
|
|
if (!f)
|
2019-03-26 07:03:57 +01:00
|
|
|
throw RuntimeError("Failed to open configuration from: {}", u);
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
return f;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
json_t * Config::decode(FILE *f)
|
2019-03-26 07:03:57 +01:00
|
|
|
{
|
|
|
|
json_error_t err;
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
json_t *root = json_loadf(f, 0, &err);
|
2019-03-26 07:03:57 +01:00
|
|
|
if (root == nullptr) {
|
2019-04-05 11:01:49 +02:00
|
|
|
#ifdef WITH_CONFIG
|
2019-03-26 07:03:57 +01:00
|
|
|
/* We try again to parse the config in the legacy format */
|
2020-06-15 22:22:20 +02:00
|
|
|
root = libconfigDecode(f);
|
2019-03-26 07:03:57 +01:00
|
|
|
#else
|
|
|
|
throw JanssonParseError(err);
|
2019-04-05 11:01:49 +02:00
|
|
|
#endif /* WITH_CONFIG */
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
2020-06-15 22:22:20 +02:00
|
|
|
|
|
|
|
return root;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 06:49:46 +01:00
|
|
|
std::list<std::filesystem::path> Config::getIncludeDirs(FILE *f) const
|
2021-02-19 06:39:47 +01:00
|
|
|
{
|
|
|
|
auto uri = fs::read_symlink(fs::path("/proc/self/fd") / std::to_string(fileno(f)));
|
|
|
|
if (isLocalFile(uri)) {
|
|
|
|
return {
|
|
|
|
uri.parent_path()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:01:49 +02:00
|
|
|
#ifdef WITH_CONFIG
|
2020-06-15 22:22:20 +02:00
|
|
|
json_t * Config::libconfigDecode(FILE *f)
|
2019-03-26 07:03:57 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
config_t cfg;
|
|
|
|
config_setting_t *cfg_root;
|
|
|
|
config_init(&cfg);
|
|
|
|
config_set_auto_convert(&cfg, 1);
|
|
|
|
|
2020-07-10 13:27:25 +02:00
|
|
|
/* Setup libconfig include path. */
|
2021-02-19 06:49:46 +01:00
|
|
|
auto inclDirs = getIncludeDirs(f);
|
2021-02-19 06:39:47 +01:00
|
|
|
if (inclDirs.size() > 0) {
|
2021-02-19 06:49:46 +01:00
|
|
|
logger->info("Setting include dir to: {}", inclDirs.front());
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2021-02-19 06:49:46 +01:00
|
|
|
config_set_include_dir(&cfg, inclDirs.front().c_str());
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2021-02-19 06:39:47 +01:00
|
|
|
if (inclDirs.size() > 1) {
|
|
|
|
logger->warn("Ignoring all but the first include directories for libconfig");
|
|
|
|
logger->warn(" libconfig does not support more than a single include dir!");
|
|
|
|
}
|
2020-07-10 13:27:25 +02:00
|
|
|
}
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
/* Rewind before re-reading */
|
|
|
|
rewind(f);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
ret = config_read(&cfg, f);
|
2019-03-26 07:03:57 +01:00
|
|
|
if (ret != CONFIG_TRUE)
|
|
|
|
throw LibconfigParseError(&cfg);
|
|
|
|
|
|
|
|
cfg_root = config_root_setting(&cfg);
|
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
json_t *root = config_to_json(cfg_root);
|
|
|
|
if (!root)
|
2019-03-26 07:03:57 +01:00
|
|
|
throw RuntimeError("Failed to convert JSON to configuration file");
|
|
|
|
|
|
|
|
config_destroy(&cfg);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
|
|
|
return root;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
2019-04-05 11:01:49 +02:00
|
|
|
#endif /* WITH_CONFIG */
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2020-09-10 17:35:35 +02:00
|
|
|
json_t * Config::walkStrings(json_t *root, str_walk_fcn_t cb)
|
2019-03-26 07:03:57 +01:00
|
|
|
{
|
2020-06-15 22:22:20 +02:00
|
|
|
const char *key;
|
|
|
|
size_t index;
|
|
|
|
json_t *val, *new_val, *new_root;
|
|
|
|
|
2020-09-10 17:35:35 +02:00
|
|
|
switch (json_typeof(root)) {
|
2020-06-15 22:22:20 +02:00
|
|
|
case JSON_STRING:
|
2020-09-10 17:35:35 +02:00
|
|
|
return cb(root);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
|
|
|
case JSON_OBJECT:
|
|
|
|
new_root = json_object();
|
|
|
|
|
2020-09-10 17:35:35 +02:00
|
|
|
json_object_foreach(root, key, val) {
|
2020-06-15 22:22:20 +02:00
|
|
|
new_val = walkStrings(val, cb);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2020-09-10 17:35:35 +02:00
|
|
|
json_object_set_new(new_root, key, new_val);
|
2020-06-15 22:22:20 +02:00
|
|
|
}
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
return new_root;
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2020-06-15 22:22:20 +02:00
|
|
|
case JSON_ARRAY:
|
|
|
|
new_root = json_array();
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2020-09-10 17:35:35 +02:00
|
|
|
json_array_foreach(root, index, val) {
|
2020-06-15 22:22:20 +02:00
|
|
|
new_val = walkStrings(val, cb);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2020-09-10 17:35:35 +02:00
|
|
|
json_array_append_new(new_root, new_val);
|
2020-06-15 22:22:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new_root;
|
|
|
|
|
|
|
|
default:
|
2020-09-10 17:35:35 +02:00
|
|
|
return root;
|
2020-06-15 22:22:20 +02:00
|
|
|
};
|
|
|
|
}
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2020-07-06 13:24:12 +02:00
|
|
|
json_t * Config::expandEnvVars(json_t *in)
|
2020-06-15 22:22:20 +02:00
|
|
|
{
|
|
|
|
static const std::regex env_re{R"--(\$\{([^}]+)\})--"};
|
|
|
|
|
2020-07-06 13:24:12 +02:00
|
|
|
return walkStrings(in, [this](json_t *str) -> json_t * {
|
2020-06-15 22:22:20 +02:00
|
|
|
std::string text = json_string_value(str);
|
|
|
|
|
|
|
|
std::smatch match;
|
|
|
|
while (std::regex_search(text, match, env_re)) {
|
|
|
|
auto const from = match[0];
|
|
|
|
auto const var_name = match[1].str().c_str();
|
2020-07-03 12:00:45 +02:00
|
|
|
char *var_value = std::getenv(var_name);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2020-07-03 12:00:45 +02:00
|
|
|
text.replace(from.first - text.begin(), from.second - from.first, var_value);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
|
|
|
logger->debug("Replace env var {} in \"{}\" with value \"{}\"",
|
|
|
|
var_name, text, var_value);
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
2020-06-15 22:22:20 +02:00
|
|
|
|
|
|
|
return json_string(text.c_str());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-06 13:24:12 +02:00
|
|
|
json_t * Config::resolveIncludes(json_t *in)
|
2020-06-15 22:22:20 +02:00
|
|
|
{
|
2020-07-06 13:24:12 +02:00
|
|
|
return walkStrings(in, [this](json_t *str) -> json_t * {
|
2020-06-15 22:22:20 +02:00
|
|
|
std::string text = json_string_value(str);
|
|
|
|
static const std::string kw = "@include ";
|
|
|
|
|
|
|
|
if (text.find(kw) != 0)
|
|
|
|
return str;
|
|
|
|
else {
|
|
|
|
std::string path = text.substr(kw.size());
|
|
|
|
|
2020-08-17 17:07:20 +02:00
|
|
|
json_t *incl = load(path);
|
2020-06-15 22:22:20 +02:00
|
|
|
if (!incl)
|
2020-08-17 17:07:20 +02:00
|
|
|
throw ConfigError(str, "Failed to include config file from {}", path);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
|
|
|
logger->debug("Included config from: {}", path);
|
|
|
|
|
|
|
|
return incl;
|
|
|
|
}
|
|
|
|
});
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|