mirror of
https://github.com/yawaflua/SpCloudCore.git
synced 2025-12-09 20:19:35 +02:00
Reworking spcloudmain and add a security policy
This commit is contained in:
@@ -13,7 +13,9 @@ add_executable(SpCloudMain
|
||||
"Service/DiscordService.cpp"
|
||||
"Service/MongoDbService.cpp"
|
||||
"Service/Logger.cpp"
|
||||
"Models/User.cpp" "Models/App.cpp")
|
||||
"Models/User.cpp"
|
||||
"Models/App.cpp"
|
||||
)
|
||||
|
||||
if (CMAKE_VERSION VERSION_GREATER 3.12)
|
||||
set_property(TARGET SpCloudMain PROPERTY CXX_STANDARD 20)
|
||||
|
||||
@@ -36,10 +36,10 @@ public:
|
||||
|
||||
const auto& filename = this->publish_app_path + req.files.begin()->second.filename;
|
||||
|
||||
if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".rar") {
|
||||
if (filename.size() >= 4) {
|
||||
if (file_processing->save_file(filename, content)) {
|
||||
|
||||
std::string app_final_file_path = app->get_name() + app->get_user_id();
|
||||
std::string app_final_file_path = app->get_name() + "_" + app->get_user_id();
|
||||
|
||||
logger_.log(INFO, "app_final_file_path: " + app_final_file_path);
|
||||
|
||||
@@ -49,12 +49,9 @@ public:
|
||||
{
|
||||
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->adjust_nginx_configuration_and_reload(app->get_name(), std::to_string(last_available_port));//
|
||||
|
||||
file_processing->create_service_file_dotnet(this->publish_app_path, app_final_file_path,
|
||||
std::to_string(last_available_port), true);
|
||||
|
||||
//this->dotnet_publish(this->publish_app_path + app_final_file_path, last_available_port);//Test
|
||||
this->dotnet_publish(this->publish_app_path + app_final_file_path, app_final_file_path, last_available_port);//Test
|
||||
|
||||
app->set_url("https://" + app->get_name() + ".almavid.ru/");
|
||||
|
||||
@@ -63,10 +60,7 @@ public:
|
||||
|
||||
if (app->get_target() == "dotnet")
|
||||
{
|
||||
file_processing->create_service_file_dotnet(this->publish_app_path, app_final_file_path,
|
||||
std::to_string(last_available_port), false);
|
||||
|
||||
//this->dotnet_publish(this->publish_app_path + app_final_file_path);//Test
|
||||
this->dotnet_publish(this->publish_app_path + app_final_file_path, app_final_file_path);
|
||||
|
||||
app->set_url("Worker Service");
|
||||
|
||||
@@ -94,27 +88,33 @@ public:
|
||||
const auto& content = req.files.begin()->second.content;
|
||||
|
||||
const auto& filename = this->publish_app_path + req.files.begin()->second.filename;
|
||||
|
||||
|
||||
if (file_processing->save_file(filename, content)) {
|
||||
|
||||
std::string app_final_file_path = app->get_name() + app->get_user_id();
|
||||
|
||||
if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".rar") {
|
||||
if (file_processing->save_file(filename, content)) {
|
||||
logger_.log(INFO, "app_final_file_path: " + app_final_file_path);
|
||||
|
||||
std::string app_final_file_path = app->get_name() + app->get_user_id();
|
||||
file_processing->delete_file(this->publish_app_path + app_final_file_path);
|
||||
|
||||
logger_.log(INFO, "app_final_file_path: " + app_final_file_path);
|
||||
|
||||
file_processing->delete_file(this->publish_app_path + app_final_file_path);
|
||||
|
||||
file_processing->unzip(filename, this->publish_app_path + app_final_file_path);
|
||||
|
||||
file_processing->stop_and_start_service_file(app_final_file_path);
|
||||
|
||||
file_processing->delete_file(filename);
|
||||
}
|
||||
else {
|
||||
return "Invalid file type. Only .rar files are allowed." + filename;
|
||||
file_processing->unzip(filename, this->publish_app_path + app_final_file_path);
|
||||
|
||||
file_processing->stop_service_file(app_final_file_path);
|
||||
|
||||
if (app->get_target() == "dotnet network") {
|
||||
check_port_and_increase_if_not_available();
|
||||
this->dotnet_publish(this->publish_app_path + app_final_file_path, app_final_file_path, last_available_port);
|
||||
}else {
|
||||
this->dotnet_publish(this->publish_app_path + app_final_file_path, app_final_file_path);
|
||||
}
|
||||
|
||||
file_processing->delete_file(filename);
|
||||
}
|
||||
else {
|
||||
return "Invalid file type. Only .rar files are allowed." + filename;
|
||||
}
|
||||
|
||||
|
||||
return "Success";
|
||||
}
|
||||
@@ -125,48 +125,38 @@ public:
|
||||
|
||||
logger_.log(INFO, "app_final_file_path: " + app_final_file_path);
|
||||
|
||||
file_processing->stop_service_file(app_final_file_path);
|
||||
|
||||
file_processing->delete_file(this->publish_app_path + app_final_file_path);
|
||||
|
||||
file_processing->delete_file("/etc/systemd/system/" + app_final_file_path + ".service");
|
||||
|
||||
file_processing->remove_nginx_configuration_block_and_reload(app->get_name());
|
||||
|
||||
file_processing->stop_service_file(app_final_file_path);
|
||||
|
||||
return "Success";
|
||||
}
|
||||
|
||||
private:
|
||||
void dotnet_publish(const std::string& path, int port)
|
||||
void dotnet_publish(const std::string& path, const std::string& folderName, int port)
|
||||
{
|
||||
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");
|
||||
}
|
||||
std::thread commandThreadBuild(&CommandService::execute_command, "docker build -t " + folderName + ".");
|
||||
std::thread commandThreadRun(&CommandService::execute_command, "docker run -d -e ASPNETCORE_URLS=http://0.0.0.0:" + std::to_string(port) + "-p" + std::to_string(port)+":"+std::to_string(port) + "--name " + folderName + " " + folderName);
|
||||
|
||||
std::string command = R"(dotnet )" + path + "/" + dll_file_name + " --urls http://localhost:" + std::to_string(port);
|
||||
logger_.log(INFO, "docker running container : " + folderName + " with port : " + std::to_string(port));
|
||||
|
||||
logger_.log(INFO, "dotnet_publish command : " + command);
|
||||
|
||||
std::thread commandThread(&CommandService::execute_command, command);
|
||||
|
||||
commandThread.detach();
|
||||
commandThreadBuild.detach();
|
||||
commandThreadRun.detach();
|
||||
}
|
||||
|
||||
void dotnet_publish(const std::string& path)//Todo test publishing not network app
|
||||
void dotnet_publish(const std::string& path, const std::string& folderName)
|
||||
{
|
||||
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");
|
||||
}
|
||||
std::thread commandThreadBuild(&CommandService::execute_command, "docker build -t " + folderName + ".");
|
||||
std::thread commandThreadRun(&CommandService::execute_command, "docker run -d --name " + folderName + " " + folderName);
|
||||
|
||||
std::string command = R"(dotnet )" + path + "/" + dll_file_name;
|
||||
logger_.log(INFO, "docker running container : " + folderName);
|
||||
|
||||
std::thread commandThread(&CommandService::execute_command, command);
|
||||
|
||||
commandThread.detach();
|
||||
commandThreadBuild.detach();
|
||||
commandThreadRun.detach();
|
||||
}
|
||||
|
||||
void check_port_and_increase_if_not_available()
|
||||
|
||||
@@ -26,7 +26,7 @@ public:
|
||||
|
||||
}
|
||||
|
||||
void adjust_nginx_configuration_and_reloud(const std::string& filename, std::string port)
|
||||
void adjust_nginx_configuration_and_reload(const std::string& filename, std::string port)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(nginx_config_mutex);
|
||||
|
||||
@@ -86,7 +86,7 @@ public:
|
||||
file_out << temp_content;
|
||||
file_out.close();
|
||||
|
||||
std::string command = "sudo systemctl reload nginx";
|
||||
std::string command = "cd ~/SpCloud/Infrastructure/ && docker compose -f nginx.yml up --build -d ";
|
||||
|
||||
std::thread commandThread(&CommandService::execute_command, command);
|
||||
|
||||
@@ -156,7 +156,7 @@ public:
|
||||
|
||||
outFile.close();
|
||||
|
||||
std::string command = "sudo systemctl reload nginx";
|
||||
std::string command = "cd ~/SpCloud/Infrastructure/ && docker compose -f nginx.yml up --build -d ";
|
||||
|
||||
std::thread commandThread(&CommandService::execute_command, command);
|
||||
|
||||
@@ -207,81 +207,25 @@ public:
|
||||
|
||||
void stop_and_start_service_file(std::string name)
|
||||
{
|
||||
std::string command_stop = "sudo systemctl stop " + name + ".service";
|
||||
std::string command_reload = "docker stop " + name;
|
||||
|
||||
std::string command_start = "sudo systemctl start " + name + ".service";
|
||||
std::string response_reload = execute_and_log_command(command_reload);
|
||||
|
||||
std::string response_reload = execute_and_log_command(command_stop);
|
||||
|
||||
std::string response_enable = execute_and_log_command(command_start);
|
||||
}
|
||||
|
||||
//Outdated
|
||||
void stop_service_file(std::string name)
|
||||
{
|
||||
std::string command_stop = "sudo systemctl stop " + name + ".service";
|
||||
std::string command_stop = "docker restart " + name;
|
||||
|
||||
std::string response_reload = execute_and_log_command(command_stop);
|
||||
}
|
||||
|
||||
void create_service_file_dotnet(std::string path, std::string name, std::string port, bool is_asp)
|
||||
{
|
||||
logger_.log(INFO, "Start create_service_file_dotnet");
|
||||
logger_.log(INFO, "Outdated. Please didnt use create_service_file_dotnet");
|
||||
|
||||
std::string dll_file_name = find_file_by_suffix(path + "/" + name, "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);
|
||||
|
||||
if (serviceFile.is_open()) {
|
||||
serviceFile << "[Unit]\n";
|
||||
serviceFile << "Description=" << name << " Service\n";
|
||||
serviceFile << "After=network.target\n\n";
|
||||
|
||||
std::string exec_start_command = "/usr/bin/dotnet /home/danilt2000/SpCloud/" + name + "/" + dll_file_name;
|
||||
logger_.log(INFO, "ExecStart command: " + exec_start_command);
|
||||
logger_.log(INFO, "ExecStart create_service_file_dotnet");
|
||||
|
||||
serviceFile << "[Service]\n";
|
||||
serviceFile << "ExecStart=" << exec_start_command << "\n";
|
||||
serviceFile << "WorkingDirectory=/home/danilt2000/SpCloud/" + name + "\n";
|
||||
serviceFile << "Restart=always\n";
|
||||
serviceFile << "User=danilt2000\n";
|
||||
|
||||
if (is_asp)
|
||||
{
|
||||
serviceFile << "Environment=ASPNETCORE_URLS=http://0.0.0.0:" + port + "\n";
|
||||
}
|
||||
|
||||
|
||||
serviceFile << "Environment=PATH=/usr/bin\n";
|
||||
serviceFile << "Environment=NODE_ENV=production\n\n";
|
||||
|
||||
serviceFile << "[Install]\n";
|
||||
serviceFile << "WantedBy=multi-user.target\n";
|
||||
|
||||
serviceFile.close();
|
||||
|
||||
std::string command_reload = "sudo systemctl daemon-reload";
|
||||
std::string command_enable = "sudo systemctl enable " + name + ".service";
|
||||
std::string command_start = "sudo systemctl start " + name + ".service";
|
||||
|
||||
std::string response_reload = execute_and_log_command(command_reload);
|
||||
std::string response_enable = execute_and_log_command(command_enable);
|
||||
std::string response_start = execute_and_log_command(command_start);
|
||||
|
||||
|
||||
logger_.log(INFO, "Service file " + filename + " created successfully.\n");
|
||||
}
|
||||
else {
|
||||
logger_.log(INFO, "Unable to open file " + filename + " for writing: " + strerror(errno) + "\n");
|
||||
}
|
||||
|
||||
}
|
||||
std::string execute_and_log_command(const std::string& command) {
|
||||
std::string result = execute_command(command);
|
||||
@@ -308,8 +252,13 @@ public:
|
||||
void unzip(const std::string& file_path, const std::string& final_files_directory) {
|
||||
create_directory(final_files_directory);
|
||||
|
||||
std::string command = "unrar x " + file_path + " " + final_files_directory;
|
||||
std::string command = "";
|
||||
|
||||
if (std::filesystem::path(file_path).extension() == ".rar") {
|
||||
command = "unrar x " + file_path + " " + final_files_directory;
|
||||
}else {
|
||||
command = "tar -xzf " + file_path + " -C " + final_files_directory;
|
||||
}
|
||||
logger_.log(INFO, "Start unzip command" + command);
|
||||
|
||||
std::thread commandThread(&CommandService::execute_command, command);
|
||||
@@ -319,7 +268,7 @@ public:
|
||||
|
||||
std::string find_file_by_suffix(const std::string& directory, const std::string& suffix) {
|
||||
for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) {
|
||||
if (entry.is_regular_file() && entry.path().filename().string().ends_with(suffix)) {
|
||||
if (entry.is_regular_file() && std::filesystem::path(entry.path().filename()).extension() == suffix) {
|
||||
return entry.path().filename().string();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -331,6 +331,29 @@ public:
|
||||
return response;
|
||||
}
|
||||
|
||||
std::string get_app(std::string name)//Todo test this method
|
||||
{
|
||||
std::string json_data = R"({
|
||||
"dataSource": "SpCloudCluster",
|
||||
"database": "SpCloud",
|
||||
"collection": "Apps",
|
||||
"filter": {
|
||||
"name": ")" + name + R"("
|
||||
}
|
||||
})";
|
||||
|
||||
std::string command = "curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/find' "
|
||||
"--header 'Content-Type: application/json' "
|
||||
"--header 'api-key: " + std::string(std::getenv("MongoDbApiKey")) + "' "
|
||||
"--data-raw '" + json_data + "'";
|
||||
|
||||
auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command);
|
||||
|
||||
std::string response = request.get();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
std::string get_app_list(std::string user_id)//Todo test this method
|
||||
{
|
||||
std::string json_data = R"({
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "Service/DiscordService.cpp"
|
||||
#include "Service/MongoDbService.cpp"
|
||||
#include "Models/App.cpp"
|
||||
#include "json.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -62,10 +64,10 @@ int main()
|
||||
|
||||
std::string user_id = req.get_file_value("UserId").content;
|
||||
std::string name = req.get_file_value("Name").content;
|
||||
|
||||
if (name.empty() || name == " ")
|
||||
{
|
||||
res.set_content("Select another app name", "text/plain");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -146,8 +148,8 @@ int main()
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
App* app = new App(name, user_id, "url", "local_url", "target", "service_name");
|
||||
nlohmann::json json_data = nlohmann::json::parse(mongo_service.get_app(name));
|
||||
App* app = new App(name, user_id, json_data["url"], json_data["url_on_local_machine"], json_data["target"], json_data["service_name"]);
|
||||
|
||||
publish_controller.process_update(req, app);
|
||||
|
||||
|
||||
24765
SpCloudMain/json.hpp
Normal file
24765
SpCloudMain/json.hpp
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user