mirror of
				https://github.com/Telecominfraproject/wlan-cloud-ucentralsec.git
				synced 2025-10-30 02:12:32 +00:00 
			
		
		
		
	Adding system reload command.
This commit is contained in:
		| @@ -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; | ||||
| 	} | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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) { | ||||
|   | ||||
| @@ -28,6 +28,7 @@ namespace OpenWifi { | ||||
|  | ||||
|             int Start() override; | ||||
|             void Stop() override; | ||||
|             void reinitialize(Poco::Util::Application &self) override; | ||||
|  | ||||
|         private: | ||||
|             static RESTAPI_InternalServer *instance_; | ||||
|   | ||||
| @@ -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"; | ||||
|   | ||||
| @@ -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 | ||||
| @@ -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_; } | ||||
|   | ||||
| @@ -13,6 +13,10 @@ | ||||
| #include "Daemon.h" | ||||
| #include "RESTAPI_protocol.h" | ||||
| #include "RESTAPI_errors.h" | ||||
| #include <thread> | ||||
| #include <chrono> | ||||
|  | ||||
| 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<Poco::JSON::Object::Ptr>(); | ||||
| 						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<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 { | ||||
| 			BadRequest("Missing command."); | ||||
| 			BadRequest("Unknown command."); | ||||
| 			return; | ||||
| 		} | ||||
| 		BadRequest("Missing command."); | ||||
| 	} | ||||
|  | ||||
| 	void RESTAPI_system_command::DoGet() { | ||||
|   | ||||
| @@ -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) {} | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 stephb9959
					stephb9959