Trying new approach
This commit is contained in:
parent
828171c977
commit
314a5fe96a
@ -75,7 +75,7 @@ bool edit_file(const std::string &file_path, bool has_bb64)
|
|||||||
std::cout << "Editing file: " << file_path << std::endl;
|
std::cout << "Editing file: " << file_path << std::endl;
|
||||||
|
|
||||||
if (has_bb64) {
|
if (has_bb64) {
|
||||||
return execute_local_command(editor_cmd, nullptr, cMode::Interactive);
|
return execute_local_command("", editor_cmd, {}, nullptr, cMode::Interactive);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// might not have bb64 at this early stage. Direct edit.
|
// might not have bb64 at this early stage. Direct edit.
|
||||||
|
@ -312,7 +312,7 @@ namespace dropshell
|
|||||||
quote(agent_path) + " " + quote("$(id -u " + server_env.get_SSH_USER() + "):$(id -g " + server_env.get_SSH_USER() + ")") + "'";
|
quote(agent_path) + " " + quote("$(id -u " + server_env.get_SSH_USER() + "):$(id -g " + server_env.get_SSH_USER() + ")") + "'";
|
||||||
|
|
||||||
//std::cout << "Executing: " << remote_cmd << std::endl;
|
//std::cout << "Executing: " << remote_cmd << std::endl;
|
||||||
if (!execute_local_command(remote_cmd, nullptr, cMode::Silent))
|
if (!execute_local_command("", remote_cmd, {}, nullptr, cMode::Silent))
|
||||||
std::cerr << "Failed to download bb64 to " << agent_path << " on remote server." << std::endl;
|
std::cerr << "Failed to download bb64 to " << agent_path << " on remote server." << std::endl;
|
||||||
else
|
else
|
||||||
std::cout << "Downloaded bb64 to " << agent_path << " on remote server." << std::endl;
|
std::cout << "Downloaded bb64 to " << agent_path << " on remote server." << std::endl;
|
||||||
|
@ -62,7 +62,7 @@ namespace dropshell
|
|||||||
quote(local_path + "/") + " " +
|
quote(local_path + "/") + " " +
|
||||||
quote(server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" +
|
quote(server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" +
|
||||||
remote_path + "/");
|
remote_path + "/");
|
||||||
return execute_local_command(rsync_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
return execute_local_command("", rsync_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
@ -147,7 +147,7 @@ bool service_runner::fullnuke()
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string rm_cmd = "rm -rf " + quote(local_service_path);
|
std::string rm_cmd = "rm -rf " + quote(local_service_path);
|
||||||
if (!execute_local_command(rm_cmd, nullptr, cMode::Silent)) {
|
if (!execute_local_command("", rm_cmd, {}, nullptr, cMode::Silent)) {
|
||||||
std::cerr << "Failed to remove service directory" << std::endl;
|
std::cerr << "Failed to remove service directory" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -267,13 +267,13 @@ bool service_runner::interactive_ssh_service()
|
|||||||
bool service_runner::scp_file_to_remote(const std::string &local_path, const std::string &remote_path, bool silent)
|
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" : "");
|
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));
|
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)
|
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" : "");
|
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));
|
return execute_local_command("", scp_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool service_runner::restore(std::string backup_file, bool silent)
|
bool service_runner::restore(std::string backup_file, bool silent)
|
||||||
|
@ -56,36 +56,9 @@ namespace dropshell
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------------------------
|
|
||||||
// execute_local_command_and_capture_output
|
|
||||||
// ----------------------------------------------------------------------------------------------------------
|
|
||||||
bool execute_local_command_and_capture_output(const sCommand &command, std::string *output)
|
|
||||||
{
|
|
||||||
ASSERT(output != nullptr, "Output string must be provided");
|
|
||||||
if (command.get_command_to_run().empty())
|
|
||||||
return false;
|
|
||||||
std::string full_cmd = command.construct_cmd(localpath::agent()) + " 2>&1";
|
|
||||||
FILE *pipe = popen(full_cmd.c_str(), "r");
|
|
||||||
if (!pipe)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
char buffer[128];
|
|
||||||
while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
|
|
||||||
{
|
|
||||||
(*output) += buffer;
|
|
||||||
}
|
|
||||||
int ret = pclose(pipe);
|
|
||||||
return EXITSTATUSCHECK(ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
// execute_local_command
|
// execute_local_command
|
||||||
// ----------------------------------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
bool execute_local_command(std::string command, std::string *output, cMode mode)
|
|
||||||
{
|
|
||||||
return execute_local_command("", command, {}, output, mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> &env_vars, std::string *output, cMode mode)
|
bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> &env_vars, std::string *output, cMode mode)
|
||||||
{
|
{
|
||||||
@ -99,32 +72,32 @@ namespace dropshell
|
|||||||
return execute_local_command_interactive(command);
|
return execute_local_command_interactive(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasFlag(mode, cMode::CaptureOutput))
|
|
||||||
{
|
|
||||||
ASSERT(output != nullptr, "Capture output mode requires an output string to be provided");
|
|
||||||
ASSERT(!hasFlag(mode, cMode::Silent), "Silent mode is not allowed with capture output mode");
|
|
||||||
|
|
||||||
return execute_local_command_and_capture_output(command, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (command.get_command_to_run().empty())
|
if (command.get_command_to_run().empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool silent = hasFlag(mode, cMode::Silent);
|
bool silent = hasFlag(mode, cMode::Silent);
|
||||||
std::string full_cmd = command.construct_cmd(localpath::agent()) + " 2>&1" + (silent ? " > /dev/null" : "");
|
|
||||||
|
|
||||||
int ret=0;
|
std::string full_cmd = command.construct_cmd(localpath::agent()) + (hasFlag(mode, cMode::CaptureOutput) ? " 2>&1" : ""); // capture both stdout and stderr
|
||||||
{
|
|
||||||
SwitchColour sc(sColour::DEBUG, std::cerr);
|
|
||||||
ret = system(full_cmd.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ok = EXITSTATUSCHECK(ret);
|
FILE *pipe = popen(full_cmd.c_str(), "r");
|
||||||
if (!ok && !silent)
|
if (!pipe)
|
||||||
{
|
{
|
||||||
PrintError("Error: Failed to execute command: ");
|
return false;
|
||||||
PrintError(full_cmd);
|
|
||||||
}
|
}
|
||||||
return ok;
|
char buffer[128];
|
||||||
|
while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
|
||||||
|
{
|
||||||
|
if (output != nullptr)
|
||||||
|
(*output) += buffer;
|
||||||
|
|
||||||
|
if (!silent)
|
||||||
|
{
|
||||||
|
std::cerr << buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int ret = pclose(pipe);
|
||||||
|
return EXITSTATUSCHECK(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
|
@ -31,7 +31,6 @@ typedef struct sSSHInfo {
|
|||||||
std::string server_ID; // dropshell name for server.
|
std::string server_ID; // dropshell name for server.
|
||||||
} sSSHInfo;
|
} sSSHInfo;
|
||||||
|
|
||||||
bool execute_local_command(std::string command, std::string * output = nullptr, cMode mode = cMode::Defaults);
|
|
||||||
bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> & env_vars, std::string * output = nullptr, cMode mode = cMode::Defaults);
|
bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> & env_vars, std::string * output = nullptr, cMode mode = cMode::Defaults);
|
||||||
bool execute_ssh_command(const sSSHInfo & ssh_info, const sCommand & remote_command, cMode mode = cMode::Defaults, std::string * output = nullptr);
|
bool execute_ssh_command(const sSSHInfo & ssh_info, const sCommand & remote_command, cMode mode = cMode::Defaults, std::string * output = nullptr);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user