146 lines
4.5 KiB
C++
146 lines
4.5 KiB
C++
#include "dropshell.hpp"
|
|
#include "server_env.hpp"
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <iomanip>
|
|
#include <boost/filesystem.hpp>
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
namespace dropshell {
|
|
|
|
std::vector<ServerInfo> get_configured_servers() {
|
|
std::vector<ServerInfo> servers;
|
|
std::string user_dir;
|
|
|
|
if (!is_config_loaded()) {
|
|
std::cerr << "Error: Config not loaded" << std::endl;
|
|
return servers;
|
|
}
|
|
if (!get_user_directory(user_dir)) {
|
|
std::cerr << "Error: User directory not set" << std::endl;
|
|
return servers;
|
|
}
|
|
fs::path servers_dir = fs::path(user_dir) / "servers";
|
|
if (!fs::exists(servers_dir)) {
|
|
std::cerr << "Error: Servers directory not found" << std::endl;
|
|
return servers;
|
|
}
|
|
|
|
for (const auto& entry : fs::directory_iterator(servers_dir)) {
|
|
if (fs::is_directory(entry)) {
|
|
fs::path env_file = entry.path() / "_server.env";
|
|
server_env env(env_file.string());
|
|
if (!env.is_valid()) {
|
|
std::cerr << "Error: Invalid server environment file: " << env_file.string() << std::endl;
|
|
continue;
|
|
}
|
|
servers.push_back({
|
|
entry.path().filename().string(),
|
|
env.get_SSH_HOST(),
|
|
env.get_SSH_USER(),
|
|
env.get_SSH_PORT()
|
|
});
|
|
}
|
|
}
|
|
|
|
return servers;
|
|
}
|
|
|
|
void list_servers() {
|
|
auto servers = get_configured_servers();
|
|
|
|
if (servers.empty()) {
|
|
std::cout << "No servers configured." << std::endl;
|
|
return;
|
|
}
|
|
|
|
// Find maximum lengths for formatting
|
|
size_t max_name_len = 4; // "Name" is 4 chars
|
|
size_t max_addr_len = 7; // "Address" is 7 chars
|
|
|
|
for (const auto& server : servers) {
|
|
max_name_len = std::max(max_name_len, server.name.length());
|
|
max_addr_len = std::max(max_addr_len, server.ssh_host.length());
|
|
}
|
|
|
|
// Print header
|
|
std::cout << std::left << std::setw(max_name_len) << "Name" << " | "
|
|
<< std::setw(max_addr_len) << "Address" << std::endl;
|
|
|
|
// Print separator
|
|
std::cout << std::string(max_name_len, '-') << "-+-"
|
|
<< std::string(max_addr_len, '-') << std::endl;
|
|
|
|
// Print server rows
|
|
for (const auto& server : servers) {
|
|
std::cout << std::left << std::setw(max_name_len) << server.name << " | "
|
|
<< std::setw(max_addr_len) << server.ssh_host << std::endl;
|
|
}
|
|
}
|
|
|
|
void show_server_details(const std::string& server_name) {
|
|
std::string user_dir;
|
|
if (!get_user_directory(user_dir)) {
|
|
std::cerr << "Error: User directory not set" << std::endl;
|
|
return;
|
|
}
|
|
|
|
fs::path server_dir = fs::path(user_dir) / "servers" / server_name;
|
|
if (!fs::exists(server_dir)) {
|
|
std::cerr << "Error: Server '" << server_name << "' not found" << std::endl;
|
|
return;
|
|
}
|
|
|
|
fs::path env_file = server_dir / "_server.env";
|
|
if (!fs::exists(env_file)) {
|
|
std::cerr << "Error: Server configuration file not found" << std::endl;
|
|
return;
|
|
}
|
|
|
|
std::cout << "Server Details: " << server_name << std::endl;
|
|
std::cout << std::string(40, '-') << std::endl;
|
|
|
|
std::ifstream file(env_file.string());
|
|
std::string line;
|
|
while (std::getline(file, line)) {
|
|
if (!line.empty() && line[0] != '#') {
|
|
std::cout << line << std::endl;
|
|
}
|
|
}
|
|
|
|
// Check if server is reachable via SSH
|
|
std::string ssh_address;
|
|
file.clear();
|
|
file.seekg(0);
|
|
while (std::getline(file, line)) {
|
|
if (boost::starts_with(line, "SSH_ADDRESS=")) {
|
|
ssh_address = line.substr(12);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!ssh_address.empty()) {
|
|
std::cout << std::endl << "Server Status:" << std::endl;
|
|
std::cout << std::string(40, '-') << std::endl;
|
|
|
|
// Try to connect to the server
|
|
std::string cmd = "ssh -o ConnectTimeout=5 " + ssh_address + " 'echo connected' 2>/dev/null";
|
|
int result = system(cmd.c_str());
|
|
if (result == 0) {
|
|
std::cout << "Status: Online" << std::endl;
|
|
|
|
// Get uptime if possible
|
|
cmd = "ssh " + ssh_address + " 'uptime' 2>/dev/null";
|
|
int rval = system(cmd.c_str());
|
|
if (rval != 0) {
|
|
std::cout << "Error: Failed to get uptime" << std::endl;
|
|
}
|
|
} else {
|
|
std::cout << "Status: Offline" << std::endl;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace dropshell
|