mirror of
				https://github.com/Telecominfraproject/wlan-cloud-ucentralsec.git
				synced 2025-10-31 18:57:51 +00:00 
			
		
		
		
	Adding system reload command.
This commit is contained in:
		| @@ -341,14 +341,23 @@ namespace OpenWifi { | |||||||
| 		return false; | 		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 MicroService::GetSubSystems() const { | ||||||
| 		Types::StringVec Result; | 		Types::StringVec Result; | ||||||
| 		for(auto i:SubSystems_) | 		for(auto i:SubSystems_) | ||||||
| 			Result.push_back(i->Name()); | 			Result.push_back(Poco::toLower(i->Name())); | ||||||
| 		return Result; | 		return Result; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	Types::StringPairVec MicroService::GetLogLevels() const { | 	Types::StringPairVec MicroService::GetLogLevels() { | ||||||
| 		Types::StringPairVec Result; | 		Types::StringPairVec Result; | ||||||
|  |  | ||||||
| 		for(auto &i:SubSystems_) { | 		for(auto &i:SubSystems_) { | ||||||
| @@ -358,7 +367,7 @@ namespace OpenWifi { | |||||||
| 		return Result; | 		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" }; | 		static Types::StringVec LevelNames{"none", "fatal", "critical", "error", "warning", "notice", "information", "debug", "trace" }; | ||||||
| 		return LevelNames; | 		return LevelNames; | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -113,8 +113,8 @@ namespace OpenWifi { | |||||||
| 		[[nodiscard]] bool Debug() const { return DebugMode_; } | 		[[nodiscard]] bool Debug() const { return DebugMode_; } | ||||||
| 		[[nodiscard]] uint64_t ID() const { return ID_; } | 		[[nodiscard]] uint64_t ID() const { return ID_; } | ||||||
| 		[[nodiscard]] Types::StringVec GetSubSystems() const; | 		[[nodiscard]] Types::StringVec GetSubSystems() const; | ||||||
| 		[[nodiscard]] Types::StringPairVec GetLogLevels() const; | 		[[nodiscard]] Types::StringPairVec GetLogLevels() ; | ||||||
| 		[[nodiscard]] const Types::StringVec & GetLogLevelNames() const; | 		[[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,const std::string & Default); | ||||||
| 		[[nodiscard]] std::string ConfigGetString(const std::string &Key); | 		[[nodiscard]] std::string ConfigGetString(const std::string &Key); | ||||||
| 		[[nodiscard]] std::string ConfigPath(const std::string &Key,const std::string & Default); | 		[[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(); }; | 		static inline uint64_t GetPID() { return Poco::Process::id(); }; | ||||||
| 		[[nodiscard]] inline const std::string GetPublicAPIEndPoint() { return MyPublicEndPoint_ + "/api/v1"; }; | 		[[nodiscard]] inline const std::string GetPublicAPIEndPoint() { return MyPublicEndPoint_ + "/api/v1"; }; | ||||||
| 		[[nodiscard]] inline const std::string & GetUIURI() const { return UIURI_;}; | 		[[nodiscard]] inline const std::string & GetUIURI() const { return UIURI_;}; | ||||||
|  | 		void Reload(const std::string &Name); | ||||||
|  |  | ||||||
| 	  private: | 	  private: | ||||||
| 		bool                        HelpRequested_ = false; | 		bool                        HelpRequested_ = false; | ||||||
|   | |||||||
| @@ -54,6 +54,13 @@ namespace OpenWifi { | |||||||
|         Logger_.information("Stopping "); |         Logger_.information("Stopping "); | ||||||
|         for( const auto & svr : RESTServers_ ) |         for( const auto & svr : RESTServers_ ) | ||||||
|             svr->stop(); |             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) { |     Poco::Net::HTTPRequestHandler *InternalRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest & Request) { | ||||||
|   | |||||||
| @@ -28,6 +28,7 @@ namespace OpenWifi { | |||||||
|  |  | ||||||
|             int Start() override; |             int Start() override; | ||||||
|             void Stop() override; |             void Stop() override; | ||||||
|  |             void reinitialize(Poco::Util::Application &self) override; | ||||||
|  |  | ||||||
|         private: |         private: | ||||||
|             static RESTAPI_InternalServer *instance_; |             static RESTAPI_InternalServer *instance_; | ||||||
|   | |||||||
| @@ -74,6 +74,8 @@ namespace OpenWifi::RESTAPI::Protocol { | |||||||
| 	static const char * HOSTNAME = "hostname"; | 	static const char * HOSTNAME = "hostname"; | ||||||
| 	static const char * PROCESSORS = "processors"; | 	static const char * PROCESSORS = "processors"; | ||||||
| 	static const char * REASON = "reason"; | 	static const char * REASON = "reason"; | ||||||
|  | 	static const char * RELOAD = "reload"; | ||||||
|  | 	static const char * SUBSYSTEMS = "subsystems"; | ||||||
| 	static const char * FILEUUID = "uuid"; | 	static const char * FILEUUID = "uuid"; | ||||||
| 	static const char * USERID = "userId"; | 	static const char * USERID = "userId"; | ||||||
| 	static const char * PASSWORD = "password"; | 	static const char * PASSWORD = "password"; | ||||||
|   | |||||||
| @@ -55,7 +55,6 @@ namespace OpenWifi { | |||||||
|             NewServer->start(); |             NewServer->start(); | ||||||
|             RESTServers_.push_back(std::move(NewServer)); |             RESTServers_.push_back(std::move(NewServer)); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         return 0; |         return 0; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -80,6 +79,13 @@ namespace OpenWifi { | |||||||
|         Logger_.information("Stopping "); |         Logger_.information("Stopping "); | ||||||
|         for( const auto & svr : RESTServers_ ) |         for( const auto & svr : RESTServers_ ) | ||||||
|             svr->stop(); |             svr->stop(); | ||||||
|  |         RESTServers_.clear(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void RESTAPI_Server::reinitialize(Poco::Util::Application &self) { | ||||||
|  |         Logger_.information("Reinitializing."); | ||||||
|  |         Stop(); | ||||||
|  |         Start(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| }  // namespace | }  // namespace | ||||||
| @@ -31,6 +31,8 @@ namespace OpenWifi { | |||||||
|  |  | ||||||
|         int Start() override; |         int Start() override; | ||||||
|         void Stop() override; |         void Stop() override; | ||||||
|  |         void reinitialize(Poco::Util::Application &self) override; | ||||||
|  |  | ||||||
|         inline const std::string & AssetDir() { return AsserDir_; } |         inline const std::string & AssetDir() { return AsserDir_; } | ||||||
|         inline const std::string & GetPasswordPolicy() const { return PasswordPolicy_; } |         inline const std::string & GetPasswordPolicy() const { return PasswordPolicy_; } | ||||||
|         inline const std::string & GetAccessPolicy() const { return AccessPolicy_; } |         inline const std::string & GetAccessPolicy() const { return AccessPolicy_; } | ||||||
|   | |||||||
| @@ -13,6 +13,10 @@ | |||||||
| #include "Daemon.h" | #include "Daemon.h" | ||||||
| #include "RESTAPI_protocol.h" | #include "RESTAPI_protocol.h" | ||||||
| #include "RESTAPI_errors.h" | #include "RESTAPI_errors.h" | ||||||
|  | #include <thread> | ||||||
|  | #include <chrono> | ||||||
|  |  | ||||||
|  | using namespace std::chrono_literals; | ||||||
|  |  | ||||||
| namespace OpenWifi { | namespace OpenWifi { | ||||||
| 	void RESTAPI_system_command::DoPost() { | 	void RESTAPI_system_command::DoPost() { | ||||||
| @@ -23,7 +27,7 @@ namespace OpenWifi { | |||||||
| 				if (Obj->has(RESTAPI::Protocol::PARAMETERS) && | 				if (Obj->has(RESTAPI::Protocol::PARAMETERS) && | ||||||
| 					Obj->isArray(RESTAPI::Protocol::PARAMETERS)) { | 					Obj->isArray(RESTAPI::Protocol::PARAMETERS)) { | ||||||
| 					auto ParametersBlock = Obj->getArray(RESTAPI::Protocol::PARAMETERS); | 					auto ParametersBlock = Obj->getArray(RESTAPI::Protocol::PARAMETERS); | ||||||
| 					for (const auto &i:*ParametersBlock) { | 					for (const auto &i : *ParametersBlock) { | ||||||
| 						Poco::JSON::Parser pp; | 						Poco::JSON::Parser pp; | ||||||
| 						auto InnerObj = pp.parse(i).extract<Poco::JSON::Object::Ptr>(); | 						auto InnerObj = pp.parse(i).extract<Poco::JSON::Object::Ptr>(); | ||||||
| 						if (InnerObj->has(RESTAPI::Protocol::TAG) && | 						if (InnerObj->has(RESTAPI::Protocol::TAG) && | ||||||
| @@ -31,7 +35,8 @@ namespace OpenWifi { | |||||||
| 							auto Name = GetS(RESTAPI::Protocol::TAG, InnerObj); | 							auto Name = GetS(RESTAPI::Protocol::TAG, InnerObj); | ||||||
| 							auto Value = GetS(RESTAPI::Protocol::VALUE, InnerObj); | 							auto Value = GetS(RESTAPI::Protocol::VALUE, InnerObj); | ||||||
| 							Daemon()->SetSubsystemLogLevel(Name, Value); | 							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(); | 					OK(); | ||||||
| @@ -41,42 +46,59 @@ namespace OpenWifi { | |||||||
| 				auto CurrentLogLevels = Daemon()->GetLogLevels(); | 				auto CurrentLogLevels = Daemon()->GetLogLevels(); | ||||||
| 				Poco::JSON::Object Result; | 				Poco::JSON::Object Result; | ||||||
| 				Poco::JSON::Array Array; | 				Poco::JSON::Array Array; | ||||||
| 				for(auto &[Name,Level]:CurrentLogLevels) { | 				for (auto &[Name, Level] : CurrentLogLevels) { | ||||||
| 					Poco::JSON::Object Pair; | 					Poco::JSON::Object Pair; | ||||||
| 					Pair.set( RESTAPI::Protocol::TAG,Name); | 					Pair.set(RESTAPI::Protocol::TAG, Name); | ||||||
| 					Pair.set(RESTAPI::Protocol::VALUE,Level); | 					Pair.set(RESTAPI::Protocol::VALUE, Level); | ||||||
| 					Array.add(Pair); | 					Array.add(Pair); | ||||||
| 				} | 				} | ||||||
| 				Result.set(RESTAPI::Protocol::TAGLIST,Array); | 				Result.set(RESTAPI::Protocol::TAGLIST, Array); | ||||||
| 				ReturnObject(Result); | 				ReturnObject(Result); | ||||||
| 				return; | 				return; | ||||||
| 			} else if (Command == RESTAPI::Protocol::GETLOGLEVELNAMES) { | 			} else if (Command == RESTAPI::Protocol::GETLOGLEVELNAMES) { | ||||||
| 				Poco::JSON::Object Result; | 				Poco::JSON::Object Result; | ||||||
| 				Poco::JSON::Array LevelNamesArray; | 				Poco::JSON::Array LevelNamesArray; | ||||||
| 				const Types::StringVec & LevelNames = Daemon()->GetLogLevelNames(); | 				const Types::StringVec &LevelNames = Daemon()->GetLogLevelNames(); | ||||||
| 				for(const auto &i:LevelNames) | 				for (const auto &i : LevelNames) | ||||||
| 					LevelNamesArray.add(i); | 					LevelNamesArray.add(i); | ||||||
| 				Result.set(RESTAPI::Protocol::LIST,LevelNamesArray); | 				Result.set(RESTAPI::Protocol::LIST, LevelNamesArray); | ||||||
| 				ReturnObject(Result); | 				ReturnObject(Result); | ||||||
| 				return; | 				return; | ||||||
| 			} else if (Command == RESTAPI::Protocol::GETSUBSYSTEMNAMES) { | 			} else if (Command == RESTAPI::Protocol::GETSUBSYSTEMNAMES) { | ||||||
| 				Poco::JSON::Object Result; | 				Poco::JSON::Object Result; | ||||||
| 				Poco::JSON::Array LevelNamesArray; | 				Poco::JSON::Array LevelNamesArray; | ||||||
| 				const Types::StringVec & SubSystemNames = Daemon()->GetSubSystems(); | 				const Types::StringVec &SubSystemNames = Daemon()->GetSubSystems(); | ||||||
| 				for(const auto &i:SubSystemNames) | 				for (const auto &i : SubSystemNames) | ||||||
| 					LevelNamesArray.add(i); | 					LevelNamesArray.add(i); | ||||||
| 				Result.set(RESTAPI::Protocol::LIST,LevelNamesArray); | 				Result.set(RESTAPI::Protocol::LIST, LevelNamesArray); | ||||||
| 				ReturnObject(Result); | 				ReturnObject(Result); | ||||||
| 				return; | 				return; | ||||||
| 			} else if (Command == RESTAPI::Protocol::STATS) { | 			} else if (Command == RESTAPI::Protocol::STATS) { | ||||||
|  |  | ||||||
|  | 			} 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<std::string> 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 { | 		} else { | ||||||
| 			BadRequest("Unknown command."); | 			BadRequest("Unknown command."); | ||||||
|  | 			return; | ||||||
| 		} | 		} | ||||||
| 		} else { |  | ||||||
| 		BadRequest("Missing command."); | 		BadRequest("Missing command."); | ||||||
| 	} | 	} | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	void RESTAPI_system_command::DoGet() { | 	void RESTAPI_system_command::DoGet() { | ||||||
| 		std::string Arg; | 		std::string Arg; | ||||||
|   | |||||||
| @@ -32,6 +32,7 @@ void SubSystemServer::initialize(Poco::Util::Application &self) { | |||||||
| 	auto i = 0; | 	auto i = 0; | ||||||
| 	bool good = true; | 	bool good = true; | ||||||
|  |  | ||||||
|  | 	ConfigServersList_.clear(); | ||||||
| 	while (good) { | 	while (good) { | ||||||
| 		std::string root{SubSystemConfigPrefix_ + ".host." + std::to_string(i) + "."}; | 		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) { | 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) {} | void SubSystemServer::defineOptions(Poco::Util::OptionSet &options) {} | ||||||
|   | |||||||
| @@ -268,6 +268,23 @@ testlogout() { | |||||||
|     listusers |     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 | shopt -s nocasematch | ||||||
|  |  | ||||||
| case "$1" in | case "$1" in | ||||||
| @@ -288,6 +305,8 @@ case "$1" in | |||||||
|     "systeminfo") login; systeminfo ; logout;; |     "systeminfo") login; systeminfo ; logout;; | ||||||
|     "sendemail") login; sendemail ; logout;; |     "sendemail") login; sendemail ; logout;; | ||||||
|     "testlogout") login; testlogout ;; |     "testlogout") login; testlogout ;; | ||||||
|  |     "getsubsystemnames") login; getsubsystemnames; logout ;; | ||||||
|  |     "reloadsubsystem") login; reloadsubsystem; logout ;; | ||||||
|     "help") login; help  ; logout ;; |     "help") login; help  ; logout ;; | ||||||
|     *) help ;; |     *) help ;; | ||||||
| esac | esac | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 stephb9959
					stephb9959