From 8da84ed07806fa0db98adb32ef456d6863c81f5c Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 18 Sep 2021 16:09:44 -0700 Subject: [PATCH] Adding system reload command. --- build | 2 +- src/MicroService.cpp | 15 ++++++-- src/MicroService.h | 5 +-- src/RESTAPI_InternalServer.cpp | 7 ++++ src/RESTAPI_InternalServer.h | 1 + src/RESTAPI_protocol.h | 2 ++ src/RESTAPI_server.cpp | 8 ++++- src/RESTAPI_server.h | 2 ++ src/RESTAPI_system_command.cpp | 66 ++++++++++++++++++++++------------ src/SubSystemServer.cpp | 6 ++-- test_scripts/curl/cli | 19 ++++++++++ 11 files changed, 102 insertions(+), 31 deletions(-) diff --git a/build b/build index 2edeafb..8fdd954 100644 --- a/build +++ b/build @@ -1 +1 @@ -20 \ No newline at end of file +22 \ No newline at end of file diff --git a/src/MicroService.cpp b/src/MicroService.cpp index afefe17..be06fac 100644 --- a/src/MicroService.cpp +++ b/src/MicroService.cpp @@ -341,14 +341,23 @@ namespace OpenWifi { return false; } + void MicroService::Reload(const std::string &Sub) { + for (auto i : SubSystems_) { + if (Poco::toLower(Sub) == Poco::toLower(i->Name())) { + i->reinitialize(Poco::Util::Application::instance()); + return; + } + } + } + Types::StringVec MicroService::GetSubSystems() const { Types::StringVec Result; for(auto i:SubSystems_) - Result.push_back(i->Name()); + Result.push_back(Poco::toLower(i->Name())); return Result; } - Types::StringPairVec MicroService::GetLogLevels() const { + Types::StringPairVec MicroService::GetLogLevels() { Types::StringPairVec Result; for(auto &i:SubSystems_) { @@ -358,7 +367,7 @@ namespace OpenWifi { return Result; } - const Types::StringVec & MicroService::GetLogLevelNames() const { + const Types::StringVec & MicroService::GetLogLevelNames() { static Types::StringVec LevelNames{"none", "fatal", "critical", "error", "warning", "notice", "information", "debug", "trace" }; return LevelNames; } diff --git a/src/MicroService.h b/src/MicroService.h index 391ceb8..07e8460 100644 --- a/src/MicroService.h +++ b/src/MicroService.h @@ -113,8 +113,8 @@ namespace OpenWifi { [[nodiscard]] bool Debug() const { return DebugMode_; } [[nodiscard]] uint64_t ID() const { return ID_; } [[nodiscard]] Types::StringVec GetSubSystems() const; - [[nodiscard]] Types::StringPairVec GetLogLevels() const; - [[nodiscard]] const Types::StringVec & GetLogLevelNames() const; + [[nodiscard]] Types::StringPairVec GetLogLevels() ; + [[nodiscard]] static const Types::StringVec & GetLogLevelNames(); [[nodiscard]] std::string ConfigGetString(const std::string &Key,const std::string & Default); [[nodiscard]] std::string ConfigGetString(const std::string &Key); [[nodiscard]] std::string ConfigPath(const std::string &Key,const std::string & Default); @@ -142,6 +142,7 @@ namespace OpenWifi { static inline uint64_t GetPID() { return Poco::Process::id(); }; [[nodiscard]] inline const std::string GetPublicAPIEndPoint() { return MyPublicEndPoint_ + "/api/v1"; }; [[nodiscard]] inline const std::string & GetUIURI() const { return UIURI_;}; + void Reload(const std::string &Name); private: bool HelpRequested_ = false; diff --git a/src/RESTAPI_InternalServer.cpp b/src/RESTAPI_InternalServer.cpp index 379a63d..b6ecbcb 100644 --- a/src/RESTAPI_InternalServer.cpp +++ b/src/RESTAPI_InternalServer.cpp @@ -54,6 +54,13 @@ namespace OpenWifi { Logger_.information("Stopping "); for( const auto & svr : RESTServers_ ) svr->stop(); + RESTServers_.clear(); + } + + void RESTAPI_InternalServer::reinitialize(Poco::Util::Application &self) { + Logger_.information("Reinitializing."); + Stop(); + Start(); } Poco::Net::HTTPRequestHandler *InternalRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest & Request) { diff --git a/src/RESTAPI_InternalServer.h b/src/RESTAPI_InternalServer.h index 9c256a5..4f46c43 100644 --- a/src/RESTAPI_InternalServer.h +++ b/src/RESTAPI_InternalServer.h @@ -28,6 +28,7 @@ namespace OpenWifi { int Start() override; void Stop() override; + void reinitialize(Poco::Util::Application &self) override; private: static RESTAPI_InternalServer *instance_; diff --git a/src/RESTAPI_protocol.h b/src/RESTAPI_protocol.h index dfeb3e8..cec532f 100644 --- a/src/RESTAPI_protocol.h +++ b/src/RESTAPI_protocol.h @@ -74,6 +74,8 @@ namespace OpenWifi::RESTAPI::Protocol { static const char * HOSTNAME = "hostname"; static const char * PROCESSORS = "processors"; static const char * REASON = "reason"; + static const char * RELOAD = "reload"; + static const char * SUBSYSTEMS = "subsystems"; static const char * FILEUUID = "uuid"; static const char * USERID = "userId"; static const char * PASSWORD = "password"; diff --git a/src/RESTAPI_server.cpp b/src/RESTAPI_server.cpp index e29e342..d24ed42 100644 --- a/src/RESTAPI_server.cpp +++ b/src/RESTAPI_server.cpp @@ -55,7 +55,6 @@ namespace OpenWifi { NewServer->start(); RESTServers_.push_back(std::move(NewServer)); } - return 0; } @@ -80,6 +79,13 @@ namespace OpenWifi { Logger_.information("Stopping "); for( const auto & svr : RESTServers_ ) svr->stop(); + RESTServers_.clear(); + } + + void RESTAPI_Server::reinitialize(Poco::Util::Application &self) { + Logger_.information("Reinitializing."); + Stop(); + Start(); } } // namespace \ No newline at end of file diff --git a/src/RESTAPI_server.h b/src/RESTAPI_server.h index 5052632..3130955 100644 --- a/src/RESTAPI_server.h +++ b/src/RESTAPI_server.h @@ -31,6 +31,8 @@ namespace OpenWifi { int Start() override; void Stop() override; + void reinitialize(Poco::Util::Application &self) override; + inline const std::string & AssetDir() { return AsserDir_; } inline const std::string & GetPasswordPolicy() const { return PasswordPolicy_; } inline const std::string & GetAccessPolicy() const { return AccessPolicy_; } diff --git a/src/RESTAPI_system_command.cpp b/src/RESTAPI_system_command.cpp index 9df7998..947d28d 100644 --- a/src/RESTAPI_system_command.cpp +++ b/src/RESTAPI_system_command.cpp @@ -13,6 +13,10 @@ #include "Daemon.h" #include "RESTAPI_protocol.h" #include "RESTAPI_errors.h" +#include +#include + +using namespace std::chrono_literals; namespace OpenWifi { void RESTAPI_system_command::DoPost() { @@ -23,7 +27,7 @@ namespace OpenWifi { if (Obj->has(RESTAPI::Protocol::PARAMETERS) && Obj->isArray(RESTAPI::Protocol::PARAMETERS)) { auto ParametersBlock = Obj->getArray(RESTAPI::Protocol::PARAMETERS); - for (const auto &i:*ParametersBlock) { + for (const auto &i : *ParametersBlock) { Poco::JSON::Parser pp; auto InnerObj = pp.parse(i).extract(); if (InnerObj->has(RESTAPI::Protocol::TAG) && @@ -31,7 +35,8 @@ namespace OpenWifi { auto Name = GetS(RESTAPI::Protocol::TAG, InnerObj); auto Value = GetS(RESTAPI::Protocol::VALUE, InnerObj); Daemon()->SetSubsystemLogLevel(Name, Value); - Logger_.information(Poco::format("Setting log level for %s at %s", Name, Value)); + Logger_.information( + Poco::format("Setting log level for %s at %s", Name, Value)); } } OK(); @@ -39,43 +44,60 @@ namespace OpenWifi { } } else if (Command == RESTAPI::Protocol::GETLOGLEVELS) { auto CurrentLogLevels = Daemon()->GetLogLevels(); - Poco::JSON::Object Result; - Poco::JSON::Array Array; - for(auto &[Name,Level]:CurrentLogLevels) { - Poco::JSON::Object Pair; - Pair.set( RESTAPI::Protocol::TAG,Name); - Pair.set(RESTAPI::Protocol::VALUE,Level); + Poco::JSON::Object Result; + Poco::JSON::Array Array; + for (auto &[Name, Level] : CurrentLogLevels) { + Poco::JSON::Object Pair; + Pair.set(RESTAPI::Protocol::TAG, Name); + Pair.set(RESTAPI::Protocol::VALUE, Level); Array.add(Pair); } - Result.set(RESTAPI::Protocol::TAGLIST,Array); + Result.set(RESTAPI::Protocol::TAGLIST, Array); ReturnObject(Result); return; } else if (Command == RESTAPI::Protocol::GETLOGLEVELNAMES) { - Poco::JSON::Object Result; - Poco::JSON::Array LevelNamesArray; - const Types::StringVec & LevelNames = Daemon()->GetLogLevelNames(); - for(const auto &i:LevelNames) + Poco::JSON::Object Result; + Poco::JSON::Array LevelNamesArray; + const Types::StringVec &LevelNames = Daemon()->GetLogLevelNames(); + for (const auto &i : LevelNames) LevelNamesArray.add(i); - Result.set(RESTAPI::Protocol::LIST,LevelNamesArray); + Result.set(RESTAPI::Protocol::LIST, LevelNamesArray); ReturnObject(Result); return; } else if (Command == RESTAPI::Protocol::GETSUBSYSTEMNAMES) { - Poco::JSON::Object Result; - Poco::JSON::Array LevelNamesArray; - const Types::StringVec & SubSystemNames = Daemon()->GetSubSystems(); - for(const auto &i:SubSystemNames) + Poco::JSON::Object Result; + Poco::JSON::Array LevelNamesArray; + const Types::StringVec &SubSystemNames = Daemon()->GetSubSystems(); + for (const auto &i : SubSystemNames) LevelNamesArray.add(i); - Result.set(RESTAPI::Protocol::LIST,LevelNamesArray); + Result.set(RESTAPI::Protocol::LIST, LevelNamesArray); ReturnObject(Result); return; } else if (Command == RESTAPI::Protocol::STATS) { - } else { - BadRequest("Unknown command."); + } else if (Command == RESTAPI::Protocol::RELOAD) { + if (Obj->has(RESTAPI::Protocol::SUBSYSTEMS) && + Obj->isArray(RESTAPI::Protocol::SUBSYSTEMS)) { + auto SubSystems = Obj->getArray(RESTAPI::Protocol::SUBSYSTEMS); + std::vector Names; + for (const auto &i : *SubSystems) + Names.push_back(i.toString()); + std::thread ReloadThread([Names](){ + std::this_thread::sleep_for(10000ms); + for(const auto &i:Names) { + Daemon()->Reload(i); + } + }); + ReloadThread.detach(); + } + OK(); + return; } } else { - BadRequest("Missing command."); + BadRequest("Unknown command."); + return; } + BadRequest("Missing command."); } void RESTAPI_system_command::DoGet() { diff --git a/src/SubSystemServer.cpp b/src/SubSystemServer.cpp index cc2f709..97bf2f9 100644 --- a/src/SubSystemServer.cpp +++ b/src/SubSystemServer.cpp @@ -32,6 +32,7 @@ void SubSystemServer::initialize(Poco::Util::Application &self) { auto i = 0; bool good = true; + ConfigServersList_.clear(); while (good) { std::string root{SubSystemConfigPrefix_ + ".host." + std::to_string(i) + "."}; @@ -81,10 +82,11 @@ void SubSystemServer::initialize(Poco::Util::Application &self) { } } -void SubSystemServer::uninitialize() {} +void SubSystemServer::uninitialize() { +} void SubSystemServer::reinitialize(Poco::Util::Application &self) { - // add your own reinitialization code here + Logger_.information("Reloading of this subsystem is not supported."); } void SubSystemServer::defineOptions(Poco::Util::OptionSet &options) {} diff --git a/test_scripts/curl/cli b/test_scripts/curl/cli index 97e52f6..74007a3 100755 --- a/test_scripts/curl/cli +++ b/test_scripts/curl/cli @@ -268,6 +268,23 @@ testlogout() { listusers } +getsubsystemnames() { + payload="{ \"command\" : \"getsubsystemnames\" }" + curl ${FLAGS} -X POST "https://${OWSEC}/api/v1/system" \ + -H "accept: application/json" \ + -H "Authorization: Bearer ${token}" \ + -d "$payload" +} + +reloadsubsystem() { + payload="{ \"command\" : \"reload\", \"subsystems\" : [ \"OUIServer\" , \"CommandManager\" ] }" + curl ${FLAGS} -X POST "https://${OWSEC}/api/v1/system" \ + -H "accept: application/json" \ + -H "Authorization: Bearer ${token}" \ + -d "$payload" +} + + shopt -s nocasematch case "$1" in @@ -288,6 +305,8 @@ case "$1" in "systeminfo") login; systeminfo ; logout;; "sendemail") login; sendemail ; logout;; "testlogout") login; testlogout ;; + "getsubsystemnames") login; getsubsystemnames; logout ;; + "reloadsubsystem") login; reloadsubsystem; logout ;; "help") login; help ; logout ;; *) help ;; esac