From 72a47d6580023e42447711b103803f843e4d8cff Mon Sep 17 00:00:00 2001 From: benoit-nexthop Date: Fri, 14 Nov 2025 01:05:03 +0100 Subject: [PATCH] Add a simple `fboss2 config reload` command. This command just calls `reloadConfig()` on the wedge_agent. --- cmake/CliFboss2.cmake | 28 +++++++++- cmake/CliFboss2Test.cmake | 2 + fboss/cli/fboss2/BUCK | 40 +++++++++++++ fboss/cli/fboss2/CmdHandlerImplConfig.cpp | 19 +++++++ fboss/cli/fboss2/CmdListConfig.cpp | 30 ++++++++++ fboss/cli/fboss2/CmdSubcommands.cpp | 2 + .../commands/config/CmdConfigReload.cpp | 32 +++++++++++ .../fboss2/commands/config/CmdConfigReload.h | 39 +++++++++++++ fboss/cli/fboss2/oss/CmdListConfig.cpp | 27 +++++++++ fboss/cli/fboss2/test/BUCK | 1 + fboss/cli/fboss2/test/CmdConfigReloadTest.cpp | 56 +++++++++++++++++++ 11 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 fboss/cli/fboss2/CmdHandlerImplConfig.cpp create mode 100644 fboss/cli/fboss2/CmdListConfig.cpp create mode 100644 fboss/cli/fboss2/commands/config/CmdConfigReload.cpp create mode 100644 fboss/cli/fboss2/commands/config/CmdConfigReload.h create mode 100644 fboss/cli/fboss2/oss/CmdListConfig.cpp create mode 100644 fboss/cli/fboss2/test/CmdConfigReloadTest.cpp diff --git a/cmake/CliFboss2.cmake b/cmake/CliFboss2.cmake index fd6a5a4d62de2..b11c987c579c7 100644 --- a/cmake/CliFboss2.cmake +++ b/cmake/CliFboss2.cmake @@ -472,7 +472,6 @@ add_library(fboss2_lib fboss/cli/fboss2/commands/stop/pcap/CmdStopPcap.h fboss/cli/fboss2/CmdSubcommands.cpp fboss/cli/fboss2/oss/CmdGlobalOptions.cpp - fboss/cli/fboss2/oss/CmdList.cpp fboss/cli/fboss2/utils/CmdUtils.cpp fboss/cli/fboss2/utils/CLIParserUtils.cpp fboss/cli/fboss2/utils/CmdClientUtils.cpp @@ -559,6 +558,7 @@ target_link_libraries(fboss2_lib add_executable(fboss2 fboss/cli/fboss2/Main.cpp + fboss/cli/fboss2/oss/CmdList.cpp ) target_link_libraries(fboss2 @@ -567,3 +567,29 @@ target_link_libraries(fboss2 ) install(TARGETS fboss2) + +# Config commands library for fboss2-dev +add_library(fboss2_config_lib + fboss/cli/fboss2/commands/config/CmdConfigReload.h + fboss/cli/fboss2/commands/config/CmdConfigReload.cpp + fboss/cli/fboss2/CmdListConfig.cpp + fboss/cli/fboss2/CmdHandlerImplConfig.cpp +) + +target_link_libraries(fboss2_config_lib + fboss2_lib + agent_dir_util +) + +add_executable(fboss2-dev + fboss/cli/fboss2/Main.cpp + fboss/cli/fboss2/oss/CmdListConfig.cpp +) + +target_link_libraries(fboss2-dev + fboss2_config_lib + fboss2_lib + Folly::folly +) + +install(TARGETS fboss2-dev) diff --git a/cmake/CliFboss2Test.cmake b/cmake/CliFboss2Test.cmake index 78ea5b271ff4a..b694f8971a219 100644 --- a/cmake/CliFboss2Test.cmake +++ b/cmake/CliFboss2Test.cmake @@ -3,6 +3,7 @@ # cmd_test - Command tests from BUCK file add_executable(fboss2_cmd_test fboss/cli/fboss2/test/TestMain.cpp + fboss/cli/fboss2/test/CmdConfigReloadTest.cpp fboss/cli/fboss2/test/CmdSetPortStateTest.cpp fboss/cli/fboss2/test/CmdShowAclTest.cpp fboss/cli/fboss2/test/CmdShowAgentSslTest.cpp @@ -39,6 +40,7 @@ add_executable(fboss2_cmd_test target_link_libraries(fboss2_cmd_test fboss2_lib + fboss2_config_lib ${GTEST} ${LIBGMOCK_LIBRARIES} Folly::folly diff --git a/fboss/cli/fboss2/BUCK b/fboss/cli/fboss2/BUCK index 558f2f688fca9..c692937d27739 100644 --- a/fboss/cli/fboss2/BUCK +++ b/fboss/cli/fboss2/BUCK @@ -758,6 +758,46 @@ auto_tab_complete( src = ":fboss2", ) +# Config commands are in a separate library/binary for now. +cpp_library( + name = "fboss2-config-lib", + srcs = [ + "CmdHandlerImplConfig.cpp", + "CmdListConfig.cpp", + "commands/config/CmdConfigReload.cpp", + ], + headers = [ + "commands/config/CmdConfigReload.h", + ], + exported_deps = [ + ":cmd-handler", + ":cmd-list-header", + ":fboss2-lib", + "//fboss/agent:agent_dir_util", + ], +) + +cpp_binary( + name = "fboss2-dev", + srcs = [ + "Main.cpp", + "oss/CmdListConfig.cpp", + ], + deps = [ + ":cmd-common-utils", + ":cmd-global-options", + ":cmd-subcommands", + ":fboss2-config-lib", + ":fboss2-lib", # @manual + "//folly/init:init", + "//folly/logging:init", + "//folly/logging:logging", + ], + external_deps = [ + "CLI11", + ], +) + cpp_binary( name = "fboss2-routing-protocol", srcs = [ diff --git a/fboss/cli/fboss2/CmdHandlerImplConfig.cpp b/fboss/cli/fboss2/CmdHandlerImplConfig.cpp new file mode 100644 index 0000000000000..b17fafd022595 --- /dev/null +++ b/fboss/cli/fboss2/CmdHandlerImplConfig.cpp @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2004-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "fboss/cli/fboss2/CmdHandler.cpp" + +#include "fboss/cli/fboss2/commands/config/CmdConfigReload.h" + +namespace facebook::fboss { + +template void CmdHandler::run(); + +} // namespace facebook::fboss diff --git a/fboss/cli/fboss2/CmdListConfig.cpp b/fboss/cli/fboss2/CmdListConfig.cpp new file mode 100644 index 0000000000000..6e6e5f0854645 --- /dev/null +++ b/fboss/cli/fboss2/CmdListConfig.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2004-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "fboss/cli/fboss2/CmdList.h" + +#include "fboss/cli/fboss2/CmdHandler.h" +#include "fboss/cli/fboss2/commands/config/CmdConfigReload.h" + +namespace facebook::fboss { + +const CommandTree& kConfigCommandTree() { + static CommandTree root = { + {"config", + "reload", + "Reload agent configuration", + commandHandler, + argTypeHandler}, + }; + sort(root.begin(), root.end()); + return root; +} + +} // namespace facebook::fboss diff --git a/fboss/cli/fboss2/CmdSubcommands.cpp b/fboss/cli/fboss2/CmdSubcommands.cpp index c1ba001451863..87485bcd711c5 100644 --- a/fboss/cli/fboss2/CmdSubcommands.cpp +++ b/fboss/cli/fboss2/CmdSubcommands.cpp @@ -34,6 +34,8 @@ const std::map& kSupportedVerbs() { {"stop", "Stop event"}, {"get", "Get object"}, {"reload", "Reload object"}, + // Only implemented in fboss2-dev for now. + {"config", "Configuration commands"}, }; return supportedVerbs; diff --git a/fboss/cli/fboss2/commands/config/CmdConfigReload.cpp b/fboss/cli/fboss2/commands/config/CmdConfigReload.cpp new file mode 100644 index 0000000000000..8ac4ddd0876a7 --- /dev/null +++ b/fboss/cli/fboss2/commands/config/CmdConfigReload.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2004-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "fboss/cli/fboss2/commands/config/CmdConfigReload.h" + +namespace facebook::fboss { + +CmdConfigReloadTraits::RetType CmdConfigReload::queryClient( + const HostInfo& hostInfo) { + auto client = + utils::createClient(hostInfo); + + try { + client->sync_reloadConfig(); + return "Config reloaded successfully"; + } catch (const std::exception& ex) { + return "Failed to reload config: " + std::string(ex.what()); + } +} + +void CmdConfigReload::printOutput(const RetType& logMsg) { + std::cout << logMsg << std::endl; +} + +} // namespace facebook::fboss diff --git a/fboss/cli/fboss2/commands/config/CmdConfigReload.h b/fboss/cli/fboss2/commands/config/CmdConfigReload.h new file mode 100644 index 0000000000000..87631afa1f98e --- /dev/null +++ b/fboss/cli/fboss2/commands/config/CmdConfigReload.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2004-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include +#include +#include "fboss/cli/fboss2/CmdHandler.h" +#include "fboss/cli/fboss2/utils/CmdClientUtils.h" +#include "fboss/cli/fboss2/utils/CmdUtils.h" + +namespace facebook::fboss { + +struct CmdConfigReloadTraits : public WriteCommandTraits { + static constexpr utils::ObjectArgTypeId ObjectArgTypeId = + utils::ObjectArgTypeId::OBJECT_ARG_TYPE_ID_NONE; + using ObjectArgType = std::monostate; + using RetType = std::string; +}; + +class CmdConfigReload + : public CmdHandler { + public: + using ObjectArgType = CmdConfigReloadTraits::ObjectArgType; + using RetType = CmdConfigReloadTraits::RetType; + + RetType queryClient(const HostInfo& hostInfo); + + void printOutput(const RetType& logMsg); +}; + +} // namespace facebook::fboss diff --git a/fboss/cli/fboss2/oss/CmdListConfig.cpp b/fboss/cli/fboss2/oss/CmdListConfig.cpp new file mode 100644 index 0000000000000..6f40d57f91079 --- /dev/null +++ b/fboss/cli/fboss2/oss/CmdListConfig.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2004-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "fboss/cli/fboss2/CmdList.h" + +namespace facebook::fboss { + +// Defined in CmdListConfig.cpp +const CommandTree& kConfigCommandTree(); + +const CommandTree& kAdditionalCommandTree() { + return kConfigCommandTree(); +} + +const std::vector& kSpecialCommands() { + static const std::vector cmds = {}; + return cmds; +} + +} // namespace facebook::fboss diff --git a/fboss/cli/fboss2/test/BUCK b/fboss/cli/fboss2/test/BUCK index a91ea9fd81478..817d121f2ba16 100644 --- a/fboss/cli/fboss2/test/BUCK +++ b/fboss/cli/fboss2/test/BUCK @@ -53,6 +53,7 @@ cpp_unittest( cpp_unittest( name = "cmd_test", srcs = [ + "CmdConfigReloadTest.cpp", "CmdGetPcapTest.cpp", "CmdSetPortStateTest.cpp", "CmdShowAclTest.cpp", diff --git a/fboss/cli/fboss2/test/CmdConfigReloadTest.cpp b/fboss/cli/fboss2/test/CmdConfigReloadTest.cpp new file mode 100644 index 0000000000000..6463e0f0cc8ab --- /dev/null +++ b/fboss/cli/fboss2/test/CmdConfigReloadTest.cpp @@ -0,0 +1,56 @@ +// (c) Facebook, Inc. and its affiliates. Confidential and proprietary. + +#include +#include +#include + +#include "fboss/cli/fboss2/commands/config/CmdConfigReload.h" + +using namespace ::testing; + +namespace facebook::fboss { + +class CmdConfigReloadTestFixture : public ::testing::Test { + public: + void SetUp() override {} +}; + +TEST_F(CmdConfigReloadTestFixture, printOutput) { + auto cmd = CmdConfigReload(); + std::string successMessage = "Config reloaded successfully"; + + // Redirect cout to capture output + std::stringstream buffer; + std::streambuf* old = std::cout.rdbuf(buffer.rdbuf()); + + cmd.printOutput(successMessage); + + // Restore cout + std::cout.rdbuf(old); + + std::string output = buffer.str(); + std::string expectedOutput = "Config reloaded successfully\n"; + + EXPECT_EQ(output, expectedOutput); +} + +TEST_F(CmdConfigReloadTestFixture, printOutputCustomMessage) { + auto cmd = CmdConfigReload(); + std::string customMessage = "Custom test message"; + + // Redirect cout to capture output + std::stringstream buffer; + std::streambuf* old = std::cout.rdbuf(buffer.rdbuf()); + + cmd.printOutput(customMessage); + + // Restore cout + std::cout.rdbuf(old); + + std::string output = buffer.str(); + std::string expectedOutput = "Custom test message\n"; + + EXPECT_EQ(output, expectedOutput); +} + +} // namespace facebook::fboss