Broken
This commit is contained in:
@ -78,13 +78,15 @@ namespace dropshell
|
||||
return true; // nothing to back up.
|
||||
}
|
||||
|
||||
std::string user = server_env.get_user_for_service(server, service);
|
||||
|
||||
// Check if basic installed stuff is in place.
|
||||
std::string remote_service_template_path = remotepath::service_template(server, service);
|
||||
std::string remote_service_template_path = remotepath(server, user).service_template(service);
|
||||
std::string remote_command_script_file = remote_service_template_path + "/" + command + ".sh";
|
||||
std::string remote_service_config_path = remotepath::service_config(server, service);
|
||||
if (!server_env.check_remote_items_exist({remotepath::service(server, service),
|
||||
std::string remote_service_config_path = remotepath(server, user).service_config(service);
|
||||
if (!server_env.check_remote_items_exist({remotepath(server, user).service(service),
|
||||
remote_command_script_file,
|
||||
remotefile::service_env(server, service)}))
|
||||
remotefile(server, user).service_env(service)}, user))
|
||||
{
|
||||
error << "Error: Required service directories not found on remote server" << std::endl;
|
||||
info << "Is the service installed?" << std::endl;
|
||||
@ -92,10 +94,10 @@ namespace dropshell
|
||||
}
|
||||
|
||||
// Create backups directory on server if it doesn't exist
|
||||
std::string remote_backups_dir = remotepath::backups(server);
|
||||
std::string remote_backups_dir = remotepath(server, user).backups();
|
||||
debug << "Remote backups directory on " << server << ": " << remote_backups_dir << std::endl;
|
||||
std::string mkdir_cmd = "mkdir -p " + quote(remote_backups_dir);
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand("", mkdir_cmd, {}), cMode::Defaults))
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(user), sCommand("", mkdir_cmd, {}), cMode::Defaults))
|
||||
{
|
||||
error << "Failed to create backups directory on server" << std::endl;
|
||||
return false;
|
||||
@ -129,7 +131,7 @@ namespace dropshell
|
||||
ASSERT(3 == count_substring(magic_string(), local_backup_file_path), "Invalid backup filename");
|
||||
|
||||
{ // Run backup script
|
||||
shared_commands::cRemoteTempFolder remote_temp_folder(server_env);
|
||||
shared_commands::cRemoteTempFolder remote_temp_folder(server_env, user);
|
||||
if (!server_env.run_remote_template_command(service, command, {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}}))
|
||||
{
|
||||
error << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
||||
|
@ -63,7 +63,8 @@ namespace dropshell
|
||||
if (!SIvalid(service_info))
|
||||
error << "Invalid service: " << service << std::endl;
|
||||
|
||||
if (server_env.check_remote_dir_exists(remotepath::service(server, service)))
|
||||
std::string user = server_env.get_user_for_service(server, service);
|
||||
if (server_env.check_remote_dir_exists(remotepath(server, user).service(service), user))
|
||||
{
|
||||
// run the nuke script on the remote server if it exists.
|
||||
// otherwise just uninstall.
|
||||
@ -82,16 +83,16 @@ namespace dropshell
|
||||
}
|
||||
|
||||
// Remove the service directory from the server, running in a docker container as root.
|
||||
if (server_env.remove_remote_dir(remotepath::service(server, service), true))
|
||||
if (server_env.remove_remote_dir(remotepath(server, user).service(service), true, user))
|
||||
{
|
||||
ASSERT(!server_env.check_remote_dir_exists(remotepath::service(server, service)), "Service directory still found on server after uninstall");
|
||||
info << "Remote service directory removed: " << remotepath::service(server, service) << std::endl;
|
||||
ASSERT(!server_env.check_remote_dir_exists(remotepath(server, user).service(service), user), "Service directory still found on server after uninstall");
|
||||
info << "Remote service directory removed: " << remotepath(server, user).service(service) << std::endl;
|
||||
}
|
||||
else
|
||||
warning << "Failed to remove remote service directory" << std::endl;
|
||||
}
|
||||
else
|
||||
warning << "Service not found on remote server: " << remotepath::service(server, service) << std::endl;
|
||||
warning << "Service not found on remote server: " << remotepath(server, user).service(service) << std::endl;
|
||||
}
|
||||
else
|
||||
warning << "Can't nuke the remote service as the server is invalid: " << server << std::endl;
|
||||
|
@ -123,7 +123,7 @@ void show_server_details(const std::string& server_name) {
|
||||
//---------------------
|
||||
// Check if server is reachable via SSH
|
||||
std::string ssh_address = env.get_SSH_HOST();
|
||||
std::string ssh_user = env.get_SSH_USER();
|
||||
std::string ssh_user = env.get_SSH_UNPRIVILEGED_USER();
|
||||
std::string ssh_port = env.get_SSH_PORT();
|
||||
if (!ssh_address.empty()) {
|
||||
info << std::endl << "Server Status:" << std::endl;
|
||||
|
@ -195,7 +195,8 @@ namespace dropshell
|
||||
|
||||
{ // restore service from backup
|
||||
info << "5) Restoring service data from backup..." << std::endl;
|
||||
std::string remote_backups_dir = remotepath::backups(server);
|
||||
std::string user = server_env.get_user_for_service(server, service);
|
||||
std::string remote_backups_dir = remotepath(server, user).backups();
|
||||
std::string remote_backup_file_path = remote_backups_dir + "/" + backup_details->get_filename();
|
||||
|
||||
debug << "Copying backup file from local to server: " << local_backup_file_path << " -> " << remote_backup_file_path << std::endl;
|
||||
@ -207,7 +208,7 @@ namespace dropshell
|
||||
return 1;
|
||||
}
|
||||
|
||||
shared_commands::cRemoteTempFolder remote_temp_folder(server_env);
|
||||
shared_commands::cRemoteTempFolder remote_temp_folder(server_env,user);
|
||||
|
||||
debug << "Running restore script on server: " << server << std::endl;
|
||||
debug << " BACKUP_FILE: " << remote_backup_file_path << std::endl;
|
||||
|
@ -61,7 +61,7 @@ namespace dropshell
|
||||
|
||||
std::string rsync_cmd = "rsync --delete --mkpath -zrpc -e 'ssh -p " + server_env.get_SSH_PORT() + "' " +
|
||||
quote(local_path + "/") + " " +
|
||||
quote(server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" +
|
||||
quote(server_env.get_SSH_UNPRIVILEGED_USER() + "@" + server_env.get_SSH_HOST() + ":" +
|
||||
remote_path + "/");
|
||||
return execute_local_command("", rsync_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||
}
|
||||
@ -84,11 +84,12 @@ namespace dropshell
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// cRemoteTempFolder : SHARED CLASS
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
cRemoteTempFolder::cRemoteTempFolder(const server_env_manager &server_env) : mServerEnv(server_env)
|
||||
cRemoteTempFolder::cRemoteTempFolder(const server_env_manager &server_env, std::string user) :
|
||||
mServerEnv(server_env), mUser(user)
|
||||
{
|
||||
std::string p = remotepath::temp_files(server_env.get_server_name()) + "/" + random_alphanumeric_string(10);
|
||||
std::string p = remotepath(server_env.get_server_name(),user).temp_files() + "/" + random_alphanumeric_string(10);
|
||||
std::string mkdir_cmd = "mkdir -p " + quote(p);
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand("", mkdir_cmd, {}), cMode::Silent))
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(user), sCommand("", mkdir_cmd, {}), cMode::Silent))
|
||||
error << "Failed to create temp directory on server" << std::endl;
|
||||
else
|
||||
mPath = p;
|
||||
@ -97,7 +98,7 @@ namespace dropshell
|
||||
cRemoteTempFolder::~cRemoteTempFolder()
|
||||
{
|
||||
std::string rm_cmd = "rm -rf " + quote(mPath);
|
||||
execute_ssh_command(mServerEnv.get_SSH_INFO(), sCommand("", rm_cmd, {}), cMode::Silent);
|
||||
execute_ssh_command(mServerEnv.get_SSH_INFO(mUser), sCommand("", rm_cmd, {}), cMode::Silent);
|
||||
}
|
||||
|
||||
std::string cRemoteTempFolder::path() const
|
||||
@ -119,49 +120,54 @@ namespace dropshell
|
||||
return status;
|
||||
}
|
||||
|
||||
std::string output;
|
||||
if (!execute_ssh_command(env.get_SSH_INFO(), sCommand(remotepath::agent(server_name), "./_allservicesstatus.sh", {{"HOST_NAME", server_name}, {"SERVER", server_name}, {"AGENT_PATH", remotepath::agent(server_name)}}),
|
||||
cMode::Silent,
|
||||
&output))
|
||||
return status;
|
||||
for (const auto& user : env.get_users()) {
|
||||
std::string output;
|
||||
std::string agentpath = remotepath(server_name,user.user).agent();
|
||||
if (!execute_ssh_command(env.get_SSH_INFO(user.user),
|
||||
sCommand(agentpath, "./_allservicesstatus.sh", {{"HOST_NAME", server_name}, {"SERVER", server_name}, {"AGENT_PATH", agentpath}}),
|
||||
cMode::Silent,
|
||||
&output))
|
||||
return status;
|
||||
|
||||
std::stringstream ss(output);
|
||||
std::string line;
|
||||
while (std::getline(ss, line))
|
||||
{
|
||||
std::string key, value;
|
||||
std::size_t pos = line.find("=");
|
||||
if (pos != std::string::npos)
|
||||
std::stringstream ss(output);
|
||||
std::string line;
|
||||
while (std::getline(ss, line))
|
||||
{
|
||||
key = dequote(trim(line.substr(0, pos)));
|
||||
value = dequote(trim(line.substr(pos + 1)));
|
||||
std::string key, value;
|
||||
std::size_t pos = line.find("=");
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
key = dequote(trim(line.substr(0, pos)));
|
||||
value = dequote(trim(line.substr(pos + 1)));
|
||||
|
||||
// decode key, it's of format SERVICENAME_[HEALTH|PORTS]
|
||||
std::string service_name = key.substr(0, key.find_last_of("_"));
|
||||
std::string status_type = key.substr(key.find_last_of("_") + 1);
|
||||
// decode key, it's of format SERVICENAME_[HEALTH|PORTS]
|
||||
std::string service_name = key.substr(0, key.find_last_of("_"));
|
||||
std::string status_type = key.substr(key.find_last_of("_") + 1);
|
||||
|
||||
if (status_type == "HEALTH")
|
||||
{ // healthy|unhealthy|unknown
|
||||
if (value == "healthy")
|
||||
status[service_name].health = HealthStatus::HEALTHY;
|
||||
else if (value == "unhealthy")
|
||||
status[service_name].health = HealthStatus::UNHEALTHY;
|
||||
else if (value == "unknown")
|
||||
status[service_name].health = HealthStatus::UNKNOWN;
|
||||
else
|
||||
status[service_name].health = HealthStatus::ERROR;
|
||||
}
|
||||
else if (status_type == "PORTS")
|
||||
{ // port1,port2,port3
|
||||
std::vector<std::string> ports = string2multi(value);
|
||||
for (const auto &port : ports)
|
||||
{
|
||||
if (port != "unknown")
|
||||
status[service_name].ports.push_back(str2int(port));
|
||||
if (status_type == "HEALTH")
|
||||
{ // healthy|unhealthy|unknown
|
||||
if (value == "healthy")
|
||||
status[service_name].health = HealthStatus::HEALTHY;
|
||||
else if (value == "unhealthy")
|
||||
status[service_name].health = HealthStatus::UNHEALTHY;
|
||||
else if (value == "unknown")
|
||||
status[service_name].health = HealthStatus::UNKNOWN;
|
||||
else
|
||||
status[service_name].health = HealthStatus::ERROR;
|
||||
}
|
||||
else if (status_type == "PORTS")
|
||||
{ // port1,port2,port3
|
||||
std::vector<std::string> ports = string2multi(value);
|
||||
for (const auto &port : ports)
|
||||
{
|
||||
if (port != "unknown")
|
||||
status[service_name].ports.push_back(str2int(port));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -215,13 +221,15 @@ namespace dropshell
|
||||
return HealthStatus::ERROR;
|
||||
}
|
||||
|
||||
if (!env.check_remote_dir_exists(remotepath::service(server, service)))
|
||||
std::string user = env.get_user_for_service(server, service);
|
||||
|
||||
if (!env.check_remote_dir_exists(remotepath(server,user).service(service), user))
|
||||
{
|
||||
return HealthStatus::NOTINSTALLED;
|
||||
}
|
||||
|
||||
std::string script_path = remotepath::service_template(server, service) + "/status.sh";
|
||||
if (!env.check_remote_file_exists(script_path))
|
||||
std::string script_path = remotepath(server,user).service_template(service) + "/status.sh";
|
||||
if (!env.check_remote_file_exists(script_path, user))
|
||||
{
|
||||
return HealthStatus::UNKNOWN;
|
||||
}
|
||||
@ -307,7 +315,7 @@ namespace dropshell
|
||||
return false;
|
||||
}
|
||||
ASSERT(!remote_path.empty() && !local_path.empty(), "Remote or local path not specified. Can't scp.");
|
||||
std::string scp_cmd = "scp -P " + server_env.get_SSH_PORT() + " " + quote(local_path) + " " + server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" + quote(remote_path) + (silent ? " > /dev/null 2>&1" : "");
|
||||
std::string scp_cmd = "scp -P " + server_env.get_SSH_PORT() + " " + quote(local_path) + " " + server_env.get_SSH_UNPRIVILEGED_USER() + "@" + server_env.get_SSH_HOST() + ":" + quote(remote_path) + (silent ? " > /dev/null 2>&1" : "");
|
||||
return execute_local_command("", scp_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||
}
|
||||
|
||||
@ -322,7 +330,7 @@ namespace dropshell
|
||||
return false;
|
||||
}
|
||||
ASSERT(!remote_path.empty() && !local_path.empty(), "Remote or local path not specified. Can't scp.");
|
||||
std::string scp_cmd = "scp -P " + server_env.get_SSH_PORT() + " " + server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" + quote(remote_path) + " " + quote(local_path) + (silent ? " > /dev/null 2>&1" : "");
|
||||
std::string scp_cmd = "scp -P " + server_env.get_SSH_PORT() + " " + server_env.get_SSH_UNPRIVILEGED_USER() + "@" + server_env.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));
|
||||
}
|
||||
|
||||
|
@ -31,12 +31,13 @@ namespace dropshell
|
||||
class cRemoteTempFolder
|
||||
{
|
||||
public:
|
||||
cRemoteTempFolder(const server_env_manager &server_env); // create a temp folder on the remote server
|
||||
cRemoteTempFolder(const server_env_manager &server_env, std::string user); // create a temp folder on the remote server
|
||||
~cRemoteTempFolder(); // delete the temp folder on the remote server
|
||||
std::string path() const; // get the path to the temp folder on the remote server
|
||||
private:
|
||||
std::string mPath;
|
||||
const server_env_manager &mServerEnv;
|
||||
std::string mUser;
|
||||
};
|
||||
|
||||
bool rsync_tree_to_remote(
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "services.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "assert.hpp"
|
||||
|
||||
namespace dropshell
|
||||
{
|
||||
@ -24,8 +25,8 @@ namespace dropshell
|
||||
ssh_handler,
|
||||
shared_commands::std_autocomplete,
|
||||
false, // hidden
|
||||
true, // requires_config
|
||||
true, // requires_install
|
||||
true, // requires_config
|
||||
true, // requires_install
|
||||
1, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"ssh SERVER",
|
||||
@ -34,13 +35,12 @@ namespace dropshell
|
||||
|
||||
ssh SERVER SERVICE SSH into a docker container for a service.
|
||||
ssh SERVER SSH into a server.
|
||||
ssh USER@SERVER SSH into a server as a specific user.
|
||||
)"});
|
||||
}
|
||||
} ssh_command_register;
|
||||
|
||||
|
||||
|
||||
bool ssh_into_server(const std::string &server)
|
||||
bool ssh_into_server(const std::string &server, std::string user)
|
||||
{
|
||||
server_env_manager server_env(server);
|
||||
if (!server_env.is_valid())
|
||||
@ -48,7 +48,7 @@ namespace dropshell
|
||||
error << "Server " << server << " is not valid" << std::endl;
|
||||
return false;
|
||||
}
|
||||
execute_ssh_command(server_env.get_SSH_INFO(), sCommand(remotepath::DROPSHELL_DIR(server), "ls --color && bash", {}), cMode::Interactive);
|
||||
execute_ssh_command(server_env.get_SSH_INFO(user), sCommand(remotepath(server, user).DROPSHELL_DIR(), "ls --color && bash", {}), cMode::Interactive);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ namespace dropshell
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -92,18 +92,41 @@ namespace dropshell
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
|
||||
// ssh into the server
|
||||
if (ctx.args.size() < 2)
|
||||
{
|
||||
// ssh into the server
|
||||
return ssh_into_server(server) ? 0 : 1;
|
||||
std::string arg1 = safearg(ctx.args, 0);
|
||||
std::string server, user;
|
||||
|
||||
// parse either user@server or server
|
||||
if (arg1.find("@") != std::string::npos)
|
||||
{
|
||||
user = arg1.substr(0, arg1.find("@"));
|
||||
server = arg1.substr(arg1.find("@") + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
server = arg1;
|
||||
|
||||
// get the first user from the server.env file, and ssh in as that user.
|
||||
server_env_manager server_env(server);
|
||||
if (!server_env.is_valid())
|
||||
{
|
||||
error << "Server " << server << " is not valid" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
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;
|
||||
}
|
||||
else
|
||||
{ // ssh into a service on the server.
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
return ssh_into_service(server, service) ? 0 : 1;
|
||||
}
|
||||
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
|
||||
// ssh into the specific service.
|
||||
return ssh_into_service(server, service) ? 0 : 1;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
@ -53,23 +53,25 @@ namespace dropshell
|
||||
return false; // should never hit this.
|
||||
}
|
||||
|
||||
std::string user = server_env.get_user_for_service(server, service);
|
||||
|
||||
// 2. Check if service directory exists on server
|
||||
if (!server_env.check_remote_dir_exists(remotepath::service(server, service)))
|
||||
if (!server_env.check_remote_dir_exists(remotepath(server, user).service(service), user))
|
||||
{
|
||||
error << "Service is not installed: " << service << std::endl;
|
||||
return true; // Nothing to uninstall
|
||||
}
|
||||
|
||||
// 3. Run uninstall script if it exists
|
||||
std::string uninstall_script = remotepath::service_template(server, service) + "/uninstall.sh";
|
||||
std::string uninstall_script = remotepath(server, user).service_template(service) + "/uninstall.sh";
|
||||
if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {}))
|
||||
warning << "Uninstall script failed, but continuing with directory removal" << std::endl;
|
||||
|
||||
// 4. Remove the service directory from the server, running in a docker container as root.
|
||||
if (server_env.remove_remote_dir(remotepath::service(server, service), false))
|
||||
if (server_env.remove_remote_dir(remotepath(server, user).service(service), false, user))
|
||||
{
|
||||
ASSERT(!server_env.check_remote_dir_exists(remotepath::service(server, service)), "Service directory still found on server after uninstall");
|
||||
info << "Removed remote service directory " << remotepath::service(server, service) << std::endl;
|
||||
ASSERT(!server_env.check_remote_dir_exists(remotepath(server, user).service(service), user), "Service directory still found on server after uninstall");
|
||||
info << "Removed remote service directory " << remotepath(server, user).service(service) << std::endl;
|
||||
}
|
||||
else
|
||||
warning << "Failed to remove remote service directory" << std::endl;
|
||||
|
Reference in New Issue
Block a user