From b758657c6528ef2046a831ac915ea706bc17842f Mon Sep 17 00:00:00 2001 From: Hepatica Date: Sat, 17 Aug 2024 03:27:37 +0200 Subject: [PATCH] broken code --- SpCloudMain/Controllers/PublishController.cpp | 51 ++++++-- SpCloudMain/Models/App.cpp | 39 +++--- SpCloudMain/Service/FileProcessingService.cpp | 111 +++++++++++++++--- SpCloudMain/Service/MongoDbService.cpp | 3 +- SpCloudMain/SpCloudMain.cpp | 5 +- 5 files changed, 163 insertions(+), 46 deletions(-) diff --git a/SpCloudMain/Controllers/PublishController.cpp b/SpCloudMain/Controllers/PublishController.cpp index 7ca47eb..6e4ba3e 100644 --- a/SpCloudMain/Controllers/PublishController.cpp +++ b/SpCloudMain/Controllers/PublishController.cpp @@ -23,8 +23,6 @@ private: int last_available_port = 8081; - //std::string publish_app_path = "C:/Temps/";// Todo delete if not needed - public: PublishController(httplib::Server& svr, AuthorizationService authorization, std::shared_ptr file_processing, Logger& logger) : authorization(authorization), file_processing(file_processing), logger_(logger) @@ -42,21 +40,40 @@ public: //if (true) {//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS if (file_processing->save_file(filename, content)) { - file_processing->unzip(filename, this->publish_app_path + app->get_user_id());//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS + std::string app_final_file_path = app->get_name() + app->get_user_id();//TODO VERY IMPORTANT CHANGE THIS RANDOM GENERATING TO GENERATE UNIQUE STRING - /*check_port_and_increase_if_not_available(); + logger_.log(INFO, "app_final_file_path: " + app_final_file_path); - file_processing->adjust_nginx_configuration_and_reloud(app->get_name(), std::to_string(last_available_port));*/ + file_processing->unzip(filename, this->publish_app_path + app_final_file_path);//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS + check_port_and_increase_if_not_available(); + + file_processing->adjust_nginx_configuration_and_reloud(app->get_name(), std::to_string(last_available_port)); + + file_processing->create_service_file(this->publish_app_path, app_final_file_path);//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS //file_processing->create_service_file(app->get_name());//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS - //this->dotnet_publish(this->publish_app_path + app->get_user_id(), last_available_port);//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS + if (app->get_target() == "dotnet network") + { + this->dotnet_publish(this->publish_app_path + app_final_file_path, last_available_port);//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS + } - //Todo introduce old binary file + if (app->get_target() == "dotnet") + { + this->dotnet_publish(this->publish_app_path + app_final_file_path);//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS + } + + //Todo introduce increase counter of available app for user in mongo db file_processing->delete_file(filename); - return "File uploaded successfully: " + filename; + /*app->set_url("https://" + app->get_name() + ".almavid.ru/"); + + app->set_url_on_local_machine("http://localhost:" + std::to_string(last_available_port));*/ + + //app->set_service_name(app_final_file_path); + + return "Publish successfully: " + filename; } else { return "Failed to save file, please ensure you are putting rar file" + filename; @@ -72,7 +89,23 @@ private: void dotnet_publish(const std::string& path, int port) {//Todo adjust to build setting from mongodb std::string dll_file_name = file_processing->find_file_by_suffix(path, "exe"); - //Todo introduce deleting old rar file after publishing + size_t pos = dll_file_name.find(".exe"); + if (pos != std::string::npos) { + dll_file_name.replace(pos, 4, ".dll"); + } + + std::string command = R"(dotnet )" + path + "/" + dll_file_name + " --urls http://localhost:" + std::to_string(port); + + logger_.log(INFO, "dotnet_publish command : " + command); + + std::thread commandThread(&CommandService::execute_command, command); + + commandThread.detach(); + } + + void dotnet_publish(const std::string& path) + {//Todo adjust to build setting from mongodb + std::string dll_file_name = file_processing->find_file_by_suffix(path, "exe"); size_t pos = dll_file_name.find(".exe"); if (pos != std::string::npos) { dll_file_name.replace(pos, 4, ".dll"); diff --git a/SpCloudMain/Models/App.cpp b/SpCloudMain/Models/App.cpp index 9ab3f19..73d7c88 100644 --- a/SpCloudMain/Models/App.cpp +++ b/SpCloudMain/Models/App.cpp @@ -3,27 +3,30 @@ #define APP_H class App { private: - std::string name; - std::string user_id; - std::string url; - std::string url_on_local_machine; - std::string target; + std::string name; + std::string user_id; + std::string url; + std::string url_on_local_machine; + std::string target; + //std::string service_name; public: - App(const std::string& name, const std::string& user_id, const std::string& url, - const std::string& url_on_local_machine, const std::string& target) - : name(name), user_id(user_id), url(url), url_on_local_machine(url_on_local_machine), target(target) {} + App(const std::string& name, const std::string& user_id, const std::string& url, + const std::string& url_on_local_machine, const std::string& target/*, const std::string& service_name*/) + : name(name), user_id(user_id), url(url), url_on_local_machine(url_on_local_machine), target(target)/*, service_name(service_name)*/ {} - std::string get_name() const { return name; } - std::string get_user_id() const { return user_id; } - std::string get_url() const { return url; } - std::string get_url_on_local_machine() const { return url_on_local_machine; } - std::string get_target() const { return target; } + std::string get_name() const { return name; } + std::string get_user_id() const { return user_id; } + std::string get_url() const { return url; } + std::string get_url_on_local_machine() const { return url_on_local_machine; } + std::string get_target() const { return target; } + //std::string get_service_name() const { return service_name; } - void set_name(const std::string& name) { this->name = name; } - void set_user_id(const std::string& user_id) { this->user_id = user_id; } - void set_url(const std::string& url) { this->url = url; } - void set_url_on_local_machine(const std::string& url_on_local_machine) { this->url_on_local_machine = url_on_local_machine; } - void set_target(const std::string& target) { this->target = target; } + void set_name(const std::string& name) { this->name = name; } + void set_user_id(const std::string& user_id) { this->user_id = user_id; } + void set_url(const std::string& url) { this->url = url; } + void set_url_on_local_machine(const std::string& url_on_local_machine) { this->url_on_local_machine = url_on_local_machine; } + void set_target(const std::string& target) { this->target = target; } + //void set_service_name(const std::string& service_name) { this->service_name = service_name; } }; #endif // APP_H diff --git a/SpCloudMain/Service/FileProcessingService.cpp b/SpCloudMain/Service/FileProcessingService.cpp index db6a7d1..4228641 100644 --- a/SpCloudMain/Service/FileProcessingService.cpp +++ b/SpCloudMain/Service/FileProcessingService.cpp @@ -21,7 +21,7 @@ public: void adjust_nginx_configuration_and_reloud(const std::string& filename, std::string port) { - std::string file_path = "/etc/nginx/nginx.conf"; + /*std::string file_path = "/etc/nginx/nginx.conf"; std::string new_text = "server {\n" @@ -31,7 +31,7 @@ public: " ssl_certificate_key /etc/letsencrypt/live/almavid.ru/privkey.pem;\n" " ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\n" " ssl_ciphers HIGH:!aNULL:!MD5;\n\n" - " client_max_body_size 2G; // Allow file uploads up to 2GB\n\n" + " client_max_body_size 2G; # Allow file uploads up to 2GB\n\n" " location / {\n" " proxy_pass http://localhost:" + port + ";\n" " proxy_set_header Host $host;\n" @@ -54,14 +54,81 @@ public: file << new_text << '\n'; - file.close(); + file.close();*/ - // Reload Nginx to apply the changes - /*int result = std::system("sudo systemctl reload nginx"); - if (result != 0) { - std::cerr << "Error: Failed to reload Nginx" << std::endl; - return 1; - }*/ + + std::string file_path = "/etc/nginx/nginx.conf"; + + // Open the nginx.conf file in read mode first + std::ifstream file_in(file_path); + if (!file_in.is_open()) { + logger_.log(INFO, "Error: Could not open file " + file_path + strerror(errno) + '\n'); + return; + } + + std::string content; + std::string line; + std::string temp_content; + + // Read the file content and store it in a temporary string + while (std::getline(file_in, line)) { + temp_content += line + "\n"; + } + file_in.close(); + + // Remove the last occurrence of "}" in the content + size_t last_brace_pos = temp_content.rfind("}"); + if (last_brace_pos != std::string::npos) { + temp_content.erase(last_brace_pos); + } + + // Now create the new server block + std::string new_text = + "\nserver {\n" + " listen 443 ssl;\n" + " server_name " + filename + ".almavid.ru;\n\n" + " ssl_certificate /etc/letsencrypt/live/almavid.ru/fullchain.pem;\n" + " ssl_certificate_key /etc/letsencrypt/live/almavid.ru/privkey.pem;\n" + " ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\n" + " ssl_ciphers HIGH:!aNULL:!MD5;\n\n" + " client_max_body_size 2G; # Allow file uploads up to 2GB\n\n" + " location / {\n" + " proxy_pass http://localhost:" + port + ";\n" + " proxy_set_header Host $host;\n" + " proxy_set_header X-Real-IP $remote_addr;\n" + " proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n" + " proxy_set_header X-Forwarded-Proto $scheme;\n\n" + " # Support for WebSocket\n" + " proxy_http_version 1.1;\n" + " proxy_set_header Upgrade $http_upgrade;\n" + " proxy_set_header Connection \"upgrade\";\n" + " }\n" + "}\n"; + + // Append the new server block to the existing content + temp_content += new_text; + + // Add the final closing brace + temp_content += "}\n"; + + // Write the updated content back to the file + std::ofstream file_out(file_path); + if (!file_out.is_open()) { + logger_.log(INFO, "Error: Could not open file " + file_path + strerror(errno) + '\n'); + return; + } + file_out << temp_content; + file_out.close(); + + + + + + + + + + //TODO FIX BUG WITH }}}}} std::string command = "sudo systemctl reload nginx"; @@ -109,10 +176,16 @@ public: return ofs.good(); } - void create_service_file(std::string name) + void create_service_file(std::string path, std::string name) { logger_.log(INFO, "Start create_service_file"); + std::string dll_file_name = find_file_by_suffix(path, "exe"); + size_t pos = dll_file_name.find(".exe"); + if (pos != std::string::npos) { + dll_file_name.replace(pos, 4, ".dll"); + } + std::string filename = "/etc/systemd/system/" + name + ".service"; std::ofstream serviceFile(filename); @@ -122,9 +195,9 @@ public: serviceFile << "After=network.target\n\n"; serviceFile << "[Service]\n"; - serviceFile << "ExecStart=/home/danilt2000/SpCloud/" + name + "/build/ " + name + "\n"; + serviceFile << "ExecStart=/usr/bin/dotnet /home/danilt2000/SpCloud/" + name + "/" + dll_file_name + "\n"; //serviceFile << "ExecStart=/home/danilt2000/SpCloud/SpCloudMain/build/SpCloudMain\n"; - serviceFile << "WorkingDirectory=/home/danilt2000/SpCloud/" + name + "/build\n"; + serviceFile << "WorkingDirectory=/home/danilt2000/SpCloud/" + name + "\n"; //serviceFile << "WorkingDirectory=/home/danilt2000/SpCloud/SpCloudMain/build\n"; serviceFile << "Restart=always\n"; serviceFile << "User=danilt2000\n"; @@ -134,11 +207,17 @@ public: serviceFile << "[Install]\n"; serviceFile << "WantedBy=multi-user.target\n"; - std::string command = "sudo systemctl daemon-reload"; + std::string command_reload = "sudo systemctl daemon-reload"; - std::thread commandThread(&CommandService::execute_command, command); + std::thread commandThreadReload(&CommandService::execute_command, command_reload); - commandThread.join(); + commandThreadReload.join(); + + std::string command_start = "sudo systemctl start " + name + ".service"; + + std::thread commandThreadStart(&CommandService::execute_command, commandThreadStart); + + commandThreadStart.join(); //Todo check service ->sudo systemctl status .service @@ -165,7 +244,7 @@ public: std::thread commandThread(&CommandService::execute_command, command); - commandThread.join(); + commandThread.join();//Todo check if unzipping happening before of deleting file } std::string find_file_by_suffix(const std::string& directory, const std::string& suffix) { diff --git a/SpCloudMain/Service/MongoDbService.cpp b/SpCloudMain/Service/MongoDbService.cpp index 0c19abf..218537a 100644 --- a/SpCloudMain/Service/MongoDbService.cpp +++ b/SpCloudMain/Service/MongoDbService.cpp @@ -114,7 +114,7 @@ public: return "App name isn't free please select another one"; } - std::string add_app(std::string name, std::string user_id, std::string url, std::string url_on_local_mahcine, std::string target) + std::string add_app(std::string name, std::string user_id, std::string url, std::string url_on_local_mahcine, std::string target/*, std::string service_name*/) { std::string json_data = R"({ "dataSource": "SpCloudCluster", @@ -124,6 +124,7 @@ public: "name": ")" + name + R"(", "user_id": ")" + user_id + R"(", "url": ")" + url + R"(", + "service_name": ")" + "TEST" + R"(", "url_on_local_machine": ")" + url_on_local_mahcine + R"(", "target": ")" + target + R"(" } diff --git a/SpCloudMain/SpCloudMain.cpp b/SpCloudMain/SpCloudMain.cpp index 26304f7..a6d1403 100644 --- a/SpCloudMain/SpCloudMain.cpp +++ b/SpCloudMain/SpCloudMain.cpp @@ -72,11 +72,12 @@ int main() return; } - App* app = new App(name, user_id, "url", "local_url", target); + App* app = new App(name, user_id, "url", "local_url", target/*,"service_name"*/); publish_controller.process_publish(req, app); - //mongo_service.add_app("test", "test", "test", "test", "test");//TODO UNCOMMENT AND FIX + //mongo_service.add_app(app->get_name(), app->get_user_id(), app->get_url(), + // app->get_url_on_local_machine(), app->get_target()/*, app->get_service_name()*/);//TODO UNCOMMENT AND FIX delete app;