dropshell release 2025.0519.0021
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
This commit is contained in:
@ -22,7 +22,6 @@ namespace dropshell
|
||||
return (ret != -1 && WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); // ret is -1 if the command failed to execute.
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------------
|
||||
// execute_local_command_interactive
|
||||
// ----------------------------------------------------------------------------------------------------------
|
||||
@ -30,7 +29,7 @@ namespace dropshell
|
||||
{
|
||||
if (command.get_command_to_run().empty())
|
||||
return false;
|
||||
std::string full_command = command.construct_cmd(localpath::agent()); // Get the command string
|
||||
std::string full_command = command.construct_cmd(localpath::agent()+"/bb64"); // Get the command string
|
||||
|
||||
pid_t pid = fork();
|
||||
|
||||
@ -62,7 +61,7 @@ namespace dropshell
|
||||
|
||||
class fancypinter
|
||||
{
|
||||
public:
|
||||
public:
|
||||
fancypinter(sColour startColour) : startColour_(startColour), currentColour_(startColour) {}
|
||||
|
||||
void print_chunk(std::string chunk)
|
||||
@ -82,18 +81,22 @@ namespace dropshell
|
||||
else if (chunk.find("info") == 0)
|
||||
currentColour_ = sColour::INFO;
|
||||
else
|
||||
currentColour_ = startColour_;
|
||||
currentColour_ = startColour_;
|
||||
}
|
||||
colourstream(currentColour_) << chunk;
|
||||
newline_ = (chunk[chunk.size()-1] == '\n');
|
||||
newline_ = (chunk[chunk.size() - 1] == '\n');
|
||||
}
|
||||
|
||||
void print(const std::string& buffer) {
|
||||
void print(const std::string &buffer)
|
||||
{
|
||||
size_t start = 0;
|
||||
while (start < buffer.size()) {
|
||||
while (start < buffer.size())
|
||||
{
|
||||
size_t newline_pos = buffer.find('\n', start);
|
||||
if (newline_pos == std::string::npos) {
|
||||
if (start < buffer.size()) {
|
||||
if (newline_pos == std::string::npos)
|
||||
{
|
||||
if (start < buffer.size())
|
||||
{
|
||||
print_chunk(buffer.substr(start));
|
||||
}
|
||||
break;
|
||||
@ -103,32 +106,36 @@ namespace dropshell
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
bool newline_ = true;
|
||||
sColour startColour_;
|
||||
sColour currentColour_;
|
||||
};
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
sCommand command(directory_to_run_in, command_to_run, env_vars);
|
||||
|
||||
if (hasFlag(mode, cMode::Interactive))
|
||||
{
|
||||
ASSERT(!hasFlag(mode, cMode::CaptureOutput), "Interactive mode and capture output mode cannot be used together");
|
||||
ASSERT(output == nullptr, "Interactive mode and an output string cannot be used together");
|
||||
|
||||
return execute_local_command_interactive(command);
|
||||
}
|
||||
|
||||
|
||||
if (command.get_command_to_run().empty())
|
||||
return false;
|
||||
|
||||
bool silent = hasFlag(mode, cMode::Silent);
|
||||
|
||||
std::string full_cmd = command.construct_cmd(localpath::agent()) + (hasFlag(mode, cMode::CaptureOutput) ? " 2>&1" : ""); // capture both stdout and stderr
|
||||
std::string full_cmd;
|
||||
if (!hasFlag(mode, cMode::NoBB64))
|
||||
full_cmd = command.construct_cmd(localpath::agent()+"/bb64");
|
||||
else
|
||||
full_cmd = command.construct_cmd("");
|
||||
|
||||
if (output != nullptr)
|
||||
full_cmd += " 2>&1"; // capture both stdout and stderr
|
||||
|
||||
FILE *pipe = popen(full_cmd.c_str(), "r");
|
||||
if (!pipe)
|
||||
@ -140,7 +147,7 @@ namespace dropshell
|
||||
while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
|
||||
{
|
||||
if (output != nullptr)
|
||||
(*output) += buffer;
|
||||
(*output) += buffer;
|
||||
|
||||
if (!silent)
|
||||
fancyprint.print(buffer);
|
||||
@ -157,30 +164,27 @@ namespace dropshell
|
||||
if (remote_command.get_command_to_run().empty())
|
||||
return false;
|
||||
|
||||
ASSERT(!(hasFlag(mode, cMode::CaptureOutput) && output == nullptr), "Capture output mode must be used with an output string");
|
||||
|
||||
std::stringstream ssh_cmd;
|
||||
ssh_cmd << "ssh -p " << ssh_info.port << " " << (hasFlag(mode, cMode::Interactive) ? "-tt " : "")
|
||||
<< ssh_info.user << "@" << ssh_info.host;
|
||||
|
||||
std::string remote_agent_path = remotepath::agent(ssh_info.server_ID);
|
||||
std::string remote_bb64_path;
|
||||
|
||||
if (!hasFlag(mode, cMode::NoBB64))
|
||||
remote_bb64_path = remotepath::agent(ssh_info.server_ID) + "/bb64";
|
||||
|
||||
bool rval = execute_local_command(
|
||||
"", // directory to run in
|
||||
ssh_cmd.str() + " " + remote_command.construct_cmd(remote_agent_path), // local command to run
|
||||
{}, // environment variables
|
||||
output, // output string
|
||||
mode // mode
|
||||
"", // local directory to run in
|
||||
ssh_cmd.str() + " " + remote_command.construct_cmd(remote_bb64_path), // local command to run
|
||||
{}, // environment variables
|
||||
output, // output string
|
||||
mode // mode
|
||||
);
|
||||
|
||||
if (!rval && !hasFlag(mode, cMode::Silent))
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< std::endl;
|
||||
std::cerr << "Error: Failed to execute ssh command:" << std::endl;
|
||||
std::cerr << "\033[90m" << ssh_cmd.str() + " " + remote_command.construct_cmd(remote_agent_path) << "\033[0m" << std::endl;
|
||||
std::cerr << std::endl
|
||||
<< std::endl;
|
||||
error << "Error: Failed to execute ssh command:" << std::endl;
|
||||
debug << ssh_cmd.str() + " " + remote_command.construct_cmd(remote_bb64_path) << std::endl;
|
||||
}
|
||||
return rval;
|
||||
}
|
||||
@ -188,19 +192,19 @@ namespace dropshell
|
||||
// ----------------------------------------------------------------------------------------------------------
|
||||
// makesafecmd
|
||||
// ----------------------------------------------------------------------------------------------------------
|
||||
std::string sCommand::makesafecmd(std::string agent_path, const std::string &command) const
|
||||
std::string sCommand::makesafecmd(std::string bb64path, const std::string &command) const
|
||||
{
|
||||
if (command.empty())
|
||||
return "";
|
||||
std::string encoded = base64_encode(dequote(trim(command)));
|
||||
std::string commandstr = agent_path + "/bb64 " + encoded;
|
||||
std::string commandstr = bb64path + " " + encoded;
|
||||
return commandstr;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------------
|
||||
// construct_cmd
|
||||
// ----------------------------------------------------------------------------------------------------------
|
||||
std::string sCommand::construct_cmd(std::string agent_path) const
|
||||
std::string sCommand::construct_cmd(std::string bb64path) const
|
||||
{
|
||||
if (mCmd.empty())
|
||||
return "";
|
||||
@ -208,17 +212,27 @@ namespace dropshell
|
||||
// need to construct to change directory and set environment variables
|
||||
std::string cmdstr;
|
||||
|
||||
if (!mDir.empty())
|
||||
cmdstr += "cd " + quote(mDir) + " && ";
|
||||
if (!bb64path.empty())
|
||||
{
|
||||
if (!mDir.empty())
|
||||
cmdstr += "cd " + quote(mDir) + " && ";
|
||||
|
||||
if (!mVars.empty())
|
||||
for (const auto &env_var : mVars)
|
||||
cmdstr += env_var.first + "=" + quote(dequote(trim(env_var.second))) + " ";
|
||||
if (!mVars.empty())
|
||||
for (const auto &env_var : mVars)
|
||||
cmdstr += env_var.first + "=" + quote(dequote(trim(env_var.second))) + " ";
|
||||
|
||||
cmdstr += mCmd;
|
||||
cmdstr += mCmd;
|
||||
|
||||
if (!agent_path.empty())
|
||||
cmdstr = makesafecmd(agent_path, cmdstr);
|
||||
cmdstr = makesafecmd(bb64path, cmdstr);
|
||||
}
|
||||
else
|
||||
{ // raw! bootstrapping only.
|
||||
ASSERT(mVars.empty(), "Bootstrapping command must not have environment variables");
|
||||
if (!mDir.empty())
|
||||
cmdstr += mDir + "/" + mCmd;
|
||||
else
|
||||
cmdstr += mCmd;
|
||||
}
|
||||
|
||||
return cmdstr;
|
||||
}
|
||||
|
Reference in New Issue
Block a user