Skip to content

Commit 7f1b50f

Browse files
authored
Revert "[sonic-db-cli] Add flag to make cli calls to container databases (#1070)" and #1111 (#1123)
Due to #1100 To unblock submodule move Revert 2 PRs: cf7c7f6 2025-12-01 | Fix issue where namespace would not be read (#1111) [Connor Roos] 4fea06e 2025-10-07 | [sonic-db-cli] Add flag to make cli calls to container databases (#1070) [Connor Roos]
1 parent 16a8a93 commit 7f1b50f

File tree

10 files changed

+55
-253
lines changed

10 files changed

+55
-253
lines changed

sonic-db-cli/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ using namespace std;
77

88
int main(int argc, char** argv)
99
{
10+
auto initializeGlobalConfig = []()
11+
{
12+
SonicDBConfig::initializeGlobalConfig(SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
13+
};
14+
15+
auto initializeConfig = []()
16+
{
17+
SonicDBConfig::initialize(SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE);
18+
};
19+
1020
return cli_exception_wrapper(
1121
argc,
1222
argv,

sonic-db-cli/sonic-db-cli.cpp

Lines changed: 21 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
11
#include <future>
2-
#include <fstream>
32
#include <iostream>
43
#include <getopt.h>
54
#include <list>
65
#include <boost/algorithm/string.hpp>
76
#include <boost/algorithm/string/find.hpp>
87
#include "common/redisreply.h"
9-
#include <nlohmann/json.hpp>
108
#include "sonic-db-cli.h"
119

1210
using namespace swss;
1311
using namespace std;
1412

15-
void initializeGlobalConfig()
16-
{
17-
SonicDBConfig::initializeGlobalConfig(SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
18-
}
19-
20-
void initializeConfig(const string& container_name = "")
21-
{
22-
if(container_name.empty())
23-
{
24-
SonicDBConfig::initialize(SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE);
25-
}
26-
else
27-
{
28-
auto path = getContainerFilePath(container_name, SONIC_DB_CONFIG_DIR, SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
29-
SonicDBConfig::initialize(path);
30-
}
31-
};
32-
3313
void printUsage()
3414
{
35-
cout << "usage: sonic-db-cli [-h] [-s] [-n NAMESPACE] [-c CONTAINER_NAME] db_or_op [cmd [cmd ...]]" << endl;
15+
cout << "usage: sonic-db-cli [-h] [-s] [-n NAMESPACE] db_or_op [cmd [cmd ...]]" << endl;
3616
cout << endl;
3717
cout << "SONiC DB CLI:" << endl;
3818
cout << endl;
@@ -45,8 +25,6 @@ void printUsage()
4525
cout << " -s, --unixsocket Override use of tcp_port and use unixsocket" << endl;
4626
cout << " -n NAMESPACE, --namespace NAMESPACE" << endl;
4727
cout << " Namespace string to use asic0/asic1.../asicn" << endl;
48-
cout << " -c CONTAINER_NAME, --container_name CONTAINER_NAME" << endl;
49-
cout << " Container name for accessing container database instead of default dpu0/dpu1.../dpu3" << endl;
5028
cout << endl;
5129
cout << "**sudo** needed for commands accesing a different namespace [-n], or using unixsocket connection [-s]" << endl;
5230
cout << endl;
@@ -138,7 +116,7 @@ int handleAllInstances(
138116
{
139117
return 1;
140118
}
141-
119+
142120
if (operation == "PING")
143121
{
144122
cout << "PONG" << endl;
@@ -208,16 +186,15 @@ void parseCliArguments(
208186
Options &options)
209187
{
210188
// Parse argument with getopt https://man7.org/linux/man-pages/man3/getopt.3.html
211-
const char* short_options = "hsn:c:";
189+
const char* short_options = "hsn";
212190
static struct option long_options[] = {
213-
{"help", no_argument, NULL, 'h' },
214-
{"unixsocket", no_argument, NULL, 's' },
215-
{"namespace", required_argument, NULL, 'n' },
216-
{"container_name", required_argument, NULL, 'c' },
191+
{"help", optional_argument, NULL, 'h' },
192+
{"unixsocket", optional_argument, NULL, 's' },
193+
{"namespace", optional_argument, NULL, 'n' },
217194
// The last element of the array has to be filled with zeros.
218195
{0, 0, 0, 0 }
219196
};
220-
197+
221198
// prevent getopt_long print "invalid option" message.
222199
opterr = 0;
223200
while(optind < argc)
@@ -235,40 +212,21 @@ void parseCliArguments(
235212
break;
236213

237214
case 'n':
238-
if (optarg)
215+
if (optind < argc)
239216
{
240-
options.m_namespace = optarg;
217+
options.m_namespace = argv[optind];
218+
optind++;
241219
}
242220
else
243221
{
244222
throw invalid_argument("namespace value is missing.");
245223
}
246224
break;
247225

248-
case 'c':
249-
if (optarg)
250-
{
251-
options.m_container_name = optarg;
252-
}
253-
else
254-
{
255-
throw invalid_argument("container_name value option used but container name is missing.");
256-
}
257-
break;
258-
259226
default:
260227
// argv contains unknown argument
261228
throw invalid_argument("Unknown argument:" + string(argv[optind]));
262229
}
263-
264-
if(!options.m_namespace.empty() && !options.m_container_name.empty())
265-
{
266-
throw invalid_argument("container_name and namespace flags cannot be used together.");
267-
}
268-
else if(options.m_unixsocket && !options.m_container_name.empty())
269-
{
270-
throw invalid_argument("container_name and unixsocket flags cannot be used together.");
271-
}
272230
}
273231
else
274232
{
@@ -290,7 +248,7 @@ int sonic_db_cli(
290248
int argc,
291249
char** argv,
292250
function<void()> initializeGlobalConfig,
293-
function<void(const string&)> initializeConfig)
251+
function<void()> initializeConfig)
294252
{
295253
Options options;
296254
try
@@ -317,9 +275,6 @@ int sonic_db_cli(
317275
return 0;
318276
}
319277

320-
// Need to reset SonicDBConfig to remove information from other database config files
321-
SonicDBConfig::reset();
322-
323278
if (!options.m_db_or_op.empty())
324279
{
325280
auto dbOrOperation = options.m_db_or_op;
@@ -338,7 +293,10 @@ int sonic_db_cli(
338293
{
339294
auto commands = options.m_cmd;
340295

341-
initializeConfig(options.m_container_name);
296+
if (netns.empty())
297+
{
298+
initializeConfig();
299+
}
342300

343301
return executeCommands(dbOrOperation, commands, netns, useUnixSocket);
344302
}
@@ -350,9 +308,11 @@ int sonic_db_cli(
350308
// sonic-db-cli catch all possible exceptions and handle it as a failure case which not return 'OK' or 'PONG'
351309
try
352310
{
353-
354-
// When database_config.json does not exist, sonic-db-cli will ignore exception and return 1.
355-
initializeConfig(options.m_container_name);
311+
if (netns.empty())
312+
{
313+
// When database_config.json does not exist, sonic-db-cli will ignore exception and return 1.
314+
initializeConfig();
315+
}
356316

357317
return handleAllInstances(netns, dbOrOperation, useUnixSocket);
358318
}
@@ -385,7 +345,7 @@ int cli_exception_wrapper(
385345
int argc,
386346
char** argv,
387347
function<void()> initializeGlobalConfig,
388-
function<void(const string&)> initializeConfig)
348+
function<void()> initializeConfig)
389349
{
390350
try
391351
{
@@ -421,31 +381,3 @@ string getCommandName(vector<string>& commands)
421381

422382
return boost::to_upper_copy<string>(commands[0]);
423383
}
424-
425-
string getContainerFilePath(const string& container_name, const string& config_directory, const string& global_config_file)
426-
{
427-
using json = nlohmann::json;
428-
ifstream i(global_config_file);
429-
json global_config = json::parse(i);
430-
for (auto& element : global_config["INCLUDES"])
431-
{
432-
if (element["container_name"] == container_name)
433-
{
434-
auto relative_path = to_string(element["include"]);
435-
436-
// remove the trailing " from the relative path (JSON string quotes)
437-
if (relative_path.front() == '"' && relative_path.back() == '"') {
438-
relative_path = relative_path.substr(1, relative_path.size() - 2);
439-
}
440-
441-
// remove all preceding "../" sequences from the relative path
442-
while (relative_path.substr(0, 3) == "../") {
443-
relative_path = relative_path.substr(3);
444-
}
445-
std::stringstream path_stream;
446-
path_stream << config_directory << "/" << relative_path;
447-
return path_stream.str();
448-
}
449-
}
450-
throw invalid_argument("container name " + container_name + " not found in global config file");
451-
}

sonic-db-cli/sonic-db-cli.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,15 @@
88
#include "common/dbinterface.h"
99
#include "common/redisreply.h"
1010

11-
static constexpr const char *SONIC_DB_CONFIG_DIR = "/var/run";
12-
1311
struct Options
1412
{
1513
bool m_help = false;
1614
bool m_unixsocket = false;
17-
std::string m_container_name = "";
1815
std::string m_namespace;
1916
std::string m_db_or_op;
2017
std::vector<std::string> m_cmd;
2118
};
2219

23-
void initializeGlobalConfig();
24-
25-
void initializeConfig(const std::string& container_name);
26-
2720
void printUsage();
2821

2922
void printRedisReply(swss::RedisReply& reply);
@@ -59,14 +52,12 @@ int sonic_db_cli(
5952
int argc,
6053
char** argv,
6154
std::function<void()> initializeGlobalConfig,
62-
std::function<void(const std::string&)> initializeConfig);
55+
std::function<void()> initializeConfig);
6356

6457
int cli_exception_wrapper(
6558
int argc,
6659
char** argv,
6760
std::function<void()> initializeGlobalConfig,
68-
std::function<void(const std::string&)> initializeConfig);
69-
70-
std::string getCommandName(std::vector<std::string>& command);
61+
std::function<void()> initializeConfig);
7162

72-
std::string getContainerFilePath(const std::string& container_name, const std::string& config_directory, const std::string& global_config_file);
63+
std::string getCommandName(std::vector<std::string>& command);

tests/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ tests_tests_SOURCES = tests/redis_ut.cpp \
3333
tests/saiaclschema_ut.cpp \
3434
tests/countertable_ut.cpp \
3535
tests/timer_ut.cpp \
36-
tests/config_ut.cpp \
3736
tests/cli_ut.cpp \
3837
tests/events_common_ut.cpp \
3938
tests/events_service_ut.cpp \

tests/c_api_ut.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ static char LOG_LEVEL_FOR_TEST[32] = "";
2828
static char LOG_OUTPUT_FOR_TEST[32] = "";
2929
static const char* LOG_NAME_FOR_TEST = "test";
3030

31-
const string config_file = "./tests/redis_multi_db_ut_config/database_config.json";
32-
const string global_config_file = "./tests/redis_multi_db_ut_config/database_global.json";
33-
static void reloadConfig() {
34-
SonicDBConfig::reset();
35-
SonicDBConfig::initializeGlobalConfig(global_config_file);
36-
}
37-
3831
static void clearDB() {
3932
DBConnector db("TEST_DB", 0, true);
4033
RedisReply r(&db, "FLUSHALL", REDIS_REPLY_STATUS);
@@ -106,7 +99,6 @@ struct SWSSStringManager {
10699
};
107100

108101
TEST(c_api, ConfigDBConnector) {
109-
reloadConfig();
110102
clearDB();
111103
SWSSStringManager sm;
112104

@@ -239,7 +231,6 @@ void logOutputNotify(const char* component, const char* outputStr)
239231
}
240232

241233
TEST(c_api, DBConnector) {
242-
reloadConfig();
243234
clearDB();
244235
SWSSStringManager sm;
245236

@@ -308,7 +299,6 @@ TEST(c_api, DBConnector) {
308299
}
309300

310301
TEST(c_api, Table) {
311-
reloadConfig();
312302
clearDB();
313303
SWSSStringManager sm;
314304

@@ -369,7 +359,6 @@ TEST(c_api, Table) {
369359
}
370360

371361
TEST(c_api, ConsumerProducerStateTables) {
372-
reloadConfig();
373362
clearDB();
374363
SWSSStringManager sm;
375364

@@ -456,7 +445,6 @@ TEST(c_api, ConsumerProducerStateTables) {
456445
}
457446

458447
TEST(c_api, SubscriberStateTable) {
459-
reloadConfig();
460448
clearDB();
461449
SWSSStringManager sm;
462450

@@ -498,7 +486,6 @@ TEST(c_api, SubscriberStateTable) {
498486
}
499487

500488
TEST(c_api, ZmqConsumerProducerStateTable) {
501-
reloadConfig();
502489
clearDB();
503490
SWSSStringManager sm;
504491

@@ -628,7 +615,6 @@ TEST(c_api, ZmqConsumerProducerStateTable) {
628615
}
629616

630617
TEST(c_api, EventPublisher) {
631-
reloadConfig();
632618
SWSSStringManager sm;
633619

634620
// Test EventPublisher creation
@@ -698,7 +684,7 @@ TEST(c_api, exceptions) {
698684
}
699685

700686
TEST(c_api, Logger) {
701-
reloadConfig();
687+
702688
clearDB();
703689
SWSSStringManager sm;
704690

tests/cli_test_data/cli_help_output.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usage: sonic-db-cli [-h] [-s] [-n NAMESPACE] [-c CONTAINER_NAME] db_or_op [cmd [cmd ...]]
1+
usage: sonic-db-cli [-h] [-s] [-n NAMESPACE] db_or_op [cmd [cmd ...]]
22

33
SONiC DB CLI:
44

@@ -11,8 +11,6 @@ optional arguments:
1111
-s, --unixsocket Override use of tcp_port and use unixsocket
1212
-n NAMESPACE, --namespace NAMESPACE
1313
Namespace string to use asic0/asic1.../asicn
14-
-c CONTAINER_NAME, --container_name CONTAINER_NAME
15-
Container name for accessing container database instead of default dpu0/dpu1.../dpu3
1614

1715
**sudo** needed for commands accesing a different namespace [-n], or using unixsocket connection [-s]
1816

0 commit comments

Comments
 (0)