This commit is contained in:
@ -161,21 +161,30 @@ bool server_env_manager::check_remote_items_exist(const std::vector<std::string>
|
||||
return true;
|
||||
}
|
||||
|
||||
bool server_env_manager::run_remote_template_command(const std::string &service_name, const std::string &command, std::vector<std::string> args, bool silent) const
|
||||
bool server_env_manager::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
|
||||
{
|
||||
sCommand scommand = construct_standard_template_run_cmd(service_name, command, args, silent);
|
||||
|
||||
// add the extra env vars to the command
|
||||
for (const auto& [key, value] : extra_env_vars)
|
||||
scommand.add_env_var(key, value);
|
||||
|
||||
if (scommand.get_command_to_run().empty())
|
||||
return false;
|
||||
cMode mode = (command=="ssh") ? (cMode::Interactive | cMode::RawCommand) : cMode::Silent;
|
||||
return execute_ssh_command(get_SSH_INFO(), scommand, mode);
|
||||
}
|
||||
|
||||
bool server_env_manager::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) const
|
||||
bool server_env_manager::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
|
||||
{
|
||||
sCommand scommand = construct_standard_template_run_cmd(service_name, command, args, false);
|
||||
if (scommand.get_command_to_run().empty())
|
||||
return false;
|
||||
|
||||
// add the extra env vars to the command
|
||||
for (const auto& [key, value] : extra_env_vars)
|
||||
scommand.add_env_var(key, value);
|
||||
|
||||
cMode mode = cMode::CaptureOutput | cMode::RawCommand;
|
||||
return execute_ssh_command(get_SSH_INFO(), scommand, mode, &output);
|
||||
}
|
||||
|
@ -53,8 +53,10 @@ class server_env_manager {
|
||||
bool check_remote_file_exists(const std::string& file_path) const;
|
||||
bool check_remote_items_exist(const std::vector<std::string>& file_paths) const;
|
||||
|
||||
bool run_remote_template_command(const std::string& service_name, const std::string& command, std::vector<std::string> args, bool silent=false) 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=false) 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;
|
||||
|
||||
private:
|
||||
sCommand construct_standard_template_run_cmd(const std::string& service_name, const std::string& command, std::vector<std::string> args, bool silent) const;
|
||||
|
@ -107,7 +107,7 @@ bool service_runner::install(bool silent) {
|
||||
|
||||
// Run install script
|
||||
{
|
||||
mServerEnv.run_remote_template_command(mService, "install", {});
|
||||
mServerEnv.run_remote_template_command(mService, "install", {}, silent, {});
|
||||
}
|
||||
|
||||
// print health tick
|
||||
@ -131,7 +131,7 @@ bool service_runner::uninstall(bool silent) {
|
||||
bool script_exists = mServerEnv.check_remote_file_exists(uninstall_script);
|
||||
|
||||
if (script_exists) {
|
||||
if (!mServerEnv.run_remote_template_command(mService, "uninstall", {})) {
|
||||
if (!mServerEnv.run_remote_template_command(mService, "uninstall", {}, silent, {})) {
|
||||
std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl;
|
||||
}
|
||||
|
||||
@ -156,13 +156,9 @@ 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::map<std::string, std::string> env_vars;
|
||||
if (!get_all_service_env_vars(mServer, mService, env_vars))
|
||||
std::cerr << "Warning: Failed to get all service env vars for " << mService << std::endl;
|
||||
|
||||
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);
|
||||
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;
|
||||
@ -206,7 +202,7 @@ bool service_runner::fullnuke()
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Run a command on the service.
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool service_runner::run_command(const std::string& command, std::vector<std::string> additional_args) {
|
||||
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;
|
||||
@ -282,7 +278,7 @@ bool service_runner::run_command(const std::string& command, std::vector<std::st
|
||||
|
||||
// Run the generic command
|
||||
std::vector<std::string> args; // not passed through yet.
|
||||
return mServerEnv.run_remote_template_command(mService, command, args);
|
||||
return mServerEnv.run_remote_template_command(mService, command, args, false, env_vars);
|
||||
}
|
||||
|
||||
|
||||
@ -306,7 +302,7 @@ std::map<std::string, ServiceStatus> service_runner::get_all_services_status(std
|
||||
}
|
||||
|
||||
std::string output;
|
||||
if (!env.run_remote_template_command_and_capture_output(service_name, command, {}, output))
|
||||
if (!env.run_remote_template_command_and_capture_output(service_name, command, {}, output, true, {}))
|
||||
return status;
|
||||
|
||||
std::stringstream ss(output);
|
||||
@ -360,7 +356,7 @@ HealthStatus service_runner::is_healthy()
|
||||
}
|
||||
|
||||
// Run status script, does not display output.
|
||||
if (!mServerEnv.run_remote_template_command(mService, "status", {}, true))
|
||||
if (!mServerEnv.run_remote_template_command(mService, "status", {}, true, {}))
|
||||
return HealthStatus::UNHEALTHY;
|
||||
return HealthStatus::HEALTHY;
|
||||
}
|
||||
@ -477,7 +473,7 @@ bool service_runner::interactive_ssh_service()
|
||||
}
|
||||
|
||||
std::vector<std::string> args; // not passed through yet.
|
||||
return mServerEnv.run_remote_template_command(mService, "ssh", args);
|
||||
return mServerEnv.run_remote_template_command(mService, "ssh", args, false, {});
|
||||
}
|
||||
|
||||
void service_runner::edit_service_config()
|
||||
@ -578,7 +574,7 @@ bool service_runner::restore(std::string backup_file, bool silent)
|
||||
}
|
||||
|
||||
cRemoteTempFolder remote_temp_folder(mServerEnv);
|
||||
mServerEnv.run_remote_template_command(mService, "restore", {remote_backup_file_path, remote_temp_folder.path()}, silent);
|
||||
mServerEnv.run_remote_template_command(mService, "restore", {}, silent, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}});
|
||||
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||
|
||||
|
||||
@ -593,7 +589,7 @@ bool service_runner::restore(std::string backup_file, bool silent)
|
||||
maketitle("6) Healthchecking service...");
|
||||
std::string green_tick = "\033[32m✓\033[0m";
|
||||
std::string red_cross = "\033[31m✗\033[0m";
|
||||
healthy= (mServerEnv.run_remote_template_command(mService, "status", {}, silent));
|
||||
healthy= (mServerEnv.run_remote_template_command(mService, "status", {}, silent, {}));
|
||||
if (!silent)
|
||||
std::cout << (healthy ? green_tick : red_cross) << " Service is " << (healthy ? "healthy" : "NOT healthy") << std::endl;
|
||||
}
|
||||
@ -685,7 +681,7 @@ bool service_runner::backup(bool silent) {
|
||||
|
||||
{ // Run backup script
|
||||
cRemoteTempFolder remote_temp_folder(mServerEnv);
|
||||
if (!mServerEnv.run_remote_template_command(mService, command, {remote_backup_file_path, remote_temp_folder.path()}, silent)) {
|
||||
if (!mServerEnv.run_remote_template_command(mService, command, {}, silent, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}})) {
|
||||
std::cerr << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ class service_runner {
|
||||
// checking that the command exists in the service directory.
|
||||
// checking that the command is a valid .sh file.
|
||||
// checking that the {service_name}.env file exists in the service directory.
|
||||
bool run_command(const std::string& command, std::vector<std::string> additional_args={});
|
||||
bool run_command(const std::string& command, std::vector<std::string> additional_args={}, std::map<std::string, std::string> env_vars={});
|
||||
|
||||
// check health of service. Silent.
|
||||
// 1. run status.sh on the server
|
||||
|
Reference in New Issue
Block a user