colour
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 21s

This commit is contained in:
Your Name
2025-05-10 16:56:54 +12:00
parent 547482edc6
commit 61218f8866
24 changed files with 97 additions and 3 deletions

View File

@ -66,6 +66,9 @@ bool service_runner::install(bool silent) {
return false;
}
// make sure all shell files are executable
make_shell_files_executable(tinfo.local_template_path().string());
// Copy template files
{
if (!rsync_copy(tinfo.local_template_path().string()+"/", remotepath::service_template(mServer, mService)+"/", silent)) {
@ -387,7 +390,7 @@ bool service_runner::interactive_ssh(const std::string & server_name, const std:
std::cerr << "Error: Invalid server environment file: " << server_name << std::endl;
return false;
}
return 0==runner::execute_cmd("bash",{}, "", {}, false, true, env.get_SSH_INFO());
return 0==runner::execute_cmd("", {}, "", {}, false, true, env.get_SSH_INFO());
}
void service_runner::edit_server(const std::string &server_name)

View File

@ -118,36 +118,56 @@ std::string ssh_build_command_only(const std::string& command, const std::vector
}
int ssh_interactive_shell_session(ssh_session session, ssh_channel channel, const std::string& remote_cmd_str, const std::string& command, std::string* output) {
// Request a PTY with xterm-256color type for color support
// First try using default terminal settings - should work with libssh 0.9.0+
int rc = ssh_channel_request_pty(channel);
if (rc != SSH_OK) {
if (output) *output = std::string("Failed to request pty: ") + ssh_get_error(session);
return -1;
}
// Try to explicitly tell the server we want colors through additional means
// 1. Set TERM environment variable
rc = ssh_channel_request_env(channel, "TERM", "xterm-256color");
// Ignore errors - this is optional
// 2. Request a shell
rc = ssh_channel_request_shell(channel);
if (rc != SSH_OK) {
if (output) *output = std::string("Failed to request shell: ") + ssh_get_error(session);
return -1;
}
struct termios orig_termios, raw_termios;
tcgetattr(STDIN_FILENO, &orig_termios);
raw_termios = orig_termios;
cfmakeraw(&raw_termios);
tcsetattr(STDIN_FILENO, TCSANOW, &raw_termios);
if (!command.empty()) {
ssh_channel_write(channel, remote_cmd_str.c_str(), remote_cmd_str.size());
ssh_channel_write(channel, "\n", 1);
} else {
// Initialize bash with color support if no specific command
std::string init_cmd = "export TERM=xterm-256color && if [ -f ~/.bashrc ]; then source ~/.bashrc; fi";
ssh_channel_write(channel, init_cmd.c_str(), init_cmd.size());
ssh_channel_write(channel, "\n", 1);
}
int maxfd = STDIN_FILENO > STDOUT_FILENO ? STDIN_FILENO : STDOUT_FILENO;
maxfd = maxfd > ssh_get_fd(session) ? maxfd : ssh_get_fd(session);
char buffer[4096];
bool done = false;
while (!done) {
fd_set fds_read;
FD_ZERO(&fds_read);
FD_SET(STDIN_FILENO, &fds_read);
FD_SET(ssh_get_fd(session), &fds_read);
int ret = select(maxfd + 1, &fds_read, nullptr, nullptr, nullptr);
if (ret < 0) break;
if (FD_ISSET(STDIN_FILENO, &fds_read)) {
ssize_t n = read(STDIN_FILENO, buffer, sizeof(buffer));
if (n > 0) {
@ -157,6 +177,7 @@ int ssh_interactive_shell_session(ssh_session session, ssh_channel channel, cons
done = true;
}
}
if (FD_ISSET(ssh_get_fd(session), &fds_read)) {
int n = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
if (n > 0) {
@ -165,10 +186,12 @@ int ssh_interactive_shell_session(ssh_session session, ssh_channel channel, cons
done = true;
}
}
if (ssh_channel_is_closed(channel) || ssh_channel_is_eof(channel)) {
done = true;
}
}
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
return 0;
}

View File

@ -31,7 +31,7 @@ int execute_cmd(
const std::map<std::string, std::string>& env,
const bool silent,
const bool interactive,
const copySSHPtr sshinfo = nullptr,
const copySSHPtr& sshinfo = nullptr,
std::string* output = nullptr
);

View File

@ -312,6 +312,32 @@ std::string random_alphanumeric_string(int length)
return random_string;
}
void make_shell_files_executable(const std::string &dir_path)
{ // recursively make all shell files in the directory executable
const auto desired_perms = std::filesystem::perms::owner_read | std::filesystem::perms::owner_exec |
std::filesystem::perms::group_read | std::filesystem::perms::group_exec |
std::filesystem::perms::others_read | std::filesystem::perms::others_exec;
for (const auto& entry : std::filesystem::directory_iterator(dir_path)) {
if (entry.path().extension() == ".sh") {
// check if permissions are already set
auto currentperms = std::filesystem::status(entry.path()).permissions();
if ((currentperms & desired_perms) == desired_perms) {
continue;
}
// set permissions
std::cout << "Setting executable permissions for " << entry.path() << std::endl;
std::filesystem::permissions(entry.path(), desired_perms, std::filesystem::perm_options::add);
}
else if (std::filesystem::is_directory(entry.path())) {
make_shell_files_executable(entry.path());
}
}
}
std::string requote(std::string str) {
return quote(trim(dequote(trim(str))));
}

View File

@ -41,4 +41,6 @@ std::string replace_with_environment_variables_like_bash(std::string str);
std::string random_alphanumeric_string(int length);
void make_shell_files_executable(const std::string& dir_path);
} // namespace dropshell