This commit is contained in:
Your Name 2025-04-21 12:48:10 +12:00
parent aa476ec88c
commit f101565aed
2 changed files with 202 additions and 0 deletions

190
src/server_service.cpp Normal file
View File

@ -0,0 +1,190 @@
#include "config.hpp"
#include "init_user_directory.hpp"
#include "server_service.hpp"
#include "server_env.hpp"
#include "templates.hpp"
#include "config.hpp"
#include <boost/filesystem.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
namespace fs = boost::filesystem;
namespace dropshell {
std::vector<std::string> get_server_services(const std::string& server_name) {
std::vector<std::string> services;
std::string user_dir;
if (!get_user_directory(user_dir)) {
std::cerr << "Error: User directory not set" << std::endl;
return services;
}
fs::path server_dir = fs::path(user_dir) / "servers" / server_name;
if (!fs::exists(server_dir)) {
std::cerr << "Error: Server directory not found" << std::endl;
return services;
}
// Look for .env files in the server directory
for (const auto& entry : fs::directory_iterator(server_dir)) {
if (entry.path().extension() == ".env" && entry.path().filename().string() != "_server.env") {
services.push_back(entry.path().stem().string());
}
}
return services;
}
server_service::server_service() : m_server_name(""), m_service_name(""), m_server_env(nullptr) {}
bool server_service::init(const std::string& server_name, const std::string& service_name) {
std::string user_dir;
if (!get_user_directory(user_dir)) {
std::cerr << "Error: User directory not set" << std::endl;
return false;
}
// Check if server exists
fs::path server_dir = fs::path(user_dir) / "servers" / server_name;
if (!fs::exists(server_dir)) {
std::cerr << "Error: Server '" << server_name << "' not found" << std::endl;
return false;
}
// Check if service env file exists
fs::path service_env = server_dir / (service_name + ".env");
if (!fs::exists(service_env)) {
std::cerr << "Error: Service environment file not found: " << service_env.string() << std::endl;
return false;
}
// Initialize server environment
try {
m_server_env = std::make_unique<server_env>(server_dir.string());
if (!m_server_env->is_valid()) {
std::cerr << "Error: Invalid server environment" << std::endl;
return false;
}
} catch (const std::exception& e) {
std::cerr << "Error: Failed to initialize server environment: " << e.what() << std::endl;
return false;
}
m_server_name = server_name;
m_service_name = service_name;
return true;
}
bool server_service::install() {
if (!m_server_env) {
std::cerr << "Error: Server service not initialized" << std::endl;
return false;
}
// Check if template exists
template_manager tm;
template_info info;
if (!tm.get_template_info(m_service_name, info)) {
std::cerr << "Error: Template '" << m_service_name << "' not found" << std::endl;
return false;
}
// Construct SSH command
std::stringstream ssh_cmd;
ssh_cmd << "ssh -p " << m_server_env->get_SSH_PORT() << " "
<< m_server_env->get_SSH_USER() << "@" << m_server_env->get_SSH_HOST() << " ";
// Create service directory
std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
std::string mkdir_cmd = ssh_cmd.str() + "'mkdir -p " + service_dir + "/template'";
if (system(mkdir_cmd.c_str()) != 0) {
std::cerr << "Error: Failed to create service directory" << std::endl;
return false;
}
// Copy template files
std::string scp_cmd = "scp -P " + m_server_env->get_SSH_PORT() + " -r " +
info.path + "/* " +
m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" +
service_dir + "/template/";
if (system(scp_cmd.c_str()) != 0) {
std::cerr << "Error: Failed to copy template files" << std::endl;
return false;
}
// Copy service env file
std::string user_dir;
if (!get_user_directory(user_dir)) {
std::cerr << "Error: User directory not set" << std::endl;
return false;
}
fs::path service_env = fs::path(user_dir) / "servers" / m_server_name / (m_service_name + ".env");
scp_cmd = "scp -P " + m_server_env->get_SSH_PORT() + " " +
service_env.string() + " " +
m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" +
service_dir + "/" + m_service_name + ".env";
if (system(scp_cmd.c_str()) != 0) {
std::cerr << "Error: Failed to copy service environment file" << std::endl;
return false;
}
// Run install script
std::string install_cmd = ssh_cmd.str() + "'cd " + service_dir + " && ./template/install.sh " +
m_service_name + ".env'";
if (system(install_cmd.c_str()) != 0) {
std::cerr << "Error: Failed to run install script" << std::endl;
return false;
}
return true;
}
bool server_service::run_command(const std::string& command) {
if (!m_server_env) {
std::cerr << "Error: Server service not initialized" << std::endl;
return false;
}
// Check if service directory exists
std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
std::stringstream ssh_cmd;
ssh_cmd << "ssh -p " << m_server_env->get_SSH_PORT() << " "
<< m_server_env->get_SSH_USER() << "@" << m_server_env->get_SSH_HOST() << " ";
// Check if service directory exists
std::string check_dir_cmd = ssh_cmd.str() + "'test -d " + service_dir + "'";
if (system(check_dir_cmd.c_str()) != 0) {
std::cerr << "Error: Service directory not found on server" << std::endl;
return false;
}
// Check if command script exists
std::string check_script_cmd = ssh_cmd.str() + "'test -f " + service_dir + "/" + command + ".sh'";
if (system(check_script_cmd.c_str()) != 0) {
std::cerr << "Error: Command script '" << command << ".sh' not found" << std::endl;
return false;
}
// Check if env file exists
std::string check_env_cmd = ssh_cmd.str() + "'test -f " + service_dir + "/" + m_service_name + ".env'";
if (system(check_env_cmd.c_str()) != 0) {
std::cerr << "Error: Service environment file not found on server" << std::endl;
return false;
}
// Run the command
std::string run_cmd = ssh_cmd.str() + "'cd " + service_dir + " && ./" + command + ".sh " +
m_service_name + ".env'";
if (system(run_cmd.c_str()) != 0) {
std::cerr << "Error: Failed to run command" << std::endl;
return false;
}
return true;
}
} // namespace dropshell

View File

@ -1,5 +1,12 @@
// server_service.hpp
//
// manage a service on a server
//
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include "server_env.hpp"
namespace dropshell { namespace dropshell {
@ -29,6 +36,11 @@ class server_service {
// checking that the command is a valid .sh file. // checking that the command is a valid .sh file.
// checking that the {service_name}.env file exists in the service directory. // checking that the {service_name}.env file exists in the service directory.
bool run_command(const std::string& command); bool run_command(const std::string& command);
private:
std::string m_server_name;
std::string m_service_name;
std::unique_ptr<server_env> m_server_env;
}; };
} // namespace dropshell } // namespace dropshell