|
| 1 | +#include "ydb_node_config.h" |
| 2 | +#include <util/system/fs.h> |
| 3 | +#include <util/folder/dirut.h> |
| 4 | +#include <ydb-cpp-sdk/client/config/config.h> |
| 5 | + |
| 6 | +namespace NYdb::NConsoleClient::NNodeConfig { |
| 7 | + |
| 8 | +constexpr const char* CONFIG_FILE_NAME = "config.yaml"; |
| 9 | +constexpr const char* MAIN_CONFIG_FILE_NAME = "main.yaml"; |
| 10 | +constexpr const char* STORAGE_CONFIG_FILE_NAME = "storage.yaml"; |
| 11 | + |
| 12 | +bool SaveConfig(const TString& config, const TString& configName, const TString& configDirPath) { |
| 13 | + try { |
| 14 | + TString tempPath = TStringBuilder() << configDirPath << "/temp_" << configName; |
| 15 | + TString configPath = TStringBuilder() << configDirPath << "/" << configName; |
| 16 | + |
| 17 | + { |
| 18 | + TFileOutput tempFile(tempPath); |
| 19 | + tempFile << config; |
| 20 | + tempFile.Flush(); |
| 21 | + } |
| 22 | + |
| 23 | + if (!NFs::Rename(tempPath, configPath)) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + } catch (const std::exception& e) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + return true; |
| 30 | +} |
| 31 | + |
| 32 | +TCommandNodeConfig::TCommandNodeConfig() |
| 33 | + : TClientCommandTree("config", {}, "Node-wide configuration") |
| 34 | + { |
| 35 | + AddCommand(std::make_unique<TCommandNodeConfigInit>()); |
| 36 | +} |
| 37 | + |
| 38 | +TCommandNodeConfigInit::TCommandNodeConfigInit() |
| 39 | + : TYdbCommand("init", {}, "Initialize node configuration") |
| 40 | +{ |
| 41 | +} |
| 42 | + |
| 43 | +void TCommandNodeConfigInit::PropagateFlags(const TCommandFlags& flags) { |
| 44 | + TYdbCommand::PropagateFlags(flags); |
| 45 | + Dangerous = false; |
| 46 | +} |
| 47 | + |
| 48 | +void TCommandNodeConfigInit::Config(TConfig& config) { |
| 49 | + TYdbCommand::Config(config); |
| 50 | + config.Opts->AddLongOption('f', "from-config", "Path to the initial configuration file.") |
| 51 | + .RequiredArgument("[config.yaml]").StoreResult(&ConfigYamlPath); |
| 52 | + config.Opts->AddLongOption('d', "config-dir", "Path to the directory for storing configuration files.") |
| 53 | + .Required().RequiredArgument("[directory]").StoreResult(&ConfigDirPath); |
| 54 | + config.Opts->AddLongOption('s', "seed-node", "Endpoint of the seed node from which the configuration will be fetched.") |
| 55 | + .RequiredArgument("[HOST:PORT]").StoreResult(&SeedNodeEndpoint); |
| 56 | + config.Opts->MutuallyExclusive("from-config", "seed-node"); |
| 57 | + config.SetFreeArgsNum(0); |
| 58 | + config.AllowEmptyDatabase = true; |
| 59 | + config.AllowEmptyAddress = true; |
| 60 | +} |
| 61 | + |
| 62 | +int TCommandNodeConfigInit::Run(TConfig& config) { |
| 63 | + |
| 64 | + MakePathIfNotExist(ConfigDirPath.c_str()); |
| 65 | + TString configYaml; |
| 66 | + |
| 67 | + if (SeedNodeEndpoint) { |
| 68 | + config.Address = SeedNodeEndpoint; |
| 69 | + auto driver = std::make_unique<NYdb::TDriver>(CreateDriver(config)); |
| 70 | + auto client = NYdb::NConfig::TConfigClient(*driver); |
| 71 | + |
| 72 | + auto result = client.FetchAllConfigs().GetValueSync(); |
| 73 | + NStatusHelpers::ThrowOnErrorOrPrintIssues(result); |
| 74 | + TString clusterConfig; |
| 75 | + TString storageConfig; |
| 76 | + for (const auto& entry : result.GetConfigs()) { |
| 77 | + std::visit([&](auto&& arg) { |
| 78 | + using T = std::decay_t<decltype(arg)>; |
| 79 | + if constexpr (std::is_same_v<T, NYdb::NConfig::TMainConfigIdentity>) { |
| 80 | + clusterConfig = entry.Config; |
| 81 | + } else if constexpr (std::is_same_v<T, NYdb::NConfig::TStorageConfigIdentity>) { |
| 82 | + storageConfig = entry.Config; |
| 83 | + } |
| 84 | + }, entry.Identity); |
| 85 | + } |
| 86 | + if (!clusterConfig.empty() && !storageConfig.empty()) { |
| 87 | + if (SaveConfig(clusterConfig, MAIN_CONFIG_FILE_NAME, ConfigDirPath) && |
| 88 | + SaveConfig(storageConfig, STORAGE_CONFIG_FILE_NAME, ConfigDirPath)) { |
| 89 | + Cout << "Initialized cluster and storage configs in " << ConfigDirPath << "/" << MAIN_CONFIG_FILE_NAME << " and " << STORAGE_CONFIG_FILE_NAME << Endl; |
| 90 | + return EXIT_SUCCESS; |
| 91 | + } else { |
| 92 | + Cout << "Failed to initialize cluster config in " << ConfigDirPath << "/" << MAIN_CONFIG_FILE_NAME << Endl; |
| 93 | + return EXIT_FAILURE; |
| 94 | + } |
| 95 | + } |
| 96 | + else if (!clusterConfig.empty() && storageConfig.empty()) { |
| 97 | + configYaml = clusterConfig; |
| 98 | + } |
| 99 | + else { |
| 100 | + Cout << "No main config found on the seed node" << Endl; |
| 101 | + return EXIT_FAILURE; |
| 102 | + } |
| 103 | + } |
| 104 | + else { |
| 105 | + configYaml = TFileInput(ConfigYamlPath).ReadAll(); |
| 106 | + } |
| 107 | + if (SaveConfig(configYaml, CONFIG_FILE_NAME, ConfigDirPath)){ |
| 108 | + Cout << "Initialized cluster config in " << ConfigDirPath << "/" << CONFIG_FILE_NAME << Endl; |
| 109 | + } else { |
| 110 | + Cout << "Failed to initialize cluster config in " << ConfigDirPath << "/" << CONFIG_FILE_NAME << Endl; |
| 111 | + return EXIT_FAILURE; |
| 112 | + } |
| 113 | + return EXIT_SUCCESS; |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +} // namespace NYdb::NConsoleClient::NNodeConfig |
0 commit comments