feat: Update 5 and remove 1 files
All checks were successful
Build-Test-Publish / build (linux/amd64) (push) Successful in 53s
Build-Test-Publish / build (linux/arm64) (push) Successful in 2m55s

This commit is contained in:
Your Name
2025-08-29 19:20:52 +12:00
parent 04d240bbd2
commit 6a77e538dd
6 changed files with 89 additions and 216 deletions

View File

@@ -1,11 +1,15 @@
#include "shared_commands.hpp"
#include <libassert/assert.hpp>
#include <algorithm>
#include <sstream>
#include <mutex>
#include "utils/utils.hpp"
#include "servers.hpp"
#include "directories.hpp"
#include "services.hpp"
#include "servers.hpp"
#include "utils/output.hpp"
#include "transwarp.hpp"
namespace dropshell
{
@@ -140,51 +144,42 @@ namespace dropshell
std::map<std::string, ServiceStatus> status;
std::string server_name = server_env.get_server_name();
std::string output;
std::string agentpath = remotepath(server_name,user).agent();
if (!execute_ssh_command(server_env.get_SSH_INFO(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)
{
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);
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));
}
}
// Get all services for this user on this server
std::vector<LocalServiceInfo> services = get_server_services_info(server_name);
// Filter services for this specific user
std::vector<LocalServiceInfo> user_services;
for (const auto& service : services) {
if (service.user == user) {
user_services.push_back(service);
}
}
// Use parallel execution to check all services simultaneously
// This matches how the list command already works for multiple servers
if (user_services.size() > 0) {
std::mutex status_mutex;
// transwarp requires at least 1 thread
size_t num_threads = std::max(size_t(1), user_services.size());
transwarp::parallel exec{num_threads};
auto task = transwarp::for_each(exec, user_services.begin(), user_services.end(),
[&status, &status_mutex, &server_name](const LocalServiceInfo& service) {
ServiceStatus service_status;
// Get health status using the existing is_healthy function
service_status.health = is_healthy(server_name, service.service_name);
// Get ports using the new get_ports function
service_status.ports = get_ports(server_name, service.service_name);
// Thread-safe update of the status map
std::lock_guard<std::mutex> lock(status_mutex);
status[service.service_name] = service_status;
});
task->wait();
}
return status;
}
@@ -258,6 +253,53 @@ namespace dropshell
return HealthStatus::HEALTHY;
}
// ------------------------------------------------------------------------------------------------
// get_ports : SHARED COMMAND
// ------------------------------------------------------------------------------------------------
std::vector<int> get_ports(const std::string &server, const std::string &service)
{
std::vector<int> ports;
ServerConfig env(server);
if (!env.is_valid())
{
error << "Server service not initialized" << std::endl;
return ports;
}
std::string user = env.get_user_for_service(service);
// Check if ports script exists
std::string script_path = remotepath(server,user).service_template(service) + "/ports.sh";
if (!env.check_remote_file_exists(script_path, user))
{
return ports; // No ports script, return empty
}
// Run ports script and capture output
std::string output;
if (env.run_remote_template_command_and_capture_output(service, "ports", {}, output, true, {}))
{
// Parse the output - expecting comma-separated or newline-separated port numbers
std::stringstream ss(output);
std::string port_str;
while (ss >> port_str)
{
// Remove any commas
port_str.erase(std::remove(port_str.begin(), port_str.end(), ','), port_str.end());
try {
int port = std::stoi(port_str);
if (port > 0 && port <= 65535)
ports.push_back(port);
} catch (...) {
// Ignore non-numeric entries
}
}
}
return ports;
}
// ------------------------------------------------------------------------------------------------
// healthmark : SHARED COMMAND
// ------------------------------------------------------------------------------------------------

View File

@@ -67,6 +67,7 @@ namespace dropshell
std::string healthtick(const std::string &server, const std::string &service);
std::string HealthStatus2String(HealthStatus status);
HealthStatus is_healthy(const std::string &server, const std::string &service);
std::vector<int> get_ports(const std::string &server, const std::string &service);
std::string healthmark(const std::string &server, const std::string &service);
void std_autocomplete(const CommandContext &ctx);

View File

@@ -323,7 +323,8 @@ namespace dropshell
for (const auto &[key, value] : extra_env_vars)
scommand->add_env_var(key, value);
return execute_ssh_command(get_SSH_INFO(user), scommand.value(), cMode::Defaults, &output);
// Use Silent mode when capturing output to avoid printing to console
return execute_ssh_command(get_SSH_INFO(user), scommand.value(), cMode::Silent, &output);
}
std::string ServerConfig::get_variable(const std::string &name) const

View File

@@ -140,7 +140,7 @@ namespace dropshell
{ // determine if the service template hash matches the template hash.
auto it = variables.find("TEMPLATE_HASH");
if (it == variables.end())
error << "Variable TEMPLATE_HASH not found in the service " << filenames::template_info_env << std::endl;
error << "Variable TEMPLATE_HASH not found in the service " << filenames::template_info_env << " for "<<server_name << " - "<< service.template_name << std::endl;
else if (tinfo.is_set())
{
uint64_t service_template_hash = std::stoull(it->second);