From 1ecf367411992e42dc5a554395d7d7cc0af927af Mon Sep 17 00:00:00 2001 From: fly_1997 Date: Fri, 10 May 2024 12:04:13 +0800 Subject: [PATCH] fix systemd service, load error and args parsing (cherry picked from commit 6c69029157b393c5ce99aa3f04a96aeee4d15348) --- ...rror-and-args-parsing-error-printing.patch | 252 ++++++++++++++++++ oeAware-manager.spec | 13 +- 2 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 0006-fix-load-error-and-args-parsing-error-printing.patch diff --git a/0006-fix-load-error-and-args-parsing-error-printing.patch b/0006-fix-load-error-and-args-parsing-error-printing.patch new file mode 100644 index 0000000..827aed8 --- /dev/null +++ b/0006-fix-load-error-and-args-parsing-error-printing.patch @@ -0,0 +1,252 @@ +From c35c26e51d93b0eb5b0aecdf08c338178079e02a Mon Sep 17 00:00:00 2001 +From: fly_1997 +Date: Fri, 10 May 2024 12:01:08 +0800 +Subject: [PATCH] fix load error and args parsing error printing + +--- + src/client/arg_parse.cpp | 28 +++++++++++++++++++++++++--- + src/client/arg_parse.h | 3 +++ + src/client/cmd_handler.cpp | 28 +++++++++++----------------- + src/plugin_mgr/config.cpp | 11 +++++------ + src/plugin_mgr/config.h | 2 +- + src/plugin_mgr/dep_handler.cpp | 2 +- + src/plugin_mgr/plugin_manager.cpp | 2 +- + 7 files changed, 47 insertions(+), 29 deletions(-) + +diff --git a/src/client/arg_parse.cpp b/src/client/arg_parse.cpp +index 70fcd4a..805f5a7 100644 +--- a/src/client/arg_parse.cpp ++++ b/src/client/arg_parse.cpp +@@ -62,10 +62,23 @@ void ArgParse::print_help() { + " --help show this help message.\n"; + } + ++void ArgParse::init_opts() { ++ opts.insert('l'); ++ opts.insert('r'); ++ opts.insert('q'); ++ opts.insert('Q'); ++ opts.insert('e'); ++ opts.insert('d'); ++ opts.insert('L'); ++ opts.insert('i'); ++ opts.insert('t'); ++} ++ + int ArgParse::init(int argc, char *argv[]) { + int cmd = -1; + int opt; + bool help = false; ++ init_opts(); + opterr = 0; + while((opt = getopt_long(argc, argv, OPT_STRING.c_str(), long_options, nullptr)) != -1) { + std::string full_opt; +@@ -76,9 +89,15 @@ int ArgParse::init(int argc, char *argv[]) { + case 'h': + help = true; + break; +- case '?': +- arg_error("unknown option."); +- return -1; ++ case '?': { ++ std::string err; ++ err += optopt; ++ if (!opts.count(optopt)) { ++ arg_error("unknown option '-" + err + "'."); ++ } else{ ++ arg_error("option -" + err + " requires an argument."); ++ } ++ } + default: { + if (opt == 'l' || opt == 'r' || opt == 'q' || opt == 'Q' || opt == 'e' || opt == 'd' || opt == 'L' || opt == 'i') { + if (cmd != -1) { +@@ -94,6 +113,9 @@ int ArgParse::init(int argc, char *argv[]) { + + } + } ++ if (cmd == 'l' && type.empty()) { ++ arg_error("option '-t' is required."); ++ } + if (help) { + print_help(); + exit(EXIT_SUCCESS); +diff --git a/src/client/arg_parse.h b/src/client/arg_parse.h +index 8535e9c..682f0e5 100644 +--- a/src/client/arg_parse.h ++++ b/src/client/arg_parse.h +@@ -12,12 +12,14 @@ + #ifndef CLIENT_ARG_PARSE_H + #define CLIENT_ARG_PARSE_H + #include ++#include + + class ArgParse { + public: + static void arg_error(const std::string &msg); + static void print_help(); + int init(int argc, char *argv[]); ++ void init_opts(); + void set_type(char* _type); + void set_arg(char* _arg); + std::string get_type() const { +@@ -29,6 +31,7 @@ public: + private: + std::string arg; + std::string type; ++ std::unordered_set opts; + static const std::string OPT_STRING; + static const int MAX_OPT_SIZE = 20; + static const struct option long_options[MAX_OPT_SIZE]; +diff --git a/src/client/cmd_handler.cpp b/src/client/cmd_handler.cpp +index 4fec95b..b410968 100644 +--- a/src/client/cmd_handler.cpp ++++ b/src/client/cmd_handler.cpp +@@ -16,14 +16,8 @@ + std::unordered_set LoadHandler::types = {"collector", "scenario", "tune"}; + + void LoadHandler::check(const std::string &arg, const std::string &type) { +- if (arg.empty()) { +- ArgParse::arg_error("plugin name empty."); +- } +- if (type.empty()) { +- ArgParse::arg_error("type empty."); +- } + if (!types.count(type)) { +- ArgParse::arg_error("this type is not supported."); ++ ArgParse::arg_error("type '" + type + "' is not supported."); + } + } + +@@ -94,9 +88,9 @@ void RemoveHandler::handler(const ArgParse &arg_parse, Msg &msg) { + + void RemoveHandler::res_handler(Msg &msg) { + if (msg.get_opt() == Opt::RESPONSE_OK) { +- std::cout << "plugin remove successfully.\n"; ++ std::cout << "Plugin remove successfully.\n"; + } else { +- std::cout << "plugin remove failed, because " << msg.payload(0) << ".\n"; ++ std::cout << "Plugin remove failed, because " << msg.payload(0) << ".\n"; + } + } + +@@ -141,9 +135,9 @@ void EnabledHandler::handler(const ArgParse &arg_parse, Msg &msg) { + + void EnabledHandler::res_handler(Msg &msg) { + if (msg.get_opt() == Opt::RESPONSE_OK) { +- std::cout << "instance enabled successfully.\n"; ++ std::cout << "Instance enabled successfully.\n"; + } else { +- std::cout << "instance enabled failed, because "<< msg.payload(0) << ".\n"; ++ std::cout << "Instance enabled failed, because "<< msg.payload(0) << ".\n"; + } + } + +@@ -155,9 +149,9 @@ void DisabledHandler::handler(const ArgParse &arg_parse, Msg &msg) { + + void DisabledHandler::res_handler(Msg &msg) { + if (msg.get_opt() == Opt::RESPONSE_OK) { +- std::cout << "instance disabled successfully.\n"; ++ std::cout << "Instance disabled successfully.\n"; + } else { +- std::cout << "instance disabled failed, because "<< msg.payload(0) << ".\n"; ++ std::cout << "Instance disabled failed, because "<< msg.payload(0) << ".\n"; + } + } + +@@ -167,10 +161,10 @@ void ListHandler::handler(const ArgParse &arg_parse, Msg &msg) { + + void ListHandler::res_handler(Msg &msg) { + if (msg.get_opt() == Opt::RESPONSE_ERROR) { +- std::cerr << "query list failed, because "<< msg.payload(0) << ".\n"; ++ std::cerr << "Query list failed, because "<< msg.payload(0) << ".\n"; + return; + } +- std::cout << "plugin list as follows.\n"; ++ std::cout << "Plugin list as follows.\n"; + std::cout << "------------------------------------------------------------\n"; + for (int i = 0; i < msg.payload_size(); ++i) { + std::cout << msg.payload(i); +@@ -186,13 +180,13 @@ void InstallHandler::handler(const ArgParse &arg_parse, Msg &msg) { + + void InstallHandler::res_handler(Msg &msg) { + if (msg.get_opt() == Opt::RESPONSE_ERROR) { +- std::cout << "download failed, because " << msg.payload(0) <<": " << this->arg.c_str() << '\n'; ++ std::cout << "Download failed, because " << msg.payload(0) <<": " << this->arg.c_str() << '\n'; + return; + } + std::string path = this->arg; + std::string url = msg.payload(0); + if (!download(url, path)) { +- std::cout << "download failed, please check url or your network.\n"; ++ std::cout << "Download failed, please check url or your network.\n"; + return; + } + std::string command = "rpm -ivh " + path; +diff --git a/src/plugin_mgr/config.cpp b/src/plugin_mgr/config.cpp +index 3c76e8e..f50399b 100644 +--- a/src/plugin_mgr/config.cpp ++++ b/src/plugin_mgr/config.cpp +@@ -84,16 +84,15 @@ bool Config::load(const std::string path) { + for (int i = 0; i < enable_list.size(); ++i) { + YAML::Node plugin = enable_list[i]["name"]; + std::string name = enable_list[i]["name"].as(); ++ YAML::Node instances = enable_list[i]["instances"]; + EnableItem enable_item(name); +- if (plugin.IsScalar()) { ++ if (instances.IsNull()) { + enable_item.set_enabled(true); +- } else if (plugin.IsSequence()) { +- for (int j = 0; j < plugin.size(); ++j) { +- std::string i_name = plugin[j].as(); ++ } else { ++ for (int j = 0; j < instances.size(); ++j) { ++ std::string i_name = instances[j].as(); + enable_item.add_instance(i_name); + } +- } else { +- continue; + } + this->enable_list.emplace_back(enable_item); + } +diff --git a/src/plugin_mgr/config.h b/src/plugin_mgr/config.h +index 5ab7672..6d0ee4d 100644 +--- a/src/plugin_mgr/config.h ++++ b/src/plugin_mgr/config.h +@@ -52,7 +52,7 @@ namespace std { + + class EnableItem { + public: +- EnableItem(std::string name) : name(name) { } ++ EnableItem(std::string name) : name(name), enabled(false) { } + void set_enabled(bool enabled) { + this->enabled = enabled; + } +diff --git a/src/plugin_mgr/dep_handler.cpp b/src/plugin_mgr/dep_handler.cpp +index 652fdce..816056d 100644 +--- a/src/plugin_mgr/dep_handler.cpp ++++ b/src/plugin_mgr/dep_handler.cpp +@@ -132,7 +132,7 @@ void DepHandler::query_node(const std::string &name, std::vector{node->name}); + for (auto cur = node->head->next; cur != nullptr; cur = cur->next) { + query.emplace_back(std::vector{node->name, cur->arc_name}); +- if (!vis.count(cur->arc_name)) { ++ if (!vis.count(cur->arc_name) && nodes.count(cur->arc_name)) { + vis.insert(cur->arc_name); + q.push(cur->arc_name); + } +diff --git a/src/plugin_mgr/plugin_manager.cpp b/src/plugin_mgr/plugin_manager.cpp +index 5ef395c..77acc40 100644 +--- a/src/plugin_mgr/plugin_manager.cpp ++++ b/src/plugin_mgr/plugin_manager.cpp +@@ -461,7 +461,7 @@ int PluginManager::run() { + PluginType type = plugin_types[msg.get_payload(1)]; + ErrorCode ret_code = load_plugin(plugin_name, type); + if(ret_code == ErrorCode::OK) { +- INFO("[PluginManager] " << plugin_name << "plugin loaded."); ++ INFO("[PluginManager] " << plugin_name << " plugin loaded."); + res.set_opt(Opt::RESPONSE_OK); + std::string lack_dep = instance_dep_check(plugin_name); + if (!lack_dep.empty()) { +-- +2.33.0 + diff --git a/oeAware-manager.spec b/oeAware-manager.spec index 7272412..00ac7cf 100644 --- a/oeAware-manager.spec +++ b/oeAware-manager.spec @@ -1,6 +1,6 @@ Name: oeAware-manager Version: v1.0.0 -Release: 4 +Release: 5 Summary: OeAware server and client License: MulanPSL2 URL: https://gitee.com/openeuler/%{name} @@ -10,6 +10,7 @@ Patch2: 0002-add-error-code-and-replace-raw-poniters-with-smart-p.patch Patch3: 0003-add-client-error-description-extract-class-and-fix-b.patch Patch4: 0004-fix-auto-enable-error-and-check-plugin-list-config.patch Patch5: 0005-modify-logs-sock-file-permission-and-fix-service-fil.patch +Patch6: 0006-fix-load-error-and-args-parsing-error-printing.patch BuildRequires: cmake make gcc-c++ BuildRequires: boost-devel @@ -40,6 +41,12 @@ install -D -m 0750 build/src/client/oeawarectl %{buildroot}%{_bindir}/oeawarectl install -D -m 0640 config.yaml %{buildroot}%{_sysconfdir}/oeAware/config.yaml install -D -p -m 0644 oeaware.service %{buildroot}%{_unitdir}/oeaware.service +%preun +%systemd_preun oeaware.service + +%post +%systemd_post oeaware.service + %files %attr(0750, root, root) %{_bindir}/oeaware %attr(0750, root, root) %{_bindir}/oeawarectl @@ -47,6 +54,10 @@ install -D -p -m 0644 oeaware.service %{buildroot}%{_unitdir}/oeaware.service %attr(0644, root, root) %{_unitdir}/oeaware.service %changelog +* Wed May 8 2024 fly_1997 -v1.0.0-5 +- fix systemd service uninstallation issue +- fix load error and args parsing error printing + * Mon May 6 2024 fly_1997 -v1.0.0-4 - add logs, modify sock file permission and serivce file