From e9a6998761ba310cabb57197383033af07fa348c Mon Sep 17 00:00:00 2001 From: j Date: Wed, 8 Oct 2025 17:57:00 +1300 Subject: [PATCH] feat: Update 4 files --- source/src/commands/install.cpp | 4 +-- source/src/commands/ssh.cpp | 53 +++++++++++++++++++++------------ source/src/servers.cpp | 4 +-- source/src/services.hpp | 2 +- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/source/src/commands/install.cpp b/source/src/commands/install.cpp index 49aacb0..26a8fbb 100644 --- a/source/src/commands/install.cpp +++ b/source/src/commands/install.cpp @@ -4,7 +4,7 @@ #include "utils/directories.hpp" #include "templates.hpp" #include "shared_commands.hpp" -#include "utils/hash.hpp" +//#include "utils/hash.hpp" #include "autogen/_agent-local.hpp" #include "autogen/_agent-remote.hpp" #include "services.hpp" @@ -15,7 +15,7 @@ #include #include #include -#include +//#include #include #include #include "servers.hpp" diff --git a/source/src/commands/ssh.cpp b/source/src/commands/ssh.cpp index 70a2357..31ee1d4 100644 --- a/source/src/commands/ssh.cpp +++ b/source/src/commands/ssh.cpp @@ -1,5 +1,6 @@ #include "command_registry.hpp" -#include "config.hpp" +//#include "config.hpp" +#include "ordered_env.hpp" #include "utils/utils.hpp" #include "utils/directories.hpp" #include "utils/execute.hpp" @@ -41,7 +42,7 @@ namespace dropshell } } ssh_command_register; - bool ssh_into_server(const std::string &server, std::string user) + bool ssh_into_server(const std::string &server, std::string user, const ordered_env_vars & env_vars, std::string remote_path) { ServerConfig server_env(server); if (!server_env.is_valid()) @@ -49,12 +50,11 @@ namespace dropshell error << "Server " << server << " is not valid" << std::endl; return false; } - std::string dropshell_dir = remotepath(server, user).DROPSHELL_DIR(); - std::string bash_cmd = "ls --color && exec bash --rcfile <(cat ~/.bashrc 2>/dev/null; echo 'cd " + quote(dropshell_dir) + "')"; + std::string bash_cmd = "ls --color && exec bash --rcfile <(cat ~/.bashrc 2>/dev/null; echo 'cd " + quote(remote_path) + "')"; - info << "SSHing into " << server << ":" << dropshell_dir << " as user " << user << std::endl; + info << "SSHing into " << server << ":" << remote_path << " as user " << user << std::endl; - execute_ssh_command(server_env.get_SSH_INFO(user), sCommand(dropshell_dir, bash_cmd, {}), cMode::Interactive); + execute_ssh_command(server_env.get_SSH_INFO(user), sCommand(remote_path, bash_cmd, env_vars), cMode::Interactive); return true; } @@ -86,14 +86,22 @@ namespace dropshell return false; } - if (!gTemplateManager().template_command_exists(sinfo.template_name, "ssh")) + if (gTemplateManager().template_command_exists(sinfo.template_name, "ssh")) { - error << "Template " << sinfo.template_name << " does not have an ssh command" << std::endl; - return false; + return server_env.run_remote_template_command(service, "ssh", {}, false, {}); // explicitly supports interactive ssh! } - server_env.run_remote_template_command(service, "ssh", {}, false, {}); // explicitly supports interactive ssh! - return true; + // ssh in without as ssh.sh script. + ordered_env_vars env_vars; + if (!get_all_service_env_vars(server, service, env_vars)) + { + error << "Failed to get all service env vars for " << service << std::endl; + return false; + } + set_var(env_vars, "HOST_NAME", server_env.get_SSH_HOST()); + std::string service_dir = remotepath(server, sinfo.user).service_template(service); + + return ssh_into_server(server, sinfo.user, env_vars, service_dir); } int ssh_handler(const CommandContext &ctx) @@ -109,6 +117,7 @@ namespace dropshell { std::string arg1 = safearg(ctx.args, 0); std::string server, user; + ordered_env_vars env_vars; // parse either user@server or server if (arg1.find("@") != std::string::npos) @@ -119,19 +128,25 @@ namespace dropshell else { server = arg1; + } - // get the first user from the server.env file, and ssh in as that user. - ServerConfig server_env(server); - if (!server_env.is_valid()) - { - error << "Server " << server << " is not valid" << std::endl; - return 1; - } + // get the first user from the server.env file, and ssh in as that user. + ServerConfig server_env(server); + if (!server_env.is_valid()) + { + error << "Server " << server << " is not valid" << std::endl; + return 1; + } + + if (user.empty()) + { ASSERT(server_env.get_users().size() > 0, "Server " + server + " has no users"); user = server_env.get_users()[0].user; } - return ssh_into_server(server, user) ? 0 : 1; + set_var(env_vars, "HOST_NAME", server_env.get_SSH_HOST()); + std::string dropshell_dir = remotepath(server, user).DROPSHELL_DIR(); + return ssh_into_server(server, user, env_vars, dropshell_dir) ? 0 : 1; } else { // ssh into a service on the server. diff --git a/source/src/servers.cpp b/source/src/servers.cpp index ab2fe64..667efff 100644 --- a/source/src/servers.cpp +++ b/source/src/servers.cpp @@ -10,7 +10,7 @@ #include "config.hpp" #include -#include +//#include #include #include #include @@ -476,7 +476,7 @@ namespace dropshell if (server_existing_dir.empty()) return false; - if (std::filesystem::exists(server_existing_dir)); + if (std::filesystem::exists(server_existing_dir)) return true; return false; } diff --git a/source/src/services.hpp b/source/src/services.hpp index 40caf0b..cb60c83 100644 --- a/source/src/services.hpp +++ b/source/src/services.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +//#include #include "utils/ordered_env.hpp" namespace dropshell {