This commit is contained in:
Your Name
2025-04-25 18:54:31 +12:00
parent bbf2575718
commit d028a4d8c6
15 changed files with 195 additions and 15 deletions

View File

@ -11,6 +11,7 @@
#include <iomanip>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <execution>
namespace fs = boost::filesystem;
@ -55,18 +56,20 @@ void list_servers() {
tableprint tp("All DropShell Servers");
tp.add_row({"Name", "Address", "Health", "Ports"});
for (const auto& server : servers) {
std::for_each(std::execution::par, servers.begin(), servers.end(), [&](const ServerInfo& server) {
std::vector<int> ports_used;
std::string serviceticks = "";
std::vector<ServiceInfo> services = get_server_services_info(server.name);
for (const auto& service : services) {
std::for_each(std::execution::par, services.begin(), services.end(), [&](const ServiceInfo& service) {
service_runner ss;
if (ss.init(server.name, service.service_name))
serviceticks += ss.healthmark() + " ";
else std::cout<<"Error: Failed to initialise service runner for server: ["<<server.name<<"] and service: ["<<service.service_name<<"]"<<std::endl;
std::vector<int> ports = ss.get_ports();
ports_used.insert(ports_used.end(), ports.begin(), ports.end());
}
});
// convert ports_used to string
std::string ports_used_str = "";
bool first = true;
@ -78,7 +81,7 @@ void list_servers() {
first = false;
}
tp.add_row({server.name, server.ssh_host, serviceticks, ports_used_str});
}
});
tp.print();
}

View File

@ -1,6 +1,7 @@
#include "templates.hpp"
#include "config.hpp"
#include "utils/directories.hpp"
#include "utils/utils.hpp"
#include <filesystem>
#include <iostream>
#include <fstream>
@ -155,7 +156,10 @@ void create_template(const std::string& template_name) {
std::string replacement_line = "TEMPLATE=" + template_name;
// replace the line in the example/service.env file with the replacement line
std::string service_env_path = new_template_path + "/example/service.env";
if (!replace_line_in_file(service_env_path, search_string, replacement_line)) {
std::cerr << "Error: Failed to replace TEMPLATE= line in service.env file" << std::endl;
return;
}
// 3. Print out the README.txt file and the path
std::string readme_path = new_template_path + "/README.txt";

View File

@ -134,14 +134,24 @@ std::string get_local_service_env_path(const std::string &server_name, const std
return env.get_DROPSHELL_DIR();
}
std::string get_remote_service_path(const std::string &server_name, const std::string &service_name)
std::string get_remote_services_path(const std::string &server_name)
{
if (server_name.empty() || service_name.empty())
if (server_name.empty())
return std::string();
std::string dropshell_path = get_remote_DROPSHELL_path(server_name);
if (dropshell_path.empty())
return std::string();
return (fs::path(dropshell_path) / service_name).string();
return (fs::path(dropshell_path) / "services").string();
}
std::string get_remote_service_path(const std::string &server_name, const std::string &service_name)
{
if (server_name.empty() || service_name.empty())
return std::string();
std::string services_path = get_remote_services_path(server_name);
if (services_path.empty())
return std::string();
return (fs::path(services_path) / service_name).string();
}
std::string get_remote_service_config_path(const std::string &server_name, const std::string &service_name)

View File

@ -23,14 +23,16 @@ namespace dropshell {
// remote paths
// DROPSHELL_DIR
// |-- service name
// |-- config
// |-- service.env
// |-- (user config files)
// |-- template
// |-- (script files)
// |-- backups
// |-- backups
// |-- services
// |-- service name
// |-- config <-- this is passed as argument to all scripts
// |-- service.env
// |-- (user config files)
// |-- template
// |-- (script files)
std::string get_remote_DROPSHELL_path(const std::string &server_name);
std::string get_remote_services_path(const std::string &server_name);
std::string get_remote_service_path(const std::string &server_name, const std::string &service_name);
std::string get_remote_service_config_path(const std::string &server_name, const std::string &service_name);
std::string get_remote_service_template_path(const std::string &server_name, const std::string &service_name);

View File

@ -1,6 +1,8 @@
#include "utils.hpp"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
namespace dropshell {
@ -10,4 +12,34 @@ void maketitle(const std::string& title) {
std::cout << std::string(title.length() + 4, '-') << std::endl;
}
bool replace_line_in_file(const std::string& file_path, const std::string& search_string, const std::string& replacement_line) {
std::ifstream input_file(file_path);
std::vector<std::string> file_lines;
std::string line;
if (!input_file.is_open()) {
std::cerr << "Error: Unable to open file: " << file_path << std::endl;
return false;
}
while (std::getline(input_file, line)) {
if (line.find(search_string) != std::string::npos)
file_lines.push_back(replacement_line);
else
file_lines.push_back(line);
}
input_file.close();
std::ofstream output_file(file_path);
if (!output_file.is_open())
{
std::cerr << "Error: Unable to open file: " << file_path << std::endl;
return false;
}
for (const auto& modified_line : file_lines)
output_file << modified_line << "\n";
output_file.close();
return true;
}
} // namespace dropshell

View File

@ -11,4 +11,6 @@ namespace dropshell {
*/
void maketitle(const std::string& title);
bool replace_line_in_file(const std::string& file_path, const std::string& search_string, const std::string& replacement_line);
} // namespace dropshell