feat: Update 14 and remove 1 files
This commit is contained in:
@@ -128,7 +128,8 @@ namespace dropshell
|
||||
|
||||
{ // Run backup script
|
||||
shared_commands::cRemoteTempFolder remote_temp_folder(server_env, user);
|
||||
if (!server_env.run_remote_template_command(service, command, {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}}))
|
||||
if (!server_env.run_remote_template_command(
|
||||
service, command, {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}},NULL))
|
||||
{
|
||||
error << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
||||
return false;
|
||||
|
||||
@@ -91,14 +91,14 @@ namespace dropshell
|
||||
if (gTemplateManager().template_command_exists(service_info.template_name, "destroy"))
|
||||
{
|
||||
info << "Running destroy script for " << service << " on " << server << std::endl;
|
||||
if (!server_env.run_remote_template_command(service, "destroy", {}, false, {}))
|
||||
if (!server_env.run_remote_template_command(service, "destroy", {}, false, {},NULL))
|
||||
warning << "Failed to run destroy script: " << service << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
info << "No destroy script found for " << service << " on " << server << std::endl;
|
||||
info << "Running uninstall script instead and will clean directories." << std::endl;
|
||||
if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {}))
|
||||
if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {},NULL))
|
||||
warning << "Failed to uninstall service: " << service << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ namespace dropshell
|
||||
}
|
||||
|
||||
shared_commands::cRemoteTempFolder remote_temp_folder(server_env, user);
|
||||
if (!server_env.run_remote_template_command(service, "install", {}, false, {{"TEMP_DIR", remote_temp_folder.path()}}))
|
||||
if (!server_env.run_remote_template_command(service, "install", {}, false, {{"TEMP_DIR", remote_temp_folder.path()}},NULL))
|
||||
{
|
||||
error << "Failed to run install script for service '" << service << "' on server '" << server << "'" << std::endl;
|
||||
error << "Template: " << service_info.template_name << std::endl;
|
||||
@@ -531,12 +531,11 @@ complete -F _dropshell_completions ds
|
||||
|
||||
// create server_info.env
|
||||
info << "Creating server_info.env on remote server... " <<std::flush;
|
||||
std::string lsiep = localpath::temp_files() + "/" + filenames::server_info_env;
|
||||
if (std::filesystem::exists(lsiep)) std::filesystem::remove(lsiep);
|
||||
shared_commands::cLocalTempFolder lt; // auto cleans up.
|
||||
std::filesystem::path lsiep = lt.path() / filenames::server_info_env;
|
||||
if (!server.createServerInfoEnv(lsiep)) return 1;
|
||||
std::string rsiep = remotefile(server.get_server_name(),user.user).server_info_env();
|
||||
shared_commands::rsync_file_to_remote(lsiep,rsiep,server,false,user.user);
|
||||
if (std::filesystem::exists(lsiep)) std::filesystem::remove(lsiep);
|
||||
info << "done." << std::endl;
|
||||
|
||||
// run the agent installer. Can't use BB64 yet, as we're installing it on the remote server.
|
||||
|
||||
@@ -226,14 +226,15 @@ namespace dropshell
|
||||
debug << " BACKUP_FILE: " << remote_backup_file_path << std::endl;
|
||||
debug << " TEMP_DIR: " << remote_temp_folder.path() << std::endl;
|
||||
|
||||
server_env.run_remote_template_command(service, "restore", {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}});
|
||||
server_env.run_remote_template_command(service, "restore", {}, false,
|
||||
{{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}},NULL);
|
||||
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||
|
||||
{ // healthcheck the service
|
||||
info << "5) Healthchecking service..." << std::endl;
|
||||
std::string green_tick = "\033[32m✓\033[0m";
|
||||
std::string red_cross = "\033[31m✗\033[0m";
|
||||
bool healthy = (server_env.run_remote_template_command(service, "status", {}, false, {}));
|
||||
bool healthy = (server_env.run_remote_template_command(service, "status", {}, false, {}, NULL));
|
||||
info << (healthy ? green_tick : red_cross) << " Service is " << (healthy ? "healthy" : "NOT healthy") << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -307,7 +307,7 @@ namespace dropshell
|
||||
}
|
||||
|
||||
// Run status script, does not display output.
|
||||
if (!env.run_remote_template_command(service, "status", {}, true, {}))
|
||||
if (!env.run_remote_template_command(service, "status", {}, true, {}, NULL))
|
||||
return HealthStatus::UNHEALTHY;
|
||||
return HealthStatus::HEALTHY;
|
||||
}
|
||||
@@ -337,7 +337,7 @@ namespace dropshell
|
||||
|
||||
// Run ports script and capture output
|
||||
std::string output;
|
||||
if (env.run_remote_template_command_and_capture_output(service, "ports", {}, output, true, {}))
|
||||
if (env.run_remote_template_command(service, "ports", {}, true, {}, &output))
|
||||
{
|
||||
// Parse the output - expecting comma-separated or newline-separated port numbers
|
||||
std::stringstream ss(output);
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace dropshell
|
||||
|
||||
if (gTemplateManager().template_command_exists(sinfo.template_name, "ssh"))
|
||||
{
|
||||
return server_env.run_remote_template_command(service, "ssh", {}, false, {}); // explicitly supports interactive ssh!
|
||||
return server_env.run_remote_template_command(service, "ssh", {}, false, {}, NULL); // explicitly supports interactive ssh!
|
||||
}
|
||||
|
||||
// ssh in without as ssh.sh script.
|
||||
|
||||
@@ -66,7 +66,7 @@ namespace dropshell
|
||||
}
|
||||
|
||||
// run the start script.
|
||||
bool started = server_env.run_remote_template_command(service, "start", {}, false, {});
|
||||
bool started = server_env.run_remote_template_command(service, "start", {}, false, {}, NULL);
|
||||
|
||||
if (started)
|
||||
{
|
||||
|
||||
@@ -66,7 +66,7 @@ namespace dropshell
|
||||
}
|
||||
|
||||
// run the stop script.
|
||||
bool stopped = server_env.run_remote_template_command(service, "stop", {}, false, {});
|
||||
bool stopped = server_env.run_remote_template_command(service, "stop", {}, false, {}, NULL);
|
||||
|
||||
if (stopped)
|
||||
{
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace dropshell
|
||||
|
||||
// 3. Run uninstall script if it exists
|
||||
std::string uninstall_script = remotepath(server, user).service_template(service) + "/uninstall.sh";
|
||||
if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {}))
|
||||
if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {}, NULL))
|
||||
warning << "Uninstall script failed, but continuing with directory removal" << std::endl;
|
||||
|
||||
// 4. Remove the service directory from the server, running in a docker container as root.
|
||||
|
||||
@@ -303,10 +303,12 @@ namespace dropshell
|
||||
const std::string &command,
|
||||
std::vector<std::string> args,
|
||||
bool silent,
|
||||
std::map<std::string, std::string> extra_env_vars) const
|
||||
std::map<std::string, std::string> extra_env_vars,
|
||||
std::string * output
|
||||
) const
|
||||
{
|
||||
std::string user = get_user_for_service(service_name);
|
||||
auto scommand = construct_standard_template_run_cmd(service_name, command, args, silent);
|
||||
auto scommand = construct_standard_template_run_cmd(service_name, command, args);
|
||||
if (!scommand.has_value())
|
||||
return false;
|
||||
|
||||
@@ -317,28 +319,7 @@ namespace dropshell
|
||||
if (scommand->get_command_to_run().empty())
|
||||
return false;
|
||||
cMode mode = (command == "ssh") ? (cMode::Interactive) : (silent ? cMode::Silent : cMode::Defaults);
|
||||
return execute_ssh_command(get_SSH_INFO(user), scommand.value(), mode);
|
||||
}
|
||||
|
||||
bool ServerConfig::run_remote_template_command_and_capture_output(
|
||||
const std::string &service_name,
|
||||
const std::string &command,
|
||||
std::vector<std::string> args,
|
||||
std::string &output,
|
||||
bool silent,
|
||||
std::map<std::string, std::string> extra_env_vars) const
|
||||
{
|
||||
std::string user = get_user_for_service(service_name);
|
||||
auto scommand = construct_standard_template_run_cmd(service_name, command, args, false);
|
||||
if (!scommand.has_value())
|
||||
return false;
|
||||
|
||||
// add the extra env vars to the command
|
||||
for (const auto &[key, value] : extra_env_vars)
|
||||
scommand->add_env_var(key, value);
|
||||
|
||||
// Use Silent mode when capturing output to avoid printing to console
|
||||
return execute_ssh_command(get_SSH_INFO(user), scommand.value(), cMode::Silent, &output);
|
||||
return execute_ssh_command(get_SSH_INFO(user), scommand.value(), mode, output);
|
||||
}
|
||||
|
||||
std::string ServerConfig::get_variable(const std::string &name) const
|
||||
@@ -351,41 +332,19 @@ namespace dropshell
|
||||
return it->second;
|
||||
}
|
||||
|
||||
std::optional<sCommand> ServerConfig::construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector<std::string> args, const bool silent) const
|
||||
std::optional<sCommand> ServerConfig::construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector<std::string> args) const
|
||||
{
|
||||
if (command.empty())
|
||||
return std::nullopt;
|
||||
|
||||
std::string user = get_user_for_service(service_name);
|
||||
|
||||
std::string remote_service_template_path = remotepath(mServerName, user).service_template(service_name);
|
||||
std::string script_path = remote_service_template_path + "/" + command + ".sh";
|
||||
|
||||
ordered_env_vars env_vars;
|
||||
if (!get_all_service_env_vars(mServerName, service_name, env_vars))
|
||||
{
|
||||
error << "Failed to get all service env vars for " << service_name << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
set_var(env_vars, "HOST_NAME", get_SSH_HOST());
|
||||
|
||||
std::string argstr = "";
|
||||
for (const auto &arg : args)
|
||||
{
|
||||
argstr += " " + quote(dequote(trim(arg)));
|
||||
}
|
||||
|
||||
sCommand sc(
|
||||
remote_service_template_path,
|
||||
quote(script_path) + argstr + (silent ? " > /dev/null 2>&1" : ""),
|
||||
env_vars);
|
||||
|
||||
if (sc.empty())
|
||||
{
|
||||
error << "Failed to construct command for " << service_name << " " << command << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
argstr += " " + requote(arg);
|
||||
|
||||
std::string dsrun = remotefile(mServerName, user).ds_run();
|
||||
sCommand sc("", dsrun + " " + requote(service_name) + " " + requote(command) + argstr, {});
|
||||
return sc;
|
||||
}
|
||||
|
||||
|
||||
@@ -74,13 +74,11 @@ namespace dropshell
|
||||
bool remove_remote_dir(const std::string &dir_path, bool silent, std::string user) const;
|
||||
|
||||
bool run_remote_template_command(const std::string &service_name, const std::string &command,
|
||||
std::vector<std::string> args, bool silent, std::map<std::string, std::string> extra_env_vars) const;
|
||||
|
||||
bool run_remote_template_command_and_capture_output(const std::string &service_name, const std::string &command,
|
||||
std::vector<std::string> args, std::string &output, bool silent, std::map<std::string, std::string> extra_env_vars) const;
|
||||
|
||||
std::vector<std::string> args, bool silent, std::map<std::string, std::string> extra_env_vars,
|
||||
std::string * output) const;
|
||||
|
||||
private:
|
||||
std::optional<sCommand> construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector<std::string> args, const bool silent) const;
|
||||
std::optional<sCommand> construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector<std::string> args) const;
|
||||
|
||||
private:
|
||||
std::string mServerName;
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
|
||||
// #include <iostream>
|
||||
// #include <fstream>
|
||||
// #include <sstream>
|
||||
// #include <cstdlib>
|
||||
// #include <chrono>
|
||||
// #include <iomanip>
|
||||
// #include <filesystem>
|
||||
// #include <unistd.h>
|
||||
// #include <libassert/assert.hpp>
|
||||
|
||||
// #include "config.hpp"
|
||||
// #include "servers.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 {
|
||||
|
||||
|
||||
// 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();
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
// // ------------------------------------------------------------------------------------------------
|
||||
// // Run a command on the service.
|
||||
// // ------------------------------------------------------------------------------------------------
|
||||
// bool service_runner::run_command(const std::string& command, std::vector<std::string> additional_args, std::map<std::string, std::string> 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 (!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 <server> <service> <backup-file>" << std::endl;
|
||||
// return false;
|
||||
// }
|
||||
// return restore(additional_args[0], false);
|
||||
// }
|
||||
// if (command == "backup") {
|
||||
// return backup(false);
|
||||
// }
|
||||
|
||||
// // Run the generic command
|
||||
// std::vector<std::string> args; // not passed through yet.
|
||||
// return mServerEnv.run_remote_template_command(mService, command, args, false, env_vars);
|
||||
// }
|
||||
|
||||
|
||||
// } // namespace dropshell
|
||||
@@ -175,6 +175,11 @@ namespace dropshell
|
||||
return remotepath(mServer_name, mUser).DROPSHELL_DIR() + "/" + filenames::server_info_env;
|
||||
}
|
||||
|
||||
std::string remotefile::ds_run()
|
||||
{
|
||||
return remotepath(mServer_name,mUser).agent() + "/" + filenames::ds_run;
|
||||
}
|
||||
|
||||
remotepath::remotepath(const std::string &server_name, const std::string &user) : mServer_name(server_name), mUser(user) {}
|
||||
|
||||
std::string remotepath::DROPSHELL_DIR() const
|
||||
|
||||
@@ -48,6 +48,7 @@ namespace dropshell {
|
||||
static const std::string readme = "README.txt";
|
||||
static const std::string server_json = "server.json";
|
||||
static const std::string dropshell_json = "dropshell.json";
|
||||
static const std::string ds_run = "ds_run.sh";
|
||||
} // namespace filenames.
|
||||
|
||||
namespace localfile {
|
||||
@@ -105,6 +106,7 @@ namespace dropshell {
|
||||
std::string service_env(const std::string &service_name) const;
|
||||
std::string server_json();
|
||||
std::string server_info_env();
|
||||
std::string ds_run();
|
||||
private:
|
||||
std::string mServer_name;
|
||||
std::string mUser;
|
||||
|
||||
Reference in New Issue
Block a user