#include #include #include #include #include #include #include #include #include "utils/assert.hpp" #include "config.hpp" #include "service_runner.hpp" #include "server_env_manager.hpp" #include "templates.hpp" #include "services.hpp" #include "utils/directories.hpp" #include "utils/utils.hpp" #include "command_registry.hpp" #include "shared_commands.hpp" namespace fs = std::filesystem; namespace dropshell { static const std::string magic_string = "-_-"; service_runner::service_runner(const std::string& server_name, const std::string& service_name) : mServerEnv(server_name), mServer(server_name), mService(service_name), mValid(false) { if (server_name.empty() || service_name.empty()) return; // Initialize server environment if (!mServerEnv.is_valid()) return; mServiceInfo = get_service_info(server_name, service_name); if (mServiceInfo.service_name.empty()) return; mService = mServiceInfo.service_name; mValid = !mServiceInfo.local_template_path.empty(); } bool service_runner::nuke(bool silent) { maketitle("Nuking " + mService + " (" + mServiceInfo.template_name + ") on " + mServer); if (!mServerEnv.is_valid()) return false; // should never hit this. std::string remote_service_path = remotepath::service(mServer, mService); bool okay = mServerEnv.run_remote_template_command("dropshell-agent", "_nuke_other", {mService, remote_service_path}, silent, {}); if (!okay) { std::cerr << "Warning: Nuke script failed" << std::endl; return false; } std::cout << "Service " << mService << " successfully nuked from " << mServer << std::endl; if (!silent) { std::cout << "There's nothing left on the remote server." << std::endl; std::cout << "You can remove the local files with:" << std::endl; std::cout << " rm -rf " << localpath::service(mServer,mService) << std::endl; } return true; } bool service_runner::fullnuke() { if (!nuke(true)) { std::cerr << "Warning: Nuke script failed, aborting fullnuke!" << std::endl; return false; } std::string local_service_path = mServiceInfo.local_service_path; if (local_service_path.empty() || !fs::exists(local_service_path)) { std::cerr << "Error: Service directory not found: " << local_service_path << std::endl; return false; } std::string rm_cmd = "rm -rf " + quote(local_service_path); if (!execute_local_command(rm_cmd, nullptr, cMode::Silent)) { std::cerr << "Failed to remove service directory" << std::endl; return false; } return true; } // ------------------------------------------------------------------------------------------------ // Run a command on the service. // ------------------------------------------------------------------------------------------------ bool service_runner::run_command(const std::string& command, std::vector additional_args, std::map env_vars) { if (!mServerEnv.is_valid()) { std::cerr << "Error: Server service not initialized" << std::endl; return false; } template_info tinfo = gTemplateManager().get_template_info(mServiceInfo.template_name); if (!tinfo.is_set()) { std::cerr << "Error: Template '" << mServiceInfo.template_name << "' not found" << std::endl; return false; } if (command == "fullnuke") return fullnuke(); if (command == "nuke") { std::cout << "Nuking " << mService << " (" << mServiceInfo.template_name << ") on " << mServer << std::endl; return nuke(); } if (!gTemplateManager().template_command_exists(mServiceInfo.template_name, command)) { std::cout << "No command script for " << mServiceInfo.template_name << " : " << command << std::endl; return true; // nothing to run. } // install doesn't require anything on the server yet. if (command == "install") return install_service(mServer, mService, false); std::string script_path = remotepath::service_template(mServer, mService) + "/" + command + ".sh"; // Check if service directory exists if (!mServerEnv.check_remote_dir_exists(remotepath::service(mServer, mService))) { std::cerr << "Error: Service is not installed: " << mService << std::endl; return false; } // Check if command script exists if (!mServerEnv.check_remote_file_exists(script_path)) { std::cerr << "Error: Remote command script not found: " << script_path << std::endl; return false; } // Check if env file exists if (!mServerEnv.check_remote_file_exists(remotefile::service_env(mServer, mService))) { std::cerr << "Error: Service config file not found: " << remotefile::service_env(mServer, mService) << std::endl; return false; } // if (command == "uninstall") // return uninstall(); if (command == "ssh") { interactive_ssh_service(); return true; } if (command == "restore") { if (additional_args.size() < 1) { std::cerr << "Error: restore requires a backup file:" << std::endl; std::cerr << "dropshell restore " << std::endl; return false; } return restore(additional_args[0], false); } if (command == "backup") { return backup(false); } // Run the generic command std::vector args; // not passed through yet. return mServerEnv.run_remote_template_command(mService, command, args, false, env_vars); } std::map service_runner::get_all_services_status(std::string server_name) { std::map status; std::string command = "_allservicesstatus"; std::string service_name = "dropshell-agent"; if (!gTemplateManager().template_command_exists(service_name, "shared/"+command)) { std::cerr << "Error: " << service_name << " does not contain the " << command << " script" << std::endl; return status; } server_env_manager env(server_name); if (!env.is_valid()) { std::cerr << "Error: Invalid server environment" << std::endl; return status; } std::string output; if (!env.run_remote_template_command_and_capture_output(service_name, "shared/"+command, {}, output, true, {})) return status; std::stringstream ss(output); std::string line; while (std::getline(ss, line)) { std::string key, value; std::size_t pos = line.find("="); if (pos != std::string::npos) { key = dequote(trim(line.substr(0, pos))); value = dequote(trim(line.substr(pos + 1))); // decode key, it's of format SERVICENAME_[HEALTH|PORTS] std::string service_name = key.substr(0, key.find_last_of("_")); std::string status_type = key.substr(key.find_last_of("_") + 1); if (status_type == "HEALTH") { // healthy|unhealthy|unknown if (value == "healthy") status[service_name].health = HealthStatus::HEALTHY; else if (value == "unhealthy") status[service_name].health = HealthStatus::UNHEALTHY; else if (value == "unknown") status[service_name].health = HealthStatus::UNKNOWN; else status[service_name].health = HealthStatus::ERROR; } else if (status_type == "PORTS") { // port1,port2,port3 std::vector ports = string2multi(value); for (const auto& port : ports) { if (port!="unknown") status[service_name].ports.push_back(str2int(port)); } } } } return status; } bool service_runner::interactive_ssh(const std::string & server_name, const std::string & command) { std::string serverpath = localpath::server(server_name); if (serverpath.empty()) { std::cerr << "Error: Server not found: " << server_name << std::endl; return false; } server_env_manager env(server_name); if (!env.is_valid()) { std::cerr << "Error: Invalid server environment file: " << server_name << std::endl; return false; } sCommand scommand("", "bash",{}); return execute_ssh_command(env.get_SSH_INFO(), scommand, cMode::Interactive); } bool service_runner::interactive_ssh_service() { std::set used_commands = get_used_commands(mServer, mService); if (used_commands.find("ssh") == used_commands.end()) { std::cerr << "Error: "<< mService <<" does not support ssh" << std::endl; return false; } std::vector args; // not passed through yet. return mServerEnv.run_remote_template_command(mService, "ssh", args, false, {}); } bool service_runner::scp_file_to_remote(const std::string &local_path, const std::string &remote_path, bool silent) { std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + quote(local_path) + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + (silent ? " > /dev/null 2>&1" : ""); return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults)); } bool service_runner::scp_file_from_remote(const std::string &remote_path, const std::string &local_path, bool silent) { std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + " " + quote(local_path) + (silent ? " > /dev/null 2>&1" : ""); return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults)); } bool service_runner::restore(std::string backup_file, bool silent) { if (backup_file.empty()) { std::cerr << "Error: not enough arguments. dropshell restore " << std::endl; return false; } std::string local_backups_dir = gConfig().get_local_backup_path(); if (backup_file == "latest") { // get the latest backup file from the server backup_file = get_latest_backup_file(mServer, mService); } std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_file).string(); if (! std::filesystem::exists(local_backup_file_path)) { std::cerr << "Error: Backup file not found at " << local_backup_file_path << std::endl; return false; } // split the backup filename into parts based on the magic string std::vector parts = dropshell::split(backup_file, magic_string); if (parts.size() != 4) { std::cerr << "Error: Backup file format is incompatible, - in one of the names?" << std::endl; return false; } std::string backup_server_name = parts[0]; std::string backup_template_name = parts[1]; std::string backup_service_name = parts[2]; std::string backup_datetime = parts[3]; if (backup_template_name != mServiceInfo.template_name) { std::cerr << "Error: Backup template does not match service template. Can't restore." << std::endl; return false; } std::string nicedate = std::string(backup_datetime).substr(0, 10); std::cout << "Restoring " << nicedate << " backup of " << backup_template_name << " taken from "< latest_datetime) { latest_datetime = datetime; latest_file = filename; } } } if (latest_file.empty()) { std::cerr << "Error: No backup files found for " << server << ", " << service << std::endl; } std::cout << "Latest backup file: " << latest_file << std::endl; return latest_file; } } // namespace dropshell