This commit is contained in:
Your Name 2025-04-30 23:35:02 +12:00
parent fce8a89491
commit 6d7a42e718
5 changed files with 45 additions and 18 deletions

View File

@ -60,14 +60,14 @@ std::string server_env_manager::get_variable(const std::string& name) const {
} }
// Helper method implementations // Helper method implementations
std::string server_env_manager::construct_ssh_cmd() const { std::string server_env_manager::construct_ssh_cmd(bool allocateTTY) const {
std::stringstream ssh_cmd; std::stringstream ssh_cmd;
ssh_cmd << "ssh -p " << get_SSH_PORT() << " " ssh_cmd << "ssh -p " << get_SSH_PORT() << " " << (allocateTTY ? "-tt " : "")
<< get_SSH_USER() << "@" << get_SSH_HOST(); << get_SSH_USER() << "@" << get_SSH_HOST();
return ssh_cmd.str(); return ssh_cmd.str();
} }
std::string server_env_manager::construct_standard_command_run_cmd(const std::string &service_name, const std::string &command, std::vector<std::string> args, bool silent) const sCommand server_env_manager::construct_standard_command_run_cmd(const std::string &service_name, const std::string &command, std::vector<std::string> args, bool silent) const
{ {
std::string remote_service_template_path = remotepath::service_template(mServerName,service_name); std::string remote_service_template_path = remotepath::service_template(mServerName,service_name);
std::string remote_service_config_path = remotepath::service_config(mServerName,service_name); std::string remote_service_config_path = remotepath::service_config(mServerName,service_name);
@ -83,8 +83,7 @@ std::string server_env_manager::construct_standard_command_run_cmd(const std::st
} }
sCommand scommand(remote_service_template_path, "bash " + quote(script_path) + argstr + (silent ? " > /dev/null 2>&1" : ""), env_vars); sCommand scommand(remote_service_template_path, "bash " + quote(script_path) + argstr + (silent ? " > /dev/null 2>&1" : ""), env_vars);
std::string run_cmd = scommand.construct_safecmd(); return scommand;
return run_cmd;
} }
@ -119,27 +118,29 @@ bool server_env_manager::check_remote_items_exist(const std::vector<std::string>
return true; return true;
} }
bool server_env_manager::execute_ssh_command(const sCommand& command) const { bool server_env_manager::execute_ssh_command(const sCommand& command, bool allocateTTY) const {
std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd()); std::string full_cmd = construct_ssh_cmd(allocateTTY) + " " + (allocateTTY ? halfquote(command.construct_rawcmd()) : quote(command.construct_safecmd()));
return execute_local_command(full_cmd); return (system(full_cmd.c_str()) == 0);
} }
bool server_env_manager::execute_ssh_command_and_capture_output(const sCommand& command, std::string &output) const bool server_env_manager::execute_ssh_command_and_capture_output(const sCommand& command, std::string &output, bool allocateTTY) const
{ {
std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd()); std::string full_cmd = construct_ssh_cmd(allocateTTY) + " " + quote(command.construct_safecmd());
return execute_local_command_and_capture_output(full_cmd, output); return execute_local_command_and_capture_output(full_cmd, output);
} }
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) const
{ {
std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args, silent); sCommand scommand = construct_standard_command_run_cmd(service_name, command, args, silent);
return execute_ssh_command(full_cmd); bool allocateTTY = (command=="ssh");
return execute_ssh_command(scommand, allocateTTY);
} }
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) const
{ {
std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args, silent); sCommand scommand = construct_standard_command_run_cmd(service_name, command, args, silent);
return execute_ssh_command_and_capture_output(full_cmd, output); bool allocateTTY = (command=="ssh");
return execute_ssh_command_and_capture_output(scommand, output, allocateTTY);
} }
bool server_env_manager::execute_local_command(const sCommand& command) { bool server_env_manager::execute_local_command(const sCommand& command) {
@ -180,6 +181,17 @@ std::string sCommand::construct_safecmd() const
return commandstr; return commandstr;
} }
std::string sCommand::construct_rawcmd() const
{
std::string rawcmd = "cd " + quote(mDir) + " && ";
for (const auto& env_var : mVars) {
rawcmd += env_var.first + "=" + quote(dequote(trim(env_var.second))) + " ";
}
rawcmd += mCmd;
return rawcmd;
}
// base64 <<< "FOO=BAR WHEE=YAY bash ./test.sh" // base64 <<< "FOO=BAR WHEE=YAY bash ./test.sh"
// echo YmFzaCAtYyAnRk9PPUJBUiBXSEVFPVlBWSBiYXNoIC4vdGVzdC5zaCcK | base64 -d | bash // echo YmFzaCAtYyAnRk9PPUJBUiBXSEVFPVlBWSBiYXNoIC4vdGVzdC5zaCcK | base64 -d | bash

View File

@ -27,6 +27,7 @@ class sCommand {
void add_env_var(const std::string& key, const std::string& value) { mVars[key] = value; } void add_env_var(const std::string& key, const std::string& value) { mVars[key] = value; }
std::string construct_safecmd() const; std::string construct_safecmd() const;
std::string construct_rawcmd() const;
private: private:
std::string mDir; std::string mDir;
@ -66,15 +67,15 @@ class server_env_manager {
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_and_capture_output(const std::string& service_name, const std::string& command, std::vector<std::string> args, std::string & output, bool silent=false) const;
public: public:
bool execute_ssh_command(const sCommand& command) const; bool execute_ssh_command(const sCommand& command, bool allocateTTY=false) const;
bool execute_ssh_command_and_capture_output(const sCommand& command, std::string & output) const; bool execute_ssh_command_and_capture_output(const sCommand& command, std::string & output, bool allocateTTY=false) const;
static bool execute_local_command(const sCommand& command); static bool execute_local_command(const sCommand& command);
static bool execute_local_command_and_capture_output(const sCommand& command, std::string & output); static bool execute_local_command_and_capture_output(const sCommand& command, std::string & output);
private: private:
std::string construct_ssh_cmd() const; std::string construct_ssh_cmd(bool allocateTTY=false) const;
std::string construct_standard_command_run_cmd(const std::string& service_name, const std::string& command, std::vector<std::string> args, bool silent) const; sCommand construct_standard_command_run_cmd(const std::string& service_name, const std::string& command, std::vector<std::string> args, bool silent) const;
private: private:
std::string mServerName; std::string mServerName;

View File

@ -72,6 +72,11 @@ std::string quote(std::string str)
return "\""+str+"\""; return "\""+str+"\"";
} }
std::string halfquote(std::string str)
{
return "'" + str + "'";
}
std::string multi2string(std::vector<std::string> values) std::string multi2string(std::vector<std::string> values)
{ {
std::string result; std::string result;

View File

@ -19,6 +19,7 @@ bool replace_line_in_file(const std::string& file_path, const std::string& searc
std::string trim(std::string str); std::string trim(std::string str);
std::string dequote(std::string str); std::string dequote(std::string str);
std::string quote(std::string str); std::string quote(std::string str);
std::string halfquote(std::string str);
std::string multi2string(std::vector<std::string> values); std::string multi2string(std::vector<std::string> values);
std::vector<std::string> string2multi(std::string values); std::vector<std::string> string2multi(std::string values);
std::vector<std::string> split(const std::string& str, const std::string& delimiter); std::vector<std::string> split(const std::string& str, const std::string& delimiter);

8
temp.sh Normal file
View File

@ -0,0 +1,8 @@
echo Y2QgIi9ob21lL2thdGllLy5kcm9wc2hlbGwvc2VydmljZXMvc3F1YXNoa2l3aS10ZXN0L3RlbXBsYXRlIiAmJiBlY2hvIFEwOU9Sa2xIWDFCQlZFZzlJaTlvYjIxbEwydGhkR2xsTHk1a2NtOXdjMmhsYkd3dmMyVnlkbWxqWlhNdmMzRjFZWE5vYTJsM2FTMTBaWE4wTDJOdmJtWnBaeUlnUTA5T1ZFRkpUa1ZTWDA1QlRVVTlJbk54ZFdGemFHdHBkMmtpSUVOUFRsUkJTVTVGVWw5UVQxSlVQU0k0TVRneElpQklUMU5VWDFCUFVsUTlJall3T0RBaUlFbE5RVWRGWDFKRlIwbFRWRkpaUFNKbmFYUmxZUzVxWkdVdWJub2lJRWxOUVVkRlgxSkZVRTg5SW5OeGRXRnphR3RwZDJrdmMzRjFZWE5vYTJsM2FTSWdTVTFCUjBWZlZFRkhQU0pzWVhSbGMzUWlJRXhQUTBGTVgwUkJWRUZmUms5TVJFVlNQU0lrZTBoUFRVVjlMeTV6YXkxMFpYTjBJaUJUUlZKV1JWSTlJbXRoZEdsbElpQlRSVkpXU1VORlBTSnpjWFZoYzJocmFYZHBMWFJsYzNRaUlGUkZUVkJNUVZSRlBTSnpjWFZoYzJocmFYZHBJaUJpWVhOb0lDSXZhRzl0WlM5cllYUnBaUzh1WkhKd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