oeAware-manager/0004-fix-auto-enable-error-and-check-plugin-list-config.patch
fly_1997 9b1ab30deb fix spec file and some bugs
(cherry picked from commit ca6063d6790f27ef39fb17c5ec7e917c99d83dce)
2024-04-29 20:28:48 +08:00

201 lines
7.2 KiB
Diff

From 7ff05329ac299727fc2be5d7f1a92a4e3b0bdd43 Mon Sep 17 00:00:00 2001
From: fly_1997 <flylove7@outlook.com>
Date: Mon, 29 Apr 2024 17:31:44 +0800
Subject: [PATCH 4/4] fix auto enable error and check plugin list config
---
oeaware.service | 2 +-
src/client/arg_parse.cpp | 7 ++++---
src/common/utils.cpp | 34 ++++++++++++++++++-------------
src/plugin_mgr/config.cpp | 21 ++++++++++++++-----
src/plugin_mgr/config.h | 3 +++
src/plugin_mgr/plugin_manager.cpp | 11 +++++++++-
6 files changed, 54 insertions(+), 24 deletions(-)
diff --git a/oeaware.service b/oeaware.service
index 3ab4b69..b321530 100644
--- a/oeaware.service
+++ b/oeaware.service
@@ -5,7 +5,7 @@ After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/oeaware /etc/oeAware/config.yaml
-ExecStop=kill -9 $MAINPID
+ExecStop=kill $MAINPID && sleep 5 && kill -9 $MAINPID
Restart=on-failure
RestartSec=1
RemainAfterExit=yes
diff --git a/src/client/arg_parse.cpp b/src/client/arg_parse.cpp
index cbf0b8e..70fcd4a 100644
--- a/src/client/arg_parse.cpp
+++ b/src/client/arg_parse.cpp
@@ -30,6 +30,7 @@ const struct option ArgParse::long_options[] = {
void ArgParse::arg_error(const std::string &msg) {
std::cerr << "oeawarectl: " << msg << "\n";
+ print_help();
exit(EXIT_FAILURE);
}
@@ -42,7 +43,7 @@ void ArgParse::set_arg(char *_arg) {
}
void ArgParse::print_help() {
- std::cout << "oeawarectl [options]...\n"
+ std::cout << "usage: oeawarectl [options]...\n"
" options\n"
" -l|--load [plugin] load plugin and need plugin type.\n"
" -t|--type [plugin_type] assign plugin type. there are three types:\n"
@@ -76,12 +77,12 @@ int ArgParse::init(int argc, char *argv[]) {
help = true;
break;
case '?':
- arg_error("unknown option. See --help.");
+ arg_error("unknown option.");
return -1;
default: {
if (opt == 'l' || opt == 'r' || opt == 'q' || opt == 'Q' || opt == 'e' || opt == 'd' || opt == 'L' || opt == 'i') {
if (cmd != -1) {
- arg_error("invalid option. See --help.\n");
+ arg_error("invalid option.");
return -1;
}
cmd = opt;
diff --git a/src/common/utils.cpp b/src/common/utils.cpp
index 9435a5b..f2c277d 100644
--- a/src/common/utils.cpp
+++ b/src/common/utils.cpp
@@ -23,28 +23,34 @@ static void curl_set_opt(CURL *curl, const std::string &url, FILE *file) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
}
+static bool curl_handle(CURL *curl, const std::string &url, const std::string &path) {
+ FILE *file = fopen(path.c_str(), "wb");
+ if (file == nullptr) {
+ return false;
+ }
+ curl_set_opt(curl, url, file);
+ CURLcode res = curl_easy_perform(curl);
+ long http_code = 0;
+ curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
+ fclose(file);
+ if (res == CURLE_OK && http_code >= 200 && http_code < 300) {
+ return true;
+ }
+ return false;
+}
+
// Downloads file from the specified url to the path.
bool download(const std::string &url, const std::string &path) {
CURL *curl = nullptr;
- CURLcode res;
- bool ok = true;
+ bool ret = true;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
- FILE *file = fopen(path.c_str(), "wb");
- if (file == nullptr) {
- return false;
- }
- curl_set_opt(curl, url, file);
- res = curl_easy_perform(curl);
- fclose(file);
- if (res != CURLE_OK) {
- ok = false;
- }
+ if (!curl_handle(curl, url, path)) ret = false;
} else {
- ok = false;
+ ret = false;
}
curl_global_cleanup();
curl_easy_cleanup(curl);
- return ok;
+ return ret;
}
\ No newline at end of file
diff --git a/src/plugin_mgr/config.cpp b/src/plugin_mgr/config.cpp
index 1de5a34..3c76e8e 100644
--- a/src/plugin_mgr/config.cpp
+++ b/src/plugin_mgr/config.cpp
@@ -30,6 +30,18 @@ bool create_dir(const std::string &path) {
return true;
}
+bool check_plugin_list(YAML::Node plugin_list_item) {
+ if (plugin_list_item["name"].IsNull()) {
+ std::cerr << "Warn: null name in plugin_list.\n";
+ return false;
+ }
+ if (plugin_list_item["url"].IsNull()) {
+ std::cerr << "Warn: null url in plugin_list.\n";
+ return false;
+ }
+ return true;
+}
+
bool Config::load(const std::string path) {
YAML::Node node;
struct stat buffer;
@@ -49,16 +61,15 @@ bool Config::load(const std::string path) {
YAML::Node plugin_list = node["plugin_list"];
if (plugin_list.IsSequence()) {
for (int i = 0; i < plugin_list.size(); ++i) {
+ if (!check_plugin_list(plugin_list[i])){
+ continue;
+ }
std::string name = plugin_list[i]["name"].as<std::string>();
std::string description = plugin_list[i]["description"].as<std::string>();
std::string url = plugin_list[i]["url"].as<std::string>();
PluginInfo info(name, description, url);
- if (name.empty()) {
- std::cerr << "Warn: " << name << " url is empty.\n";
- continue;
- }
if (this->plugin_list.count(name)) {
- std::cerr << "Warn: duplicate " << name << " in plugin_list.\n";
+ std::cerr << "Warn: duplicate \"" << name << "\" in plugin_list.\n";
continue;
}
this->plugin_list.insert(std::make_pair(name, info));
diff --git a/src/plugin_mgr/config.h b/src/plugin_mgr/config.h
index 16c7871..5ab7672 100644
--- a/src/plugin_mgr/config.h
+++ b/src/plugin_mgr/config.h
@@ -65,6 +65,9 @@ public:
size_t get_instance_size() const {
return this->instance.size();
}
+ std::string get_instance_name(int i) {
+ return this->instance[i];
+ }
std::string get_name() const {
return this->name;
}
diff --git a/src/plugin_mgr/plugin_manager.cpp b/src/plugin_mgr/plugin_manager.cpp
index c9981ef..0826a60 100644
--- a/src/plugin_mgr/plugin_manager.cpp
+++ b/src/plugin_mgr/plugin_manager.cpp
@@ -348,7 +348,16 @@ void PluginManager::pre_enable() {
}
std::shared_ptr<Plugin> plugin = memory_store.get_plugin(name);
for (int j = 0; j < plugin->get_instance_len(); ++j) {
- instance_enabled(plugin->get_instance(i)->get_name());
+ instance_enabled(plugin->get_instance(j)->get_name());
+ }
+ } else {
+ for (int j = 0; j < item.get_instance_size(); ++j) {
+ std::string name = item.get_instance_name(j);
+ if (!memory_store.is_instance_exist(name)) {
+ WARN("[PluginManager] instance " << name << " cannot be enabled, because it does not exist.");
+ continue;
+ }
+ instance_enabled(name);
}
}
}
--
2.33.0