broken code

This commit is contained in:
Hepatica
2024-08-17 03:27:37 +02:00
parent 2356358163
commit b758657c65
5 changed files with 163 additions and 46 deletions

View File

@@ -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-name>.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) {