2017-03-24 15:05:54 +00:00
|
|
|
#include <iostream>
|
2022-07-08 14:13:32 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
2017-03-24 15:05:54 +00:00
|
|
|
|
|
|
|
#include <boost/program_options.hpp>
|
2022-07-08 14:13:32 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2017-04-11 20:17:34 +00:00
|
|
|
#include <Poco/Logger.h>
|
|
|
|
#include <Poco/ConsoleChannel.h>
|
|
|
|
#include <Poco/FormattingChannel.h>
|
|
|
|
#include <Poco/PatternFormatter.h>
|
|
|
|
#include <Poco/AutoPtr.h>
|
2017-03-24 15:05:54 +00:00
|
|
|
#include <Poco/Util/XMLConfiguration.h>
|
|
|
|
|
2017-06-19 20:06:35 +00:00
|
|
|
#include <Common/ZooKeeper/ZooKeeperNodeCache.h>
|
2018-02-28 20:34:25 +00:00
|
|
|
#include <Common/Config/ConfigProcessor.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2022-07-08 14:13:32 +00:00
|
|
|
#include <Common/parseGlobs.h>
|
2024-01-07 22:28:08 +00:00
|
|
|
#include <Common/re2.h>
|
2017-03-24 15:05:54 +00:00
|
|
|
|
2017-04-11 20:17:34 +00:00
|
|
|
static void setupLogging(const std::string & log_level)
|
|
|
|
{
|
|
|
|
Poco::AutoPtr<Poco::ConsoleChannel> channel(new Poco::ConsoleChannel);
|
|
|
|
Poco::AutoPtr<Poco::PatternFormatter> formatter(new Poco::PatternFormatter);
|
|
|
|
formatter->setProperty("pattern", "%L%Y-%m-%d %H:%M:%S.%i <%p> %s: %t");
|
|
|
|
Poco::AutoPtr<Poco::FormattingChannel> formatting_channel(new Poco::FormattingChannel(formatter, channel));
|
|
|
|
Poco::Logger::root().setChannel(formatting_channel);
|
|
|
|
Poco::Logger::root().setLevel(log_level);
|
|
|
|
}
|
|
|
|
|
2022-07-08 14:13:32 +00:00
|
|
|
|
|
|
|
static std::vector<std::string> extactFromConfigAccordingToGlobs(DB::ConfigurationPtr configuration, const std::string & pattern, bool try_get)
|
|
|
|
{
|
|
|
|
auto pattern_prefix = pattern.substr(0, pattern.find_first_of("*?{"));
|
|
|
|
boost::algorithm::trim_if(pattern_prefix, [](char s){ return s == '.'; });
|
|
|
|
|
|
|
|
auto matcher = std::make_unique<re2::RE2>(DB::makeRegexpPatternFromGlobs(pattern));
|
|
|
|
|
|
|
|
std::vector<std::string> result;
|
|
|
|
std::queue<std::string> working_queue;
|
|
|
|
working_queue.emplace(pattern_prefix);
|
|
|
|
|
|
|
|
while (!working_queue.empty())
|
|
|
|
{
|
|
|
|
auto node = working_queue.front();
|
|
|
|
working_queue.pop();
|
|
|
|
|
|
|
|
/// Disclose one more layer
|
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
configuration->keys(node, keys);
|
|
|
|
|
|
|
|
/// This is a leave
|
|
|
|
if (keys.empty())
|
|
|
|
{
|
|
|
|
if (!re2::RE2::FullMatch(node, *matcher))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if (try_get)
|
|
|
|
{
|
|
|
|
auto value = configuration->getString(node, "");
|
|
|
|
if (!value.empty())
|
|
|
|
result.emplace_back(value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.emplace_back(configuration->getString(node));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add new nodes to working queue
|
|
|
|
for (const auto & key : keys)
|
|
|
|
working_queue.emplace(fmt::format("{}.{}", node, key));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::vector<std::string> extractFromConfig(
|
2017-12-15 19:34:19 +00:00
|
|
|
const std::string & config_path, const std::string & key, bool process_zk_includes, bool try_get = false)
|
2017-03-24 15:05:54 +00:00
|
|
|
{
|
2018-11-27 16:11:46 +00:00
|
|
|
DB::ConfigProcessor processor(config_path, /* throw_on_bad_incl = */ false, /* log_to_console = */ false);
|
2017-03-24 15:05:54 +00:00
|
|
|
bool has_zk_includes;
|
2018-11-27 16:11:46 +00:00
|
|
|
DB::XMLDocumentPtr config_xml = processor.processConfig(&has_zk_includes);
|
2017-03-24 15:05:54 +00:00
|
|
|
if (has_zk_includes && process_zk_includes)
|
|
|
|
{
|
2018-11-27 16:11:46 +00:00
|
|
|
DB::ConfigurationPtr bootstrap_configuration(new Poco::Util::XMLConfiguration(config_xml));
|
2022-03-08 08:11:21 +00:00
|
|
|
|
2023-03-23 08:58:56 +00:00
|
|
|
zkutil::validateZooKeeperConfig(*bootstrap_configuration);
|
2022-03-08 08:11:21 +00:00
|
|
|
|
2024-02-15 21:17:31 +00:00
|
|
|
zkutil::ZooKeeperPtr zookeeper = zkutil::ZooKeeper::createWithoutKillingPreviousSessions(
|
|
|
|
*bootstrap_configuration, bootstrap_configuration->has("zookeeper") ? "zookeeper" : "keeper");
|
2022-03-08 08:11:21 +00:00
|
|
|
|
2017-03-24 15:05:54 +00:00
|
|
|
zkutil::ZooKeeperNodeCache zk_node_cache([&] { return zookeeper; });
|
2017-11-21 16:54:25 +00:00
|
|
|
config_xml = processor.processConfig(&has_zk_includes, &zk_node_cache);
|
2017-03-24 15:05:54 +00:00
|
|
|
}
|
2018-11-27 16:11:46 +00:00
|
|
|
DB::ConfigurationPtr configuration(new Poco::Util::XMLConfiguration(config_xml));
|
2022-07-08 14:13:32 +00:00
|
|
|
|
|
|
|
/// Check if a key has globs.
|
|
|
|
if (key.find_first_of("*?{") != std::string::npos)
|
|
|
|
return extactFromConfigAccordingToGlobs(configuration, key, try_get);
|
|
|
|
|
|
|
|
/// Do not throw exception if not found.
|
2017-12-15 19:34:19 +00:00
|
|
|
if (try_get)
|
2022-07-08 14:13:32 +00:00
|
|
|
return {configuration->getString(key, "")};
|
|
|
|
return {configuration->getString(key)};
|
2017-03-24 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 12:54:34 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wunused-function"
|
|
|
|
#pragma clang diagnostic ignored "-Wmissing-declarations"
|
2019-12-15 06:34:43 +00:00
|
|
|
|
2017-03-24 15:05:54 +00:00
|
|
|
int mainEntryClickHouseExtractFromConfig(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
bool print_stacktrace = false;
|
|
|
|
bool process_zk_includes = false;
|
2017-12-15 19:34:19 +00:00
|
|
|
bool try_get = false;
|
2017-04-11 20:17:34 +00:00
|
|
|
std::string log_level;
|
2017-03-24 15:05:54 +00:00
|
|
|
std::string config_path;
|
|
|
|
std::string key;
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
po::options_description options_desc("Allowed options");
|
|
|
|
options_desc.add_options()
|
|
|
|
("help", "produce this help message")
|
|
|
|
("stacktrace", po::bool_switch(&print_stacktrace), "print stack traces of exceptions")
|
|
|
|
("process-zk-includes", po::bool_switch(&process_zk_includes),
|
|
|
|
"if there are from_zk elements in config, connect to ZooKeeper and process them")
|
2017-12-15 19:34:19 +00:00
|
|
|
("try", po::bool_switch(&try_get), "Do not warn about missing keys")
|
2017-04-11 20:17:34 +00:00
|
|
|
("log-level", po::value<std::string>(&log_level)->default_value("error"), "log level")
|
2017-03-24 15:05:54 +00:00
|
|
|
("config-file,c", po::value<std::string>(&config_path)->required(), "path to config file")
|
|
|
|
("key,k", po::value<std::string>(&key)->required(), "key to get value for");
|
|
|
|
|
|
|
|
po::positional_options_description positional_desc;
|
|
|
|
positional_desc.add("config-file", 1);
|
|
|
|
positional_desc.add("key", 1);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
po::variables_map options;
|
|
|
|
po::store(po::command_line_parser(argc, argv).options(options_desc).positional(positional_desc).run(), options);
|
|
|
|
|
|
|
|
if (options.count("help"))
|
|
|
|
{
|
2023-04-20 22:54:34 +00:00
|
|
|
std::cerr << "Preprocess config file and extract value of the given key." << std::endl
|
2017-03-24 15:05:54 +00:00
|
|
|
<< std::endl;
|
2023-04-20 22:54:34 +00:00
|
|
|
std::cerr << "Usage: clickhouse extract-from-config [options]" << std::endl
|
2017-03-24 15:05:54 +00:00
|
|
|
<< std::endl;
|
2023-04-20 22:54:34 +00:00
|
|
|
std::cerr << options_desc << std::endl;
|
2017-03-24 15:05:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
po::notify(options);
|
|
|
|
|
2017-04-11 20:17:34 +00:00
|
|
|
setupLogging(log_level);
|
2022-07-08 14:13:32 +00:00
|
|
|
for (const auto & value : extractFromConfig(config_path, key, process_zk_includes, try_get))
|
|
|
|
std::cout << value << std::endl;
|
2017-03-24 15:05:54 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cerr << DB::getCurrentExceptionMessage(print_stacktrace, true) << std::endl;
|
|
|
|
return DB::getCurrentExceptionCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|