Skip to content

Commit 79103ef

Browse files
authored
Merge 8d4ab14 into 9659d35
2 parents 9659d35 + 8d4ab14 commit 79103ef

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

ydb/public/lib/ydb_cli/commands/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SRCS(
1717
ydb_debug.cpp
1818
ydb_dynamic_config.cpp
1919
ydb_latency.cpp
20+
ydb_node_config.cpp
2021
ydb_ping.cpp
2122
ydb_profile.cpp
2223
ydb_root_common.cpp

ydb/public/lib/ydb_cli/commands/ydb_admin.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ydb_admin.h"
22

33
#include "ydb_dynamic_config.h"
4+
#include "ydb_node_config.h"
45
#include "ydb_storage_config.h"
56
#include "ydb_cluster.h"
67

@@ -20,7 +21,10 @@ class TCommandNode : public TClientCommandTree {
2021
public:
2122
TCommandNode()
2223
: TClientCommandTree("node", {}, "Node-wide administration")
23-
{}
24+
{
25+
AddCommand(std::make_unique<NDynamicConfig::TCommandConfig>());
26+
AddCommand(std::make_unique<NNodeConfig::TCommandNodeConfig>());
27+
}
2428
};
2529

2630
class TCommandDatabase : public TClientCommandTree {
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include "ydb_command.h"
4+
5+
namespace NYdb::NConsoleClient::NNodeConfig {
6+
7+
class TCommandNodeConfig : public TClientCommandTree {
8+
public:
9+
TCommandNodeConfig();
10+
};
11+
12+
class TCommandNodeConfigInit : public TYdbCommand {
13+
public:
14+
TCommandNodeConfigInit();
15+
void PropagateFlags(const TCommandFlags& flags) override;
16+
void Config(TConfig& config) override;
17+
int Run(TConfig& config) override;
18+
private:
19+
TString ConfigYamlPath;
20+
TString ConfigDirPath;
21+
TString SeedNodeEndpoint;
22+
};
23+
24+
} // namespace NYdb::NConsoleClient::NNodeConfig

0 commit comments

Comments
 (0)