This commit is contained in:
parent
a6cac3a426
commit
efeab5a97f
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -55,7 +54,7 @@ bool service_runner::install(bool silent) {
|
|||||||
|
|
||||||
// Create service directory
|
// Create service directory
|
||||||
std::string remote_service_path = remotepath::service(mServer, mService);
|
std::string remote_service_path = remotepath::service(mServer, mService);
|
||||||
if (!execute_command(mServerEnv.get_SSH_INFO(), sCommand({"mkdir", "-p", remote_service_path}), cMode::Silent))
|
if (!execute_command(mServerEnv.get_SSH_INFO(), sCommand({"mkdir", "-p", quote(remote_service_path)}), cMode::Silent))
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to create service directory " << remote_service_path << std::endl;
|
std::cerr << "Failed to create service directory " << remote_service_path << std::endl;
|
||||||
return false;
|
return false;
|
||||||
@ -71,8 +70,8 @@ bool service_runner::install(bool silent) {
|
|||||||
// Copy template files
|
// Copy template files
|
||||||
{
|
{
|
||||||
std::cout << "Copying: [LOCAL] " << tinfo.local_template_path() << std::endl << std::string(8,' ')<<"[REMOTE] " << remotepath::service_template(mServer, mService) << "/" << std::endl;
|
std::cout << "Copying: [LOCAL] " << tinfo.local_template_path() << std::endl << std::string(8,' ')<<"[REMOTE] " << remotepath::service_template(mServer, mService) << "/" << std::endl;
|
||||||
std::vector<std::string> rsync_cmd = {"rsync", "--delete", "-zrpc", "-e", "ssh","-p", mServerEnv.get_SSH_PORT(),
|
std::vector<std::string> rsync_cmd = {"rsync", "--delete", "-zrpc", "-e", quote("ssh -p " + mServerEnv.get_SSH_PORT()),
|
||||||
tinfo.local_template_path().string()+"/",
|
quote(tinfo.local_template_path().string()+"/"),
|
||||||
mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remotepath::service_template(mServer, mService)+"/")
|
mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remotepath::service_template(mServer, mService)+"/")
|
||||||
};
|
};
|
||||||
if (!execute_command(rsync_cmd, silent ? cMode::Silent : cMode::Defaults))
|
if (!execute_command(rsync_cmd, silent ? cMode::Silent : cMode::Defaults))
|
||||||
@ -90,7 +89,7 @@ bool service_runner::install(bool silent) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::cout << "Copying: [LOCAL] " << local_service_path << std::endl <<std::string(8,' ')<<"[REMOTE] " << remotepath::service_config(mServer,mService) << std::endl;
|
std::cout << "Copying: [LOCAL] " << local_service_path << std::endl <<std::string(8,' ')<<"[REMOTE] " << remotepath::service_config(mServer,mService) << std::endl;
|
||||||
std::vector<std::string> rsync_cmd = {"rsync", "--delete", "-zrpc", "-e", "ssh", "-p", mServerEnv.get_SSH_PORT(),
|
std::vector<std::string> rsync_cmd = {"rsync", "--delete", "-zrpc", "-e", quote("ssh -p " + mServerEnv.get_SSH_PORT()),
|
||||||
quote(local_service_path + "/"),
|
quote(local_service_path + "/"),
|
||||||
mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remotepath::service_config(mServer,mService) + "/")
|
mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remotepath::service_config(mServer,mService) + "/")
|
||||||
};
|
};
|
||||||
|
@ -101,29 +101,52 @@ bool execute_command(const sSSHInfo * ssh_info, const sCommand command, cMode mo
|
|||||||
|
|
||||||
std::vector<std::string> commandvec;
|
std::vector<std::string> commandvec;
|
||||||
|
|
||||||
if (ssh_info)
|
// Construct the shell command with proper environment variables and directory
|
||||||
{
|
std::string shell_command;
|
||||||
std::vector<std::string> ssh_command = {"/usr/bin/ssh", "-p", ssh_info->port, "-tt", ssh_info->user + "@" + ssh_info->host};
|
|
||||||
commandvec.insert(commandvec.end(), ssh_command.begin(), ssh_command.end());
|
// Add working directory if provided
|
||||||
|
if (!command.get_directory_to_run_in().empty()) {
|
||||||
|
shell_command += "cd " + quote(command.get_directory_to_run_in()) + " && ";
|
||||||
}
|
}
|
||||||
|
|
||||||
commandvec.push_back("bash");
|
// Add environment variables if provided
|
||||||
commandvec.push_back("-c");
|
for (const auto& env_var : command.get_env_vars()) {
|
||||||
|
shell_command += env_var.first + "=" + quote(env_var.second) + " ";
|
||||||
std::string shell_command = "";
|
|
||||||
for (auto & x : command.get_command_to_run()) {
|
|
||||||
shell_command += x + " ";
|
|
||||||
}
|
}
|
||||||
shell_command = shell_command.substr(0, shell_command.size() - 1);
|
|
||||||
commandvec.push_back(shell_command);
|
|
||||||
|
|
||||||
|
// Add the command arguments properly joined
|
||||||
|
const auto& args = command.get_command_to_run();
|
||||||
|
std::ostringstream cmd_stream;
|
||||||
|
for (size_t i = 0; i < args.size(); ++i) {
|
||||||
|
if (i > 0) cmd_stream << " ";
|
||||||
|
cmd_stream << args[i];
|
||||||
|
}
|
||||||
|
shell_command += cmd_stream.str();
|
||||||
|
|
||||||
|
if (ssh_info) {
|
||||||
|
// Use bash -c for SSH to ensure proper command execution
|
||||||
|
commandvec = {
|
||||||
|
"/usr/bin/ssh",
|
||||||
|
"-p", ssh_info->port,
|
||||||
|
(hasFlag(mode, cMode::CaptureOutput) ? "" : "-tt"),
|
||||||
|
ssh_info->user + "@" + ssh_info->host,
|
||||||
|
"bash", "-c", halfquote(shell_command)
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// Local execution
|
||||||
|
commandvec = {"bash", "-c", halfquote(shell_command)};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove any empty strings
|
||||||
|
commandvec.erase(
|
||||||
|
std::remove_if(commandvec.begin(), commandvec.end(),
|
||||||
|
[](const std::string& s) { return s.empty(); }),
|
||||||
|
commandvec.end());
|
||||||
|
|
||||||
if (hasFlag(mode, cMode::CaptureOutput)) {
|
if (hasFlag(mode, cMode::CaptureOutput)) {
|
||||||
ASSERT(output, "Capture output mode requires an output string to be provided");
|
ASSERT(output, "Capture output mode requires an output string to be provided");
|
||||||
return __execute_command(commandvec, output);
|
return __execute_command(commandvec, output);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return __execute_command(commandvec, nullptr);
|
return __execute_command(commandvec, nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user